| 
									
										
										
										
											2010-02-16 23:30:08 -08:00
										 |  |  | <?php | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * StatusNet, the distributed open-source microblogging tool | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Data class for Conversations | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * PHP version 5 | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * LICENCE: This program 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. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * This program 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 this program.  If not, see <http://www.gnu.org/licenses/>. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * @category  Data | 
					
						
							|  |  |  |  * @package   StatusNet | 
					
						
							|  |  |  |  * @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 | 
					
						
							| 
									
										
										
										
											2010-02-16 23:30:08 -08:00
										 |  |  |  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0 | 
					
						
							|  |  |  |  * @link      http://status.net/ | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-01 17:06:29 +01:00
										 |  |  | if (!defined('GNUSOCIAL')) { exit(1); } | 
					
						
							| 
									
										
										
										
											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
 | 
					
						
							| 
									
										
										
										
											2010-02-16 23:30:08 -08: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
 | 
					
						
							| 
									
										
										
										
											2010-02-16 23:30:08 -08:00
										 |  |  |     public $created;                         // datetime   not_null
 | 
					
						
							|  |  |  |     public $modified;                        // timestamp   not_null default_CURRENT_TIMESTAMP
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-08-22 17:52:02 -04:00
										 |  |  |     public static function schemaDef() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return array( | 
					
						
							|  |  |  |             'fields' => array( | 
					
						
							| 
									
										
										
										
											2014-03-01 17:06:29 +01:00
										 |  |  |                 'id' => array('type' => 'int', 'not null' => true, 'description' => 'should be set from root notice id (since 2014-03-01 commit)'), | 
					
						
							| 
									
										
										
										
											2015-02-12 18:18:55 +01:00
										 |  |  |                 'uri' => array('type' => 'varchar', 'not null'=>true, 'length' => 191, 'description' => 'URI of the conversation'), | 
					
						
							| 
									
										
										
										
											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'), | 
					
						
							|  |  |  |             ), | 
					
						
							|  |  |  |             'primary key' => array('id'), | 
					
						
							|  |  |  |             'unique keys' => array( | 
					
						
							|  |  |  |                 'conversation_uri_key' => array('uri'), | 
					
						
							|  |  |  |             ), | 
					
						
							|  |  |  |         ); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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 | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2014-11-27 14:27:41 +01:00
										 |  |  |     static function create(Notice $notice, $uri=null) | 
					
						
							| 
									
										
										
										
											2010-02-17 01:11:14 -08:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2014-03-01 17:06:29 +01:00
										 |  |  |         if (empty($notice->id)) { | 
					
						
							| 
									
										
										
										
											2014-03-01 17:19:10 +01:00
										 |  |  |             throw new ServerException(_('Tried to create conversation for not yet inserted notice')); | 
					
						
							| 
									
										
										
										
											2014-03-01 17:06:29 +01:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2010-02-17 01:11:14 -08:00
										 |  |  |         $conv = new Conversation(); | 
					
						
							|  |  |  |         $conv->created = common_sql_now(); | 
					
						
							| 
									
										
										
										
											2014-03-01 17:06:29 +01:00
										 |  |  |         $conv->id = $notice->id; | 
					
						
							| 
									
										
										
										
											2014-11-27 17:05:01 +01:00
										 |  |  |         $conv->uri = $uri ?: sprintf('%s%s=%d:%s=%s:%s=%x', | 
					
						
							| 
									
										
										
										
											2014-05-01 15:47:51 +02:00
										 |  |  |                              TagURI::mint(), | 
					
						
							|  |  |  |                              'noticeId', $notice->id, | 
					
						
							| 
									
										
										
										
											2014-11-27 17:05:01 +01:00
										 |  |  |                              'objectType', 'thread', | 
					
						
							|  |  |  |                              'crc32', crc32($notice->content)); | 
					
						
							| 
									
										
										
										
											2014-03-01 17:06:29 +01:00
										 |  |  |         $result = $conv->insert(); | 
					
						
							| 
									
										
										
										
											2010-02-17 01:11:14 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-01 17:06:29 +01:00
										 |  |  |         if ($result === false) { | 
					
						
							| 
									
										
										
										
											2010-02-17 01:11:14 -08:00
										 |  |  |             common_log_db_error($conv, 'INSERT', __FILE__); | 
					
						
							| 
									
										
										
										
											2014-03-01 17:06:29 +01:00
										 |  |  |             throw new ServerException(_('Failed to create conversation for notice')); | 
					
						
							| 
									
										
										
										
											2010-02-17 01:11:14 -08:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $conv; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2011-04-06 23:17:17 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     static function noticeCount($id) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $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
										 |  |  | 
 | 
					
						
							|  |  |  |     static public function getUrlFromNotice(Notice $notice, $anchor=true) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2015-05-26 13:11:08 +02:00
										 |  |  |         $conv = new Conversation(); | 
					
						
							|  |  |  |         $conv->id = $notice->conversation; | 
					
						
							| 
									
										
										
										
											2015-09-04 20:35:11 +02:00
										 |  |  |         if (!$conv->find(true)) { | 
					
						
							| 
									
										
										
										
											2015-05-26 13:11:08 +02:00
										 |  |  |             common_debug('Conversation does not exist for notice ID: '.$notice->id); | 
					
						
							|  |  |  |             throw new NoResultException($conv); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2014-05-01 15:25:19 +02:00
										 |  |  |         return $conv->getUrl($anchor ? $notice->id : null); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function getUri() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return $this->uri; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function getUrl($noticeId=null) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         // FIXME: the URL router should take notice-id as an argument...
 | 
					
						
							|  |  |  |         return common_local_url('conversation', array('id' => $this->id)) . | 
					
						
							|  |  |  |                 ($noticeId===null ? '' : "#notice-{$noticeId}"); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2014-05-12 10:27:52 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // FIXME: ...will 500 ever be too low? Taken from ConversationAction::MAX_NOTICES
 | 
					
						
							|  |  |  |     public function getNotices($offset=0, $limit=500, Profile $scoped=null) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         if ($scoped === null) { | 
					
						
							|  |  |  |             $scoped = Profile::current(); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         $stream = new ConversationNoticeStream($this->id, $scoped); | 
					
						
							|  |  |  |         $notices = $stream->getNotices($offset, $limit); | 
					
						
							|  |  |  |         return $notices; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2010-02-16 23:30:08 -08:00
										 |  |  | } |