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 const PLUGIN_VERSION = '2.0.0';
29 public function onCheckSchema()
31 $schema = Schema::get();
32 $schema->ensureTable('message', Message::schemaDef());
36 public function onRouterInitialized(URLMapper $m)
38 // web front-end actions
39 $m->connect('message/new', array('action' => 'newmessage'));
40 $m->connect('message/new?to=:to', array('action' => 'newmessage'), array('to' => Nickname::DISPLAY_FMT));
41 $m->connect('message/:message',
42 array('action' => 'showmessage'),
43 array('message' => '[0-9]+'));
46 $m->connect('api/direct_messages.:format',
47 array('action' => 'ApiDirectMessage',
48 'format' => '(xml|json|rss|atom)'));
49 $m->connect('api/direct_messages/sent.:format',
50 array('action' => 'ApiDirectMessage',
51 'format' => '(xml|json|rss|atom)',
53 $m->connect('api/direct_messages/new.:format',
54 array('action' => 'ApiDirectMessageNew',
55 'format' => '(xml|json)'));
60 public function onAppendUserActivityStreamObjects(UserActivityStream $uas, array &$objs)
62 // Messages _from_ the user
63 $msgMap = Message::listGet('from_profile', array($uas->getUser()->id));
64 $messages = $msgMap[$uas->getUser()->id];
65 if (!empty($uas->after)) {
66 $messages = array_filter($messages, array($uas, 'createdAfter'));
68 foreach ($messages as $message) {
69 $objs[] = clone($message);
72 // Messages _to_ the user
73 $msgMap = Message::listGet('to_profile', array($uas->getUser()->id));
74 $messages = $msgMap[$uas->getUser()->id];
75 if (!empty($uas->after)) {
76 $messages = array_filter($messages, array($uas, 'createdAfter'));
78 foreach ($messages as $message) {
79 $objs[] = clone($message);
86 * Are we allowed to perform a certain command over the API?
88 public function onCommandSupportedAPI(Command $cmd, &$supported)
90 $supported = $supported || $cmd instanceof MessageCommand;
95 * EndInterpretCommand will handle the 'd' and 'dm' commands.
97 * @param string $cmd Command being run
98 * @param string $arg Rest of the message (including address)
99 * @param User $user User sending the message
100 * @param Command &$result The resulting command object to be run.
102 * @return boolean hook value
104 public function onStartInterpretCommand($cmd, $arg, $user, &$result)
106 $dm_cmds = array('d', 'dm');
108 if ($result === false && in_array($cmd, $dm_cmds)) {
110 list($other, $extra) = CommandInterpreter::split_arg($arg);
111 if (!empty($extra)) {
112 $result = new MessageCommand($user, $other, $extra);
120 public function onEndPersonalGroupNav(Menu $menu, Profile $target, Profile $scoped=null)
122 if ($scoped instanceof Profile && $scoped->id == $target->id
123 && !common_config('singleuser', 'enabled')) {
125 $menu->out->menuItem(common_local_url('inbox', array('nickname' =>
126 $target->getNickname())),
127 // TRANS: Menu item in personal group navigation menu.
128 _m('MENU','Messages'),
129 // TRANS: Menu item title in personal group navigation menu.
130 _('Your incoming messages'),
131 $scoped->id === $target->id && $menu->actionName =='inbox');
135 public function onEndProfilePageActionsElements(HTMLOutputter $out, Profile $profile)
137 $scoped = Profile::current();
138 if (!$scoped instanceof Profile) {
142 if ($profile->isLocal() && $scoped->mutuallySubscribed($profile)) {
143 $out->elementStart('li', 'entity_send-a-message');
144 $out->element('a', array('href' => common_local_url('newmessage', array('to' => $profile->id)),
145 // TRANS: Link title for link on user profile.
146 'title' => _('Send a direct message to this user.')),
147 // TRANS: Link text for link on user profile.
148 _m('BUTTON','Message'));
149 $out->elementEnd('li');
154 public function onProfileDeleteRelated(Profile $profile, &$related)
156 $msg = new Message();
157 $msg->from_profile = $profile->id;
160 $msg = new Message();
161 $msg->to_profile = $profile->id;
166 public function onPluginVersion(array &$versions)
168 $versions[] = array('name' => 'Direct Message',
169 'version' => self::PLUGIN_VERSION,
170 'author' => 'Mikael Nordfeldth',
171 'homepage' => 'http://gnu.io/',
173 // TRANS: Plugin description.
174 _m('Direct Message to other local users (broken out of core).'));