@@ -243,10 +243,12 @@ void populateReply(TwitchChannel *channel, Communi::IrcMessage *message,
243243 TwitchMessageBuilder &builder)
244244{
245245 const auto &tags = message->tags ();
246- if (const auto it = tags.find (" reply-parent-msg-id" ); it != tags.end ())
246+ if (const auto it = tags.find (" reply-thread-parent-msg-id" );
247+ it != tags.end ())
247248 {
248249 const QString replyID = it.value ().toString ();
249250 auto threadIt = channel->threads ().find (replyID);
251+ std::shared_ptr<MessageThread> rootThread;
250252 if (threadIt != channel->threads ().end ())
251253 {
252254 auto owned = threadIt->second .lock ();
@@ -256,43 +258,80 @@ void populateReply(TwitchChannel *channel, Communi::IrcMessage *message,
256258 updateReplyParticipatedStatus (tags, message->nick (), builder,
257259 owned, false );
258260 builder.setThread (owned);
259- return ;
261+ rootThread = owned ;
260262 }
261263 }
262264
263- MessagePtr foundMessage;
264-
265- // Thread does not yet exist, find root reply and create thread.
266- // Linear search is justified by the infrequent use of replies
267- for (const auto &otherMsg : otherLoaded)
265+ if (!rootThread)
268266 {
269- if (otherMsg->id == replyID)
267+ MessagePtr foundMessage;
268+
269+ // Thread does not yet exist, find root reply and create thread.
270+ // Linear search is justified by the infrequent use of replies
271+ for (const auto &otherMsg : otherLoaded)
270272 {
271- // Found root reply message
272- foundMessage = otherMsg;
273- break ;
273+ if (otherMsg->id == replyID)
274+ {
275+ // Found root reply message
276+ foundMessage = otherMsg;
277+ break ;
278+ }
274279 }
275- }
276280
277- if (!foundMessage)
278- {
279- // We didn't find the reply root message in the otherLoaded messages
280- // which are typically the already-parsed recent messages from the
281- // Recent Messages API. We could have a really old message that
282- // still exists being replied to, so check for that here.
283- foundMessage = channel->findMessage (replyID);
281+ if (!foundMessage)
282+ {
283+ // We didn't find the reply root message in the otherLoaded messages
284+ // which are typically the already-parsed recent messages from the
285+ // Recent Messages API. We could have a really old message that
286+ // still exists being replied to, so check for that here.
287+ foundMessage = channel->findMessage (replyID);
288+ }
289+
290+ if (foundMessage)
291+ {
292+ std::shared_ptr<MessageThread> newThread =
293+ std::make_shared<MessageThread>(foundMessage);
294+ updateReplyParticipatedStatus (tags, message->nick (), builder,
295+ newThread, true );
296+
297+ builder.setThread (newThread);
298+ rootThread = newThread;
299+ // Store weak reference to thread in channel
300+ channel->addReplyThread (newThread);
301+ }
284302 }
285303
286- if (foundMessage)
304+ if (const auto parentIt = tags.find (" reply-parent-msg-id" );
305+ parentIt != tags.end ())
287306 {
288- std::shared_ptr<MessageThread> newThread =
289- std::make_shared<MessageThread>(foundMessage);
290- updateReplyParticipatedStatus (tags, message->nick (), builder,
291- newThread, true );
292-
293- builder.setThread (newThread);
294- // Store weak reference to thread in channel
295- channel->addReplyThread (newThread);
307+ const QString parentID = parentIt.value ().toString ();
308+ if (replyID == parentID)
309+ {
310+ if (rootThread)
311+ {
312+ builder.setParent (rootThread->root ());
313+ }
314+ }
315+ else
316+ {
317+ auto parentThreadIt = channel->threads ().find (parentID);
318+ if (parentThreadIt != channel->threads ().end ())
319+ {
320+ auto thread = parentThreadIt->second .lock ();
321+ if (thread)
322+ {
323+ builder.setParent (thread->root ());
324+ }
325+ }
326+ else
327+ {
328+ auto parent = channel->findMessage (parentID);
329+ if (parent)
330+ {
331+ builder.setParent (parent);
332+ }
333+ }
334+ }
296335 }
297336 }
298337}
@@ -1283,17 +1322,20 @@ void IrcMessageHandler::addMessage(Communi::IrcMessage *message,
12831322 TwitchMessageBuilder builder (chan.get (), message, args, content, isAction);
12841323 builder.setMessageOffset (messageOffset);
12851324
1286- if (const auto it = tags.find (" reply-parent-msg-id" ); it != tags.end ())
1325+ if (const auto it = tags.find (" reply-thread-parent-msg-id" );
1326+ it != tags.end ())
12871327 {
12881328 const QString replyID = it.value ().toString ();
1289- auto threadIt = channel->threads_ .find (replyID);
1290- if (threadIt != channel->threads_ .end () && !threadIt->second .expired ())
1329+ auto threadIt = channel->threads ().find (replyID);
1330+ std::shared_ptr<MessageThread> rootThread;
1331+ if (threadIt != channel->threads ().end () && !threadIt->second .expired ())
12911332 {
12921333 // Thread already exists (has a reply)
12931334 auto thread = threadIt->second .lock ();
12941335 updateReplyParticipatedStatus (tags, message->nick (), builder,
12951336 thread, false );
12961337 builder.setThread (thread);
1338+ rootThread = thread;
12971339 }
12981340 else
12991341 {
@@ -1307,10 +1349,44 @@ void IrcMessageHandler::addMessage(Communi::IrcMessage *message,
13071349 newThread, true );
13081350
13091351 builder.setThread (newThread);
1352+ rootThread = newThread;
13101353 // Store weak reference to thread in channel
13111354 channel->addReplyThread (newThread);
13121355 }
13131356 }
1357+
1358+ if (const auto parentIt = tags.find (" reply-parent-msg-id" );
1359+ parentIt != tags.end ())
1360+ {
1361+ const QString parentID = parentIt.value ().toString ();
1362+ if (replyID == parentID)
1363+ {
1364+ if (rootThread)
1365+ {
1366+ builder.setParent (rootThread->root ());
1367+ }
1368+ }
1369+ else
1370+ {
1371+ auto parentThreadIt = channel->threads ().find (parentID);
1372+ if (parentThreadIt != channel->threads ().end ())
1373+ {
1374+ auto thread = parentThreadIt->second .lock ();
1375+ if (thread)
1376+ {
1377+ builder.setParent (thread->root ());
1378+ }
1379+ }
1380+ else
1381+ {
1382+ auto parent = channel->findMessage (parentID);
1383+ if (parent)
1384+ {
1385+ builder.setParent (parent);
1386+ }
1387+ }
1388+ }
1389+ }
13141390 }
13151391
13161392 if (isSub || !builder.isIgnored ())
0 commit comments