Rasmus Vind
Well-known member
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:
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:
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();
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: