]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/DirectMessage/DirectMessagePlugin.php
Merge remote-tracking branch 'upstream/master' into social-master
[quix0rs-gnu-social.git] / plugins / DirectMessage / DirectMessagePlugin.php
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
20 if (!defined('GNUSOCIAL')) { exit(1); }
21
22 /**
23  * @maintainer  Mikael Nordfeldth <mmn@hethane.se>
24  */
25 class DirectMessagePlugin extends Plugin
26 {
27     public function onCheckSchema()
28     {
29         $schema = Schema::get();
30         $schema->ensureTable('message', Message::schemaDef());
31         return true;
32     }
33
34     public function onRouterInitialized(URLMapper $m)
35     {
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]+'));
42
43         // direct messages
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)',
50                           'sent' => true));
51         $m->connect('api/direct_messages/new.:format',
52                     array('action' => 'ApiDirectMessageNew',
53                           'format' => '(xml|json)'));
54
55         return true;
56     }
57
58     public function onAppendUserActivityStreamObjects(UserActivityStream $uas, array &$objs)
59     {
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'));
65         }
66         foreach ($messages as $message) {
67             $objs[] = clone($message);
68         }
69
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'));
75         }
76         foreach ($messages as $message) {
77             $objs[] = clone($message);
78         }
79
80         return true;
81     }
82
83     /**
84      * Are we allowed to perform a certain command over the API?
85      */
86     public function onCommandSupportedAPI(Command $cmd, &$supported)
87     {
88         $supported = $supported || $cmd instanceof MessageCommand;
89         return true;
90     }
91
92     /**
93      * EndInterpretCommand will handle the 'd' and 'dm' commands.
94      *
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.
99      *
100      * @return boolean hook value
101      */
102     public function onStartInterpretCommand($cmd, $arg, $user, &$result)
103     {
104         $dm_cmds = array('d', 'dm');
105
106         if ($result === false && in_array($cmd, $dm_cmds)) {
107             if (!empty($arg)) {
108                 list($other, $extra) = CommandInterpreter::split_arg($arg);
109                 if (!empty($extra)) {
110                     $result = new MessageCommand($user, $other, $extra);
111                 }
112             }
113             return false;
114         }
115         return true;
116     }
117
118     public function onEndPersonalGroupNav(Menu $menu, Profile $target, Profile $scoped=null)
119     {
120         if ($scoped instanceof Profile && $scoped->id == $target->id
121                 && !common_config('singleuser', 'enabled')) {
122
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');
130         }
131     }
132
133     public function onEndProfilePageActionsElements(HTMLOutputter $out, Profile $profile)
134     {
135         $scoped = Profile::current();
136         if (!$scoped instanceof Profile) {
137             return true;
138         }
139
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');
148         }
149         return true;
150     }
151
152     public function onProfileDeleteRelated(Profile $profile, array &$related)
153     {
154         $msg = new Message();
155         $msg->from_profile = $profile->id;
156         $msg->delete();
157
158         $msg = new Message();
159         $msg->to_profile = $profile->id;
160         $msg->delete();
161         return true;
162     }
163
164     public function onPluginVersion(array &$versions)
165     {
166         $versions[] = array('name' => 'Direct Message',
167                             'version' => GNUSOCIAL_VERSION,
168                             'author' => 'Mikael Nordfeldth',
169                             'homepage' => 'http://gnu.io/',
170                             'rawdescription' =>
171                             // TRANS: Plugin description.
172                             _m('Direct Message to other local users (broken out of core).'));
173
174         return true;
175     }
176 }