3 * GNU Social - a federating social network
4 * Copyright (C) 2014, Free Software Foundation, Inc.
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.
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.
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/>.
20 if (!defined('GNUSOCIAL')) { exit(1); }
23 * @maintainer Mikael Nordfeldth <mmn@hethane.se>
25 class DirectMessagePlugin extends Plugin
27 public function onCheckSchema()
29 $schema = Schema::get();
30 $schema->ensureTable('message', Message::schemaDef());
34 public function onRouterInitialized(URLMapper $m)
36 // web front-end actions
37 $m->connect('message/new', array('action' => 'newmessage'));
38 $m->connect('message/new?to=:to', array('action' => 'newmessage'), array('to' => Nickname::DISPLAY_FMT));
39 $m->connect('message/:message',
40 array('action' => 'showmessage'),
41 array('message' => '[0-9]+'));
44 $m->connect('api/direct_messages.:format',
45 array('action' => 'ApiDirectMessage',
46 'format' => '(xml|json|rss|atom)'));
47 $m->connect('api/direct_messages/sent.:format',
48 array('action' => 'ApiDirectMessage',
49 'format' => '(xml|json|rss|atom)',
51 $m->connect('api/direct_messages/new.:format',
52 array('action' => 'ApiDirectMessageNew',
53 'format' => '(xml|json)'));
58 public function onAppendUserActivityStreamObjects(UserActivityStream $uas, array &$objs)
60 // Messages _from_ the user
61 $msgMap = Message::listGet('from_profile', array($uas->getUser()->id));
62 $messages = $msgMap[$uas->getUser()->id];
63 if (!empty($uas->after)) {
64 $messages = array_filter($messages, array($uas, 'createdAfter'));
66 foreach ($messages as $message) {
67 $objs[] = clone($message);
70 // Messages _to_ the user
71 $msgMap = Message::listGet('to_profile', array($uas->getUser()->id));
72 $messages = $msgMap[$uas->getUser()->id];
73 if (!empty($uas->after)) {
74 $messages = array_filter($messages, array($uas, 'createdAfter'));
76 foreach ($messages as $message) {
77 $objs[] = clone($message);
84 * Are we allowed to perform a certain command over the API?
86 public function onCommandSupportedAPI(Command $cmd, &$supported)
88 $supported = $supported || $cmd instanceof MessageCommand;
93 * EndInterpretCommand will handle the 'd' and 'dm' commands.
95 * @param string $cmd Command being run
96 * @param string $arg Rest of the message (including address)
97 * @param User $user User sending the message
98 * @param Command &$result The resulting command object to be run.
100 * @return boolean hook value
102 public function onStartInterpretCommand($cmd, $arg, $user, &$result)
104 $dm_cmds = array('d', 'dm');
106 if ($result === false && in_array($cmd, $dm_cmds)) {
108 list($other, $extra) = CommandInterpreter::split_arg($arg);
109 if (!empty($extra)) {
110 $result = new MessageCommand($user, $other, $extra);
118 public function onEndPersonalGroupNav(Menu $menu, Profile $target, Profile $scoped=null)
120 if ($scoped instanceof Profile && $scoped->id == $target->id
121 && !common_config('singleuser', 'enabled')) {
123 $menu->out->menuItem(common_local_url('inbox', array('nickname' =>
124 $target->getNickname())),
125 // TRANS: Menu item in personal group navigation menu.
126 _m('MENU','Messages'),
127 // TRANS: Menu item title in personal group navigation menu.
128 _('Your incoming messages'),
129 $scoped->id === $target->id && $menu->actionName =='inbox');
133 public function onEndProfilePageActionsElements(HTMLOutputter $out, Profile $profile)
135 $scoped = Profile::current();
136 if (!$scoped instanceof Profile) {
140 if ($profile->isLocal() && $scoped->mutuallySubscribed($profile)) {
141 $out->elementStart('li', 'entity_send-a-message');
142 $out->element('a', array('href' => common_local_url('newmessage', array('to' => $profile->id)),
143 // TRANS: Link title for link on user profile.
144 'title' => _('Send a direct message to this user.')),
145 // TRANS: Link text for link on user profile.
146 _m('BUTTON','Message'));
147 $out->elementEnd('li');
152 public function onProfileDeleteRelated(Profile $profile, &$related)
154 $msg = new Message();
155 $msg->from_profile = $profile->id;
158 $msg = new Message();
159 $msg->to_profile = $profile->id;
164 public function onPluginVersion(array &$versions)
166 $versions[] = array('name' => 'Direct Message',
167 'version' => GNUSOCIAL_VERSION,
168 'author' => 'Mikael Nordfeldth',
169 'homepage' => 'http://gnu.io/',
171 // TRANS: Plugin description.
172 _m('Direct Message to other local users (broken out of core).'));