diff --git a/components/Tag/Tag.php b/components/Tag/Tag.php index 507f7940fc..043aabdaf5 100644 --- a/components/Tag/Tag.php +++ b/components/Tag/Tag.php @@ -74,11 +74,12 @@ class Tag extends Component public function onProcessNoteContent(Note $note, string $content, string $content_type, array $extra_args): bool { $matched_tags = []; - preg_match_all(self::TAG_REGEX, $content, $matched_tags, \PREG_SET_ORDER); + // XXX: We remove because when content is in html the tag comes as #hashtag + preg_match_all(self::TAG_REGEX, str_replace('', '', $content), $matched_tags, \PREG_SET_ORDER); $matched_tags = array_unique(F\map($matched_tags, fn ($m) => $m[2])); foreach ($matched_tags as $match) { $tag = self::ensureValid($match); - $canonical_tag = self::canonicalTag($tag, Language::getById($note->getLanguageId())->getLocale()); + $canonical_tag = self::canonicalTag($tag, \is_null($lang_id = $note->getLanguageId()) ? null : Language::getById($lang_id)->getLocale()); DB::persist(NoteTag::create([ 'tag' => $tag, 'canonical' => $canonical_tag, diff --git a/plugins/ActivityPub/Util/Model/Note.php b/plugins/ActivityPub/Util/Model/Note.php index e99b96b334..aa79b906e6 100644 --- a/plugins/ActivityPub/Util/Model/Note.php +++ b/plugins/ActivityPub/Util/Model/Note.php @@ -241,18 +241,18 @@ class Note extends Model } $attr = [ - '@context' => 'https://www.w3.org/ns/activitystreams', - 'type' => 'Note', - 'id' => $object->getUrl(), - 'published' => $object->getCreated()->format(DateTimeInterface::RFC3339), - 'attributedTo' => $object->getActor()->getUri(Router::ABSOLUTE_URL), - 'to' => ['https://www.w3.org/ns/activitystreams#Public'], // TODO: implement proper scope address - 'cc' => ['https://www.w3.org/ns/activitystreams#Public'], - 'content' => $object->getRendered(), - 'attachment' => [], - 'tag' => [], - 'conversation' => $object->getConversationUri(), - 'directMessage' => false, // TODO: implement proper scope address + '@context' => 'https://www.w3.org/ns/activitystreams', + 'type' => 'Note', + 'id' => $object->getUrl(), + 'published' => $object->getCreated()->format(DateTimeInterface::RFC3339), + 'attributedTo' => $object->getActor()->getUri(Router::ABSOLUTE_URL), + 'to' => ['https://www.w3.org/ns/activitystreams#Public'], // TODO: implement proper scope address + 'cc' => ['https://www.w3.org/ns/activitystreams#Public'], + 'content' => $object->getRendered(), + 'attachment' => [], + 'tag' => [], + 'inConversation' => $object->getConversationUri(), + 'directMessage' => false, // TODO: implement proper scope address ]; // Mentions @@ -265,6 +265,15 @@ class Note extends Model $attr['cc'][] = $href; } + // Hashtags + foreach ($object->getTags() as $hashtag) { + $attr['tag'][] = [ + 'type' => 'Hashtag', + 'href' => $hashtag->getUrl(type: Router::ABSOLUTE_URL), + 'name' => "#{$hashtag->getTag()}", + ]; + } + // Attachments foreach ($object->getAttachments() as $attachment) { $attr['attachment'][] = [ diff --git a/src/Entity/Note.php b/src/Entity/Note.php index 957f40e38e..bad754ebdb 100644 --- a/src/Entity/Note.php +++ b/src/Entity/Note.php @@ -324,6 +324,11 @@ class Note extends Entity }); } + public function getTags(): array + { + return Cache::get('note-tags-' . $this->getId(), fn () => DB::findBy('note_tag', ['note_id' => $this->getId()])); + } + /** * Returns this Note's reply_to/parent. * diff --git a/src/Entity/NoteTag.php b/src/Entity/NoteTag.php index 0cd1e9adea..29f85b8bd0 100644 --- a/src/Entity/NoteTag.php +++ b/src/Entity/NoteTag.php @@ -123,13 +123,13 @@ class NoteTag extends Entity return Cache::getList(self::cacheKey($note_id), fn () => DB::dql('select nt from note_tag nt join note n with n.id = nt.note_id where n.id = :id', ['id' => $note_id])); } - public function getUrl(?Actor $actor = null): string + public function getUrl(?Actor $actor = null, int $type = Router::ABSOLUTE_PATH): string { $params = ['canon' => $this->getCanonical(), 'tag' => $this->getTag()]; if (!\is_null($actor)) { $params['lang'] = $actor->getTopLanguage()->getLocale(); } - return Router::url('single_note_tag', $params); + return Router::url(id: 'single_note_tag', args: $params, type: $type); } public static function schemaDef(): array