From 2f7fdf6ee4aac869e22af82c3e853789f2d47640 Mon Sep 17 00:00:00 2001 From: Diogo Peralta Cordeiro Date: Sat, 19 Mar 2022 22:21:15 +0000 Subject: [PATCH] [PLUGIN][ActivityPub][Test] Activity: Create Page Fixed a couple of bugs --- .../gnusocial/activities/create_page.jsonld | 27 +++--- .../Fixtures/gnusocial/objects/note.jsonld | 3 + .../objects/note_with_mention.jsonld | 93 ++++++++++--------- .../Fixtures/gnusocial/objects/page.jsonld | 42 +++++---- .../Fixtures/gnusocial/objects/reply.jsonld | 3 + .../Test/Objects/GSActivityCreatePageTest.php | 48 ++++++++++ .../Test/Objects/GSObjectNoteTest.php | 4 +- .../Test/Objects/GSObjectPageTest.php | 9 +- plugins/ActivityPub/Util/Model/Note.php | 15 +-- 9 files changed, 150 insertions(+), 94 deletions(-) create mode 100644 plugins/ActivityPub/Test/Objects/GSActivityCreatePageTest.php diff --git a/plugins/ActivityPub/Test/Fixtures/gnusocial/activities/create_page.jsonld b/plugins/ActivityPub/Test/Fixtures/gnusocial/activities/create_page.jsonld index 40944a950b..a430c25218 100755 --- a/plugins/ActivityPub/Test/Fixtures/gnusocial/activities/create_page.jsonld +++ b/plugins/ActivityPub/Test/Fixtures/gnusocial/activities/create_page.jsonld @@ -17,22 +17,31 @@ "@id": "gs:inConversation", "@type": "@id" } + }, + { + "@language": "en" } ], "id": "https://instance.gnusocial.test/activity/1338", - "published": "2022-03-01T18:19:51+00:00", + "published": "2022-03-17T23:30:26+00:00", "actor": "https://instance.gnusocial.test/actor/42", + "to": [ + "https://www.w3.org/ns/activitystreams#Public" + ], + "cc": [ + "https://instance.gnusocial.test/actor/21" + ], "object": { "type": "Page", "id": "https://instance.gnusocial.test/object/note/1338", - "published": "2022-03-01T22:44:29+00:00", + "published": "2022-03-17T23:30:26+00:00", "attributedTo": "https://instance.gnusocial.test/actor/42", "name": "hello, world.", "content": "

This is an interesting page.

", "mediaType": "text/html", "source": { "content": "This is an interesting page.", - "mediaType": "text/plain" + "mediaType": "text/markdown" }, "attachment": [], "tag": [], @@ -41,15 +50,7 @@ "https://www.w3.org/ns/activitystreams#Public" ], "cc": [ - "https://instance.gnusocial.test/actor/42/subscribers", "https://instance.gnusocial.test/actor/21" ] - }, - "to": [ - "https://www.w3.org/ns/activitystreams#Public" - ], - "cc": [ - "https://instance.gnusocial.test/actor/42/subscribers", - "https://instance.gnusocial.test/actor/21" - ] -} + } +} \ No newline at end of file diff --git a/plugins/ActivityPub/Test/Fixtures/gnusocial/objects/note.jsonld b/plugins/ActivityPub/Test/Fixtures/gnusocial/objects/note.jsonld index 00f32a9f4c..499520c9e6 100755 --- a/plugins/ActivityPub/Test/Fixtures/gnusocial/objects/note.jsonld +++ b/plugins/ActivityPub/Test/Fixtures/gnusocial/objects/note.jsonld @@ -17,6 +17,9 @@ "@id": "gs:inConversation", "@type": "@id" } + }, + { + "@language": "en" } ], "id": "https://instance.gnusocial.test/object/note/1337", diff --git a/plugins/ActivityPub/Test/Fixtures/gnusocial/objects/note_with_mention.jsonld b/plugins/ActivityPub/Test/Fixtures/gnusocial/objects/note_with_mention.jsonld index 0fca0cf93f..df5bd9642c 100755 --- a/plugins/ActivityPub/Test/Fixtures/gnusocial/objects/note_with_mention.jsonld +++ b/plugins/ActivityPub/Test/Fixtures/gnusocial/objects/note_with_mention.jsonld @@ -1,47 +1,50 @@ { - "type": "Note", - "@context": [ - "https://www.w3.org/ns/activitystreams", - "https://w3id.org/security/v1", - { - "gs": "https://www.gnu.org/software/social/ns#" - }, - { - "litepub": "http://litepub.social/ns#" - }, - { - "chatMessage": "litepub:chatMessage" - }, - { - "inConversation": { - "@id": "gs:inConversation", - "@type": "@id" - } - } - ], - "id": "https://instance.gnusocial.test/object/note/1340", - "published": "2022-03-16T21:54:43+00:00", - "attributedTo": "https://instance.gnusocial.test/actor/42", - "content": "

This is a public root note with a mention to @alice@another_instance.gnusocial.test.

", - "mediaType": "text/html", - "source": { - "content": "This is a public root note with a mention to @alice@another_instance.gnusocial.test.", - "mediaType": "text/plain" - }, - "attachment": [], - "tag": [ - { - "type": "Mention", - "href": "https://another_instance.gnusocial.test/actor/43", - "name": "@alice@another_instance.gnusocial.test" - } - ], - "inConversation": "https://instance.gnusocial.test/conversation/1340", - "to": [ - "https://www.w3.org/ns/activitystreams#Public", - "https://testv3.gnusocial.rocks/actor/43" - ], - "cc": [ - "https://testv3.gnusocial.rocks/actor/43" - ] + "type": "Note", + "@context": [ + "https://www.w3.org/ns/activitystreams", + "https://w3id.org/security/v1", + { + "gs": "https://www.gnu.org/software/social/ns#" + }, + { + "litepub": "http://litepub.social/ns#" + }, + { + "chatMessage": "litepub:chatMessage" + }, + { + "inConversation": { + "@id": "gs:inConversation", + "@type": "@id" + } + }, + { + "@language": "en" + } + ], + "id": "https://instance.gnusocial.test/object/note/1340", + "published": "2022-03-16T21:54:43+00:00", + "attributedTo": "https://instance.gnusocial.test/actor/42", + "content": "

This is a public root note with a mention to @alice@another_instance.gnusocial.test.

", + "mediaType": "text/html", + "source": { + "content": "This is a public root note with a mention to @alice@another_instance.gnusocial.test.", + "mediaType": "text/plain" + }, + "attachment": [], + "tag": [ + { + "type": "Mention", + "href": "https://another_instance.gnusocial.test/actor/43", + "name": "@alice@another_instance.gnusocial.test" + } + ], + "inConversation": "https://instance.gnusocial.test/conversation/1340", + "to": [ + "https://www.w3.org/ns/activitystreams#Public", + "https://testv3.gnusocial.rocks/actor/43" + ], + "cc": [ + "https://testv3.gnusocial.rocks/actor/43" + ] } \ No newline at end of file diff --git a/plugins/ActivityPub/Test/Fixtures/gnusocial/objects/page.jsonld b/plugins/ActivityPub/Test/Fixtures/gnusocial/objects/page.jsonld index 7d58ec04c3..981cb0539f 100755 --- a/plugins/ActivityPub/Test/Fixtures/gnusocial/objects/page.jsonld +++ b/plugins/ActivityPub/Test/Fixtures/gnusocial/objects/page.jsonld @@ -17,26 +17,28 @@ "@id": "gs:inConversation", "@type": "@id" } + }, + { + "@language": "en" } ], - "id": "https://instance.gnusocial.test/object/note/1338", - "published": "2022-03-01T22:44:29+00:00", - "attributedTo": "https://instance.gnusocial.test/actor/42", - "name": "hello, world.", - "content": "

This is an interesting page.

", - "mediaType": "text/html", - "source": { - "content": "This is an interesting page.", - "mediaType": "text/plain" - }, - "attachment": [], - "tag": [], - "inConversation": "https://instance.gnusocial.test/conversation/1338", - "to": [ - "https://www.w3.org/ns/activitystreams#Public" - ], - "cc": [ - "https://instance.gnusocial.test/actor/42/subscribers", - "https://instance.gnusocial.test/actor/21" - ] + "id": "https://instance.gnusocial.test/object/note/1338", + "published": "2022-03-17T23:30:26+00:00", + "attributedTo": "https://instance.gnusocial.test/actor/42", + "name": "hello, world.", + "content": "

This is an interesting page.

", + "mediaType": "text/html", + "source": { + "content": "This is an interesting page.", + "mediaType": "text/markdown" + }, + "attachment": [], + "tag": [], + "inConversation": "https://instance.gnusocial.test/conversation/1338", + "to": [ + "https://www.w3.org/ns/activitystreams#Public" + ], + "cc": [ + "https://instance.gnusocial.test/actor/21" + ] } \ No newline at end of file diff --git a/plugins/ActivityPub/Test/Fixtures/gnusocial/objects/reply.jsonld b/plugins/ActivityPub/Test/Fixtures/gnusocial/objects/reply.jsonld index de1b2af575..18e7f6d309 100755 --- a/plugins/ActivityPub/Test/Fixtures/gnusocial/objects/reply.jsonld +++ b/plugins/ActivityPub/Test/Fixtures/gnusocial/objects/reply.jsonld @@ -17,6 +17,9 @@ "@id": "gs:inConversation", "@type": "@id" } + }, + { + "@language": "en" } ], "id": "https://instance.gnusocial.test/object/note/1339", diff --git a/plugins/ActivityPub/Test/Objects/GSActivityCreatePageTest.php b/plugins/ActivityPub/Test/Objects/GSActivityCreatePageTest.php new file mode 100644 index 0000000000..5df7acd528 --- /dev/null +++ b/plugins/ActivityPub/Test/Objects/GSActivityCreatePageTest.php @@ -0,0 +1,48 @@ +. + +// }}} + +namespace Plugin\ActivityPub\Test\Objects; + +use App\Util\GNUsocialTestCase; +use Plugin\ActivityPub\Entity\ActivitypubActivity; +use Plugin\ActivityPub\Util\Explorer; + +class GSActivityCreatePageTest extends GNUsocialTestCase +{ + public function testNoteFromJson() + { + self::bootKernel(); + + $activity_uri = 'https://instance.gnusocial.test/activity/1338'; + $group_uri = 'https://instance.gnusocial.test/actor/21'; + $ap_activity = ActivitypubActivity::getByPK(['activity_uri' => $activity_uri]); + + $activity = $ap_activity->getActivity(); + static::assertSame('create', $activity->getVerb()); + static::assertSame('note', $activity->getObjectType()); + static::assertSame('ActivityPub', $activity->getSource()); + + static::assertCount(1, $attT = $ap_activity->getAttentionTargets()); + static::assertObjectEquals(Explorer::getOneFromUri($group_uri, try_online: false), $attT[0]); + } +} diff --git a/plugins/ActivityPub/Test/Objects/GSObjectNoteTest.php b/plugins/ActivityPub/Test/Objects/GSObjectNoteTest.php index 8cc9640afe..06ae49e7b5 100644 --- a/plugins/ActivityPub/Test/Objects/GSObjectNoteTest.php +++ b/plugins/ActivityPub/Test/Objects/GSObjectNoteTest.php @@ -50,7 +50,7 @@ class GSObjectNoteTest extends GNUsocialTestCase static::assertFalse($note->getIsLocal()); static::assertSame(VisibilityScope::EVERYWHERE, $note->getScope()); static::assertSame($object_uri, $note->getUrl()); - static::assertNull($note->getLanguageId()); + static::assertSame('en', $note->getLanguageLocale()); static::assertSame('note', $note->getType()); static::assertNull($note->getTitle()); @@ -83,7 +83,7 @@ class GSObjectNoteTest extends GNUsocialTestCase static::assertFalse($note->getIsLocal()); static::assertSame(VisibilityScope::EVERYWHERE, $note->getScope()); static::assertSame($object_uri, $note->getUrl()); - static::assertNull($note->getLanguageId()); + static::assertSame('en', $note->getLanguageLocale()); static::assertSame('note', $note->getType()); static::assertNull($note->getTitle()); diff --git a/plugins/ActivityPub/Test/Objects/GSObjectPageTest.php b/plugins/ActivityPub/Test/Objects/GSObjectPageTest.php index 8a8f26d58d..5b242a780b 100644 --- a/plugins/ActivityPub/Test/Objects/GSObjectPageTest.php +++ b/plugins/ActivityPub/Test/Objects/GSObjectPageTest.php @@ -38,11 +38,12 @@ class GSObjectPageTest extends GNUsocialTestCase $actor_uri = 'https://instance.gnusocial.test/actor/42'; $object_uri = 'https://instance.gnusocial.test/object/note/1338'; + $group_uri = 'https://instance.gnusocial.test/actor/21'; $page = ActivityPub::getObjectByUri($object_uri, try_online: false); static::assertInstanceOf(Note::class, $page); static::assertSame(Explorer::getOneFromUri($actor_uri)->getId(), $page->getActorId()); - static::assertSame('text/plain', $page->getContentType()); + static::assertSame('text/markdown', $page->getContentType()); static::assertSame('This is an interesting page.', $page->getContent()); static::assertSame('

This is an interesting page.

', $page->getRendered()); static::assertSame('ActivityPub', $page->getSource()); @@ -50,7 +51,7 @@ class GSObjectPageTest extends GNUsocialTestCase static::assertFalse($page->getIsLocal()); static::assertSame(VisibilityScope::EVERYWHERE, $page->getScope()); static::assertSame($object_uri, $page->getUrl()); - static::assertNull($page->getLanguageId()); + static::assertSame('en', $page->getLanguageLocale()); static::assertSame('page', $page->getType()); static::assertSame('hello, world.', $page->getTitle()); @@ -59,6 +60,8 @@ class GSObjectPageTest extends GNUsocialTestCase static::assertSame($object_uri, $ap_object->getObjectUri()); static::assertSame($page->getId(), $ap_object->getObjectId()); - //dd($page->getAttentionTargets(), $page->getMentionTargets()); + static::assertCount(1, $attT = $page->getAttentionTargets()); + static::assertObjectEquals(Explorer::getOneFromUri($group_uri, try_online: false), $attT[0]); + static::assertSame([], $page->getMentionTargets()); } } diff --git a/plugins/ActivityPub/Util/Model/Note.php b/plugins/ActivityPub/Util/Model/Note.php index adf98d618b..18392359ec 100644 --- a/plugins/ActivityPub/Util/Model/Note.php +++ b/plugins/ActivityPub/Util/Model/Note.php @@ -143,9 +143,9 @@ class Note extends Model } // Figure the locale of the note when possible - $locale = null; - if (\array_key_exists('@language', $type_note->get('@context'))) { - $locale = $type_note->get('@context')['@language']; + $locale = array_column($type_note->get('@context'), '@language'); + if ($locale !== []) { + $locale = $locale[0]; if ($locale === 'und') { $locale = null; } @@ -155,7 +155,7 @@ class Note extends Model 'is_local' => false, 'created' => new DateTime($type_note->get('published') ?? 'now'), 'title' => $type_note->get('name') ?? null, - 'language_id' => $type_note->get('contentLang') ?? null, + 'language_id' => \is_null($locale) ? null : Language::getByLocale($locale)->getId(), 'url' => $type_note->get('url') ?? $type_note->get('id'), 'actor_id' => $actor_id, 'reply_to' => $reply_to = $handleInReplyTo($type_note), @@ -167,13 +167,6 @@ class Note extends Model 'source' => $source, ]; - // Language - if (!\is_null($map['language_id'])) { - $map['language_id'] = Language::getByLocale($map['language_id'])->getId(); - } else { - $map['language_id'] = null; - } - // Scope if (\in_array('https://www.w3.org/ns/activitystreams#Public', $to)) { // Public: Visible for all, shown in public feeds