Browse Source

Refactor and cleanup the Post Comments related code

pull/1474/head
Timothée Jaussoin 2 months ago
parent
commit
9a9d111804
  1. 2
      app/Info.php
  2. 3
      app/Post.php
  3. 2
      app/Subscription.php
  4. 12
      app/Widgets/Account/Account.php
  5. 8
      app/Widgets/AccountNext/AccountNext.php
  6. 6
      app/Widgets/CommunityAffiliations/CommunityAffiliations.php
  7. 12
      app/Widgets/Config/Config.php
  8. 10
      app/Widgets/Confirm/Confirm.php
  9. 4
      app/Widgets/ContactBlogConfig/ContactBlogConfig.php
  10. 5
      app/Widgets/Post/Post.php
  11. 12
      app/Widgets/PostActions/PostActions.php
  12. 15
      app/Widgets/Publish/Publish.php
  13. 2
      app/Widgets/Stories/Stories.php
  14. 4
      app/Widgets/Upload/Upload.php
  15. 2
      src/Moxl/Stanza/Pubsub.php
  16. 6
      src/Moxl/Stanza/PubsubAtom.php
  17. 15
      src/Moxl/Xec/Action/Microblog/CommentPublish.php
  18. 5
      src/Moxl/Xec/Action/Microblog/CommentsGet.php
  19. 7
      src/Moxl/Xec/Action/Pubsub/PostDelete.php

2
app/Info.php

@ -552,7 +552,7 @@ class Info extends Model
public function isMicroblogCommentsNode(): bool
{
return (substr($this->node, 0, 29) == 'urn:xmpp:microblog:0:comments');
return (str_starts_with($this->node, Post::COMMENTS_NODE));
}
public function checkCapabilityHash(): bool

3
app/Post.php

@ -36,6 +36,7 @@ class Post extends Model
public $tags = [];
public const MICROBLOG_NODE = 'urn:xmpp:microblog:0';
public const COMMENTS_NODE = 'urn:xmpp:microblog:0:comments';
public const STORIES_NODE = 'urn:xmpp:pubsub-social-feed:stories:0';
public function contact()
@ -880,7 +881,7 @@ class Post extends Model
public function isComment(): bool
{
return (substr($this->node, 0, 30) == 'urn:xmpp:microblog:0:comments/');
return (str_starts_with($this->node, Post::COMMENTS_NODE));
}
public function hasCommentsNode(): bool

2
app/Subscription.php

@ -27,7 +27,7 @@ class Subscription extends Model
public function scopeNotComments($query)
{
return $query->where('node', 'not like', 'urn:xmpp:microblog:0:comments/%');
return $query->where('node', 'not like', Post::COMMENTS_NODE . '/%');
}
public function toArray()

12
app/Widgets/Account/Account.php

@ -34,16 +34,16 @@ class Account extends \Movim\Widget\Base
$this->rpc('Account.refreshFingerprints');
}
public function onAdHocList($package)
public function onAdHocList($packet)
{
$list = $package->content;
$list = $packet->content;
$view = $this->tpl();
$view->assign('list', $list);
$this->rpc(
'MovimTpl.fill',
'#gateway_' . cleanupId($package->from),
'#gateway_' . cleanupId($packet->from),
$view->draw('_account_gateway_adhoc_list')
);
}
@ -69,9 +69,9 @@ class Account extends \Movim\Widget\Base
Toast::send($this->__('client.registered'));
}
public function onRegister($package)
public function onRegister($packet)
{
$content = $package->content;
$content = $packet->content;
$view = $this->tpl();
@ -80,7 +80,7 @@ class Account extends \Movim\Widget\Base
$form = $xml->getHTML($content->x);
$view->assign('form', $form);
$view->assign('from', $package->from);
$view->assign('from', $packet->from);
$view->assign('attributes', $content->attributes());
$view->assign('actions', null);
if (isset($content->actions)) {

8
app/Widgets/AccountNext/AccountNext.php

@ -23,9 +23,9 @@ class AccountNext extends \Movim\Widget\Base
$this->registerEvent('register_get_errorserviceunavailable', 'onServiceUnavailable', 'accountnext');
}
public function onForm($package)
public function onForm($packet)
{
$form = $package->content;
$form = $packet->content;
$xtf = new XMPPtoForm;
$html = '';
@ -60,9 +60,9 @@ class AccountNext extends \Movim\Widget\Base
Toast::send($this->__('error.service_unavailable'));
}
public function onRegisterError($package)
public function onRegisterError($packet)
{
$error = $package->content;
$error = $packet->content;
Toast::send($error);
}

6
app/Widgets/CommunityAffiliations/CommunityAffiliations.php

@ -12,7 +12,7 @@ use Moxl\Xec\Action\Pubsub\Delete;
use Moxl\Xec\Action\Pubsub\GetAffiliations;
use Moxl\Xec\Action\Pubsub\SetAffiliations;
use Moxl\Xec\Action\Pubsub\GetSubscriptions;
use Moxl\Xec\Payload\Packet;
use Respect\Validation\Validator;
class CommunityAffiliations extends Base
@ -106,11 +106,11 @@ class CommunityAffiliations extends Base
Dialog::fill($view->draw('_communityaffiliations_subscriptions'), true);
}
private function deleted($packet)
private function deleted(Packet $packet)
{
if (
$packet->content['server'] != $this->me->id
&& substr($packet->content['node'], 0, 29) != 'urn:xmpp:microblog:0:comments'
&& str_starts_with($packet->content['node'], Post::COMMENTS_NODE)
) {
$this->rpc(
'MovimUtils.redirect',

12
app/Widgets/Config/Config.php

@ -44,9 +44,9 @@ class Config extends Base
return $view->draw('_config_form');
}
public function onConfig($package)
public function onConfig($packet)
{
$this->me->setConfig($package->content);
$this->me->setConfig($packet->content);
$this->me->save();
$this->refreshConfig();
@ -54,10 +54,10 @@ class Config extends Base
Toast::send($this->__('config.updated'));
}
public function onMAMConfig($package)
public function onMAMConfig($packet)
{
$view = $this->tpl();
$view->assign('default', $package->content);
$view->assign('default', $packet->content);
$this->rpc('MovimTpl.fill', '#config_widget_mam', $view->draw('_config_mam'));
}
@ -71,11 +71,11 @@ class Config extends Base
Toast::send($this->__('config.blog_saved'));
}
public function onBlogConfig($package)
public function onBlogConfig($packet)
{
$view = $this->tpl();
$value = $package->content['config']->xpath('//field[@var=\'pubsub#access_model\']/value/text()');
$value = $packet->content['config']->xpath('//field[@var=\'pubsub#access_model\']/value/text()');
if (is_array($value)) {
$view->assign('default', (string)$value[0]);

10
app/Widgets/Confirm/Confirm.php

@ -16,14 +16,14 @@ class Confirm extends Base
$this->registerEvent('confirm', 'onConfirm');
}
public function onConfirm($package)
public function onConfirm($packet)
{
$view = $this->tpl();
$view->assign('from', $package->content['from']);
$view->assign('id', $package->content['id']);
$view->assign('url', $package->content['url']);
$view->assign('method', $package->content['method']);
$view->assign('from', $packet->content['from']);
$view->assign('id', $packet->content['id']);
$view->assign('url', $packet->content['url']);
$view->assign('method', $packet->content['method']);
Dialog::fill($view->draw('_confirm'));
}

4
app/Widgets/ContactBlogConfig/ContactBlogConfig.php

@ -22,9 +22,9 @@ class ContactBlogConfig extends Base
}
}
public function onBlogConfig($package)
public function onBlogConfig($packet)
{
if ($package->content['access_model'] == 'presence') {
if ($packet->content['access_model'] == 'presence') {
$view = $this->tpl();
$this->rpc('MovimTpl.fill', '#contact_blog_config_widget', $view->draw('_contactblogconfig'));
}

5
app/Widgets/Post/Post.php

@ -56,8 +56,7 @@ class Post extends Base
public function onCommentPublished(Packet $packet)
{
$isLike = $packet->content;
Toast::send($isLike
Toast::send($packet->content
? $this->__('post.comment_like_published')
: $this->__('post.comment_published'));
}
@ -201,7 +200,7 @@ class Post extends Base
$cp = new CommentPublish;
$cp->setTo($p->commentserver)
->setFrom($this->me->id)
->setCommentNodeId($p->commentnodeid)
->setId($p->commentnodeid)
->setTitle(htmlspecialchars(rawurldecode($comment)))
->setParentId($p->id)
->request();

12
app/Widgets/PostActions/PostActions.php

@ -2,6 +2,7 @@
namespace App\Widgets\PostActions;
use App\Post as AppPost;
use App\Widgets\Dialog\Dialog;
use App\Widgets\Post\Post;
use App\Widgets\Toast\Toast;
@ -9,6 +10,7 @@ use Movim\Widget\Base;
use Moxl\Xec\Action\Pubsub\PostDelete;
use Moxl\Xec\Action\Pubsub\Delete;
use Moxl\Xec\Payload\Packet;
class PostActions extends Base
{
@ -19,18 +21,18 @@ class PostActions extends Base
$this->addjs('postactions.js');
}
public function onDelete($packet)
public function onDelete(Packet $packet)
{
list($server, $node, $id) = array_values($packet->content);
if (substr($node, 0, 29) == 'urn:xmpp:microblog:0:comments') {
if (str_starts_with($node, AppPost::COMMENTS_NODE)) {
Toast::send($this->__('post.comment_deleted'));
} else {
Toast::send($this->__('post.deleted'));
$this->rpc(
'PostActions.handleDelete',
($node == 'urn:xmpp:microblog:0') ?
($node == AppPost::MICROBLOG_NODE) ?
$this->route('news') :
$this->route('community', [$server, $node])
);
@ -39,7 +41,7 @@ class PostActions extends Base
$this->rpc('MovimTpl.remove', '#' . cleanupId($id));
}
public function ajaxLike($to, $node, $id)
public function ajaxLike(string $to, string $node, string $id)
{
$p = \App\Post::where('server', $to)
->where('node', $node)
@ -87,7 +89,7 @@ class PostActions extends Base
if (!$post->isComment()) {
$p = new Delete;
$p->setTo($post->commentserver)
->setNode('urn:xmpp:microblog:0:comments/' . $post->commentnodeid)
->setNode(AppPost::COMMENTS_NODE . '/' . $post->commentnodeid)
->request();
}
}

15
app/Widgets/Publish/Publish.php

@ -21,6 +21,7 @@ use App\Widgets\Dialog\Dialog;
use App\Widgets\Drawer\Drawer;
use App\Widgets\Post\Post;
use App\Widgets\Toast\Toast;
use Moxl\Xec\Payload\Packet;
class Publish extends Base
{
@ -52,9 +53,9 @@ class Publish extends Base
}
}
public function onBlogConfig($package)
public function onBlogConfig($packet)
{
if ($package->content['access_model'] == 'presence') {
if ($packet->content['access_model'] == 'presence') {
$view = $this->tpl();
$this->rpc('MovimTpl.fill', '#publish_blog_presence', $view->draw('_publish_blog_presence'));
}
@ -67,18 +68,18 @@ class Publish extends Base
$this->rpc('Publish.enableSend');
}
public function onCommentNodeCreated($packet)
public function onCommentNodeCreated(Packet $packet)
{
list($server, $parentid) = array_values($packet->content);
$s = new Subscribe;
$s->setTo($server)
->setFrom($this->me->id)
->setNode('urn:xmpp:microblog:0:comments/' . $parentid)
->setNode(AppPost::COMMENTS_NODE . '/' . $parentid)
->request();
}
public function ajaxCreateComments($server, $id)
public function ajaxCreateComments(string $server, string $id)
{
if (!validateServerNode($server, $id)) {
return;
@ -363,8 +364,8 @@ class Publish extends Base
$slug = $this->titleToSlug($draft->title);
$view->assign('link', ($draft->node == AppPost::MICROBLOG_NODE)
? $this->route('blog', [$draft->server, $slug])
: $this->route('community', [$draft->server, $draft->node, $slug]));
? $this->route('blog', [$draft->server, $slug])
: $this->route('community', [$draft->server, $draft->node, $slug]));
$this->rpc('MovimTpl.fill', '#publish_preview_url', $view->draw('_publish_preview_url'));
return;

2
app/Widgets/Stories/Stories.php

@ -29,7 +29,7 @@ class Stories extends Base
}
}
public function onDelete($packet)
public function onDelete(Packet $packet)
{
if ($packet->content['server'] == $this->me->id
&& $packet->content['node'] == Post::STORIES_NODE) {

4
app/Widgets/Upload/Upload.php

@ -28,9 +28,9 @@ class Upload extends Base
}
}
public function onRequested($package)
public function onRequested($packet)
{
$content = $package->content;
$content = $packet->content;
$upload = \App\Upload::find($content['id']);
$upload->puturl = $content['put'];

2
src/Moxl/Stanza/Pubsub.php

@ -53,7 +53,7 @@ class Pubsub
$pubsub->setAttribute('xmlns', 'http://jabber.org/protocol/pubsub');
$create = $dom->createElement('create');
$create->setAttribute('node', 'urn:xmpp:microblog:0:comments/' . $node);
$create->setAttribute('node', Post::COMMENTS_NODE . '/' . $node);
$pubsub->appendChild($create);
$configure = $dom->createElement('configure');

6
src/Moxl/Stanza/PubsubAtom.php

@ -75,11 +75,11 @@ class PubsubAtom
$link->setAttribute('title', 'comments');
if ($this->repost) {
$link->setAttribute('href', 'xmpp:' . $this->repost[0] . '?;node=urn:xmpp:microblog:0:comments/' . $this->repost[2]);
$link->setAttribute('href', 'xmpp:' . $this->repost[0] . '?;node=' . Post::COMMENTS_NODE . '/' . $this->repost[2]);
} elseif ($this->comments === true) {
$link->setAttribute('href', 'xmpp:' . $this->to . '?;node=urn:xmpp:microblog:0:comments/' . $this->id);
$link->setAttribute('href', 'xmpp:' . $this->to . '?;node=' . Post::COMMENTS_NODE . '/' . $this->id);
} else {
$link->setAttribute('href', 'xmpp:' . $this->comments . '?;node=urn:xmpp:microblog:0:comments/' . $this->id);
$link->setAttribute('href', 'xmpp:' . $this->comments . '?;node=' . Post::COMMENTS_NODE . '/' . $this->id);
}
$entry->appendChild($link);

15
src/Moxl/Xec/Action/Microblog/CommentPublish.php

@ -2,6 +2,7 @@
namespace Moxl\Xec\Action\Microblog;
use App\Post;
use Moxl\Stanza\Pubsub;
use Moxl\Stanza\PubsubAtom;
use Moxl\Xec\Action;
@ -12,7 +13,6 @@ class CommentPublish extends Action
protected $_to;
protected $_node;
protected $_parentid;
protected $_commentnodeid;
protected PubsubAtom $_atom;
@ -35,10 +35,9 @@ class CommentPublish extends Action
return $this;
}
public function setCommentNodeId($commentnodeid)
public function setId(string $id)
{
$this->_commentnodeid = $commentnodeid;
$this->_node = 'urn:xmpp:microblog:0:comments/'.$this->_commentnodeid;
$this->_node = Post::COMMENTS_NODE . '/' . $id;
$this->_atom->node = $this->_node;
return $this;
}
@ -71,10 +70,10 @@ class CommentPublish extends Action
{
$g = new GetItem;
$g->setTo($this->_to)
->setNode($this->_node)
->setId($this->_atom->id)
->setParentId($this->_parentid)
->request();
->setNode($this->_node)
->setId($this->_atom->id)
->setParentId($this->_parentid)
->request();
$this->pack(($this->_atom->title === '♥'));
$this->deliver();

5
src/Moxl/Xec/Action/Microblog/CommentsGet.php

@ -2,13 +2,13 @@
namespace Moxl\Xec\Action\Microblog;
use App\Post;
use Moxl\Xec\Action;
use Moxl\Stanza\Pubsub;
class CommentsGet extends Action
{
protected $_to;
protected $_id;
protected $_node;
protected $_parentid;
@ -20,8 +20,7 @@ class CommentsGet extends Action
public function setId($id)
{
$this->_id = $id;
$this->_node = 'urn:xmpp:microblog:0:comments/' . $this->_id;
$this->_node = Post::COMMENTS_NODE . '/' . $id;
return $this;
}

7
src/Moxl/Xec/Action/Pubsub/PostDelete.php

@ -20,12 +20,13 @@ class PostDelete extends Action
public function handle(?\SimpleXMLElement $stanza = null, ?\SimpleXMLElement $parent = null)
{
\App\Post::where('server', $this->_to)->where('node', $this->_node)
->where('nodeid', $this->_id)->delete();
->where('nodeid', $this->_id)->delete();
$this->pack([
'server' => $this->_to,
'node' => $this->_node,
'id' => $this->_id]);
'id' => $this->_id
]);
$this->deliver();
}
@ -33,6 +34,6 @@ class PostDelete extends Action
public function error(string $errorId, ?string $message = null)
{
\App\Post::where('server', $this->_to)->where('node', $this->_node)
->where('nodeid', $this->_id)->delete();
->where('nodeid', $this->_id)->delete();
}
}
Loading…
Cancel
Save