| 
									
										
										
										
											2014-11-07 15:47:10 +01:00
										 |  |  | <?php | 
					
						
							| 
									
										
										
										
											2019-08-19 22:51:51 +01:00
										 |  |  | // This file is part of GNU social - https://www.gnu.org/software/social
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // GNU social is free software: you can redistribute it and/or modify
 | 
					
						
							|  |  |  | // it under the terms of the GNU Affero General Public License as published by
 | 
					
						
							|  |  |  | // the Free Software Foundation, either version 3 of the License, or
 | 
					
						
							|  |  |  | // (at your option) any later version.
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // GNU social is distributed in the hope that it will be useful,
 | 
					
						
							|  |  |  | // but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
					
						
							|  |  |  | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | 
					
						
							|  |  |  | // GNU Affero General Public License for more details.
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // You should have received a copy of the GNU Affero General Public License
 | 
					
						
							|  |  |  | // along with GNU social.  If not, see <http://www.gnu.org/licenses/>.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * GNUsocial implementation of Direct Messages | 
					
						
							| 
									
										
										
										
											2014-11-07 15:47:10 +01:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2019-08-19 22:51:51 +01:00
										 |  |  |  * @package   GNUsocial | 
					
						
							|  |  |  |  * @author    Mikael Nordfeldth <mmn@hethane.se> | 
					
						
							|  |  |  |  * @author    Bruno Casteleiro <brunoccast@fc.up.pt> | 
					
						
							|  |  |  |  * @copyright 2019 Free Software Foundation, Inc http://www.fsf.org | 
					
						
							|  |  |  |  * @license   https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later | 
					
						
							| 
									
										
										
										
											2014-11-07 15:47:10 +01:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-19 22:51:51 +01:00
										 |  |  | defined('GNUSOCIAL') || die(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // require needed abstractions first
 | 
					
						
							|  |  |  | require_once __DIR__ . DIRECTORY_SEPARATOR . 'lib' . DIRECTORY_SEPARATOR . 'messagelist.php'; | 
					
						
							|  |  |  | require_once __DIR__ . DIRECTORY_SEPARATOR . 'lib' . DIRECTORY_SEPARATOR . 'messagelistitem.php'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Import plugin libs
 | 
					
						
							|  |  |  | foreach (glob(__DIR__ . DIRECTORY_SEPARATOR . 'lib' . DIRECTORY_SEPARATOR . '*.php') as $filename) { | 
					
						
							|  |  |  |     require_once $filename; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | // Import plugin models
 | 
					
						
							|  |  |  | foreach (glob(__DIR__ . DIRECTORY_SEPARATOR . 'lib' . DIRECTORY_SEPARATOR . 'models' . DIRECTORY_SEPARATOR . '*.php') as $filename) { | 
					
						
							|  |  |  |     require_once $filename; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2014-11-07 15:47:10 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							| 
									
										
										
										
											2019-08-19 22:51:51 +01:00
										 |  |  |  * @category  Plugin | 
					
						
							|  |  |  |  * @package   GNUsocial | 
					
						
							|  |  |  |  * @author    Mikael Nordfeldth <mmn@hethane.se> | 
					
						
							|  |  |  |  * @author    Bruno Casteleiro <brunoccast@fc.up.pt> | 
					
						
							|  |  |  |  * @license   https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later | 
					
						
							| 
									
										
										
										
											2014-11-07 15:47:10 +01:00
										 |  |  |  */ | 
					
						
							|  |  |  | class DirectMessagePlugin extends Plugin | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2019-08-19 22:51:51 +01:00
										 |  |  |     const PLUGIN_VERSION = '3.0.0'; | 
					
						
							| 
									
										
										
										
											2014-11-07 15:47:10 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     public function onRouterInitialized(URLMapper $m) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         // web front-end actions
 | 
					
						
							| 
									
										
										
										
											2020-06-24 12:42:05 +01:00
										 |  |  |         $m->connect( | 
					
						
							|  |  |  |             'message/new', | 
					
						
							|  |  |  |             ['action' => 'newmessage'] | 
					
						
							|  |  |  |         ); | 
					
						
							|  |  |  |         $m->connect( | 
					
						
							|  |  |  |             'message/new?to=:to', | 
					
						
							|  |  |  |             ['action' => 'newmessage'], | 
					
						
							|  |  |  |             ['to'     => '[0-9]+'] | 
					
						
							|  |  |  |         ); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $m->connect( | 
					
						
							|  |  |  |             'message/:message', | 
					
						
							|  |  |  |             ['action'  => 'showmessage'], | 
					
						
							|  |  |  |             ['message' => '[0-9]+'] | 
					
						
							|  |  |  |         ); | 
					
						
							| 
									
										
										
										
											2014-11-07 15:47:10 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         // direct messages
 | 
					
						
							| 
									
										
										
										
											2020-06-24 12:42:05 +01:00
										 |  |  |         $m->connect( | 
					
						
							|  |  |  |             'api/direct_messages.:format', | 
					
						
							|  |  |  |             ['action' => 'ApiDirectMessage'], | 
					
						
							|  |  |  |             ['format' => '(xml|json|rss|atom)'] | 
					
						
							|  |  |  |         ); | 
					
						
							|  |  |  |         $m->connect( | 
					
						
							|  |  |  |             'api/direct_messages/sent.:format', | 
					
						
							| 
									
										
										
										
											2020-07-28 04:46:10 +01:00
										 |  |  |             [ | 
					
						
							|  |  |  |                 'action' => 'ApiDirectMessage', | 
					
						
							|  |  |  |                 'sent'   => true, | 
					
						
							|  |  |  |             ], | 
					
						
							| 
									
										
										
										
											2020-06-24 12:42:05 +01:00
										 |  |  |             ['format' => '(xml|json|rss|atom)'] | 
					
						
							|  |  |  |         ); | 
					
						
							|  |  |  |         $m->connect( | 
					
						
							|  |  |  |             'api/direct_messages/new.:format', | 
					
						
							|  |  |  |             ['action' => 'ApiDirectMessageNew'], | 
					
						
							|  |  |  |             ['format' => '(xml|json)'] | 
					
						
							|  |  |  |         ); | 
					
						
							| 
									
										
										
										
											2014-11-07 15:47:10 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         return true; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-07 16:23:34 +01:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Are we allowed to perform a certain command over the API? | 
					
						
							| 
									
										
										
										
											2020-06-24 12:42:05 +01:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2019-08-19 22:51:51 +01:00
										 |  |  |      * @param Command $cmd | 
					
						
							|  |  |  |      * @param bool &$supported | 
					
						
							|  |  |  |      * @return bool hook value | 
					
						
							| 
									
										
										
										
											2014-11-07 16:23:34 +01:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2019-08-19 22:51:51 +01:00
										 |  |  |     public function onCommandSupportedAPI(Command $cmd, ?bool &$supported) : bool | 
					
						
							| 
									
										
										
										
											2014-11-07 16:23:34 +01:00
										 |  |  |     { | 
					
						
							|  |  |  |         $supported = $supported || $cmd instanceof MessageCommand; | 
					
						
							|  |  |  |         return true; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * EndInterpretCommand will handle the 'd' and 'dm' commands. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @param string  $cmd     Command being run | 
					
						
							|  |  |  |      * @param string  $arg     Rest of the message (including address) | 
					
						
							|  |  |  |      * @param User    $user    User sending the message | 
					
						
							| 
									
										
										
										
											2020-06-24 12:42:05 +01:00
										 |  |  |      * @param MessageCommand|bool &$result The resulting command object to be run. | 
					
						
							| 
									
										
										
										
											2019-08-19 22:51:51 +01:00
										 |  |  |      * @return bool hook value | 
					
						
							| 
									
										
										
										
											2014-11-07 16:23:34 +01:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2019-08-23 23:44:19 +01:00
										 |  |  |     public function onStartInterpretCommand(string $cmd, ?string $arg, User $user, &$result) : bool | 
					
						
							| 
									
										
										
										
											2014-11-07 16:23:34 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2019-08-19 22:51:51 +01:00
										 |  |  |         $dm_cmds = ['d', 'dm']; | 
					
						
							| 
									
										
										
										
											2014-11-07 16:23:34 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         if ($result === false && in_array($cmd, $dm_cmds)) { | 
					
						
							|  |  |  |             if (!empty($arg)) { | 
					
						
							|  |  |  |                 list($other, $extra) = CommandInterpreter::split_arg($arg); | 
					
						
							|  |  |  |                 if (!empty($extra)) { | 
					
						
							|  |  |  |                     $result = new MessageCommand($user, $other, $extra); | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             return false; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         return true; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-19 22:51:51 +01:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Show Message button in someone's left-side navigation menu | 
					
						
							| 
									
										
										
										
											2020-06-24 12:42:05 +01:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2019-08-19 22:51:51 +01:00
										 |  |  |      * @param Menu $menu | 
					
						
							|  |  |  |      * @param Profile $target | 
					
						
							|  |  |  |      * @param Profile $scoped | 
					
						
							|  |  |  |      * @return void | 
					
						
							| 
									
										
										
										
											2020-06-24 12:42:05 +01:00
										 |  |  |      * @throws ServerException | 
					
						
							| 
									
										
										
										
											2019-08-19 22:51:51 +01:00
										 |  |  |      */ | 
					
						
							|  |  |  |     public function onEndPersonalGroupNav(Menu $menu, Profile $target, Profile $scoped = null) | 
					
						
							| 
									
										
										
										
											2014-11-07 15:47:10 +01:00
										 |  |  |     { | 
					
						
							|  |  |  |         if ($scoped instanceof Profile && $scoped->id == $target->id | 
					
						
							|  |  |  |                 && !common_config('singleuser', 'enabled')) { | 
					
						
							| 
									
										
										
										
											2020-06-24 12:42:05 +01:00
										 |  |  |             $menu->out->menuItem( | 
					
						
							|  |  |  |                 common_local_url('inbox', ['nickname' => $target->getNickname()]), | 
					
						
							| 
									
										
										
										
											2014-11-07 15:47:10 +01:00
										 |  |  |                                  // TRANS: Menu item in personal group navigation menu.
 | 
					
						
							| 
									
										
										
										
											2020-06-24 12:42:05 +01:00
										 |  |  |                                  _m('MENU', 'Messages'), | 
					
						
							| 
									
										
										
										
											2014-11-07 15:47:10 +01:00
										 |  |  |                                  // TRANS: Menu item title in personal group navigation menu.
 | 
					
						
							|  |  |  |                                  _('Your incoming messages'), | 
					
						
							| 
									
										
										
										
											2020-06-24 12:42:05 +01:00
										 |  |  |                 $scoped->id === $target->id && $menu->actionName =='inbox' | 
					
						
							|  |  |  |             ); | 
					
						
							| 
									
										
										
										
											2014-11-07 15:47:10 +01:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-19 22:51:51 +01:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Show Message button in someone's profile page | 
					
						
							| 
									
										
										
										
											2020-06-24 12:42:05 +01:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2019-08-19 22:51:51 +01:00
										 |  |  |      * @param HTMLOutputter $out | 
					
						
							|  |  |  |      * @param Profile $profile | 
					
						
							|  |  |  |      * @return bool hook flag | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function onEndProfilePageActionsElements(HTMLOutputter $out, Profile $profile) : bool | 
					
						
							| 
									
										
										
										
											2014-11-07 15:47:10 +01:00
										 |  |  |     { | 
					
						
							|  |  |  |         $scoped = Profile::current(); | 
					
						
							| 
									
										
										
										
											2019-08-19 22:51:51 +01:00
										 |  |  |         if (!$scoped instanceof Profile || $scoped->getID() === $profile->getID()) { | 
					
						
							| 
									
										
										
										
											2014-11-07 15:47:10 +01:00
										 |  |  |             return true; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-19 22:51:51 +01:00
										 |  |  |         if (!$profile->isLocal() && Event::handle('DirectMessageProfilePageActions', [$profile])) { | 
					
						
							|  |  |  |             // nothing to do if remote profile and no one to validate it
 | 
					
						
							|  |  |  |             return true; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (!$profile->hasBlocked($scoped)) { | 
					
						
							| 
									
										
										
										
											2014-11-07 15:47:10 +01:00
										 |  |  |             $out->elementStart('li', 'entity_send-a-message'); | 
					
						
							| 
									
										
										
										
											2020-06-24 12:42:05 +01:00
										 |  |  |             $out->element( | 
					
						
							|  |  |  |                 'a', | 
					
						
							|  |  |  |                 ['href' => common_local_url('newmessage', ['to' => $profile->getID()]), | 
					
						
							| 
									
										
										
										
											2019-08-19 22:51:51 +01:00
										 |  |  |                         // TRANS: Link title for link on user profile.
 | 
					
						
							|  |  |  |                         'title' => _('Send a direct message to this user.')], | 
					
						
							|  |  |  |                         // TRANS: Link text for link on user profile.
 | 
					
						
							| 
									
										
										
										
											2020-06-24 12:42:05 +01:00
										 |  |  |                         _m('BUTTON', 'Message') | 
					
						
							|  |  |  |             ); | 
					
						
							| 
									
										
										
										
											2014-11-07 15:47:10 +01:00
										 |  |  |             $out->elementEnd('li'); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2020-07-28 04:46:10 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-07 15:47:10 +01:00
										 |  |  |         return true; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-19 22:51:51 +01:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Notice table is used to store private messages in a newer version of the plugin, | 
					
						
							|  |  |  |      * this ensures we migrate entries from the old message table. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return bool hook flag | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function onEndUpgrade() : bool | 
					
						
							| 
									
										
										
										
											2014-11-07 15:47:10 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2019-08-19 22:51:51 +01:00
										 |  |  |         try { | 
					
						
							|  |  |  |             $schema = Schema::get(); | 
					
						
							|  |  |  |             $schema->getTableDef('message'); | 
					
						
							|  |  |  |         } catch (SchemaTableMissingException $e) { | 
					
						
							|  |  |  |             return true; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $message = new Message(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $message->selectAdd(); // clears it
 | 
					
						
							|  |  |  |         $message->selectAdd('id'); | 
					
						
							|  |  |  |         $message->orderBy('created ASC'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if ($message->find()) { | 
					
						
							|  |  |  |             while ($message->fetch()) { | 
					
						
							|  |  |  |                 $msg = Message::getKV('id', $message->id); | 
					
						
							|  |  |  |                 $act = $msg->asActivity(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-24 12:42:05 +01:00
										 |  |  |                 Notice::saveActivity( | 
					
						
							|  |  |  |                     $act, | 
					
						
							|  |  |  |                     $msg->getFrom(), | 
					
						
							| 
									
										
										
										
											2020-07-28 04:46:10 +01:00
										 |  |  |                     [ | 
					
						
							|  |  |  |                         'source' => 'web', | 
					
						
							|  |  |  |                         'scope'  => NOTICE::MESSAGE_SCOPE, | 
					
						
							|  |  |  |                     ] | 
					
						
							| 
									
										
										
										
											2020-06-24 12:42:05 +01:00
										 |  |  |                 ); | 
					
						
							| 
									
										
										
										
											2019-08-19 22:51:51 +01:00
										 |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $message->free(); | 
					
						
							|  |  |  |         $message = null; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $schema->dropTable('message'); | 
					
						
							| 
									
										
										
										
											2014-11-07 15:47:10 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         return true; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-12 15:03:30 +01:00
										 |  |  |     public function onPluginVersion(array &$versions): bool | 
					
						
							| 
									
										
										
										
											2014-11-07 15:47:10 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2019-08-12 15:03:30 +01:00
										 |  |  |         $versions[] = [ | 
					
						
							|  |  |  |             'name' => 'Direct Message', | 
					
						
							|  |  |  |             'version' => self::PLUGIN_VERSION, | 
					
						
							|  |  |  |             'author' => 'Mikael Nordfeldth, Bruno Casteleiro', | 
					
						
							|  |  |  |             'homepage' => 'https://gnu.social/', | 
					
						
							|  |  |  |             'rawdescription' => | 
					
						
							|  |  |  |             // TRANS: Plugin description.
 | 
					
						
							|  |  |  |             _m('Direct Message to other local users.') | 
					
						
							|  |  |  |         ]; | 
					
						
							| 
									
										
										
										
											2014-11-07 15:47:10 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         return true; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } |