@@ -15,12 +15,17 @@ extension MessageStore {
1515 }
1616 guard let pollGUID = candidateGUIDs. first else { return poll }
1717
18+ let sourcePollGUID = try sourcePollGUID ( forAny: candidateGUIDs, db: db) ?? pollGUID
1819 let optionTexts = try pollOptionTextsByID (
19- pollGUID: pollGUID ,
20+ pollGUID: sourcePollGUID ,
2021 db: db,
2122 cache: & cache
2223 )
23- return poll. resolvingVoteOptionTexts ( optionTexts)
24+ let resolvedPoll = poll. resolvingVoteOptionTexts ( optionTexts)
25+ return resolvedPoll. resolvingPollReference (
26+ pollGUID: sourcePollGUID,
27+ originalGUID: sourcePollGUID
28+ )
2429 }
2530
2631 /// Ordered options of the poll identified by `guid`, decoded from its
@@ -109,4 +114,37 @@ extension MessageStore {
109114 }
110115 return options
111116 }
117+
118+ private func sourcePollGUID( forAny candidates: [ String ] , db: Connection ) throws -> String ? {
119+ guard schema. hasReactionColumns else { return nil }
120+ for candidate in candidates {
121+ if let source = try sourcePollGUID ( forUpdateRow: candidate, db: db) {
122+ return source
123+ }
124+ }
125+ return candidates. first
126+ }
127+
128+ private func sourcePollGUID( forUpdateRow guid: String , db: Connection ) throws -> String ? {
129+ let rows = try db. prepareRowIterator (
130+ """
131+ SELECT associated_message_guid
132+ FROM message
133+ WHERE guid = ?
134+ AND associated_message_type = ?
135+ AND IFNULL(associated_message_guid, '') != ''
136+ LIMIT 1
137+ """ ,
138+ bindings: [ guid, MessagePollDecoder . updateAssociatedMessageType]
139+ )
140+ guard
141+ let row = try rows. failableNext ( ) ,
142+ let associatedGUID = try row. get ( Expression < String ? > ( " associated_message_guid " ) )
143+ else {
144+ return nil
145+ }
146+
147+ let normalized = normalizeAssociatedGUID ( associatedGUID)
148+ return normalized. isEmpty ? nil : normalized
149+ }
112150}
0 commit comments