DirectMessagePlugin.php 6.5 KB

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