X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=classes%2FConversation.php;h=d18321deba657e8c49d61c2131860669f0beb625;hb=0df853183485add783c59e9fac3a58258918817b;hp=56f61c63ab2c1453ebd8250c6c17b721bd358bcc;hpb=d32fef60394dbacca2f1dec24fd55043e328d0d1;p=quix0rs-gnu-social.git diff --git a/classes/Conversation.php b/classes/Conversation.php index 56f61c63ab..d18321deba 100644 --- a/classes/Conversation.php +++ b/classes/Conversation.php @@ -34,8 +34,9 @@ if (!defined('GNUSOCIAL')) { exit(1); } class Conversation extends Managed_DataObject { public $__table = 'conversation'; // table name - public $id; // int(4) primary_key not_null - public $uri; // varchar(255) unique_key + public $id; // int(4) primary_key not_null auto_increment + public $uri; // varchar(191) unique_key not 255 because utf8mb4 takes more space + public $url; // varchar(191) unique_key not 255 because utf8mb4 takes more space public $created; // datetime not_null public $modified; // timestamp not_null default_CURRENT_TIMESTAMP @@ -43,8 +44,9 @@ class Conversation extends Managed_DataObject { 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'), + '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, 'description' => 'date this record was created'), 'modified' => array('type' => 'timestamp', 'not null' => true, 'description' => 'date this record was modified'), ), @@ -55,6 +57,32 @@ class Conversation extends Managed_DataObject ); } + static public function beforeSchemaUpdate() + { + $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 + if ($schemadef['fields']['id']['auto_increment']) { + // 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(); + } + } + } + /** * Factory method for creating a new conversation. * @@ -63,25 +91,23 @@ class Conversation extends Managed_DataObject * * @return Conversation the new conversation DO */ - static function create(Notice $notice, $uri=null) + static function create(ActivityContext $ctx=null, $created=null) { - if (empty($notice->id)) { - throw new ServerException(_('Tried to create conversation for not yet inserted notice')); - } + // Be aware that the Notice does not have an id yet since it's not inserted! $conv = new Conversation(); - $conv->created = common_sql_now(); - $conv->id = $notice->id; - $conv->uri = $uri ?: sprintf('%s%s=%d:%s=%s:%s=%x', + $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(), - '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')); + 'nonce', common_random_hexstr(8)); + $conv->url = null; // locally generated Conversation objects don't get static URLs stored } + // This insert throws exceptions on failure + $conv->insert(); return $conv; } @@ -108,8 +134,8 @@ class Conversation extends Managed_DataObject static public function getUrlFromNotice(Notice $notice, $anchor=true) { - $conv = self::getKV('id', $notice->conversation); - return $conv->getUrl($anchor ? $notice->id : null); + $conv = Conversation::getByID($notice->conversation); + return $conv->getUrl($anchor ? $notice->getID() : null); } public function getUri() @@ -120,18 +146,25 @@ class Conversation extends Managed_DataObject 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)) . + return common_local_url('conversation', array('id' => $this->getID())) . ($noticeId===null ? '' : "#notice-{$noticeId}"); } // FIXME: ...will 500 ever be too low? Taken from ConversationAction::MAX_NOTICES - public function getNotices($offset=0, $limit=500, Profile $scoped=null) + public function getNotices(Profile $scoped=null, $offset=0, $limit=500) { - if ($scoped === null) { - $scoped = Profile::current(); - } - $stream = new ConversationNoticeStream($this->id, $scoped); + $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; + } }