• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

Not a bug Merging threads that contain zero posts

Ralle

Active member
#1
Hello guys,

I know this is a weird bug report and I understand if you feel this way.

I am working on a system that is tightly coupled with threads. The user creates an item in my system and this item is coupled with a thread that contains no posts. This thread is created using XenForo's own datawriter this way:
PHP:
$title = 'Some title';
$nodeId = 1;
$visitor = XenForo_Visitor::getInstance();

$writer = XenForo_DataWriter::create('XenForo_DataWriter_Discussion_Thread');

$writer->bulkSet(array(
    'user_id' => $visitor['user_id'],
    'username' => $visitor['username'],
    'title' => $title,
    'node_id' => $nodeId
));

$writer->setOption(XenForo_DataWriter_Discussion::OPTION_REQUIRE_INSERT_FIRST_MESSAGE, false);
$writer->setOption(XenForo_DataWriter_Discussion::OPTION_UPDATE_CONTAINER, true);
$writer->save();
As you can see, XenForo supports the creation of empty threads. It also supports viewing and moving them. Now, my problem arises when I try to merge them. I already wrote the code to merge my item type, but when it comes to merging the thread, it fails. Either I get a SQL query for a list of post IDs which is empty or when trying to set the first post.

Below is a patch I wrote to fix this issue:
Code:
diff --git a/library/XenForo/Model/Thread.php b/library/XenForo/Model/Thread.php
index 3fa4cda..639efcd 100644
--- a/library/XenForo/Model/Thread.php
+++ b/library/XenForo/Model/Thread.php
@@ -2272,10 +2272,14 @@ class XenForo_Model_Thread extends XenForo_Model

        $idsQuoted = $db->quote($mergeFromThreadIds);

-        $db->update('xf_post',
-            array('thread_id' => $targetThreadId),
-            'post_id IN (' . $db->quote($movePostIds) . ')'
-        );
+        if ($movePostIds)
+        {
+            // Only update posts if there are any
+            $db->update('xf_post',
+                array('thread_id' => $targetThreadId),
+                'post_id IN (' . $db->quote($movePostIds) . ')'
+            );
+        }
        $db->query("
            UPDATE IGNORE xf_thread_watch SET
                thread_id = ?
@@ -2288,11 +2292,7 @@ class XenForo_Model_Thread extends XenForo_Model
        ", array($targetThreadId));

        $newCounters = $postModel->recalculatePostPositionsInThread($targetThreadId);
-        if (!$newCounters['firstPostId'])
-        {
-            XenForo_Db::rollback($db);
-            return false;
-        }
+        // [Removed code block] Add support for merging empty threads

        // TODO: user message counts will go off if merging from a visible thread into a hidden one or vice versa
        // TODO: user message counts will also go off if merging from a thread in a counting forum into a non-counting forum, or vice versa
 
Last edited:

imthebest

Formerly Super120
#2
If I understand correctly, you are creating a thread which doesn't even contain the first post? How a thread could exist if it doesn't have at least one post?
 

Ralle

Active member
#3
Well, as I showed, you can create one. What I do is that I display some extra content before the posts. When submitting an item (which automatically has a thread attached), posts are essentially comments. There is no first comment unless someone posts one.
 

Mike

XenForo developer
Staff member
#4
I would say that while the system physically supports threads with no posts, it's not something that is logically allowed by XenForo and your code shouldn't be creating a thread with no posts.
 

Ralle

Active member
#5
I was waiting for you to say that. Too bad. I see how you did with your resource system and I would prefer doing it differently. with not having both a thread and a separate resource display page. I think this is a better design and you should use it yourself, but oh well :).