diff --git a/blueprints/admin.py b/blueprints/admin.py index c3e666e..3996c2b 100644 --- a/blueprints/admin.py +++ b/blueprints/admin.py @@ -24,6 +24,7 @@ from config import DB from config import ID from config import PASS from core.activitypub import Box +from core.activitypub import _meta from core.activitypub import post_to_outbox from core.db import find_one_activity from core.meta import by_object_id @@ -193,14 +194,8 @@ def admin_lookup() -> _Response: if request.args.get("url"): data = lookup(request.args.get("url")) # type: ignore if data: - if data.has_type(ap.ActivityType.ANNOUNCE): - meta = dict( - object=data.get_object().to_dict(), - object_actor=data.get_object().get_actor().to_dict(), - actor=data.get_actor().to_dict(), - ) - - elif data.has_type(ap.ActivityType.QUESTION): + meta = _meta(data) + if data.has_type(ap.ActivityType.QUESTION): p.push(data.id, "/task/fetch_remote_question") print(data) @@ -302,12 +297,10 @@ def admin_new() -> _Response: if data: reply = ap.parse_activity(data["activity"]) else: - data = dict( - meta={}, - activity=dict( - object=ap.get_backend().fetch_iri(request.args.get("reply")) - ), - ) + obj = ap.get_backend().fetch_iri(request.args.get("reply")) + data = dict(meta=_meta(ap.parse_activity(obj)), activity=dict(object=obj)) + data["_id"] = obj["id"] + data["remote_id"] = obj["id"] reply = ap.parse_activity(data["activity"]["object"]) # Fetch the post visibility, in case it's follower only default_visibility = ap.get_visibility(reply) diff --git a/core/activitypub.py b/core/activitypub.py index 9e0ffee..ad0c74c 100644 --- a/core/activitypub.py +++ b/core/activitypub.py @@ -113,8 +113,7 @@ def _is_local_reply(create: ap.Create) -> bool: return False -def save(box: Box, activity: ap.BaseActivity) -> None: - """Custom helper for saving an activity to the DB.""" +def _meta(activity: ap.BaseActivity) -> _NewMeta: visibility = ap.get_visibility(activity) is_public = False if visibility in [ap.Visibility.PUBLIC, ap.Visibility.UNLISTED]: @@ -134,10 +133,26 @@ def save(box: Box, activity: ap.BaseActivity) -> None: actor_id = activity.get_actor().id + return { + MetaKey.UNDO.value: False, + MetaKey.DELETED.value: False, + MetaKey.PUBLIC.value: is_public, + MetaKey.SERVER.value: urlparse(activity.id).netloc, + MetaKey.VISIBILITY.value: visibility.name, + MetaKey.ACTOR_ID.value: actor_id, + MetaKey.OBJECT_ID.value: object_id, + MetaKey.OBJECT_VISIBILITY.value: object_visibility, + MetaKey.POLL_ANSWER.value: False, + MetaKey.PUBLISHED.value: activity.published if activity.published else now(), + } + + +def save(box: Box, activity: ap.BaseActivity) -> None: + """Custom helper for saving an activity to the DB.""" # Set some "type"-related neta - extra: Dict[str, Any] = {} + meta = _meta(activity) if box == Box.OUTBOX and activity.has_type(ap.ActivityType.FOLLOW): - extra[MetaKey.FOLLOW_STATUS.value] = FollowStatus.WAITING.value + meta[MetaKey.FOLLOW_STATUS.value] = FollowStatus.WAITING.value elif activity.has_type(ap.ActivityType.CREATE): mentions = [] obj = activity.get_object() @@ -146,7 +161,7 @@ def save(box: Box, activity: ap.BaseActivity) -> None: hashtags = [] for h in obj.get_hashtags(): hashtags.append(h.name[1:]) # Strip the # - extra.update( + meta.update( {MetaKey.MENTIONS.value: mentions, MetaKey.HASHTAGS.value: hashtags} ) @@ -156,21 +171,7 @@ def save(box: Box, activity: ap.BaseActivity) -> None: "activity": activity.to_dict(), "type": _to_list(activity.type), "remote_id": activity.id, - "meta": { - MetaKey.UNDO.value: False, - MetaKey.DELETED.value: False, - MetaKey.PUBLIC.value: is_public, - MetaKey.SERVER.value: urlparse(activity.id).netloc, - MetaKey.VISIBILITY.value: visibility.name, - MetaKey.ACTOR_ID.value: actor_id, - MetaKey.OBJECT_ID.value: object_id, - MetaKey.OBJECT_VISIBILITY.value: object_visibility, - MetaKey.POLL_ANSWER.value: False, - MetaKey.PUBLISHED.value: activity.published - if activity.published - else now(), - **extra, - }, + "meta": meta, } ) diff --git a/core/meta.py b/core/meta.py index 4f6a9b5..7377cc0 100644 --- a/core/meta.py +++ b/core/meta.py @@ -9,6 +9,7 @@ from typing import Union from little_boxes import activitypub as ap _SubQuery = Dict[str, Any] +_Meta = Dict["MetaKey", Any] @unique diff --git a/core/shared.py b/core/shared.py index 3e63d66..e7990c2 100644 --- a/core/shared.py +++ b/core/shared.py @@ -21,6 +21,7 @@ import config from config import DB from config import ME from core import activitypub +from core.activitypub import _meta from core.db import find_activities from core.meta import MetaKey from core.meta import by_object_id @@ -145,10 +146,11 @@ def _build_thread(data, include_children=True, query=None): # noqa: C901 query = {} data["_requested"] = True app.logger.info(f"_build_thread({data!r})") - root_id = data["meta"].get( - MetaKey.THREAD_ROOT_PARENT.value, - data["meta"].get(MetaKey.OBJECT_ID.value, data["meta"].get("remote_id")), - ) + root_id = data["meta"].get(MetaKey.THREAD_ROOT_PARENT.value) + if not root_id: + root_id = data["meta"].get(MetaKey.OBJECT_ID.value) + if not root_id: + root_id = data["remote_id"] replies = [data] for dat in find_activities( diff --git a/templates/lookup.html b/templates/lookup.html index 2fad534..ad8369a 100644 --- a/templates/lookup.html +++ b/templates/lookup.html @@ -28,15 +28,15 @@ {{ utils.display_actor_inline(data, size=80) }} {% elif data | has_type('Create') %} - {{ utils.display_note(data.object) }} + {{ utils.display_note(data.object, meta=meta) }} {% elif data | has_type(['Note', 'Article', 'Video', 'Audio', 'Page', 'Question']) %} - {{ utils.display_note(data) }} + {{ utils.display_note(data, meta=meta) }} {% elif data | has_type('Announce') %} {% set boost_actor = meta.actor %}
- {{ utils.display_note(meta.object) }} + {{ utils.display_note(meta.object, meta=meta) }} {% endif %}