[PLUGIN][ActivityPub] Support tags in notes

This commit is contained in:
Diogo Peralta Cordeiro 2021-12-24 21:02:02 +00:00
parent 36483a6ecd
commit 9d0b39e680
Signed by: diogo
GPG Key ID: 18D2D35001FBFAB0
4 changed files with 31 additions and 16 deletions

View File

@ -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 <span> because when content is in html the tag comes as #<span>hashtag</span>
preg_match_all(self::TAG_REGEX, str_replace('<span>', '', $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,

View File

@ -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'][] = [

View File

@ -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.
*

View File

@ -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