You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

299 lines
8.9 KiB

6 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
9 years ago
8 years ago
4 years ago
6 years ago
8 years ago
  1. <?php
  2. use Movim\Widget\Base;
  3. use Moxl\Xec\Action\Pubsub\GetItem;
  4. use Moxl\Xec\Action\Microblog\CommentsGet;
  5. use Moxl\Xec\Action\Microblog\CommentPublish;
  6. use Respect\Validation\Validator;
  7. class Post extends Base
  8. {
  9. public function load()
  10. {
  11. $this->addjs('post.js');
  12. $this->addcss('post.css');
  13. $this->registerEvent('microblog_commentsget_handle', 'onComments', 'post');
  14. $this->registerEvent('microblog_commentpublish_handle', 'onCommentPublished');
  15. $this->registerEvent('microblog_commentpublish_error', 'onCommentPublishError');
  16. $this->registerEvent('microblog_commentsget_error', 'onCommentsError');
  17. $this->registerEvent('pubsub_getitem_handle', 'onHandle', 'post');
  18. $this->registerEvent('pubsub_postdelete_handle', 'onDelete', 'post');
  19. }
  20. public function onHandle($packet)
  21. {
  22. $post = $packet->content;
  23. if ($post->isComment()) {
  24. $parent = $post->getParent();
  25. $this->rpc(
  26. 'MovimTpl.fill',
  27. '#post_widget.'.cleanupId($parent->nodeid).' #comments',
  28. $this->prepareComments($post->getParent())
  29. );
  30. $this->rpc('MovimUtils.applyAutoheight');
  31. } else {
  32. $this->rpc(
  33. 'MovimTpl.fill',
  34. '#post_widget.'.cleanupId($post->nodeid),
  35. $this->preparePost($post)
  36. );
  37. $this->rpc('MovimUtils.enhanceArticlesContent');
  38. }
  39. }
  40. public function onCommentPublished($packet)
  41. {
  42. $isLike = $packet->content;
  43. Toast::send($isLike
  44. ? $this->__('post.comment_like_published')
  45. : $this->__('post.comment_published'));
  46. }
  47. public function onCommentPublishError()
  48. {
  49. Toast::send($this->__('post.comment_publish_error'));
  50. }
  51. public function onComments($packet)
  52. {
  53. $post = \App\Post::find($packet->content);
  54. if ($post) {
  55. $this->rpc(
  56. 'MovimTpl.fill',
  57. '#post_widget.'.cleanupId($post->nodeid).' #comments',
  58. $this->prepareComments($post)
  59. );
  60. $this->rpc('MovimUtils.applyAutoheight');
  61. }
  62. }
  63. public function onCommentsError($packet)
  64. {
  65. $view = $this->tpl();
  66. $view->assign('post', \App\Post::find($packet->content));
  67. $this->rpc('MovimTpl.fill', '#comments', $view->draw('_post_comments_error'));
  68. }
  69. public function onDelete($packet)
  70. {
  71. $this->rpc('Post.refreshComments');
  72. }
  73. public function ajaxGetContact($jid)
  74. {
  75. $c = new ContactActions;
  76. $c->ajaxGetDrawer($jid);
  77. }
  78. public function ajaxGetPost(string $server, string $node, string $nodeid)
  79. {
  80. $p = \App\Post::where('server', $server)
  81. ->where('node', $node)
  82. ->where('nodeid', $nodeid)
  83. ->with('tags')
  84. ->first();
  85. $gi = new GetItem;
  86. $gi->setTo($server)
  87. ->setNode($node)
  88. ->setId($nodeid)
  89. ->setManual()
  90. ->request();
  91. if ($p) {
  92. $p->userViews()->syncWithoutDetaching($this->user->id);
  93. $html = $this->preparePost($p, false, false, false);
  94. $this->rpc('MovimTpl.fill', '#post_widget.'.cleanupId($p->nodeid), $html);
  95. $this->rpc('MovimUtils.enhanceArticlesContent');
  96. $this->rpc('Notif.setTitle', $this->__('page.post') . ' • ' . $p->title);
  97. // If the post is a reply but we don't have the original
  98. if ($p->isReply() && !$p->getReply()) {
  99. $gi = new GetItem;
  100. $gi->setTo($p->replyserver)
  101. ->setNode($p->replynode)
  102. ->setId($p->replynodeid)
  103. ->setAskReply($p->id)
  104. ->request();
  105. }
  106. } else {
  107. $this->rpc('MovimTpl.fill', '#post_widget', $this->prepareNotFound());
  108. }
  109. }
  110. public function ajaxGetPostComments(string $server, string $node, string $id)
  111. {
  112. $post = \App\Post::where('server', $server)
  113. ->where('node', $node)
  114. ->where('nodeid', $id)
  115. ->first();
  116. if ($post) {
  117. $this->requestComments($post);
  118. }
  119. }
  120. public function ajaxShare(string $server, string $node, string $id)
  121. {
  122. $this->rpc('MovimUtils.redirect', $this->route('publish', [$server, $node, $id, 'share']));
  123. }
  124. public function requestComments(\App\Post $post)
  125. {
  126. if ($post->id == null) {
  127. return;
  128. }
  129. \App\Post::whereNotNull('parent_id')
  130. ->where('parent_id', $post->id)
  131. ->delete();
  132. $c = new CommentsGet;
  133. $c->setTo($post->commentserver)
  134. ->setId($post->commentnodeid)
  135. ->setParentId($post->id)
  136. ->request();
  137. }
  138. public function publishComment($comment, $to, $node, $id)
  139. {
  140. if (!Validator::stringType()->notEmpty()->validate($comment)
  141. || !Validator::stringType()->length(6, 128)->noWhitespace()->validate($id)) {
  142. return;
  143. }
  144. $p = \App\Post::where('server', $to)
  145. ->where('node', $node)
  146. ->where('nodeid', $id)
  147. ->first();
  148. if ($p) {
  149. $cp = new CommentPublish;
  150. $cp->setTo($p->commentserver)
  151. ->setFrom($this->user->id)
  152. ->setCommentNodeId($p->commentnodeid)
  153. ->setTitle(htmlspecialchars(rawurldecode($comment)))
  154. ->setParentId($p->id)
  155. ->request();
  156. }
  157. }
  158. public function ajaxPublishComment($form, $to, $node, $id)
  159. {
  160. $comment = trim($form->comment->value);
  161. if ($comment != '♥') {
  162. $this->publishComment($comment, $to, $node, $id);
  163. }
  164. }
  165. public function prepareComments(\App\Post $post, $public = false)
  166. {
  167. $view = $this->tpl();
  168. $view->assign('post', $post);
  169. $view->assign('public', $public);
  170. $view->assign('hearth', addEmojis('♥'));
  171. return $view->draw('_post_comments');
  172. }
  173. public function prepareNotFound()
  174. {
  175. $view = $this->tpl();
  176. return $view->draw('_post_not_found');
  177. }
  178. public function preparePost(\App\Post $post, $public = false, $card = false, $requestComments = true)
  179. {
  180. if (isset($post)) {
  181. $view = $this->tpl();
  182. $commentsDisabled = false;
  183. if ($post->hasCommentsNode()
  184. && !$public && !$card) {
  185. if ($requestComments) {
  186. $this->requestComments($post); // Broken in case of repost
  187. }
  188. } elseif (!$card) {
  189. $viewd = $this->tpl();
  190. $viewd->assign('post', $post);
  191. if ($requestComments) {
  192. $commentsDisabled = $viewd->draw('_post_comments_error');
  193. }
  194. }
  195. $view->assign('commentsdisabled', $commentsDisabled);
  196. $view->assign('public', $public);
  197. $view->assign('reply', $post->isReply() ? $post->getReply() : false);
  198. $view->assign('repost', $post->isRecycled() ? \App\Contact::find($post->server) : false);
  199. $view->assign('nsfw', $this->user->nsfw);
  200. $view->assign('post', $post);
  201. return ($card)
  202. ? $view->draw('_post_card')
  203. : $view->draw('_post');
  204. }
  205. return $this->prepareNotFound();
  206. }
  207. public function prepareTicket(\App\Post $post)
  208. {
  209. $view = $this->tpl();
  210. $view->assign('post', $post);
  211. return $view->draw('_post_ticket');
  212. }
  213. public function preparePostLinks(\App\Post $post)
  214. {
  215. $view = $this->tpl();
  216. $view->assign('post', $post);
  217. return $view->draw('_post_links');
  218. }
  219. public function preparePostReply(\App\Post $post)
  220. {
  221. if (!$post->isReply()) {
  222. return '';
  223. }
  224. $view = $this->tpl();
  225. $view->assign('reply', $post->getReply());
  226. return $view->draw('_post_reply');
  227. }
  228. public function preparePreviousNext(\App\Post $post)
  229. {
  230. $view = $this->tpl();
  231. $view->assign('post', $post);
  232. return $view->draw('_post_prevnext');
  233. }
  234. public function preparePreviousNextBack(\App\Post $post)
  235. {
  236. $view = $this->tpl();
  237. $view->assign('post', $post);
  238. $view->assign('info', \App\Info::where('server', $post->server)
  239. ->where('node', $post->node)
  240. ->first());
  241. return $view->draw('_post_prevnext_back');
  242. }
  243. public function display()
  244. {
  245. $this->view->assign('nodeid', false);
  246. if (Validator::stringType()->length(3, 256)->validate($this->get('i'))) {
  247. $this->view->assign('nodeid', $this->get('i'));
  248. }
  249. }
  250. }