Skip to content

Commit b429e8d

Browse files
committed
Collect nearby modifiers in lite thread requests
1 parent c8a2a7e commit b429e8d

File tree

1 file changed

+54
-9
lines changed

1 file changed

+54
-9
lines changed

AirMessage/Database/DatabaseManager.swift

+54-9
Original file line numberDiff line numberDiff line change
@@ -556,33 +556,78 @@ class DatabaseManager {
556556
public func fetchLiteThread(chatGUID: String, before: Int64?) throws -> [ConversationItem] {
557557
guard let dbConnection = dbConnection else { throw DatabaseDisconnectedError() }
558558

559+
//Filter by chat and optionally message ID
559560
var fetchWhere: String = "chat.GUID = ?"
560561
var fetchBindings: [Binding?] = [chatGUID]
561562
if let before = before {
562563
fetchWhere += " AND message.ROWID < ?"
563564
fetchBindings.append(before)
564565
}
565566

567+
//Fetch messages from newest to oldest
566568
let stmt = try fetchMessages(
567569
using: dbConnection,
568570
where: fetchWhere,
569571
sort: "message.ROWID DESC",
570-
limit: 24,
571572
bindings: fetchBindings
572573
)
573574
let indices = DatabaseConverter.makeColumnIndexDict(stmt.columnNames)
574575

575-
return try stmt.map { row in
576-
try DatabaseConverter.processMessageRow(row, withIndices: indices, ofDB: dbConnection)
577-
}.compactMap { row -> ConversationItem? in
578-
switch row {
576+
/*
577+
Messages are iterated in reverse order.
578+
In order to properly apply modifiers, we'll keep track of any modifiers
579+
that we hit in a dictionary keyed by their message GUID.
580+
We can pull in these modifiers when we reach the target message.
581+
582+
This does mean that if a message and its modifiers are queried for in
583+
separate requests, the modifiers will be dropped.
584+
*/
585+
var conversationItemArray: [ConversationItem] = []
586+
var isolatedModifierDict: [String: [ModifierInfo]] = [:]
587+
588+
//Collect up to 24 message items
589+
while conversationItemArray.count < 24 {
590+
let row = try stmt.failableNext()
591+
592+
//End of iterator, exit loop
593+
guard let row = row else {
594+
break
595+
}
596+
597+
//Process the message row
598+
let messageRow = try DatabaseConverter.processMessageRow(row, withIndices: indices, ofDB: dbConnection)
599+
guard let messageRow = messageRow else { continue }
600+
601+
switch messageRow {
579602
case .message(let conversationItem):
580-
return conversationItem
581-
default:
582-
//Discard modifiers
583-
return nil
603+
if var message = conversationItem as? MessageInfo {
604+
//Apply any modifiers
605+
if let modifierArray = isolatedModifierDict[message.guid] {
606+
for modifier in modifierArray {
607+
switch modifier {
608+
case let tapback as TapbackModifierInfo:
609+
message.tapbacks.append(tapback)
610+
case let sticker as StickerModifierInfo:
611+
message.stickers.append(sticker)
612+
default:
613+
break
614+
}
615+
}
616+
}
617+
618+
//Save the message
619+
conversationItemArray.append(message)
620+
} else {
621+
//Save the conversation item
622+
conversationItemArray.append(conversationItem)
623+
}
624+
case .modifier(let modifier):
625+
//Record the modifier for later reference
626+
isolatedModifierDict[modifier.messageGUID] = (isolatedModifierDict[modifier.messageGUID] ?? []) + [modifier]
584627
}
585628
}
629+
630+
return conversationItemArray
586631
}
587632
}
588633

0 commit comments

Comments
 (0)