3 * StatusNet, the distributed open-source microblogging tool
5 * Data class for Conversations
9 * LICENCE: This program is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU Affero General Public License as published by
11 * the Free Software Foundation, either version 3 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Affero General Public License for more details.
19 * You should have received a copy of the GNU Affero General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 * @author Zach Copley <zach@status.net>
25 * @author Mikael Nordfeldth <mmn@hethane.se>
26 * @copyright 2010 StatusNet Inc.
27 * @copyright 2009-2014 Free Software Foundation, Inc http://www.fsf.org
28 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
29 * @link http://status.net/
32 if (!defined('GNUSOCIAL')) { exit(1); }
34 class Conversation extends Managed_DataObject
36 public $__table = 'conversation'; // table name
37 public $id; // int(4) primary_key not_null auto_increment
38 public $uri; // varchar(191) unique_key not 255 because utf8mb4 takes more space
39 public $url; // varchar(191) unique_key not 255 because utf8mb4 takes more space
40 public $created; // datetime not_null
41 public $modified; // timestamp not_null default_CURRENT_TIMESTAMP
43 public static function schemaDef()
47 'id' => array('type' => 'serial', 'not null' => true, 'description' => 'Unique identifier, (again) unrelated to notice id since 2016-01-06'),
48 'uri' => array('type' => 'varchar', 'not null'=>true, 'length' => 191, 'description' => 'URI of the conversation'),
49 'url' => array('type' => 'varchar', 'length' => 191, 'description' => 'Resolvable URL, preferrably remote (local can be generated on the fly)'),
50 'created' => array('type' => 'datetime', 'not null' => true, 'description' => 'date this record was created'),
51 'modified' => array('type' => 'timestamp', 'not null' => true, 'description' => 'date this record was modified'),
53 'primary key' => array('id'),
54 'unique keys' => array(
55 'conversation_uri_key' => array('uri'),
60 static public function beforeSchemaUpdate()
62 $table = strtolower(get_called_class());
63 $schema = Schema::get();
64 $schemadef = $schema->getTableDef($table);
66 // 2016-01-06 We have to make sure there is no conversation with id==0 since it will screw up auto increment resequencing
67 if ($schemadef['fields']['id']['auto_increment']) {
68 // since we already have auto incrementing ('serial') we can continue
72 // The conversation will be recreated in upgrade.php, which will
73 // generate a new URI, but that's collateral damage for you.
74 $conv = new Conversation();
77 while ($conv->fetch()) {
78 // Since we have filtered on 0 this only deletes such entries
79 // which I have been afraid wouldn't work, but apparently does!
80 // (I thought it would act as null or something and find _all_ conversation entries)
87 * Factory method for creating a new conversation.
89 * Use this for locally initiated conversations. Remote notices should
90 * preferrably supply their own conversation URIs in the OStatus feed.
92 * @return Conversation the new conversation DO
94 static function create(ActivityContext $ctx=null, $created=null)
96 // Be aware that the Notice does not have an id yet since it's not inserted!
97 $conv = new Conversation();
98 $conv->created = $created ?: common_sql_now();
99 if ($ctx instanceof ActivityContext) {
100 $conv->uri = $ctx->conversation;
101 $conv->url = $ctx->conversation_url;
103 $conv->uri = sprintf('%s%s=%s:%s=%s',
105 'objectType', 'thread',
106 'nonce', common_random_hexstr(8));
107 $conv->url = null; // locally generated Conversation objects don't get static URLs stored
109 // This insert throws exceptions on failure
115 static function noticeCount($id)
117 $keypart = sprintf('conversation:notice_count:%d', $id);
119 $cnt = self::cacheGet($keypart);
121 if ($cnt !== false) {
125 $notice = new Notice();
126 $notice->conversation = $id;
127 $notice->whereAddIn('verb', array(ActivityVerb::POST, ActivityUtils::resolveUri(ActivityVerb::POST, true)), $notice->columnType('verb'));
128 $cnt = $notice->count();
130 self::cacheSet($keypart, $cnt);
135 static public function getUrlFromNotice(Notice $notice, $anchor=true)
137 $conv = Conversation::getByID($notice->conversation);
138 return $conv->getUrl($anchor ? $notice->getID() : null);
141 public function getUri()
146 public function getUrl($noticeId=null)
148 // FIXME: the URL router should take notice-id as an argument...
149 return common_local_url('conversation', array('id' => $this->getID())) .
150 ($noticeId===null ? '' : "#notice-{$noticeId}");
153 // FIXME: ...will 500 ever be too low? Taken from ConversationAction::MAX_NOTICES
154 public function getNotices(Profile $scoped=null, $offset=0, $limit=500)
156 $stream = new ConversationNoticeStream($this->getID(), $scoped);
157 $notices = $stream->getNotices($offset, $limit);
161 public function insert()
163 $result = parent::insert();
164 if ($result === false) {
165 common_log_db_error($this, 'INSERT', __FILE__);
166 throw new ServerException(_('Failed to insert Conversation into database'));