Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Update the Reactions implementation to fit with the upcoming XEP-xxxx…
…: Message Reactions
  • Loading branch information
edhelas committed Jul 13, 2019
1 parent c12112b commit 24e4d5d
Show file tree
Hide file tree
Showing 5 changed files with 141 additions and 95 deletions.
97 changes: 57 additions & 40 deletions app/Message.php
Expand Up @@ -118,12 +118,12 @@ public function set($stanza, $parent = false)
$this->published = gmdate('Y-m-d H:i:s');
}

if ($stanza->body || $stanza->subject) {
$this->type = 'chat';
if ($stanza->attributes()->type) {
$this->type = (string)$stanza->attributes()->type;
}
$this->type = 'chat';
if ($stanza->attributes()->type) {
$this->type = (string)$stanza->attributes()->type;
}

if ($stanza->body || $stanza->subject) {
/*if (isset($stanza->attributes()->id)) {
$this->id = (string)$stanza->attributes()->id;
}*/
Expand All @@ -132,40 +132,6 @@ public function set($stanza, $parent = false)
$this->body = (string)$stanza->body;
}

# XEP-0367: Message Attaching
if (isset($stanza->{'attach-to'})
&& $stanza->{'attach-to'}->attributes()->xmlns == 'urn:xmpp:message-attaching:1') {
$reaction = new Reaction;

$parentMessage = $this->user
->messages()
->where('replaceid', $stanza->{'attach-to'}->attributes()->id)
->where(function ($query) {
$query->where('jidfrom', $this->jidfrom)
->orWhere('jidto', $this->jidfrom);
})
->first();

$emoji = \Movim\Emoji::getInstance();
$emoji->replace($this->body);

if ($parentMessage && $emoji->isSingleEmoji()) {
$reaction->message_mid = $parentMessage->mid;
$reaction->emoji = $this->body;
$reaction->jidfrom = ($this->type == 'groupchat')
? $this->resource
: $this->jidfrom;

try {
$reaction->save();
} catch (QueryException $exception) {
// Duplicate ?
}

return $parentMessage;
}
}

if ($stanza->x
&& (string)$stanza->x->attributes()->xmlns == 'http://jabber.org/protocol/muc#user'
&& isset($jid[1])) {
Expand Down Expand Up @@ -330,12 +296,63 @@ public function set($stanza, $parent = false)

//return $this->checkPicture();
} elseif (isset($stanza->x)
&& $stanza->x->attributes()->xmlns == 'jabber:x:conference') {
&& $stanza->x->attributes()->xmlns == 'jabber:x:conference') {
$this->type = 'invitation';
$this->body = (string)$stanza->x->attributes()->reason;
$this->subject = (string)$stanza->x->attributes()->jid;
}

# XEP-xxxx: Message Reactions
elseif (isset($stanza->reactions)
&& $stanza->reactions->attributes()->xmlns == 'urn:xmpp:reactions:0') {

$parentMessage = $this->user
->messages()
->where('replaceid', (string)$stanza->reactions->attributes()->to)
->where(function ($query) {
$query->where('jidfrom', $this->jidfrom)
->orWhere('jidto', $this->jidfrom);
})
->first();

if ($parentMessage) {
$resource = ($this->type == 'groupchat')
? $this->resource
: $this->jidfrom;

$parentMessage
->reactions()
->where('jidfrom', $resource)
->delete();

$emojis = [];
$now = \Carbon\Carbon::now();
$emoji = \Movim\Emoji::getInstance();

foreach ($stanza->reactions->reaction as $children) {
$emoji->replace((string)$children);
if ($emoji->isSingleEmoji()) {
$reaction = new Reaction;
$reaction->message_mid = $parentMessage->mid;
$reaction->emoji = (string)$children;
$reaction->jidfrom = $resource;
$reaction->created_at = $now;
$reaction->updated_at = $now;

\array_push($emojis, $reaction->toArray());
}
}

try {
Reaction::insert($emojis);
} catch (QueryException $exception) {
// Duplicate ?
}

return $parentMessage;
}
}

return $this;
}

Expand Down
112 changes: 67 additions & 45 deletions app/widgets/Chat/Chat.php
@@ -1,6 +1,7 @@
<?php

use Moxl\Xec\Action\Message\Publish;
use Moxl\Xec\Action\Message\Reactions;

use Moxl\Xec\Action\Muc\GetConfig;
use Moxl\Xec\Action\Muc\SetConfig;
Expand Down Expand Up @@ -287,7 +288,7 @@ public function ajaxGetContact($jid)
* @param string $message
* @return void
*/
public function ajaxHttpSendMessage($to, $message = false, $muc = false, $resource = false, $replace = false, $file = false, $attachId = false)
public function ajaxHttpSendMessage($to, $message = false, $muc = false, $resource = false, $replace = false, $file = false)
{
$message = trim($message);
if (filter_var($message, FILTER_VALIDATE_URL)) {
Expand Down Expand Up @@ -375,40 +376,16 @@ public function ajaxHttpSendMessage($to, $message = false, $muc = false, $resour
$p->setFile($file);
}

if ($attachId) {
$parentMessage = $this->user->messages()
->where('replaceid', $attachId)
->first();

if ($parentMessage) {
if (!$p->getMuc()) {
$reaction = new Reaction;
$reaction->message_mid = $parentMessage->mid;
$reaction->jidfrom = ($muc)
? $this->user->session->username
: $this->user->id;
$reaction->emoji = $body;
$reaction->save();
}

$p->setAttachId($attachId);

$m = $parentMessage;
}
}

(ChatOwnState::getInstance())->halt();

$p->request();

/* Is it really clean ? */
if (!$p->getMuc()) {
if ($attachId == false) {
$m->oldid = $oldid;
$m->body = htmlentities(trim($m->body), ENT_XML1, 'UTF-8');
$m->save();
$m = $m->fresh();
}
$m->oldid = $oldid;
$m->body = htmlentities(trim($m->body), ENT_XML1, 'UTF-8');
$m->save();
$m = $m->fresh();

$packet = new \Moxl\Xec\Payload\Packet;
$packet->content = $m;
Expand Down Expand Up @@ -442,36 +419,81 @@ public function ajaxHttpCorrect($to, $message)
*/
public function ajaxHttpSendReaction(string $mid, string $emoji)
{
$message = $this->user->messages()
$parentMessage = $this->user->messages()
->where('mid', $mid)
->first();

if ($message) {
$emojiHandler = \Movim\Emoji::getInstance();
$emojiHandler->replace($emoji);

if ($parentMessage && $emojiHandler->isSingleEmoji()) {
// Try to load the MUC presence and resolve the resource
$mucPresence = null;
if ($message->type == 'groupchat') {
if ($parentMessage->type == 'groupchat') {
$mucPresence = $this->user->session->presences()
->where('jid', $message->jidfrom)
->where('jid', $parentMessage->jidfrom)
->where('mucjid', $this->user->id)
->where('muc', true)
->first();

if (!$mucPresence) return;
}

if ($message->reactions()
$jidfrom = ($parentMessage->type == 'groupchat')
? $mucPresence->resource
: $this->user->id;

$emojis = $parentMessage->reactions()
->where('jidfrom', $jidfrom)
->get();

$r = new Reactions;
$newEmojis = [];

// This reaction was not published yet
if ($emojis->where('emoji', $emoji)->count() == 0) {
$reaction = new Reaction;
$reaction->message_mid = $parentMessage->mid;
$reaction->jidfrom = ($parentMessage->type == 'groupchat')
? $this->user->session->username
: $this->user->id;
$reaction->emoji = $emoji;

if ($parentMessage->type != 'groupchat') {
$reaction->save();
}

$newEmojis = $emojis->push($reaction);
} else {
if ($parentMessage->type != 'groupchat') {
$parentMessage->reactions()
->where('jidfrom', $jidfrom)
->where('emoji', $emoji)
->where('jidfrom', ($message->type == 'groupchat')
? $mucPresence->resource
: $this->user->id)
->count() == 0) {
$this->ajaxHttpSendMessage(
$message->jidfrom != $message->user_id
? $message->jidfrom
: $message->jidto,
$emoji, $message->type == 'groupchat',
false, false, false, $message->replaceid
);
->delete();
}

$newEmojis = $emojis->filter(function ($value, $key) use ($emoji) {
return $value->emoji != $emoji;
});
}

$r->setTo($parentMessage->jidfrom != $parentMessage->user_id
? $parentMessage->jidfrom
: $parentMessage->jidto)
->setId(\generateUUID())
->setParentId($parentMessage->replaceid)
->setReactions($newEmojis->pluck('emoji')->toArray());

if ($parentMessage->type == 'groupchat') {
$r->setMuc();
}

$r->request();

if ($parentMessage->type != 'groupchat') {
$packet = new \Moxl\Xec\Payload\Packet;
$packet->content = $parentMessage;
$this->onMessage($packet);
}
}
}
Expand Down
22 changes: 14 additions & 8 deletions lib/moxl/src/Moxl/Stanza/Message.php
Expand Up @@ -18,7 +18,8 @@ public static function maker(
$replace = false,
$file = false,
$invite = false,
$attachId = false
$parentId = false,
array $reactions = []
) {
$session = Session::start();

Expand Down Expand Up @@ -153,19 +154,24 @@ public static function maker(
$x->appendChild($xinvite);
}

if ($attachId != false) {
$attach = $dom->createElement('attach-to');
$attach->setAttribute('xmlns', 'urn:xmpp:message-attaching:1');
$attach->setAttribute('id', $attachId);
$root->appendChild($attach);
if ($parentId != false) {
$reactionsn = $dom->createElement('reactions');
$reactionsn->setAttribute('xmlns', 'urn:xmpp:reactions:0');
$reactionsn->setAttribute('to', $parentId);

foreach ($reactions as $emoji) {
$reaction = $dom->createElement('reaction', $emoji);
$reactionsn->appendChild($reaction);
}
$root->appendChild($reactionsn);
}

\Moxl\API::request($dom->saveXML($dom->documentElement));
}

public static function message($to, $content, $html = false, $id = false, $replace = false, $file = false, $attachId = false)
public static function message($to, $content = false, $html = false, $id = false, $replace = false, $file = false, $parentId = false, array $reactions = [])
{
self::maker($to, $content, $html, 'chat', 'active', 'request', $id, $replace, $file, false, $attachId);
self::maker($to, $content, $html, 'chat', 'active', 'request', $id, $replace, $file, false, $parentId, $reactions);
}

public static function composing($to)
Expand Down
4 changes: 2 additions & 2 deletions lib/moxl/src/Moxl/Stanza/Muc.php
Expand Up @@ -8,9 +8,9 @@

class Muc
{
public static function message($to, $content, $html = false, $id = false, $file = false, $attachId = false)
public static function message($to, $content = false, $html = false, $id = false, $file = false, $parentId = false, array $reactions = [])
{
Message::maker($to, $content, $html, 'groupchat', false, false, $id, false, $file, false, $attachId);
Message::maker($to, $content, $html, 'groupchat', false, false, $id, false, $file, false, $parentId, $reactions);
}

public static function composing($to)
Expand Down
1 change: 1 addition & 0 deletions lib/moxl/src/Moxl/Utils.php
Expand Up @@ -42,6 +42,7 @@ public static function getSupportedServices()
'urn:xmpp:chat-markers:0',
'urn:xmpp:reference:0',
'urn:xmpp:message-attaching:1',
'urn:xmpp:reactions:0',

// Jingle
'http://jabber.org/protocol/jingle',
Expand Down

0 comments on commit 24e4d5d

Please sign in to comment.