Fixed New reply notifications can be missing as a result of merging threads/posts

Affected version
2.1.2

Steffen

Well-known member
Consider two threads A and B with the last post in thread B being more recent than the last post in thread A. Consider furthermore that a user has completely read thread A but not thread B (or at least not its most recent post). Now, a moderator merges thread B into thread A (or merges the last post of thread B into thread A). Now assume that someone else replies to thread A. The problem is that our user won't be notified of such replies in thread A (because it already contains at least one unread post, namely the last post of the former thread B).

Could XenForo update the xf_thread_read table when merging posts/threads?
 

Kier

XenForo developer
Staff member
I'm just wondering if we could do something along the lines of
SQL:
UPDATE xf_thread_read AS dst
LEFT JOIN xf_thread_read AS src ON (dst.user_id = src.user_id AND src.thread_id = ?)
SET dst.thread_read_date = LEAST(dst.thread_read_date, src.thread_read_date)
WHERE dst.thread_id = ?;
Passing in the IDs of the source and destination threads...
 

Kier

XenForo developer
Staff member
... of course that only handles one thread being merged into another - I forgot that you can merge multiple!
 

Kier

XenForo developer
Staff member
Might have a reasonable solution - to be called after updateUserCounters() in the thread merger:
PHP:
    protected function updateThreadReadData()
    {
        $sourceThreadIds = $this->db()->quote(array_keys($this->sourceThreads));

        $this->db()->query("
            UPDATE xf_thread_read AS tr_dest,
            (
                SELECT MIN(thread_read_date) AS min_thread_read_date, user_id
                FROM xf_thread_read
                WHERE thread_id IN({$sourceThreadIds})
                GROUP BY user_id
            ) AS tr_src
            SET tr_dest.thread_read_date = tr_src.min_thread_read_date
            WHERE tr_dest.user_id = tr_src.user_id
            AND tr_dest.thread_id = ?
        ", $this->target->thread_id);
    }
 

XF Bug Bot

XenForo bug fixer bot
Staff member
Thank you for reporting this issue. It has now been resolved and we are aiming to include it in a future XF release (2.1.5).

Change log:
Update the xf_thread_read table after a thread merge to reflect the earliest read date for each source thread for each user, so that unread posts from source threads are not marked as read when they are merged into a destination thread whose original content has itself already been read
Any changes made as a result of this issue being resolved may not be rolled out here until later.
 

Xon

Well-known member
@Kier shouldn't there be a check for tr_dest.thread_read_date > tr_src.min_thread_read_date in the where statement?, that way if the destination thread has an older read date it will not cause notifications to unexpectedly fail in the resulting thread
 
Top