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.

152 lines
4.6 KiB

  1. <?php
  2. use Movim\EmbedLight;
  3. use Movim\Widget\Base;
  4. use Moxl\Xec\Action\Roster\AddItem;
  5. use Moxl\Xec\Action\Presence\Subscribe;
  6. use Respect\Validation\Validator;
  7. include_once WIDGETS_PATH.'Chat/Chat.php';
  8. include_once WIDGETS_PATH . 'Post/Post.php';
  9. class ContactActions extends Base
  10. {
  11. public function load()
  12. {
  13. $this->addjs('contactactions.js');
  14. $this->registerEvent('roster_additem_handle', 'onAdd', 'contact');
  15. $this->registerEvent('roster_removeitem_handle', 'onDelete');
  16. $this->registerEvent('roster_updateitem_handle', 'onUpdate');
  17. }
  18. public function onDelete($packet)
  19. {
  20. Toast::send($this->__('roster.deleted'));
  21. }
  22. public function onAdd($packet)
  23. {
  24. Toast::send($this->__('roster.added'));
  25. }
  26. public function onUpdate($packet = false)
  27. {
  28. Toast::send($this->__('roster.updated'));
  29. }
  30. public function ajaxAddAsk($jid)
  31. {
  32. $view = $this->tpl();
  33. $view->assign('contact', App\Contact::firstOrNew(['id' => $jid]));
  34. $view->assign('groups', $this->user->session->contacts()->select('group')->groupBy('group')->pluck('group')->toArray());
  35. Dialog::fill($view->draw('_contactactions_add'));
  36. }
  37. public function ajaxGetDrawer($jid)
  38. {
  39. if (!$this->validateJid($jid)) {
  40. return;
  41. }
  42. $tpl = $this->tpl();
  43. $tpl->assign('contact', \App\Contact::firstOrNew(['id' => $jid]));
  44. if ($jid != $this->user->id) {
  45. $tpl->assign('pictures', \App\Message::jid($jid)
  46. ->where('picture', true)
  47. ->orderBy('published', 'desc')
  48. ->take(24)
  49. ->get());
  50. $tpl->assign('links', \App\Message::jid($jid)
  51. ->where('picture', false)
  52. ->whereNotNull('urlid')
  53. ->orderBy('published', 'desc')
  54. ->take(24)
  55. ->get());
  56. $tpl->assign('roster', $this->user->session->contacts()->where('jid', $jid)->first());
  57. } else {
  58. $tpl->assign('pictures', collect());
  59. $tpl->assign('links', collect());
  60. $tpl->assign('roster', null);
  61. }
  62. $hasFingerprints = ($this->user->bundles()->where('jid', $jid)->count() > 0);
  63. $tpl->assign('jid', $jid);
  64. $tpl->assign('clienttype', getClientTypes());
  65. $tpl->assign('hasfingerprints', $hasFingerprints);
  66. $tpl->assign('posts', \App\Post::where('server', $jid)
  67. ->restrictToMicroblog()
  68. ->where('open', true)
  69. ->orderBy('published', 'desc')
  70. ->take(4)
  71. ->get()
  72. );
  73. Drawer::fill($tpl->draw('_contactactions_drawer'));
  74. $this->rpc('Tabs.create');
  75. if ($hasFingerprints) {
  76. $this->rpc('ContactActions.getDrawerFingerprints', $jid);
  77. }
  78. }
  79. public function ajaxGetDrawerFingerprints($jid, $deviceId)
  80. {
  81. $tpl = $this->tpl();
  82. $tpl->assign('fingerprints', $this->user->bundles()->where('jid', $jid)->with('sessions')->get());
  83. $tpl->assign('deviceid', $deviceId);
  84. $this->rpc('MovimTpl.fill', '#omemo_fingerprints', $tpl->draw('_contactactions_drawer_fingerprints'));
  85. }
  86. public function ajaxAdd($form)
  87. {
  88. $r = new AddItem;
  89. $r->setTo((string)$form->searchjid->value)
  90. ->setName((string)$form->alias->value)
  91. ->setGroup((string)$form->group->value)
  92. ->request();
  93. $p = new Subscribe;
  94. $p->setTo((string)$form->searchjid->value)
  95. ->request();
  96. (new Dialog)->ajaxClear();
  97. }
  98. public function ajaxChat($jid)
  99. {
  100. if (!$this->validateJid($jid)) {
  101. return;
  102. }
  103. $c = new Chats;
  104. $c->ajaxOpen($jid);
  105. $this->rpc('MovimUtils.redirect', $this->route('chat', $jid));
  106. }
  107. public function prepareEmbedUrl(EmbedLight $embed)
  108. {
  109. return (new \Chat)->prepareEmbed($embed, true);
  110. }
  111. public function prepareTicket(\App\Post $post)
  112. {
  113. return (new \Post)->prepareTicket($post);
  114. }
  115. /**
  116. * @brief Validate the jid
  117. *
  118. * @param string $jid
  119. */
  120. private function validateJid($jid)
  121. {
  122. $validate_jid = Validator::stringType()->noWhitespace()->length(6, 60);
  123. return ($validate_jid->validate($jid));
  124. }
  125. }