DirectMessagePlugin.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <?php
  2. /*
  3. * GNU Social - a federating social network
  4. * Copyright (C) 2014, Free Software Foundation, Inc.
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Affero General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Affero General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Affero General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. if (!defined('GNUSOCIAL')) { exit(1); }
  20. /**
  21. * @maintainer Mikael Nordfeldth <mmn@hethane.se>
  22. */
  23. class DirectMessagePlugin extends Plugin
  24. {
  25. const PLUGIN_VERSION = '2.0.0';
  26. public function onCheckSchema()
  27. {
  28. $schema = Schema::get();
  29. $schema->ensureTable('message', Message::schemaDef());
  30. return true;
  31. }
  32. public function onRouterInitialized(URLMapper $m)
  33. {
  34. // web front-end actions
  35. $m->connect('message/new', array('action' => 'newmessage'));
  36. $m->connect('message/new?to=:to', array('action' => 'newmessage'), array('to' => Nickname::DISPLAY_FMT));
  37. $m->connect('message/:message',
  38. array('action' => 'showmessage'),
  39. array('message' => '[0-9]+'));
  40. // direct messages
  41. $m->connect('api/direct_messages.:format',
  42. array('action' => 'ApiDirectMessage',
  43. 'format' => '(xml|json|rss|atom)'));
  44. $m->connect('api/direct_messages/sent.:format',
  45. array('action' => 'ApiDirectMessage',
  46. 'format' => '(xml|json|rss|atom)',
  47. 'sent' => true));
  48. $m->connect('api/direct_messages/new.:format',
  49. array('action' => 'ApiDirectMessageNew',
  50. 'format' => '(xml|json)'));
  51. return true;
  52. }
  53. public function onAppendUserActivityStreamObjects(UserActivityStream $uas, array &$objs)
  54. {
  55. // Messages _from_ the user
  56. $msgMap = Message::listGet('from_profile', array($uas->getUser()->id));
  57. $messages = $msgMap[$uas->getUser()->id];
  58. if (!empty($uas->after)) {
  59. $messages = array_filter($messages, array($uas, 'createdAfter'));
  60. }
  61. foreach ($messages as $message) {
  62. $objs[] = clone($message);
  63. }
  64. // Messages _to_ the user
  65. $msgMap = Message::listGet('to_profile', array($uas->getUser()->id));
  66. $messages = $msgMap[$uas->getUser()->id];
  67. if (!empty($uas->after)) {
  68. $messages = array_filter($messages, array($uas, 'createdAfter'));
  69. }
  70. foreach ($messages as $message) {
  71. $objs[] = clone($message);
  72. }
  73. return true;
  74. }
  75. /**
  76. * Are we allowed to perform a certain command over the API?
  77. */
  78. public function onCommandSupportedAPI(Command $cmd, &$supported)
  79. {
  80. $supported = $supported || $cmd instanceof MessageCommand;
  81. return true;
  82. }
  83. /**
  84. * EndInterpretCommand will handle the 'd' and 'dm' commands.
  85. *
  86. * @param string $cmd Command being run
  87. * @param string $arg Rest of the message (including address)
  88. * @param User $user User sending the message
  89. * @param Command &$result The resulting command object to be run.
  90. *
  91. * @return boolean hook value
  92. */
  93. public function onStartInterpretCommand($cmd, $arg, $user, &$result)
  94. {
  95. $dm_cmds = array('d', 'dm');
  96. if ($result === false && in_array($cmd, $dm_cmds)) {
  97. if (!empty($arg)) {
  98. list($other, $extra) = CommandInterpreter::split_arg($arg);
  99. if (!empty($extra)) {
  100. $result = new MessageCommand($user, $other, $extra);
  101. }
  102. }
  103. return false;
  104. }
  105. return true;
  106. }
  107. public function onEndPersonalGroupNav(Menu $menu, Profile $target, Profile $scoped=null)
  108. {
  109. if ($scoped instanceof Profile && $scoped->id == $target->id
  110. && !common_config('singleuser', 'enabled')) {
  111. $menu->out->menuItem(common_local_url('inbox', array('nickname' =>
  112. $target->getNickname())),
  113. // TRANS: Menu item in personal group navigation menu.
  114. _m('MENU','Messages'),
  115. // TRANS: Menu item title in personal group navigation menu.
  116. _('Your incoming messages'),
  117. $scoped->id === $target->id && $menu->actionName =='inbox');
  118. }
  119. }
  120. public function onEndProfilePageActionsElements(HTMLOutputter $out, Profile $profile)
  121. {
  122. $scoped = Profile::current();
  123. if (!$scoped instanceof Profile) {
  124. return true;
  125. }
  126. if ($profile->isLocal() && $scoped->mutuallySubscribed($profile)) {
  127. $out->elementStart('li', 'entity_send-a-message');
  128. $out->element('a', array('href' => common_local_url('newmessage', array('to' => $profile->id)),
  129. // TRANS: Link title for link on user profile.
  130. 'title' => _('Send a direct message to this user.')),
  131. // TRANS: Link text for link on user profile.
  132. _m('BUTTON','Message'));
  133. $out->elementEnd('li');
  134. }
  135. return true;
  136. }
  137. public function onProfileDeleteRelated(Profile $profile, &$related)
  138. {
  139. $msg = new Message();
  140. $msg->from_profile = $profile->id;
  141. $msg->delete();
  142. $msg = new Message();
  143. $msg->to_profile = $profile->id;
  144. $msg->delete();
  145. return true;
  146. }
  147. public function onPluginVersion(array &$versions)
  148. {
  149. $versions[] = array('name' => 'Direct Message',
  150. 'version' => self::PLUGIN_VERSION,
  151. 'author' => 'Mikael Nordfeldth',
  152. 'homepage' => 'http://gnu.io/',
  153. 'rawdescription' =>
  154. // TRANS: Plugin description.
  155. _m('Direct Message to other local users (broken out of core).'));
  156. return true;
  157. }
  158. }