Skip to content

Commit 1c8bf04

Browse files
steipeteomarshahine
andcommitted
fix: preserve preview search associations
Co-authored-by: Omar Shahine <[email protected]>
1 parent ceb9c25 commit 1c8bf04

3 files changed

Lines changed: 44 additions & 4 deletions

File tree

Sources/IMsgCore/MessageStore+MessageConstruction.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ extension MessageStore {
128128
.mapValues { Set($0.map(\.chatID)) }
129129
let selection = MessageRowSelection(store: self, includeChatID: true)
130130
var previews: [Message] = []
131-
var seenPreviewRowIDs = Set<Int64>()
131+
var seenPreviewChatIDsByRowID: [Int64: Set<Int64>] = [:]
132132
var parentCache: ReplyParentCache = [:]
133133
var pollOptionCache = PollOptionTextCache()
134134
let maximumDateWindowsPerQuery = 200
@@ -185,7 +185,9 @@ extension MessageStore {
185185
else {
186186
continue
187187
}
188-
guard seenPreviewRowIDs.insert(decoded.rowID).inserted else { continue }
188+
guard
189+
seenPreviewChatIDsByRowID[decoded.rowID, default: []].insert(preview.chatID).inserted
190+
else { continue }
189191
previews.append(preview)
190192
}
191193
}

Sources/IMsgCore/MessageStore+Search.swift

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,13 +78,17 @@ extension MessageStore {
7878
}
7979
let queriedMessageCount = messages.count
8080
if let firstTextRowID = messages.filter({ !isURLPreviewBalloon($0) }).map(\.rowID).min() {
81-
let existingRowIDs = Set(messages.map(\.rowID))
81+
let existingChatIDsByRowID = Dictionary(grouping: messages, by: \.rowID)
82+
.mapValues { Set($0.map(\.chatID)) }
8283
let linkedPreviews = try linkedURLPreviewLookahead(
8384
afterRowID: firstTextRowID,
8485
candidates: messages,
8586
db: db
8687
)
87-
messages.append(contentsOf: linkedPreviews.filter { !existingRowIDs.contains($0.rowID) })
88+
messages.append(
89+
contentsOf: linkedPreviews.filter {
90+
existingChatIDsByRowID[$0.rowID]?.contains($0.chatID) != true
91+
})
8892
}
8993
var usedFallbackReplacement = false
9094
let coalesced = try coalesceURLPreviewMessages(

Tests/IMsgCoreTests/MessageStoreURLPreviewRoutingTests.swift

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,3 +326,37 @@ func searchLookaheadKeepsCandidateChatAssociation() throws {
326326
#expect(messages.first?.chatID == 1)
327327
#expect(messages.first?.urlPreview == nil)
328328
}
329+
330+
@Test
331+
func searchLookaheadCoalescesEveryMultiChatAssociation() throws {
332+
let db = try makeURLPreviewTestDB()
333+
let now = Date()
334+
try db.run("INSERT INTO handle(ROWID, id) VALUES (1, '+123')")
335+
try insertURLPreviewTestMessage(
336+
db,
337+
rowID: 1,
338+
chatID: 1,
339+
text: "Check this out",
340+
guid: "text-guid",
341+
date: now
342+
)
343+
try db.run("INSERT INTO chat_message_join(chat_id, message_id) VALUES (2, 1)")
344+
try insertURLPreviewTestMessage(
345+
db,
346+
rowID: 2,
347+
chatID: 1,
348+
text: "https://example.com",
349+
guid: "preview-guid",
350+
replyToGUID: "text-guid",
351+
balloonBundleID: MessageStore.urlPreviewBalloonBundleID,
352+
date: now.addingTimeInterval(1)
353+
)
354+
try db.run("INSERT INTO chat_message_join(chat_id, message_id) VALUES (2, 2)")
355+
356+
let store = try MessageStore(connection: db, path: ":memory:")
357+
let messages = try store.searchMessages(query: "Check this", match: "contains", limit: 10)
358+
359+
#expect(messages.count == 2)
360+
#expect(Set(messages.map(\.chatID)) == Set([1, 2]))
361+
#expect(messages.allSatisfy { $0.urlPreview?.rowID == 2 })
362+
}

0 commit comments

Comments
 (0)