123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138 |
- <?php
- if (!defined('GNUSOCIAL')) { exit(1); }
- class Conversation extends Managed_DataObject
- {
- public $__table = 'conversation';
- public $id;
- public $uri;
- public $created;
- public $modified;
- public static function schemaDef()
- {
- return array(
- 'fields' => array(
- 'id' => array('type' => 'int', 'not null' => true, 'description' => 'should be set from root notice id (since 2014-03-01 commit)'),
- 'uri' => array('type' => 'varchar', 'not null'=>true, 'length' => 255, 'description' => 'URI of the conversation'),
- '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'),
- ),
- );
- }
-
- static function create(Notice $notice, $uri=null)
- {
- if (empty($notice->id)) {
- throw new ServerException(_('Tried to create conversation for not yet inserted notice'));
- }
- $conv = new Conversation();
- $conv->created = common_sql_now();
- $conv->id = $notice->id;
- $conv->uri = $uri ?: sprintf('%s%s=%d:%s=%s:%s=%x',
- TagURI::mint(),
- 'noticeId', $notice->id,
- 'objectType', 'thread',
- 'crc32', crc32($notice->content));
- $result = $conv->insert();
- if ($result === false) {
- common_log_db_error($conv, 'INSERT', __FILE__);
- throw new ServerException(_('Failed to create conversation for notice'));
- }
- return $conv;
- }
- 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;
- }
- static public function getUrlFromNotice(Notice $notice, $anchor=true)
- {
- $conv = self::getKV('id', $notice->conversation);
- return $conv->getUrl($anchor ? $notice->id : null);
- }
- public function getUri()
- {
- return $this->uri;
- }
- public function getUrl($noticeId=null)
- {
-
- return common_local_url('conversation', array('id' => $this->id)) .
- ($noticeId===null ? '' : "#notice-{$noticeId}");
- }
-
- 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;
- }
- }
|