| 
									
										
										
										
											2010-02-16 23:30:08 -08:00
										 |  |  | <?php | 
					
						
							| 
									
										
										
										
											2019-09-11 08:32:19 +03: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/>.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-02-16 23:30:08 -08:00
										 |  |  | /** | 
					
						
							|  |  |  |  * Data class for Conversations | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * @category  Data | 
					
						
							| 
									
										
										
										
											2019-09-11 08:32:19 +03:00
										 |  |  |  * @package   GNUsocial | 
					
						
							| 
									
										
										
										
											2010-02-16 23:30:08 -08:00
										 |  |  |  * @author    Zach Copley <zach@status.net> | 
					
						
							| 
									
										
										
										
											2014-03-01 17:06:29 +01:00
										 |  |  |  * @author    Mikael Nordfeldth <mmn@hethane.se> | 
					
						
							| 
									
										
										
										
											2010-02-16 23:30:08 -08:00
										 |  |  |  * @copyright 2010 StatusNet Inc. | 
					
						
							| 
									
										
										
										
											2014-03-01 17:06:29 +01:00
										 |  |  |  * @copyright 2009-2014 Free Software Foundation, Inc http://www.fsf.org | 
					
						
							| 
									
										
										
										
											2019-09-11 08:32:19 +03:00
										 |  |  |  * @license   https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later | 
					
						
							| 
									
										
										
										
											2010-02-16 23:30:08 -08:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-11 08:32:19 +03:00
										 |  |  | defined('GNUSOCIAL') || die(); | 
					
						
							| 
									
										
										
										
											2010-02-16 23:30:08 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-08-22 17:52:02 -04:00
										 |  |  | class Conversation extends Managed_DataObject | 
					
						
							| 
									
										
										
										
											2010-02-16 23:30:08 -08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2014-03-01 17:06:29 +01:00
										 |  |  |     public $__table = 'conversation';        // table name
 | 
					
						
							| 
									
										
										
										
											2016-01-06 22:26:17 +01:00
										 |  |  |     public $id;                              // int(4)  primary_key not_null auto_increment
 | 
					
						
							| 
									
										
										
										
											2015-02-12 18:18:55 +01:00
										 |  |  |     public $uri;                             // varchar(191)  unique_key   not 255 because utf8mb4 takes more space
 | 
					
						
							| 
									
										
										
										
											2017-05-02 18:58:22 +02:00
										 |  |  |     public $url;                             // varchar(191)  unique_key   not 255 because utf8mb4 takes more space
 | 
					
						
							| 
									
										
										
										
											2019-07-25 00:43:25 +01:00
										 |  |  |     public $created;                         // datetime()   not_null default_0000-00-00%2000%3A00%3A00
 | 
					
						
							|  |  |  |     public $modified;                        // datetime()   not_null default_CURRENT_TIMESTAMP
 | 
					
						
							| 
									
										
										
										
											2010-02-16 23:30:08 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-08-22 17:52:02 -04:00
										 |  |  |     public static function schemaDef() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return array( | 
					
						
							|  |  |  |             'fields' => array( | 
					
						
							| 
									
										
										
										
											2016-01-06 13:58:46 +01:00
										 |  |  |                 'id' => array('type' => 'serial', 'not null' => true, 'description' => 'Unique identifier, (again) unrelated to notice id since 2016-01-06'), | 
					
						
							| 
									
										
										
										
											2015-02-12 18:18:55 +01:00
										 |  |  |                 'uri' => array('type' => 'varchar', 'not null'=>true, 'length' => 191, 'description' => 'URI of the conversation'), | 
					
						
							| 
									
										
										
										
											2017-05-02 18:58:22 +02:00
										 |  |  |                 'url' => array('type' => 'varchar', 'length' => 191, 'description' => 'Resolvable URL, preferrably remote (local can be generated on the fly)'), | 
					
						
							| 
									
										
										
										
											2019-07-25 00:43:25 +01:00
										 |  |  |                 'created' => array('type' => 'datetime', 'not null' => true, 'default' => '0000-00-00 00:00:00', 'description' => 'date this record was created'), | 
					
						
							|  |  |  |                 'modified' => array('type' => 'datetime', 'not null' => true, 'default' => 'CURRENT_TIMESTAMP', 'description' => 'date this record was modified'), | 
					
						
							| 
									
										
										
										
											2011-08-22 17:52:02 -04:00
										 |  |  |             ), | 
					
						
							|  |  |  |             'primary key' => array('id'), | 
					
						
							|  |  |  |             'unique keys' => array( | 
					
						
							|  |  |  |                 'conversation_uri_key' => array('uri'), | 
					
						
							|  |  |  |             ), | 
					
						
							|  |  |  |         ); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-11 08:32:19 +03:00
										 |  |  |     public static function beforeSchemaUpdate() | 
					
						
							| 
									
										
										
										
											2016-01-06 22:26:17 +01:00
										 |  |  |     { | 
					
						
							|  |  |  |         $table = strtolower(get_called_class()); | 
					
						
							|  |  |  |         $schema = Schema::get(); | 
					
						
							|  |  |  |         $schemadef = $schema->getTableDef($table); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // 2016-01-06 We have to make sure there is no conversation with id==0 since it will screw up auto increment resequencing
 | 
					
						
							| 
									
										
										
										
											2019-09-11 08:32:19 +03:00
										 |  |  |         if ($schemadef['fields']['id']['auto_increment'] ?? false) { | 
					
						
							| 
									
										
										
										
											2016-01-06 22:26:17 +01:00
										 |  |  |             // since we already have auto incrementing ('serial') we can continue
 | 
					
						
							|  |  |  |             return; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // The conversation will be recreated in upgrade.php, which will
 | 
					
						
							|  |  |  |         // generate a new URI, but that's collateral damage for you.
 | 
					
						
							|  |  |  |         $conv = new Conversation(); | 
					
						
							|  |  |  |         $conv->id = 0; | 
					
						
							|  |  |  |         if ($conv->find()) { | 
					
						
							|  |  |  |             while ($conv->fetch()) { | 
					
						
							|  |  |  |                 // Since we have filtered on 0 this only deletes such entries
 | 
					
						
							|  |  |  |                 // which I have been afraid wouldn't work, but apparently does!
 | 
					
						
							|  |  |  |                 // (I thought it would act as null or something and find _all_ conversation entries)
 | 
					
						
							|  |  |  |                 $conv->delete(); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-02-17 01:11:14 -08:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2014-05-01 15:47:51 +02:00
										 |  |  |      * Factory method for creating a new conversation. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * Use this for locally initiated conversations. Remote notices should | 
					
						
							|  |  |  |      * preferrably supply their own conversation URIs in the OStatus feed. | 
					
						
							| 
									
										
										
										
											2010-02-17 01:11:14 -08:00
										 |  |  |      * | 
					
						
							|  |  |  |      * @return Conversation the new conversation DO | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2019-09-11 08:32:19 +03:00
										 |  |  |     public static function create(ActivityContext $ctx = null, $created = null) | 
					
						
							| 
									
										
										
										
											2010-02-17 01:11:14 -08:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2016-01-06 13:58:46 +01:00
										 |  |  |         // Be aware that the Notice does not have an id yet since it's not inserted!
 | 
					
						
							| 
									
										
										
										
											2010-02-17 01:11:14 -08:00
										 |  |  |         $conv = new Conversation(); | 
					
						
							| 
									
										
										
										
											2016-01-06 13:58:46 +01:00
										 |  |  |         $conv->created = $created ?: common_sql_now(); | 
					
						
							| 
									
										
										
										
											2017-05-02 18:58:22 +02:00
										 |  |  |         if ($ctx instanceof ActivityContext) { | 
					
						
							|  |  |  |             $conv->uri = $ctx->conversation; | 
					
						
							|  |  |  |             $conv->url = $ctx->conversation_url; | 
					
						
							|  |  |  |         } else { | 
					
						
							| 
									
										
										
										
											2019-09-11 08:32:19 +03:00
										 |  |  |             $conv->uri = sprintf( | 
					
						
							|  |  |  |                 '%s%s=%s:%s=%s', | 
					
						
							|  |  |  |                 TagURI::mint(), | 
					
						
							|  |  |  |                 'objectType', | 
					
						
							|  |  |  |                 'thread', | 
					
						
							|  |  |  |                 'nonce', | 
					
						
							|  |  |  |                 common_random_hexstr(8) | 
					
						
							|  |  |  |             ); | 
					
						
							|  |  |  |             // locally generated Conversation objects don't get static URLs stored
 | 
					
						
							|  |  |  |             $conv->url = DB_DataObject_Cast::sql('NULL'); | 
					
						
							| 
									
										
										
										
											2017-05-02 18:58:22 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2016-01-06 13:58:46 +01:00
										 |  |  |         // This insert throws exceptions on failure
 | 
					
						
							|  |  |  |         $conv->insert(); | 
					
						
							| 
									
										
										
										
											2010-02-17 01:11:14 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         return $conv; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2011-04-06 23:17:17 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-11 08:32:19 +03:00
										 |  |  |     public static function noticeCount($id) | 
					
						
							| 
									
										
										
										
											2011-04-06 23:17:17 -04:00
										 |  |  |     { | 
					
						
							|  |  |  |         $keypart = sprintf('conversation:notice_count:%d', $id); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $cnt = self::cacheGet($keypart); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if ($cnt !== false) { | 
					
						
							|  |  |  |             return $cnt; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $notice               = new Notice(); | 
					
						
							|  |  |  |         $notice->conversation = $id; | 
					
						
							| 
									
										
										
										
											2014-07-15 14:26:42 +02:00
										 |  |  |         $notice->whereAddIn('verb', array(ActivityVerb::POST, ActivityUtils::resolveUri(ActivityVerb::POST, true)), $notice->columnType('verb')); | 
					
						
							| 
									
										
										
										
											2011-04-06 23:17:17 -04:00
										 |  |  |         $cnt                  = $notice->count(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         self::cacheSet($keypart, $cnt); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $cnt; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2014-05-01 15:25:19 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-11 08:32:19 +03:00
										 |  |  |     public static function getUrlFromNotice(Notice $notice, $anchor = true) | 
					
						
							| 
									
										
										
										
											2014-05-01 15:25:19 +02:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2016-01-06 13:58:46 +01:00
										 |  |  |         $conv = Conversation::getByID($notice->conversation); | 
					
						
							|  |  |  |         return $conv->getUrl($anchor ? $notice->getID() : null); | 
					
						
							| 
									
										
										
										
											2014-05-01 15:25:19 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function getUri() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return $this->uri; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function getUrl($noticeId=null) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         // FIXME: the URL router should take notice-id as an argument...
 | 
					
						
							| 
									
										
										
										
											2016-01-06 13:58:46 +01:00
										 |  |  |         return common_local_url('conversation', array('id' => $this->getID())) . | 
					
						
							| 
									
										
										
										
											2014-05-01 15:25:19 +02:00
										 |  |  |                 ($noticeId===null ? '' : "#notice-{$noticeId}"); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2014-05-12 10:27:52 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // FIXME: ...will 500 ever be too low? Taken from ConversationAction::MAX_NOTICES
 | 
					
						
							| 
									
										
										
										
											2016-01-06 13:58:46 +01:00
										 |  |  |     public function getNotices(Profile $scoped=null, $offset=0, $limit=500) | 
					
						
							| 
									
										
										
										
											2014-05-12 10:27:52 +02:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2016-01-06 13:58:46 +01:00
										 |  |  |         $stream = new ConversationNoticeStream($this->getID(), $scoped); | 
					
						
							| 
									
										
										
										
											2014-05-12 10:27:52 +02:00
										 |  |  |         $notices = $stream->getNotices($offset, $limit); | 
					
						
							|  |  |  |         return $notices; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2016-01-06 13:58:46 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     public function insert() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $result = parent::insert(); | 
					
						
							|  |  |  |         if ($result === false) { | 
					
						
							|  |  |  |             common_log_db_error($this, 'INSERT', __FILE__); | 
					
						
							|  |  |  |             throw new ServerException(_('Failed to insert Conversation into database')); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         return $result; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2010-02-16 23:30:08 -08:00
										 |  |  | } |