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.

275 lines
7.4 KiB

4 years ago
4 years ago
4 months ago
  1. <?php
  2. namespace App\Widgets\ChatActions;
  3. use App\Message;
  4. use App\Url;
  5. use App\Widgets\Chat\Chat;
  6. use App\Widgets\ContactActions\ContactActions;
  7. use App\Widgets\Dialog\Dialog;
  8. use App\Widgets\Drawer\Drawer;
  9. use App\Widgets\Toast\Toast;
  10. use Moxl\Xec\Action\Blocking\Block;
  11. use Moxl\Xec\Action\Blocking\Unblock;
  12. use Moxl\Xec\Action\Message\Moderate;
  13. use Moxl\Xec\Action\Message\Retract;
  14. use Illuminate\Database\Capsule\Manager as DB;
  15. class ChatActions extends \Movim\Widget\Base
  16. {
  17. public function load()
  18. {
  19. $this->addjs('chatactions.js');
  20. $this->addcss('chatactions.css');
  21. $this->registerEvent('blocking_block_handle', 'onBlock');
  22. $this->registerEvent('blocking_unblock_handle', 'onUnblock');
  23. }
  24. public function onBlock($packet)
  25. {
  26. Toast::send($this->__('blocked.account_blocked'));
  27. $this->rpc('Chat_ajaxGet', $packet->content);
  28. }
  29. public function onUnblock($packet)
  30. {
  31. Toast::send($this->__('blocked.account_unblocked'));
  32. $this->rpc('Chat_ajaxGet', $packet->content);
  33. }
  34. /**
  35. * @brief Get a Drawer view of a contact
  36. */
  37. public function ajaxGetContact(string $jid)
  38. {
  39. $c = new ContactActions();
  40. $c->ajaxGetDrawer($jid);
  41. }
  42. /**
  43. * @brief Block the contact
  44. */
  45. public function ajaxBlock(string $jid)
  46. {
  47. $block = new Block;
  48. $block->setJid($jid);
  49. $block->request();
  50. }
  51. /**
  52. * @brief Unblock the contact
  53. */
  54. public function ajaxUnblock(string $jid)
  55. {
  56. $unblock = new Unblock;
  57. $unblock->setJid($jid);
  58. $unblock->request();
  59. }
  60. /**
  61. * @brief Display the message dialog
  62. */
  63. public function ajaxShowMessageDialog(string $mid)
  64. {
  65. $message = $this->user->messages()
  66. ->where('mid', $mid)
  67. ->with('reactions.contact')
  68. ->first();
  69. if ($message && $message->isClassic() && !$message->retracted) {
  70. $view = $this->tpl();
  71. $message->body = $message->inlinedBody;
  72. $view->assign('message', $message);
  73. if ($message->isMuc()) {
  74. $view->assign('conference', $this->user->session->conferences()
  75. ->where('conference', $message->jidfrom)
  76. ->with('info')
  77. ->first());
  78. } else {
  79. $view->assign('conference', null);
  80. }
  81. $this->rpc('ChatActions.setMessage', $message);
  82. Dialog::fill($view->draw('_chatactions_message_dialog'));
  83. }
  84. }
  85. /**
  86. * @brief Display the search dialog
  87. */
  88. public function ajaxShowSearchDialog(string $jid, ?bool $muc = false)
  89. {
  90. if (DB::getDriverName() != 'pgsql') return;
  91. $view = $this->tpl();
  92. $view->assign('jid', $jid);
  93. $view->assign('muc', $muc);
  94. Drawer::fill('chat_search', $view->draw('_chatactions_search'));
  95. $this->rpc('ChatActions.focusSearch');
  96. }
  97. public function ajaxSearchMessages(string $jid, string $keywords, ?bool $muc = false)
  98. {
  99. if (DB::getDriverName() != 'pgsql') return;
  100. if (!validateJid($jid)) return;
  101. if (!empty($keywords)) {
  102. $keywords = str_replace(' ', ' & ', trim($keywords));
  103. $messagesQuery = \App\Message::jid($jid)
  104. ->selectRaw('*, ts_headline(\'simple\', body, plainto_tsquery(\'simple\', ?), \'StartSel=<mark>,StopSel=</mark>\') AS headline', [$keywords])
  105. ->whereRaw('to_tsvector(\'simple\', body) @@ to_tsquery(\'simple\', ?)', [$keywords])
  106. ->orderBy('published', 'desc')
  107. ->where('encrypted', false)
  108. ->where('retracted', false)
  109. ->take(20);
  110. $messagesQuery = $muc
  111. ? $messagesQuery->whereIn('type', Message::MESSAGE_TYPE_MUC)->whereNull('subject')
  112. : $messagesQuery->whereIn('type', Message::MESSAGE_TYPE);
  113. $messages = $messagesQuery->get();
  114. $view = $this->tpl();
  115. $view->assign('messages', $messages);
  116. $this->rpc('MovimTpl.fill', '#chat_search', $view->draw('_chatactions_search_result'));
  117. } else {
  118. $this->rpc('MovimTpl.fill', '#chat_search', $this->prepareSearchPlaceholder());
  119. }
  120. }
  121. public function prepareMessage(Message $message, ?bool $search = false)
  122. {
  123. $view = $this->tpl();
  124. $view->assign('message', $message);
  125. $view->assign('search', $search);
  126. return $view->draw('_chatactions_message');
  127. }
  128. public function prepareSearchPlaceholder()
  129. {
  130. $view = $this->tpl();
  131. return $view->draw('_chatactions_search_placeholder');
  132. }
  133. public function ajaxCopiedMessageText()
  134. {
  135. Toast::send($this->__('chatactions.copied_text'));
  136. }
  137. /**
  138. * @brief Edit a message
  139. */
  140. /*public function ajaxEditMessage($mid)
  141. {
  142. $this->rpc('Dialog.clear');
  143. $this->rpc('Chat.editMessage', $mid);
  144. }*/
  145. /**
  146. * @brief Retract a message
  147. *
  148. * @param string $mid
  149. * @return void
  150. */
  151. public function ajaxHttpDaemonRetract($mid)
  152. {
  153. $retract = $this->user->messages()
  154. ->where('mid', $mid)
  155. ->first();
  156. if ($retract && $retract->originid) {
  157. $this->rpc('Dialog.clear');
  158. $r = new Retract;
  159. $r->setTo($retract->jidto)
  160. ->setOriginid($retract->originid)
  161. ->request();
  162. $retract->retract();
  163. $retract->save();
  164. $packet = new \Moxl\Xec\Payload\Packet;
  165. $packet->content = $retract;
  166. $c = new Chat;
  167. $c->onMessage($packet, false, true);
  168. }
  169. }
  170. /**
  171. * @brief Moderate a message
  172. *
  173. * @param string $mid
  174. * @return void
  175. */
  176. public function ajaxHttpDaemonModerate($mid)
  177. {
  178. $retract = $this->user->messages()
  179. ->where('mid', $mid)
  180. ->first();
  181. if ($retract && $retract->stanzaid) {
  182. $this->rpc('Dialog.clear');
  183. $r = new Moderate;
  184. $r->setTo($retract->jidfrom)
  185. ->setStanzaid($retract->stanzaid)
  186. ->request();
  187. }
  188. }
  189. /**
  190. * @brief Try to resolve a message URL
  191. */
  192. public function ajaxHttpResolveMessage($mid)
  193. {
  194. $message = $this->user->messages()
  195. ->where('mid', $mid)
  196. ->first();
  197. if ($message && $message->resolved == false) {
  198. try {
  199. $url = new Url;
  200. $url->resolve(htmlspecialchars_decode(trim($message->body)));
  201. $message->urlid = $url->id;
  202. if ($url->file) {
  203. $messageFile = $url->file;
  204. $messageFile->message_mid = $message->mid;
  205. $messageFile->save();
  206. }
  207. $this->rpc('Chat.refreshMessage', $message->mid);
  208. } catch (\Exception $e) {
  209. logError($e);
  210. }
  211. $message->resolved = true;
  212. $message->save();
  213. }
  214. }
  215. /**
  216. * @brief Resolve a URL
  217. */
  218. public function ajaxHttpResolveUrl(string $url)
  219. {
  220. try {
  221. $embed = (new Url)->resolve(trim($url));
  222. if ($embed != null) {
  223. $this->rpc('MovimTpl.fill', '#embed', (new Chat)->prepareEmbed($embed));
  224. }
  225. } catch (\Exception $e) {
  226. }
  227. $this->rpc('Chat.disableSending');
  228. }
  229. }