From 0edf5f0651375a059f072299b49e3c83d3ff623c Mon Sep 17 00:00:00 2001 From: Thomas Sileo Date: Fri, 16 Aug 2019 22:27:59 +0200 Subject: [PATCH] Fix thread display --- blueprints/admin.py | 14 ++++++++------ core/meta.py | 4 ++++ core/shared.py | 46 +++++++++++++++++++++++++-------------------- 3 files changed, 38 insertions(+), 26 deletions(-) diff --git a/blueprints/admin.py b/blueprints/admin.py index eadf329..af1769d 100644 --- a/blueprints/admin.py +++ b/blueprints/admin.py @@ -25,6 +25,9 @@ from config import ID from config import PASS from core.activitypub import Box from core.activitypub import post_to_outbox +from core.db import find_one_activity +from core.meta import by_object_id +from core.meta import by_type from core.shared import MY_PERSON from core.shared import _build_thread from core.shared import _Response @@ -195,12 +198,11 @@ def admin_lookup() -> _Response: @blueprint.route("/admin/thread") @login_required def admin_thread() -> _Response: - data = DB.activities.find_one( - { - "type": ap.ActivityType.CREATE.value, - "activity.object.id": request.args.get("oid"), - } - ) + oid = request.args.get("oid") + if not oid: + abort(404) + + data = find_one_activity({**by_type(ap.ActivityType.CREATE), **by_object_id(oid)}) if not data: abort(404) diff --git a/core/meta.py b/core/meta.py index 6067b8b..34b694e 100644 --- a/core/meta.py +++ b/core/meta.py @@ -74,6 +74,10 @@ def not_undo() -> _SubQuery: return flag(MetaKey.UNDO, False) +def not_deleted() -> _SubQuery: + return flag(MetaKey.DELETED, False) + + def by_actor(actor: ap.BaseActivity) -> _SubQuery: return flag(MetaKey.ACTOR_ID, actor.id) diff --git a/core/shared.py b/core/shared.py index b649a4e..7583614 100644 --- a/core/shared.py +++ b/core/shared.py @@ -19,6 +19,11 @@ import config from config import DB from config import ME from core import activitypub +from core.db import find_activities +from core.meta import MetaKey +from core.meta import by_type +from core.meta import flag +from core.meta import not_deleted # _Response = Union[flask.Response, werkzeug.wrappers.Response, str, Any] _Response = Any @@ -113,29 +118,30 @@ def _get_ip(): def _build_thread(data, include_children=True): # noqa: C901 data["_requested"] = True app.logger.info(f"_build_thread({data!r})") - root_id = data["meta"].get("thread_root_parent", data["activity"]["object"]["id"]) + root_id = data["meta"][MetaKey.OBJECT_ID.value] - query = { - "$or": [{"meta.thread_root_parent": root_id}, {"activity.object.id": root_id}], - "meta.deleted": False, - } replies = [data] - for dat in DB.activities.find(query): - print(dat["type"]) - if dat["type"][0] == ap.ActivityType.CREATE.value: - replies.append(dat) - if dat["type"][0] == ap.ActivityType.UPDATE.value: - continue - else: - # Make a Note/Question/... looks like a Create - dat = { - "activity": {"object": dat["activity"]}, - "meta": dat["meta"], - "_id": dat["_id"], - } - replies.append(dat) + for dat in find_activities( + { + **flag(MetaKey.THREAD_ROOT_PARENT, root_id), + **not_deleted(), + **by_type(ap.ActivityType.CREATE), + } + ): + replies.append(dat) - replies = sorted(replies, key=lambda d: d["activity"]["object"]["published"]) + for dat in DB.replies.find( + {**flag(MetaKey.THREAD_ROOT_PARENT, root_id), **not_deleted()} + ): + # Make a Note/Question/... looks like a Create + dat = { + "activity": {"object": dat["activity"]}, + "meta": dat["meta"], + "_id": dat["_id"], + } + replies.append(dat) + + replies = sorted(replies, key=lambda d: d["meta"]["published"]) # Index all the IDs in order to build a tree idx = {}