From de59740a584ac1fd09be7110b593a54e71876598 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timoth=C3=A9e=20Jaussoin?= Date: Fri, 6 Nov 2020 12:21:55 +0100 Subject: [PATCH] Don't use the parent id attribute anymore but respect https://xmpp.org/extensions/xep-0201.html#new for the Reply mechanism --- app/Message.php | 13 +++++-- app/widgets/Chat/Chat.php | 5 +-- ...01328_add_parent_mid_to_messages_table.php | 35 +++++++++++++++++++ lib/moxl/src/Stanza/Message.php | 7 +--- lib/moxl/src/Xec/Action/Message/Publish.php | 5 ++- 5 files changed, 51 insertions(+), 14 deletions(-) create mode 100644 database/migrations/20201106101328_add_parent_mid_to_messages_table.php diff --git a/app/Message.php b/app/Message.php index 519e82472..36342d995 100644 --- a/app/Message.php +++ b/app/Message.php @@ -39,7 +39,7 @@ class Message extends Model public function parent() { - return $this->belongsTo('App\Message', 'parentthread', 'thread'); + return $this->belongsTo('App\Message', 'parentmid', 'mid'); } public function from() @@ -222,8 +222,15 @@ class Message extends Model if ($stanza->thread) { $this->thread = (string)$stanza->thread; - if ($stanza->thread->attributes()->parent) { - $this->parentthread = (string)$stanza->thread->attributes()->parent; + // Resolve the parent message if it exists + $parent = $this->user->messages() + ->jid($this->jidfrom) + ->where('thread', $this->thread) + ->orderBy('published', 'asc') + ->first(); + + if ($parent) { + $this->parentmid = $parent->mid; } } diff --git a/app/widgets/Chat/Chat.php b/app/widgets/Chat/Chat.php index 2499c8ce4..5fe6b7852 100644 --- a/app/widgets/Chat/Chat.php +++ b/app/widgets/Chat/Chat.php @@ -414,7 +414,9 @@ class Chat extends \Movim\Widget\Base ->first(); if ($reply) { - $m->parentthread = $reply->thread; + // See https://xmpp.org/extensions/xep-0201.html#new + $m->thread = $reply->thread; + $m->parentmid = $reply->mid; } } @@ -449,7 +451,6 @@ class Chat extends \Movim\Widget\Base $p->setId($m->id); $p->setThreadid($m->thread); - $p->setParentthreadid($m->parentthread); $p->setOriginid($m->originid); if ($muc) { diff --git a/database/migrations/20201106101328_add_parent_mid_to_messages_table.php b/database/migrations/20201106101328_add_parent_mid_to_messages_table.php new file mode 100644 index 000000000..638eec250 --- /dev/null +++ b/database/migrations/20201106101328_add_parent_mid_to_messages_table.php @@ -0,0 +1,35 @@ +disableForeignKeyCheck(); + + $this->schema->table('messages', function (Blueprint $table) { + $table->integer('parentmid')->unsigned()->nullable(); + $table->dropColumn('parentthread'); + + $table->foreign('parentmid') + ->references('mid')->on('messages') + ->onDelete('set null'); + }); + + $this->enableForeignKeyCheck(); + } + + public function down() + { + $this->disableForeignKeyCheck(); + + $this->schema->table('messages', function (Blueprint $table) { + $table->dropColumn('parentmid'); + $table->string('parentthread', 128)->nullable(); + }); + + $this->enableForeignKeyCheck(); + } +} diff --git a/lib/moxl/src/Stanza/Message.php b/lib/moxl/src/Stanza/Message.php index c09f9b1b3..8cc5f8eec 100644 --- a/lib/moxl/src/Stanza/Message.php +++ b/lib/moxl/src/Stanza/Message.php @@ -20,8 +20,7 @@ class Message $parentId = false, array $reactions = [], $originId = false, - $threadid = false, - $parentthreadid = false + $threadid = false ) { $session = Session::start(); @@ -55,10 +54,6 @@ class Message if ($threadid) { $thread = $dom->createElement('thread', $threadid); - if ($parentthreadid) { - $thread->setAttribute('parent', $parentthreadid); - } - $root->appendChild($thread); } diff --git a/lib/moxl/src/Xec/Action/Message/Publish.php b/lib/moxl/src/Xec/Action/Message/Publish.php index ba7aca672..de1d5e669 100644 --- a/lib/moxl/src/Xec/Action/Message/Publish.php +++ b/lib/moxl/src/Xec/Action/Message/Publish.php @@ -18,7 +18,6 @@ class Publish extends Action protected $_attachid = false; protected $_originid = false; protected $_threadid = false; - protected $_parentthreadid = false; public function request() { @@ -26,11 +25,11 @@ class Publish extends Action if ($this->_muc) { Muc::message($this->_to, $this->_content, $this->_html, $this->_id, $this->_file, $this->_attachid, [], $this->_originid, - $this->_threadid, $this->_parentthreadid); + $this->_threadid); } else { Message::message($this->_to, $this->_content, $this->_html, $this->_id, $this->_replace, $this->_file, $this->_attachid, [], - $this->_originid, $this->_threadid, $this->_parentthreadid); + $this->_originid, $this->_threadid); } }