From 786816f0a2ec13b39ca940787ffac4372f22e06d Mon Sep 17 00:00:00 2001 From: Thomas Sileo Date: Sun, 3 Jun 2018 14:34:04 +0200 Subject: [PATCH] Tweak replies management, improve tombstone support --- activitypub.py | 15 ++++++++++++--- app.py | 19 +++++++++++++++---- 2 files changed, 27 insertions(+), 7 deletions(-) diff --git a/activitypub.py b/activitypub.py index d9764f2..0b4fa0b 100644 --- a/activitypub.py +++ b/activitypub.py @@ -866,21 +866,21 @@ class Create(BaseActivity): 'meta.count_reply': 1, 'meta.count_direct_reply': direct_reply, }, + '$addToSet': {'meta.thread_children': obj.id}, }): DB.outbox.update_one({'activity.object.id': reply.id}, { '$inc': { 'meta.count_reply': 1, 'meta.count_direct_reply': direct_reply, }, + '$addToSet': {'meta.thread_children': obj.id}, }) direct_reply = 0 reply_id = reply.id reply = reply.get_local_reply() logger.debug(f'next_reply={reply}') - if reply: - # Only append to threads if it's not the root - threads.append(reply_id) + threads.append(reply_id) if reply_id: if not DB.inbox.find_one_and_update({'activity.object.id': obj.id}, { @@ -1018,6 +1018,15 @@ class Note(BaseActivity): return Delete(object=Tombstone(id=self.id).to_dict(embed=True)) + def get_tombstone(self, deleted: Optional[str]) -> BaseActivity: + return Tombstone( + id=self.id, + published=self.published, + deleted=deleted, + updated=updated, + ) + + _ACTIVITY_TYPE_TO_CLS = { ActivityType.IMAGE: Image, ActivityType.PERSON: Person, diff --git a/app.py b/app.py index ebc775a..60e786d 100644 --- a/app.py +++ b/app.py @@ -409,9 +409,11 @@ def index(): @app.route('/note/') def note_by_id(note_id): - data = DB.outbox.find_one({'id': note_id, 'meta.deleted': False}) + data = DB.outbox.find_one({'id': note_id}) if not data: - return Response(status=404) + abort(404) + if data['meta'].get('deleted', False): + abort(410) replies = list(DB.inbox.find({ 'type': 'Create', @@ -570,12 +572,18 @@ def outbox(): @app.route('/outbox/') def outbox_detail(item_id): - doc = DB.outbox.find_one({'id': item_id, 'meta.deleted': False}) + doc = DB.outbox.find_one({'id': item_id}) + if doc['meta'].get('deleted', False): + obj = activitypub.parse_activity(doc['activity']) + resp = jsonify(**obj.get_object().get_tombstone()) + resp.status_code = 410 + return resp return jsonify(**activity_from_doc(doc)) @app.route('/outbox//activity') def outbox_activity(item_id): + # TODO(tsileo): handle Tombstone data = DB.outbox.find_one({'id': item_id, 'meta.deleted': False}) if not data: abort(404) @@ -587,6 +595,7 @@ def outbox_activity(item_id): @app.route('/outbox//replies') def outbox_activity_replies(item_id): + # TODO(tsileo): handle Tombstone if not is_api_request(): abort(404) data = DB.outbox.find_one({'id': item_id, 'meta.deleted': False}) @@ -614,6 +623,7 @@ def outbox_activity_replies(item_id): @app.route('/outbox//likes') def outbox_activity_likes(item_id): + # TODO(tsileo): handle Tombstone if not is_api_request(): abort(404) data = DB.outbox.find_one({'id': item_id, 'meta.deleted': False}) @@ -642,6 +652,7 @@ def outbox_activity_likes(item_id): @app.route('/outbox//shares') def outbox_activity_shares(item_id): + # TODO(tsileo): handle Tombstone if not is_api_request(): abort(404) data = DB.outbox.find_one({'id': item_id, 'meta.deleted': False}) @@ -755,7 +766,7 @@ def api_user_key(): return flask_jsonify(api_key=ADMIN_API_KEY) -def _user_api_arg(key: str, **kwargs: Dict[str, Any]) -> str: +def _user_api_arg(key: str, **kwargs): """Try to get the given key from the requests, try JSON body, form data and query arg.""" if request.is_json: oid = request.json.get(key)