DirectMessagePlugin.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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', ['action' => 'newmessage']);
  36. $m->connect('message/new?to=:to',
  37. ['action' => 'newmessage'],
  38. ['to' => Nickname::DISPLAY_FMT]);
  39. $m->connect('message/:message',
  40. ['action' => 'showmessage'],
  41. ['message' => '[0-9]+']);
  42. // direct messages
  43. $m->connect('api/direct_messages.:format',
  44. ['action' => 'ApiDirectMessage'],
  45. ['format' => '(xml|json|rss|atom)']);
  46. $m->connect('api/direct_messages/sent.:format',
  47. ['action' => 'ApiDirectMessage',
  48. 'sent' => true],
  49. ['format' => '(xml|json|rss|atom)']);
  50. $m->connect('api/direct_messages/new.:format',
  51. ['action' => 'ApiDirectMessageNew'],
  52. ['format' => '(xml|json)']);
  53. return true;
  54. }
  55. public function onAppendUserActivityStreamObjects(UserActivityStream $uas, array &$objs)
  56. {
  57. // Messages _from_ the user
  58. $msgMap = Message::listGet('from_profile', array($uas->getUser()->id));
  59. $messages = $msgMap[$uas->getUser()->id];
  60. if (!empty($uas->after)) {
  61. $messages = array_filter($messages, array($uas, 'createdAfter'));
  62. }
  63. foreach ($messages as $message) {
  64. $objs[] = clone($message);
  65. }
  66. // Messages _to_ the user
  67. $msgMap = Message::listGet('to_profile', array($uas->getUser()->id));
  68. $messages = $msgMap[$uas->getUser()->id];
  69. if (!empty($uas->after)) {
  70. $messages = array_filter($messages, array($uas, 'createdAfter'));
  71. }
  72. foreach ($messages as $message) {
  73. $objs[] = clone($message);
  74. }
  75. return true;
  76. }
  77. /**
  78. * Are we allowed to perform a certain command over the API?
  79. */
  80. public function onCommandSupportedAPI(Command $cmd, &$supported)
  81. {
  82. $supported = $supported || $cmd instanceof MessageCommand;
  83. return true;
  84. }
  85. /**
  86. * EndInterpretCommand will handle the 'd' and 'dm' commands.
  87. *
  88. * @param string $cmd Command being run
  89. * @param string $arg Rest of the message (including address)
  90. * @param User $user User sending the message
  91. * @param Command &$result The resulting command object to be run.
  92. *
  93. * @return boolean hook value
  94. */
  95. public function onStartInterpretCommand($cmd, $arg, $user, &$result)
  96. {
  97. $dm_cmds = array('d', 'dm');
  98. if ($result === false && in_array($cmd, $dm_cmds)) {
  99. if (!empty($arg)) {
  100. list($other, $extra) = CommandInterpreter::split_arg($arg);
  101. if (!empty($extra)) {
  102. $result = new MessageCommand($user, $other, $extra);
  103. }
  104. }
  105. return false;
  106. }
  107. return true;
  108. }
  109. public function onEndPersonalGroupNav(Menu $menu, Profile $target, Profile $scoped=null)
  110. {
  111. if ($scoped instanceof Profile && $scoped->id == $target->id
  112. && !common_config('singleuser', 'enabled')) {
  113. $menu->out->menuItem(common_local_url('inbox', array('nickname' =>
  114. $target->getNickname())),
  115. // TRANS: Menu item in personal group navigation menu.
  116. _m('MENU','Messages'),
  117. // TRANS: Menu item title in personal group navigation menu.
  118. _('Your incoming messages'),
  119. $scoped->id === $target->id && $menu->actionName =='inbox');
  120. }
  121. }
  122. public function onEndProfilePageActionsElements(HTMLOutputter $out, Profile $profile)
  123. {
  124. $scoped = Profile::current();
  125. if (!$scoped instanceof Profile) {
  126. return true;
  127. }
  128. if ($profile->isLocal() && $scoped->mutuallySubscribed($profile)) {
  129. $out->elementStart('li', 'entity_send-a-message');
  130. $out->element('a', array('href' => common_local_url('newmessage', array('to' => $profile->id)),
  131. // TRANS: Link title for link on user profile.
  132. 'title' => _('Send a direct message to this user.')),
  133. // TRANS: Link text for link on user profile.
  134. _m('BUTTON','Message'));
  135. $out->elementEnd('li');
  136. }
  137. return true;
  138. }
  139. public function onProfileDeleteRelated(Profile $profile, &$related)
  140. {
  141. $msg = new Message();
  142. $msg->from_profile = $profile->id;
  143. $msg->delete();
  144. $msg = new Message();
  145. $msg->to_profile = $profile->id;
  146. $msg->delete();
  147. return true;
  148. }
  149. public function onPluginVersion(array &$versions)
  150. {
  151. $versions[] = array('name' => 'Direct Message',
  152. 'version' => self::PLUGIN_VERSION,
  153. 'author' => 'Mikael Nordfeldth',
  154. 'homepage' => 'http://gnu.io/',
  155. 'rawdescription' =>
  156. // TRANS: Plugin description.
  157. _m('Direct Message to other local users (broken out of core).'));
  158. return true;
  159. }
  160. }