Add Undo of Post Vote to federation

This commit is contained in:
freamon 2024-04-16 18:40:48 +01:00
parent a8f7ebf442
commit 79239b7273
2 changed files with 33 additions and 15 deletions

View file

@ -708,7 +708,7 @@ def process_inbox_request(request_json, activitypublog_id, ip_address):
else: else:
activity_log.exception_message = 'PostReply not found' activity_log.exception_message = 'PostReply not found'
elif request_json['object']['type'] == 'Undo': elif request_json['object']['type'] == 'Undo':
if request_json['object']['object']['type'] == 'Like': if request_json['object']['object']['type'] == 'Like' or request_json['object']['object']['type'] == 'Dislike':
activity_log.activity_type = request_json['object']['object']['type'] activity_log.activity_type = request_json['object']['object']['type']
user_ap_id = request_json['object']['actor'] user_ap_id = request_json['object']['actor']
user = find_actor_or_create(user_ap_id) user = find_actor_or_create(user_ap_id)

View file

@ -279,6 +279,7 @@ def show_post(post_id: int):
def post_vote(post_id: int, vote_direction): def post_vote(post_id: int, vote_direction):
post = Post.query.get_or_404(post_id) post = Post.query.get_or_404(post_id)
existing_vote = PostVote.query.filter_by(user_id=current_user.id, post_id=post.id).first() existing_vote = PostVote.query.filter_by(user_id=current_user.id, post_id=post.id).first()
undo = None
if existing_vote: if existing_vote:
if not post.community.low_quality: if not post.community.low_quality:
post.author.reputation -= existing_vote.effect post.author.reputation -= existing_vote.effect
@ -287,6 +288,7 @@ def post_vote(post_id: int, vote_direction):
db.session.delete(existing_vote) db.session.delete(existing_vote)
post.up_votes -= 1 post.up_votes -= 1
post.score -= 1 post.score -= 1
undo = 'Like'
else: # new vote is down while previous vote was up, so reverse their previous vote else: # new vote is down while previous vote was up, so reverse their previous vote
existing_vote.effect = -1 existing_vote.effect = -1
post.up_votes -= 1 post.up_votes -= 1
@ -297,6 +299,7 @@ def post_vote(post_id: int, vote_direction):
db.session.delete(existing_vote) db.session.delete(existing_vote)
post.down_votes -= 1 post.down_votes -= 1
post.score += 1 post.score += 1
undo = 'Dislike'
else: # new vote is up while previous vote was down, so reverse their previous vote else: # new vote is up while previous vote was down, so reverse their previous vote
existing_vote.effect = 1 existing_vote.effect = 1
post.up_votes += 1 post.up_votes += 1
@ -333,6 +336,21 @@ def post_vote(post_id: int, vote_direction):
db.session.add(vote) db.session.add(vote)
if not post.community.local_only: if not post.community.local_only:
if undo:
action_json = {
'actor': current_user.profile_id(),
'type': 'Undo',
'id': f"https://{current_app.config['SERVER_NAME']}/activities/undo/{gibberish(15)}",
'audience': post.community.profile_id(),
'object': {
'actor': current_user.profile_id(),
'object': post.profile_id(),
'type': undo,
'id': f"https://{current_app.config['SERVER_NAME']}/activities/{undo.lower()}/{gibberish(15)}",
'audience': post.community.profile_id()
}
}
else:
action_type = 'Like' if vote_direction == 'upvote' else 'Dislike' action_type = 'Like' if vote_direction == 'upvote' else 'Dislike'
action_json = { action_json = {
'actor': current_user.profile_id(), 'actor': current_user.profile_id(),
@ -375,9 +393,9 @@ def post_vote(post_id: int, vote_direction):
recently_upvoted = [] recently_upvoted = []
recently_downvoted = [] recently_downvoted = []
if vote_direction == 'upvote': if vote_direction == 'upvote' and undo is None:
recently_upvoted = [post_id] recently_upvoted = [post_id]
elif vote_direction == 'downvote': elif vote_direction == 'downvote' and undo is None:
recently_downvoted = [post_id] recently_downvoted = [post_id]
cache.delete_memoized(recently_upvoted_posts, current_user.id) cache.delete_memoized(recently_upvoted_posts, current_user.id)
cache.delete_memoized(recently_downvoted_posts, current_user.id) cache.delete_memoized(recently_downvoted_posts, current_user.id)