Fixed the parsing of ostatus:conversation etc.

Conversation will now start storing remote URL

The namespace features don't work the way they were written for here
so I fixed that, making the ostatus: namespace properly looked up and
then the homegrown getLink function looks for what is back-compat with
StatusNet etc. if I remember correctly.
This commit is contained in:
Mikael Nordfeldth 2017-05-02 18:58:22 +02:00
parent e8eb9f9614
commit 07458e5375
3 changed files with 34 additions and 9 deletions

View File

@ -36,6 +36,7 @@ class Conversation extends Managed_DataObject
public $__table = 'conversation'; // table name public $__table = 'conversation'; // table name
public $id; // int(4) primary_key not_null auto_increment public $id; // int(4) primary_key not_null auto_increment
public $uri; // varchar(191) unique_key not 255 because utf8mb4 takes more space 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 $created; // datetime not_null
public $modified; // timestamp not_null default_CURRENT_TIMESTAMP public $modified; // timestamp not_null default_CURRENT_TIMESTAMP
@ -45,6 +46,7 @@ class Conversation extends Managed_DataObject
'fields' => array( 'fields' => array(
'id' => array('type' => 'serial', 'not null' => true, 'description' => 'Unique identifier, (again) unrelated to notice id since 2016-01-06'), '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'), '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'), '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'),
), ),
@ -89,15 +91,21 @@ class Conversation extends Managed_DataObject
* *
* @return Conversation the new conversation DO * @return Conversation the new conversation DO
*/ */
static function create($uri=null, $created=null) static function create(ActivityContext $ctx=null, $created=null)
{ {
// Be aware that the Notice does not have an id yet since it's not inserted! // Be aware that the Notice does not have an id yet since it's not inserted!
$conv = new Conversation(); $conv = new Conversation();
$conv->created = $created ?: common_sql_now(); $conv->created = $created ?: common_sql_now();
$conv->uri = $uri ?: sprintf('%s%s=%s:%s=%s', if ($ctx instanceof ActivityContext) {
$conv->uri = $ctx->conversation;
$conv->url = $ctx->conversation_url;
} else {
$conv->uri = sprintf('%s%s=%s:%s=%s',
TagURI::mint(), TagURI::mint(),
'objectType', 'thread', 'objectType', 'thread',
'nonce', common_random_hexstr(8)); 'nonce', common_random_hexstr(8));
$conv->url = null; // locally generated Conversation objects don't get static URLs stored
}
// This insert throws exceptions on failure // This insert throws exceptions on failure
$conv->insert(); $conv->insert();

View File

@ -624,8 +624,13 @@ class Notice extends Managed_DataObject
} else { } else {
// Conversation entry with specified URI was not found, so we must create it. // Conversation entry with specified URI was not found, so we must create it.
common_debug('Conversation URI not found, so we will create it with the URI given in the options to Notice::saveNew: '.$options['conversation']); common_debug('Conversation URI not found, so we will create it with the URI given in the options to Notice::saveNew: '.$options['conversation']);
$convctx = new ActivityContext();
$convctx->conversation = $options['conversation'];
if (array_key_exists('conversation_url', $options)) {
$convctx->conversation_url = $options['conversation_url'];
}
// The insert in Conversation::create throws exception on failure // The insert in Conversation::create throws exception on failure
$conv = Conversation::create($options['conversation'], $notice->created); $conv = Conversation::create($convctx, $notice->created);
} }
$notice->conversation = $conv->getID(); $notice->conversation = $conv->getID();
unset($conv); unset($conv);
@ -921,7 +926,7 @@ class Notice extends Managed_DataObject
// Conversation entry with specified URI was not found, so we must create it. // Conversation entry with specified URI was not found, so we must create it.
common_debug('Conversation URI not found, so we will create it with the URI given in the context of the activity: '.$act->context->conversation); common_debug('Conversation URI not found, so we will create it with the URI given in the context of the activity: '.$act->context->conversation);
// The insert in Conversation::create throws exception on failure // The insert in Conversation::create throws exception on failure
$conv = Conversation::create($act->context->conversation, $stored->created); $conv = Conversation::create($act->context, $stored->created);
} }
$stored->conversation = $conv->getID(); $stored->conversation = $conv->getID();
unset($conv); unset($conv);
@ -2008,6 +2013,7 @@ class Notice extends Managed_DataObject
$conv = Conversation::getKV('id', $this->conversation); $conv = Conversation::getKV('id', $this->conversation);
if ($conv instanceof Conversation) { if ($conv instanceof Conversation) {
$ctx->conversation = $conv->uri; $ctx->conversation = $conv->uri;
$ctx->conversation_url = $conv->url;
} }
} }

View File

@ -39,6 +39,7 @@ class ActivityContext
public $location; public $location;
public $attention = array(); // 'uri' => 'type' public $attention = array(); // 'uri' => 'type'
public $conversation; public $conversation;
public $conversation_url;
public $scope; public $scope;
const THR = 'http://purl.org/syndication/thread/1.0'; const THR = 'http://purl.org/syndication/thread/1.0';
@ -51,7 +52,7 @@ class ActivityContext
// OStatus element names with prefixes // OStatus element names with prefixes
const OBJECTTYPE = 'ostatus:object-type'; // FIXME: Undocumented! const OBJECTTYPE = 'ostatus:object-type'; // FIXME: Undocumented!
const CONVERSATION = 'ostatus:conversation'; const CONVERSATION = 'conversation';
const POINT = 'point'; const POINT = 'point';
@ -74,13 +75,22 @@ class ActivityContext
$this->location = $this->getLocation($element); $this->location = $this->getLocation($element);
$convs = $element->getElementsByTagNameNS(self::OSTATUS, self::CONVERSATION); foreach ($element->getElementsByTagNameNS(self::OSTATUS, self::CONVERSATION) as $conv) {
foreach ($convs as $conv) { if ($conv->hasAttribute('ref')) {
$this->conversation = $conv->textContent; $this->conversation = $conv->getAttribute('ref');
if ($conv->hasAttribute('href')) {
$this->conversation_url = $conv->getAttribute('href');
}
} else {
$this->conversation = $conv->textContent;
}
if (!empty($this->conversation)) {
break;
}
} }
if (empty($this->conversation)) { if (empty($this->conversation)) {
// fallback to the atom:link rel="ostatus:conversation" element // fallback to the atom:link rel="ostatus:conversation" element
$this->conversation = ActivityUtils::getLink($element, self::CONVERSATION); $this->conversation = ActivityUtils::getLink($element, 'ostatus:'.self::CONVERSATION);
} }
// Multiple attention links allowed // Multiple attention links allowed
@ -148,6 +158,7 @@ class ActivityContext
$context['inReplyTo'] = $this->getInReplyToArray(); $context['inReplyTo'] = $this->getInReplyToArray();
$context['conversation'] = $this->conversation; $context['conversation'] = $this->conversation;
$context['conversation_url'] = $this->conversation_url;
return array_filter($context); return array_filter($context);
} }