@@ -556,33 +556,78 @@ class DatabaseManager {
556
556
public func fetchLiteThread( chatGUID: String , before: Int64 ? ) throws -> [ ConversationItem ] {
557
557
guard let dbConnection = dbConnection else { throw DatabaseDisconnectedError ( ) }
558
558
559
+ //Filter by chat and optionally message ID
559
560
var fetchWhere : String = " chat.GUID = ? "
560
561
var fetchBindings : [ Binding ? ] = [ chatGUID]
561
562
if let before = before {
562
563
fetchWhere += " AND message.ROWID < ? "
563
564
fetchBindings. append ( before)
564
565
}
565
566
567
+ //Fetch messages from newest to oldest
566
568
let stmt = try fetchMessages (
567
569
using: dbConnection,
568
570
where: fetchWhere,
569
571
sort: " message.ROWID DESC " ,
570
- limit: 24 ,
571
572
bindings: fetchBindings
572
573
)
573
574
let indices = DatabaseConverter . makeColumnIndexDict ( stmt. columnNames)
574
575
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 {
579
602
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]
584
627
}
585
628
}
629
+
630
+ return conversationItemArray
586
631
}
587
632
}
588
633
0 commit comments