From 78fd925282c469426c80401b19c85be82fa95d00 Mon Sep 17 00:00:00 2001 From: freamon Date: Tue, 7 Jan 2025 10:53:47 +0000 Subject: [PATCH] Add votes to posts from PeerTube instances when they send an update --- app/activitypub/routes.py | 5 ----- app/activitypub/util.py | 31 ++++++++++++++++++++++++++++++- 2 files changed, 30 insertions(+), 6 deletions(-) diff --git a/app/activitypub/routes.py b/app/activitypub/routes.py index 8c5d2d56..bf20f9e2 100644 --- a/app/activitypub/routes.py +++ b/app/activitypub/routes.py @@ -570,11 +570,6 @@ def replay_inbox_request(request_json): process_delete_request(request_json, True) return - # testing verify_object_from_source() - if ((request_json['type'] == 'Create' or request_json['type'] == 'Update') and - isinstance(request_json['object'], dict) and 'id' in request_json['object'] and isinstance(request_json['object']['id'], str)): - request_json['object'] = request_json['object']['id'] - process_inbox_request(request_json, True) return diff --git a/app/activitypub/util.py b/app/activitypub/util.py index 6b0da9ff..c0139e72 100644 --- a/app/activitypub/util.py +++ b/app/activitypub/util.py @@ -1782,8 +1782,37 @@ def update_post_from_activity(post: Post, request_json: dict): post.edited_at = utcnow() if request_json['object']['type'] == 'Video': + # fetching individual user details to attach to votes is probably too convoluted, so take the instance's word for it + upvotes = 1 # from OP + downvotes = 0 + endpoints = ['likes', 'dislikes'] + for endpoint in endpoints: + if endpoint in request_json['object']: + try: + object_request = get_request(request_json['object'][endpoint], headers={'Accept': 'application/activity+json'}) + except httpx.HTTPError: + time.sleep(3) + try: + object_request = get_request(request_json['object'][endpoint], headers={'Accept': 'application/activity+json'}) + except httpx.HTTPError: + object_request = None + if object_request and object_request.status_code == 200: + try: + object = object_request.json() + except: + object_request.close() + object = None + object_request.close() + if object and 'totalItems' in object: + if endpoint == 'likes': + upvotes += object['totalItems'] + if endpoint == 'dislikes': + downvotes += object['totalItems'] + post.up_votes = upvotes + post.down_votes = downvotes + post.score = upvotes - downvotes + post.ranking = post.post_ranking(post.score, post.posted_at) # return now for PeerTube, otherwise rest of this function breaks the post - # consider querying the Likes endpoint (that mostly seems to be what Updates are about) db.session.commit() return