From 7ee61390685f7818a338a0538bc8817163e0a7c2 Mon Sep 17 00:00:00 2001 From: rimu <3310831+rimu@users.noreply.github.com> Date: Fri, 5 Jan 2024 09:39:20 +1300 Subject: [PATCH] announce undo vote --- app/activitypub/routes.py | 14 ++++++++++++-- app/activitypub/util.py | 11 +++++------ 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/app/activitypub/routes.py b/app/activitypub/routes.py index edf80253..d81e17f1 100644 --- a/app/activitypub/routes.py +++ b/app/activitypub/routes.py @@ -555,6 +555,16 @@ def process_inbox_request(request_json, activitypublog_id, ip_address): activity_log.result = 'success' else: activity_log.exception_message = 'PostReply not found' + elif request_json['object']['type'] == 'Undo': + if request_json['object']['object']['type'] == 'Like': + activity_log.activity_type = request_json['object']['object']['type'] + user_ap_id = request_json['object']['actor'] + user = find_actor_or_create(user_ap_id) + post = None + comment = None + target_ap_id = request_json['object']['object']['object'] # object object object! + post = undo_vote(activity_log, comment, post, target_ap_id, user) + activity_log.result = 'success' else: activity_log.exception_message = 'Invalid type for Announce' @@ -641,7 +651,7 @@ def process_inbox_request(request_json, activitypublog_id, ip_address): comment = None target_ap_id = request_json['object']['object'] post = undo_vote(activity_log, comment, post, target_ap_id, user) - + activity_log.result = 'success' elif request_json['object']['type'] == 'Dislike': # Undoing a downvote - probably unused activity_log.activity_type = request_json['object']['type'] user_ap_id = request_json['actor'] @@ -650,7 +660,7 @@ def process_inbox_request(request_json, activitypublog_id, ip_address): comment = None target_ap_id = request_json['object']['object'] post = undo_downvote(activity_log, comment, post, target_ap_id, user) - + activity_log.result = 'success' elif request_json['type'] == 'Update': activity_log.activity_type = 'Update' if request_json['object']['type'] == 'Page': # Editing a post diff --git a/app/activitypub/util.py b/app/activitypub/util.py index 8191cb93..fa0fb86c 100644 --- a/app/activitypub/util.py +++ b/app/activitypub/util.py @@ -1052,11 +1052,9 @@ def undo_downvote(activity_log, comment, post, target_ap_id, user): def undo_vote(activity_log, comment, post, target_ap_id, user): - if '/comment/' in target_ap_id: - comment = PostReply.query.filter_by(ap_id=target_ap_id).first() - if '/post/' in target_ap_id: - post = Post.query.filter_by(ap_id=target_ap_id).first() - if (user and not user.is_local()) and post: + voted_on = find_liked_object(target_ap_id) + if (user and not user.is_local()) and isinstance(voted_on, Post): + post = voted_on user.last_seen = utcnow() existing_vote = PostVote.query.filter_by(user_id=user.id, post_id=post.id).first() if existing_vote: @@ -1068,7 +1066,8 @@ def undo_vote(activity_log, comment, post, target_ap_id, user): post.score -= existing_vote.effect db.session.delete(existing_vote) activity_log.result = 'success' - if (user and not user.is_local()) and comment: + if (user and not user.is_local()) and isinstance(voted_on, PostReply): + comment = voted_on existing_vote = PostReplyVote.query.filter_by(user_id=user.id, post_reply_id=comment.id).first() if existing_vote: comment.author.reputation -= existing_vote.effect