123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173 |
- <?php
- defined('GNUSOCIAL') || die();
- class Conversation extends Managed_DataObject
- {
- public $__table = 'conversation';
- public $id;
- public $uri;
- public $url;
- public $created;
- public $modified;
- public static function schemaDef()
- {
- return array(
- 'fields' => array(
- 'id' => array('type' => 'serial', 'not null' => true, 'description' => 'Unique identifier, (again) unrelated to notice id since 2016-01-06'),
- 'uri' => array('type' => 'varchar', 'not null'=>true, 'length' => 191, 'description' => 'URI of the conversation'),
- 'url' => array('type' => 'varchar', 'length' => 191, 'description' => 'Resolvable URL, preferrably remote (local can be generated on the fly)'),
- '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'),
- ),
- 'primary key' => array('id'),
- 'unique keys' => array(
- 'conversation_uri_key' => array('uri'),
- ),
- );
- }
- public static function beforeSchemaUpdate()
- {
- $table = strtolower(get_called_class());
- $schema = Schema::get();
- $schemadef = $schema->getTableDef($table);
-
- if ($schemadef['fields']['id']['auto_increment'] ?? false) {
-
- return;
- }
-
-
- $conv = new Conversation();
- $conv->id = 0;
- if ($conv->find()) {
- while ($conv->fetch()) {
-
-
-
- $conv->delete();
- }
- }
- }
-
- public static function create(ActivityContext $ctx = null, $created = null)
- {
-
- $conv = new Conversation();
- $conv->created = $created ?: common_sql_now();
- if ($ctx instanceof ActivityContext) {
- $conv->uri = $ctx->conversation;
- $conv->url = $ctx->conversation_url;
- } else {
- $conv->uri = sprintf(
- '%s%s=%s:%s=%s',
- TagURI::mint(),
- 'objectType',
- 'thread',
- 'nonce',
- common_random_hexstr(8)
- );
-
- $conv->url = $conv->sqlValue('NULL');
- }
-
- $conv->insert();
- return $conv;
- }
- public 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;
- $notice->whereAddIn('verb', array(ActivityVerb::POST, ActivityUtils::resolveUri(ActivityVerb::POST, true)), $notice->columnType('verb'));
- $cnt = $notice->count();
- self::cacheSet($keypart, $cnt);
- return $cnt;
- }
- public static function getUrlFromNotice(Notice $notice, $anchor = true)
- {
- $conv = Conversation::getByID($notice->conversation);
- return $conv->getUrl($anchor ? $notice->getID() : null);
- }
- public function getUri()
- {
- return $this->uri;
- }
- public function getUrl($noticeId=null)
- {
-
- return common_local_url('conversation', array('id' => $this->getID())) .
- ($noticeId===null ? '' : "#notice-{$noticeId}");
- }
-
- public function getNotices(Profile $scoped=null, $offset=0, $limit=500)
- {
- $stream = new ConversationNoticeStream($this->getID(), $scoped);
- $notices = $stream->getNotices($offset, $limit);
- return $notices;
- }
- 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;
- }
- }
|