| 
									
										
										
										
											2008-09-16 14:51:26 -04: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/>.
 | 
					
						
							| 
									
										
										
										
											2014-11-07 16:33:19 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-09-16 14:51:26 -04:00
										 |  |  | /** | 
					
						
							| 
									
										
										
										
											2019-08-19 22:51:51 +01:00
										 |  |  |  * GNUsocial implementation of Direct Messages | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * @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 | 
					
						
							| 
									
										
										
										
											2008-09-16 14:51:26 -04:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-19 22:51:51 +01:00
										 |  |  | defined('GNUSOCIAL') || die(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Table definition for message. | 
					
						
							| 
									
										
										
										
											2020-07-31 16:12:48 +03:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2019-08-19 22:51:51 +01:00
										 |  |  |  * Since the new updates this class only has the necessary | 
					
						
							|  |  |  |  * logic to upgrade te plugin. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * @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 | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2011-08-22 17:52:02 -04:00
										 |  |  | class Message extends Managed_DataObject | 
					
						
							| 
									
										
										
										
											2008-09-16 14:51:26 -04:00
										 |  |  | { | 
					
						
							|  |  |  |     ###START_AUTOCODE
 | 
					
						
							|  |  |  |     /* the code below is auto generated do not remove the above tag */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-19 22:51:51 +01:00
										 |  |  |     public $__table = 'message';             // table name
 | 
					
						
							| 
									
										
										
										
											2008-09-16 14:51:26 -04:00
										 |  |  |     public $id;                              // int(4)  primary_key not_null
 | 
					
						
							| 
									
										
										
										
											2015-02-12 18:18:55 +01:00
										 |  |  |     public $uri;                             // varchar(191)  unique_key   not 255 because utf8mb4 takes more space
 | 
					
						
							| 
									
										
										
										
											2008-09-16 14:51:26 -04:00
										 |  |  |     public $from_profile;                    // int(4)   not_null
 | 
					
						
							|  |  |  |     public $to_profile;                      // int(4)   not_null
 | 
					
						
							| 
									
										
										
										
											2009-08-20 17:03:49 -04:00
										 |  |  |     public $content;                         // text()
 | 
					
						
							|  |  |  |     public $rendered;                        // text()
 | 
					
						
							| 
									
										
										
										
											2015-02-12 18:18:55 +01:00
										 |  |  |     public $url;                             // varchar(191)   not 255 because utf8mb4 takes more space
 | 
					
						
							| 
									
										
										
										
											2008-09-16 14:51:26 -04:00
										 |  |  |     public $created;                         // datetime()   not_null
 | 
					
						
							|  |  |  |     public $modified;                        // timestamp()   not_null default_CURRENT_TIMESTAMP
 | 
					
						
							| 
									
										
										
										
											2009-08-20 17:03:49 -04:00
										 |  |  |     public $source;                          // varchar(32)
 | 
					
						
							| 
									
										
										
										
											2008-09-16 14:51:26 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /* the code above is auto generated do not remove the tag below */ | 
					
						
							|  |  |  |     ###END_AUTOCODE
 | 
					
						
							| 
									
										
										
										
											2009-08-20 17:03:49 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-08-22 17:52:02 -04:00
										 |  |  |     public static function schemaDef() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return array( | 
					
						
							|  |  |  |             'fields' => array( | 
					
						
							|  |  |  |                 'id' => array('type' => 'serial', 'not null' => true, 'description' => 'unique identifier'), | 
					
						
							| 
									
										
										
										
											2015-02-12 18:18:55 +01:00
										 |  |  |                 'uri' => array('type' => 'varchar', 'length' => 191, 'description' => 'universally unique identifier'), | 
					
						
							| 
									
										
										
										
											2011-08-22 17:52:02 -04:00
										 |  |  |                 'from_profile' => array('type' => 'int', 'not null' => true, 'description' => 'who the message is from'), | 
					
						
							|  |  |  |                 'to_profile' => array('type' => 'int', 'not null' => true, 'description' => 'who the message is to'), | 
					
						
							|  |  |  |                 'content' => array('type' => 'text', 'description' => 'message content'), | 
					
						
							|  |  |  |                 'rendered' => array('type' => 'text', 'description' => 'HTML version of the content'), | 
					
						
							| 
									
										
										
										
											2015-02-12 18:18:55 +01:00
										 |  |  |                 'url' => array('type' => 'varchar', 'length' => 191, 'description' => 'URL of any attachment (image, video, bookmark, whatever)'), | 
					
						
							| 
									
										
										
										
											2011-08-22 17:52:02 -04:00
										 |  |  |                 'created' => array('type' => 'datetime', 'not null' => true, 'description' => 'date this record was created'), | 
					
						
							|  |  |  |                 'modified' => array('type' => 'timestamp', 'not null' => true, 'description' => 'date this record was modified'), | 
					
						
							|  |  |  |                 'source' => array('type' => 'varchar', 'length' => 32, 'description' => 'source of comment, like "web", "im", or "clientname"'), | 
					
						
							|  |  |  |             ), | 
					
						
							|  |  |  |             'primary key' => array('id'), | 
					
						
							|  |  |  |             'unique keys' => array( | 
					
						
							|  |  |  |                 'message_uri_key' => array('uri'), | 
					
						
							|  |  |  |             ), | 
					
						
							|  |  |  |             'foreign keys' => array( | 
					
						
							|  |  |  |                 'message_from_profile_fkey' => array('profile', array('from_profile' => 'id')), | 
					
						
							|  |  |  |                 'message_to_profile_fkey' => array('profile', array('to_profile' => 'id')), | 
					
						
							|  |  |  |             ), | 
					
						
							|  |  |  |             'indexes' => array( | 
					
						
							|  |  |  |                 // @fixme these are really terrible indexes, since you can only sort on one of them at a time.
 | 
					
						
							|  |  |  |                 // looks like we really need a (to_profile, created) for inbox and a (from_profile, created) for outbox
 | 
					
						
							| 
									
										
										
										
											2020-07-31 16:12:48 +03:00
										 |  |  |                 'message_from_profile_idx' => array('from_profile'), | 
					
						
							|  |  |  |                 'message_to_profile_idx' => array('to_profile'), | 
					
						
							| 
									
										
										
										
											2011-08-22 17:52:02 -04:00
										 |  |  |                 'message_created_idx' => array('created'), | 
					
						
							|  |  |  |             ), | 
					
						
							|  |  |  |         ); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-31 16:12:48 +03:00
										 |  |  |     public function getFrom() | 
					
						
							| 
									
										
										
										
											2008-12-23 14:33:23 -05:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2013-08-18 13:04:58 +02:00
										 |  |  |         return Profile::getKV('id', $this->from_profile); | 
					
						
							| 
									
										
										
										
											2008-12-23 14:19:07 -05:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2009-08-20 17:03:49 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-31 16:12:48 +03:00
										 |  |  |     public function getTo() | 
					
						
							| 
									
										
										
										
											2008-12-23 14:33:23 -05:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2013-08-18 13:04:58 +02:00
										 |  |  |         return Profile::getKV('id', $this->to_profile); | 
					
						
							| 
									
										
										
										
											2008-12-23 14:19:07 -05:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2009-08-20 17:03:49 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-31 16:12:48 +03:00
										 |  |  |     public function getSource() | 
					
						
							| 
									
										
										
										
											2013-06-08 17:45:49 -04:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2014-11-05 19:44:22 +01:00
										 |  |  |         if (empty($this->source)) { | 
					
						
							|  |  |  |             return false; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-06-08 17:45:49 -04:00
										 |  |  |         $ns = new Notice_source(); | 
					
						
							| 
									
										
										
										
											2014-11-05 19:44:22 +01:00
										 |  |  |         switch ($this->source) { | 
					
						
							|  |  |  |         case 'web': | 
					
						
							|  |  |  |         case 'xmpp': | 
					
						
							|  |  |  |         case 'mail': | 
					
						
							|  |  |  |         case 'omb': | 
					
						
							|  |  |  |         case 'system': | 
					
						
							|  |  |  |         case 'api': | 
					
						
							|  |  |  |             $ns->code = $this->source; | 
					
						
							|  |  |  |             break; | 
					
						
							|  |  |  |         default: | 
					
						
							|  |  |  |             $ns = Notice_source::getKV($this->source); | 
					
						
							|  |  |  |             if (!$ns instanceof Notice_source) { | 
					
						
							|  |  |  |                 $ns = new Notice_source(); | 
					
						
							| 
									
										
										
										
											2013-06-08 17:45:49 -04:00
										 |  |  |                 $ns->code = $this->source; | 
					
						
							| 
									
										
										
										
											2014-11-05 19:44:22 +01:00
										 |  |  |                 $app = Oauth_application::getKV('name', $this->source); | 
					
						
							|  |  |  |                 if ($app) { | 
					
						
							|  |  |  |                     $ns->name = $app->name; | 
					
						
							|  |  |  |                     $ns->url  = $app->source_url; | 
					
						
							| 
									
										
										
										
											2013-06-08 17:45:49 -04:00
										 |  |  |                 } | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2014-11-05 19:44:22 +01:00
										 |  |  |             break; | 
					
						
							| 
									
										
										
										
											2013-06-08 17:45:49 -04:00
										 |  |  |         } | 
					
						
							|  |  |  |         return $ns; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-31 16:12:48 +03:00
										 |  |  |     public function asActivity() | 
					
						
							| 
									
										
										
										
											2013-06-08 17:45:49 -04:00
										 |  |  |     { | 
					
						
							|  |  |  |         $act = new Activity(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (Event::handle('StartMessageAsActivity', array($this, &$act))) { | 
					
						
							| 
									
										
										
										
											2019-08-19 22:51:51 +01:00
										 |  |  |             $act->verb = ActivityVerb::POST; | 
					
						
							|  |  |  |             $act->time = strtotime($this->created); | 
					
						
							| 
									
										
										
										
											2013-06-08 17:45:49 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-19 22:51:51 +01:00
										 |  |  |             $actor_profile = $this->getFrom(); | 
					
						
							|  |  |  |             if (is_null($actor_profile)) { | 
					
						
							| 
									
										
										
										
											2013-06-08 17:45:49 -04:00
										 |  |  |                 throw new Exception(sprintf("Sender profile not found: %d", $this->from_profile)); | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2019-08-19 22:51:51 +01:00
										 |  |  |             $act->actor = $actor_profile->asActivityObject(); | 
					
						
							| 
									
										
										
										
											2013-06-08 17:45:49 -04:00
										 |  |  |              | 
					
						
							| 
									
										
										
										
											2019-08-19 22:51:51 +01:00
										 |  |  |             $act->context = new ActivityContext(); | 
					
						
							|  |  |  |             $options = ['source' => $this->source, | 
					
						
							|  |  |  |                         'uri'    => TagURI::mint(sprintf('activity:message:%d', $this->id)), | 
					
						
							|  |  |  |                         'url'    => $this->uri, | 
					
						
							|  |  |  |                         'scope'  => Notice::MESSAGE_SCOPE]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             $to_profile = $this->getTo(); | 
					
						
							|  |  |  |             if (is_null($to_profile)) { | 
					
						
							| 
									
										
										
										
											2013-06-08 17:45:49 -04:00
										 |  |  |                 throw new Exception(sprintf("Receiver profile not found: %d", $this->to_profile)); | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2019-08-19 22:51:51 +01:00
										 |  |  |             $act->context->attention[$to_profile->getUri()] = ActivityObject::PERSON; | 
					
						
							| 
									
										
										
										
											2013-06-08 17:45:49 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-19 22:51:51 +01:00
										 |  |  |             $act->objects[] = ActivityObject::fromMessage($this); | 
					
						
							| 
									
										
										
										
											2013-06-08 17:45:49 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |             $source = $this->getSource(); | 
					
						
							| 
									
										
										
										
											2014-11-05 19:44:22 +01:00
										 |  |  |             if ($source instanceof Notice_source) { | 
					
						
							| 
									
										
										
										
											2013-06-08 17:45:49 -04:00
										 |  |  |                 $act->generator = ActivityObject::fromNoticeSource($source); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             Event::handle('EndMessageAsActivity', array($this, &$act)); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $act; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2008-09-16 14:51:26 -04:00
										 |  |  | } |