[ActivityPub] Fix note URIs

This commit is contained in:
Diogo Cordeiro 2020-08-29 23:51:46 +01:00 committed by Diogo Peralta Cordeiro
parent c3cdde0873
commit 238652b15d
6 changed files with 32 additions and 8 deletions

View File

@ -97,8 +97,8 @@ class ActivityPubPlugin extends Plugin
// Look for a local notice (unfortunately GNU social doesn't // Look for a local notice (unfortunately GNU social doesn't
// provide this functionality natively) // provide this functionality natively)
try { try {
$candidate = Notice::getByID((int)substr($url, (strlen(common_local_url('apNotice', ['id' => 0]))-1))); $candidate = Notice::getByID((int)substr($url, (strlen(Activitypub_notice::note_uri(0))-1)));
if (common_local_url('apNotice', ['id' => $candidate->getID()]) === $url) { // Sanity check if (Activitypub_notice::note_uri($candidate->getID()) === $url) { // Sanity check
return $candidate; return $candidate;
} else { } else {
common_debug('ActivityPubPlugin Notice Grabber: '.$candidate->getUrl(). ' is different of '.$url); common_debug('ActivityPubPlugin Notice Grabber: '.$candidate->getUrl(). ' is different of '.$url);
@ -196,6 +196,12 @@ class ActivityPubPlugin extends Plugin
$acceptHeaders $acceptHeaders
); );
$m->connect(
'object/note/:id',
['action' => 'apNotice'],
['id' => '[0-9]+'],
);
$m->connect( $m->connect(
'user/:id/liked.json', 'user/:id/liked.json',
['action' => 'apActorLiked'], ['action' => 'apActorLiked'],

View File

@ -124,6 +124,7 @@ class apActorOutboxAction extends ManagedAction
if ($note->object_type == 'http://activitystrea.ms/schema/1.0/note') { if ($note->object_type == 'http://activitystrea.ms/schema/1.0/note') {
$notices[] = Activitypub_create::create_to_array( $notices[] = Activitypub_create::create_to_array(
$note->getProfile()->getUri(), $note->getProfile()->getUri(),
common_local_url('apNotice', ['id' => $note->getID()]),
Activitypub_notice::notice_to_array($note) Activitypub_notice::notice_to_array($note)
); );
} }

View File

@ -40,16 +40,17 @@ class Activitypub_create
* Generates an ActivityPub representation of a Create * Generates an ActivityPub representation of a Create
* *
* @param string $actor * @param string $actor
* @param array $object * @param string $uri
* @param mixed $object
* @param bool $directMessage whether it is a private Create activity or not * @param bool $directMessage whether it is a private Create activity or not
* @return array pretty array to be used in a response * @return array pretty array to be used in a response
* @author Diogo Cordeiro <diogo@fc.up.pt> * @author Diogo Cordeiro <diogo@fc.up.pt>
*/ */
public static function create_to_array(string $actor, array $object, bool $directMessage = false): array public static function create_to_array(string $actor, string $uri, $object, bool $directMessage = false): array
{ {
$res = [ $res = [
'@context' => 'https://www.w3.org/ns/activitystreams', '@context' => 'https://www.w3.org/ns/activitystreams',
'id' => $object['id'] . '#create', 'id' => $uri,
'type' => 'Create', 'type' => 'Create',
'directMessage' => $directMessage, 'directMessage' => $directMessage,
'to' => $object['to'], 'to' => $object['to'],

View File

@ -100,7 +100,7 @@ class Activitypub_notice
} else { // Note } else { // Note
$item = [ $item = [
'@context' => 'https://www.w3.org/ns/activitystreams', '@context' => 'https://www.w3.org/ns/activitystreams',
'id' => self::getUri($notice), 'id' => self::note_uri($notice->getID()),
'type' => 'Note', 'type' => 'Note',
'published' => str_replace(' ', 'T', $notice->getCreated()) . 'Z', 'published' => str_replace(' ', 'T', $notice->getCreated()) . 'Z',
'url' => $notice->getUrl(), 'url' => $notice->getUrl(),
@ -310,6 +310,7 @@ class Activitypub_notice
* @throws InvalidUrlException * @throws InvalidUrlException
* @throws Exception * @throws Exception
* @author Bruno Casteleiro <brunoccast@fc.up.pt> * @author Bruno Casteleiro <brunoccast@fc.up.pt>
* @see note_uri when it's not a generic activity but a object type note
*/ */
public static function getUri(Notice $notice): string public static function getUri(Notice $notice): string
{ {
@ -320,6 +321,19 @@ class Activitypub_notice
} }
} }
/**
* Use this if your Notice is in fact a note
*
* @param int $id
* @return string it's uri
* @author Diogo Cordeiro <diogo@fc.up.pt>
* @see getUri for every other activity that aren't objects of a certain type like note
*/
public static function note_uri(int $id): string
{
return common_root_url() . 'object/note/' . $id;
}
/** /**
* Extract note policy type from note targets. * Extract note policy type from note targets.
* *

View File

@ -48,7 +48,7 @@ class Activitypub_tombstone
$dead = Deleted_notice::getByID($id); $dead = Deleted_notice::getByID($id);
$res = [ $res = [
'@context' => 'https://www.w3.org/ns/activitystreams', '@context' => 'https://www.w3.org/ns/activitystreams',
'id' => Activitypub_notice::getUri($id), 'id' => Activitypub_notice::note_uri($id),
'type' => 'Tombstone', 'type' => 'Tombstone',
'created' => str_replace(' ', 'T', $dead->act_created) . 'Z', 'created' => str_replace(' ', 'T', $dead->act_created) . 'Z',
'deleted' => str_replace(' ', 'T', $dead->created) . 'Z' 'deleted' => str_replace(' ', 'T', $dead->created) . 'Z'

View File

@ -290,6 +290,7 @@ class Activitypub_postman
{ {
$data = Activitypub_create::create_to_array( $data = Activitypub_create::create_to_array(
$this->actor_uri, $this->actor_uri,
common_local_url('apNotice', ['id' => $notice->getID()]),
Activitypub_notice::notice_to_array($notice) Activitypub_notice::notice_to_array($notice)
); );
$data = json_encode($data, JSON_UNESCAPED_SLASHES); $data = json_encode($data, JSON_UNESCAPED_SLASHES);
@ -321,6 +322,7 @@ class Activitypub_postman
{ {
$data = Activitypub_create::create_to_array( $data = Activitypub_create::create_to_array(
$this->actor_uri, $this->actor_uri,
common_local_url('apNotice', ['id' => $message->getID()]),
Activitypub_message::message_to_array($message), Activitypub_message::message_to_array($message),
true true
); );