update chat room list
This commit is contained in:
@@ -179,12 +179,13 @@ class MessageManager(private val nostr: Nostr) {
|
|||||||
val kind = Kind.fromStd(KindStandard.APPLICATION_SPECIFIC_DATA)
|
val kind = Kind.fromStd(KindStandard.APPLICATION_SPECIFIC_DATA)
|
||||||
val kTag = SingleLetterTag.lowercase(Alphabet.K)
|
val kTag = SingleLetterTag.lowercase(Alphabet.K)
|
||||||
|
|
||||||
// Get all DM events
|
// Get all rumors (DMs and Reactions)
|
||||||
val filter = Filter().kind(kind).customTags(kTag, listOf("14", "dm"))
|
val filter = Filter().kind(kind).customTags(kTag, listOf("14", "dm", "reaction"))
|
||||||
val events = client?.database()?.query(filter)?.toVec() ?: return null
|
val events = client?.database()?.query(filter)?.toVec() ?: return null
|
||||||
|
|
||||||
// Collect rooms
|
// Collect rooms
|
||||||
val roomsMap: MutableMap<Long, Room> = mutableMapOf()
|
val roomsMap: MutableMap<Long, Room> = mutableMapOf()
|
||||||
|
val lastDmTimestampMap: MutableMap<Long, ULong> = mutableMapOf()
|
||||||
|
|
||||||
events
|
events
|
||||||
.map { UnsignedEvent.fromJson(it.content()) }
|
.map { UnsignedEvent.fromJson(it.content()) }
|
||||||
@@ -192,18 +193,41 @@ class MessageManager(private val nostr: Nostr) {
|
|||||||
.forEach { rumor ->
|
.forEach { rumor ->
|
||||||
val id = rumor.roomId()
|
val id = rumor.roomId()
|
||||||
val isFromMe = rumor.author() == userPubkey
|
val isFromMe = rumor.author() == userPubkey
|
||||||
|
val isReaction = rumor.kind().asStd() == KindStandard.REACTION
|
||||||
val existing = roomsMap[id]
|
val existing = roomsMap[id]
|
||||||
val createdAt = rumor.createdAt()
|
val createdAt = rumor.createdAt()
|
||||||
|
|
||||||
// If the room is new or the current rumor is newer than the existing one
|
if (existing == null) {
|
||||||
if (existing == null || createdAt.asSecs() > existing.createdAt.asSecs()) {
|
|
||||||
// A room is "Ongoing" if it was already marked as such or if the current rumor is from the user
|
|
||||||
val isOngoing = (existing?.kind == RoomKind.Ongoing) || isFromMe
|
|
||||||
val room = Room.new(rumor = rumor, userPubkey = userPubkey, id = id)
|
val room = Room.new(rumor = rumor, userPubkey = userPubkey, id = id)
|
||||||
roomsMap[id] = if (isOngoing) room.copy(kind = RoomKind.Ongoing) else room
|
// If the first event we see is a reaction, don't use it as lastMessage
|
||||||
} else if (isFromMe && existing.kind != RoomKind.Ongoing) {
|
roomsMap[id] = if (isReaction) {
|
||||||
// If it's an older rumor but sent by the user, mark the room as Ongoing
|
room.copy(lastMessage = null)
|
||||||
roomsMap[id] = existing.copy(kind = RoomKind.Ongoing)
|
} else {
|
||||||
|
lastDmTimestampMap[id] = createdAt.asSecs()
|
||||||
|
room
|
||||||
|
}
|
||||||
|
if (isFromMe) {
|
||||||
|
roomsMap[id] = roomsMap[id]!!.copy(kind = RoomKind.Ongoing)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// Update the overall room timestamp (for sorting) if this event is newer
|
||||||
|
if (createdAt.asSecs() > existing.createdAt.asSecs()) {
|
||||||
|
roomsMap[id] = roomsMap[id]!!.copy(createdAt = createdAt)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update the last message content if this is a DM and it's newer than the last DM we've seen
|
||||||
|
if (!isReaction) {
|
||||||
|
val lastDmTs = lastDmTimestampMap[id] ?: 0uL
|
||||||
|
if (createdAt.asSecs() >= lastDmTs) {
|
||||||
|
lastDmTimestampMap[id] = createdAt.asSecs()
|
||||||
|
roomsMap[id] = roomsMap[id]!!.copy(lastMessage = rumor.content())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// If any event is from the user, mark the room as Ongoing
|
||||||
|
if (isFromMe && roomsMap[id]?.kind != RoomKind.Ongoing) {
|
||||||
|
roomsMap[id] = roomsMap[id]!!.copy(kind = RoomKind.Ongoing)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -281,13 +281,14 @@ class ChatRepository(
|
|||||||
lastMessage = if (isReaction) null else event.content()
|
lastMessage = if (isReaction) null else event.content()
|
||||||
)
|
)
|
||||||
rooms[newRoom.id] = newRoom
|
rooms[newRoom.id] = newRoom
|
||||||
} else if (!isReaction && event.createdAt().asSecs() >= existingRoom.createdAt.asSecs()) {
|
} else if (event.createdAt().asSecs() >= existingRoom.createdAt.asSecs()) {
|
||||||
// Only update preview if message is newer (handles sync/late arrivals)
|
// Update timestamp for any newer event (DM or Reaction)
|
||||||
|
// But only update preview if it's a DM
|
||||||
rooms[roomId] = existingRoom.copy(
|
rooms[roomId] = existingRoom.copy(
|
||||||
lastMessage = event.content(),
|
lastMessage = if (isReaction) existingRoom.lastMessage else event.content(),
|
||||||
createdAt = event.createdAt(),
|
createdAt = event.createdAt(),
|
||||||
kind = newKind,
|
kind = newKind,
|
||||||
unreadCount = if (isFromMe) existingRoom.unreadCount else existingRoom.unreadCount + 1
|
unreadCount = if (isFromMe || isReaction) existingRoom.unreadCount else existingRoom.unreadCount + 1
|
||||||
)
|
)
|
||||||
} else if (isFromMe && existingRoom.kind != RoomKind.Ongoing) {
|
} else if (isFromMe && existingRoom.kind != RoomKind.Ongoing) {
|
||||||
// Even if it's an older message or reaction, if it's from me, the room is ongoing
|
// Even if it's an older message or reaction, if it's from me, the room is ongoing
|
||||||
|
|||||||
Reference in New Issue
Block a user