Conversation ID now gets set from initial notice.

This will work without much extra effort because there will always be
more notices (higher value) than conversations (so no collisions).

But please run upgrade.php to avoid having an autoincrement id on
conversation table.

Installations using code after 2014-03-01 will have identical
conversation IDs to the initial (conversation root) notice IDs. This
will not affect older installations, which will have very different
values.
This commit is contained in:
Mikael Nordfeldth 2014-03-01 17:06:29 +01:00
parent 9291225a69
commit 5c505d8539
3 changed files with 27 additions and 33 deletions

View File

@ -22,32 +22,28 @@
* @category Data * @category Data
* @package StatusNet * @package StatusNet
* @author Zach Copley <zach@status.net> * @author Zach Copley <zach@status.net>
* @author Mikael Nordfeldth <mmn@hethane.se>
* @copyright 2010 StatusNet Inc. * @copyright 2010 StatusNet Inc.
* @copyright 2009-2014 Free Software Foundation, Inc http://www.fsf.org
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
* @link http://status.net/ * @link http://status.net/
*/ */
require_once INSTALLDIR . '/classes/Memcached_DataObject.php'; if (!defined('GNUSOCIAL')) { exit(1); }
class Conversation extends Managed_DataObject class Conversation extends Managed_DataObject
{ {
###START_AUTOCODE
/* the code below is auto generated do not remove the above tag */
public $__table = 'conversation'; // table name public $__table = 'conversation'; // table name
public $id; // int(4) primary_key not_null public $id; // int(4) primary_key not_null
public $uri; // varchar(255) unique_key public $uri; // varchar(255) unique_key
public $created; // datetime not_null public $created; // datetime not_null
public $modified; // timestamp not_null default_CURRENT_TIMESTAMP public $modified; // timestamp not_null default_CURRENT_TIMESTAMP
/* the code above is auto generated do not remove the tag below */
###END_AUTOCODE
public static function schemaDef() public static function schemaDef()
{ {
return array( return array(
'fields' => array( 'fields' => array(
'id' => array('type' => 'serial', 'not null' => true, 'description' => 'unique identifier'), 'id' => array('type' => 'int', 'not null' => true, 'description' => 'should be set from root notice id (since 2014-03-01 commit)'),
'uri' => array('type' => 'varchar', 'length' => 255, 'description' => 'URI of the conversation'), 'uri' => array('type' => 'varchar', 'length' => 255, 'description' => 'URI of the conversation'),
'created' => array('type' => 'datetime', 'not null' => true, 'description' => 'date this record was created'), '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'), 'modified' => array('type' => 'timestamp', 'not null' => true, 'description' => 'date this record was modified'),
@ -64,25 +60,21 @@ class Conversation extends Managed_DataObject
* *
* @return Conversation the new conversation DO * @return Conversation the new conversation DO
*/ */
static function create() static function create(Notice $notice)
{ {
$conv = new Conversation(); if (empty($notice->id)) {
$conv->created = common_sql_now(); common_debug('Tried to create conversation for not yet inserted notice');
$id = $conv->insert();
if (empty($id)) {
common_log_db_error($conv, 'INSERT', __FILE__);
return null; return null;
} }
$conv = new Conversation();
$conv->created = common_sql_now();
$conv->id = $notice->id;
$conv->uri = common_local_url('conversation', array('id' => $notice->id), null, null, false);
$result = $conv->insert();
$orig = clone($conv); if ($result === false) {
$orig->uri = common_local_url('conversation', array('id' => $id), common_log_db_error($conv, 'INSERT', __FILE__);
null, null, false); throw new ServerException(_('Failed to create conversation for notice'));
$result = $orig->update($conv);
if (empty($result)) {
common_log_db_error($conv, 'UPDATE', __FILE__);
return null;
} }
return $conv; return $conv;

View File

@ -587,13 +587,13 @@ class Notice extends Managed_DataObject
// the beginning of a new conversation. // the beginning of a new conversation.
if (empty($notice->conversation)) { if (empty($notice->conversation)) {
$conv = Conversation::create(); $conv = Conversation::create($notice);
$notice->conversation = $conv->id; $notice->conversation = $conv->id;
$changed = true; $changed = true;
} }
if ($changed) { if ($changed) {
if (!$notice->update($orig)) { if ($notice->update($orig) === false) {
common_log_db_error($notice, 'UPDATE', __FILE__); common_log_db_error($notice, 'UPDATE', __FILE__);
// TRANS: Server exception thrown when a notice cannot be updated. // TRANS: Server exception thrown when a notice cannot be updated.
throw new ServerException(_('Problem saving notice.')); throw new ServerException(_('Problem saving notice.'));

View File

@ -165,12 +165,6 @@ class TwitterImport
} }
} }
if (empty($notice->conversation)) {
$conv = Conversation::create();
$notice->conversation = $conv->id;
common_log(LOG_INFO, "No known conversation for status {$statusId} so making a new one {$conv->id}.");
}
$notice->is_local = Notice::GATEWAY; $notice->is_local = Notice::GATEWAY;
$notice->content = html_entity_decode($this->linkify($status, FALSE), ENT_QUOTES, 'UTF-8'); $notice->content = html_entity_decode($this->linkify($status, FALSE), ENT_QUOTES, 'UTF-8');
@ -180,11 +174,19 @@ class TwitterImport
$id = $notice->insert(); $id = $notice->insert();
if (!$id) { if ($id === false) {
common_log_db_error($notice, 'INSERT', __FILE__); common_log_db_error($notice, 'INSERT', __FILE__);
common_log(LOG_ERR, __METHOD__ . ' - Problem saving notice.'); common_log(LOG_ERR, __METHOD__ . ' - Problem saving notice.');
} }
if (empty($notice->conversation)) {
$orig = clone($notice);
$conv = Conversation::create($notice);
common_log(LOG_INFO, "No known conversation for status {$statusId} so a new one ({$conv->id}) was created.");
$notice->conversation = $conv->id;
$notice->update($orig);
}
Event::handle('EndNoticeSave', array($notice)); Event::handle('EndNoticeSave', array($notice));
} }