From df8b9a5280fe7d65cfcb5eddf66cd3dcbc90fc67 Mon Sep 17 00:00:00 2001 From: Thomas Sileo Date: Sun, 1 Sep 2019 20:01:53 +0200 Subject: [PATCH] Add get_hashtags helper --- little_boxes/activitypub.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/little_boxes/activitypub.py b/little_boxes/activitypub.py index 5a31184..398f4e7 100644 --- a/little_boxes/activitypub.py +++ b/little_boxes/activitypub.py @@ -109,6 +109,7 @@ class ActivityType(Enum): # Others MENTION = "Mention" + HASHTAG = "Hashtag" # Mastodon specific? QUESTION = "Question" @@ -616,6 +617,15 @@ class Mention(BaseActivity): ACTOR_REQUIRED = False +class Hashtag(BaseActivity): + ACTIVITY_TYPE = ActivityType.HASHTAG + OBJECT_REQUIRED = False + ACTOR_REQUIRED = False + + def get_value(self) -> str: + return self.name[1:] + + class Person(BaseActivity): ACTIVITY_TYPE = ActivityType.PERSON OBJECT_REQUIRED = False @@ -929,6 +939,21 @@ class Note(BaseActivity): return mentions + def get_hashtags(self) -> List["Hashtag"]: + if self.tag is None: + return [] + + hashtags = [] + for tag in self.tag: + # Some AP implemention return "type"less tag for links + if "type" not in tag: + continue + + if _has_type(tag["type"], ActivityType.HASHTAG): + hashtags.append(Hashtag(**tag)) + + return hashtags + def get_in_reply_to(self) -> Optional[str]: return _get_id(self.inReplyTo)