]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/DirectMessage/DirectMessagePlugin.php
[INSTALL] Fixed issue in installing where default.php needs util.php but it's not...
[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     const PLUGIN_VERSION = '2.0.0';
28
29     public function onCheckSchema()
30     {
31         $schema = Schema::get();
32         $schema->ensureTable('message', Message::schemaDef());
33         return true;
34     }
35
36     public function onRouterInitialized(URLMapper $m)
37     {
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]+'));
44
45         // direct messages
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)',
52                           'sent' => true));
53         $m->connect('api/direct_messages/new.:format',
54                     array('action' => 'ApiDirectMessageNew',
55                           'format' => '(xml|json)'));
56
57         return true;
58     }
59
60     public function onAppendUserActivityStreamObjects(UserActivityStream $uas, array &$objs)
61     {
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'));
67         }
68         foreach ($messages as $message) {
69             $objs[] = clone($message);
70         }
71
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'));
77         }
78         foreach ($messages as $message) {
79             $objs[] = clone($message);
80         }
81
82         return true;
83     }
84
85     /**
86      * Are we allowed to perform a certain command over the API?
87      */
88     public function onCommandSupportedAPI(Command $cmd, &$supported)
89     {
90         $supported = $supported || $cmd instanceof MessageCommand;
91         return true;
92     }
93
94     /**
95      * EndInterpretCommand will handle the 'd' and 'dm' commands.
96      *
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.
101      *
102      * @return boolean hook value
103      */
104     public function onStartInterpretCommand($cmd, $arg, $user, &$result)
105     {
106         $dm_cmds = array('d', 'dm');
107
108         if ($result === false && in_array($cmd, $dm_cmds)) {
109             if (!empty($arg)) {
110                 list($other, $extra) = CommandInterpreter::split_arg($arg);
111                 if (!empty($extra)) {
112                     $result = new MessageCommand($user, $other, $extra);
113                 }
114             }
115             return false;
116         }
117         return true;
118     }
119
120     public function onEndPersonalGroupNav(Menu $menu, Profile $target, Profile $scoped=null)
121     {
122         if ($scoped instanceof Profile && $scoped->id == $target->id
123                 && !common_config('singleuser', 'enabled')) {
124
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');
132         }
133     }
134
135     public function onEndProfilePageActionsElements(HTMLOutputter $out, Profile $profile)
136     {
137         $scoped = Profile::current();
138         if (!$scoped instanceof Profile) {
139             return true;
140         }
141
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');
150         }
151         return true;
152     }
153
154     public function onProfileDeleteRelated(Profile $profile, &$related)
155     {
156         $msg = new Message();
157         $msg->from_profile = $profile->id;
158         $msg->delete();
159
160         $msg = new Message();
161         $msg->to_profile = $profile->id;
162         $msg->delete();
163         return true;
164     }
165
166     public function onPluginVersion(array &$versions)
167     {
168         $versions[] = array('name' => 'Direct Message',
169                             'version' => self::PLUGIN_VERSION,
170                             'author' => 'Mikael Nordfeldth',
171                             'homepage' => 'http://gnu.io/',
172                             'rawdescription' =>
173                             // TRANS: Plugin description.
174                             _m('Direct Message to other local users (broken out of core).'));
175
176         return true;
177     }
178 }