forked from mamoe/mirai
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMockEssences.kt
59 lines (50 loc) · 2.09 KB
/
MockEssences.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/*
* Copyright 2019-2023 Mamoe Technologies and contributors.
*
* 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
* Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
*
* https://github.com/mamoe/mirai/blob/dev/LICENSE
*/
package net.mamoe.mirai.mock.internal.contact.essence
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.asFlow
import net.mamoe.mirai.contact.NormalMember
import net.mamoe.mirai.contact.essence.EssenceMessageRecord
import net.mamoe.mirai.message.data.MessageSource
import net.mamoe.mirai.mock.contact.essence.MockEssences
import net.mamoe.mirai.mock.internal.contact.MockGroupImpl
import net.mamoe.mirai.utils.ConcurrentHashMap
import net.mamoe.mirai.utils.currentTimeSeconds
internal class MockEssencesImpl(
private val group: MockGroupImpl
) : MockEssences {
private val cache: MutableMap<MessageSource, EssenceMessageRecord> = ConcurrentHashMap()
override fun mockSetEssences(source: MessageSource, actor: NormalMember) {
val record = EssenceMessageRecord(
group = group,
sender = group[source.fromId],
senderId = source.fromId,
senderNick = group[source.fromId]?.nick.orEmpty(),
senderTime = source.time,
operator = actor,
operatorId = actor.id,
operatorNick = actor.nick,
operatorTime = currentTimeSeconds().toInt(),
loadMessageSource = { source }
)
cache[source] = record
}
override suspend fun getPage(start: Int, limit: Int): List<EssenceMessageRecord> {
return cache.values.toList().subList(start, start + limit)
}
override suspend fun share(source: MessageSource): String {
return "https://qun.qq.com/essence/share?_wv=3&_wwv=128&_wvx=2&sharekey=..."
}
override suspend fun remove(source: MessageSource) {
cache.remove(source)
}
override fun asFlow(): Flow<EssenceMessageRecord> {
return cache.values.asFlow()
}
}