mirror of
https://codeberg.org/rimu/pyfedi
synced 2025-01-24 03:43:42 -08:00
Federate Comment Votes by local users in local communities
This commit is contained in:
parent
79239b7273
commit
c8161b7204
1 changed files with 47 additions and 19 deletions
|
@ -411,61 +411,89 @@ def post_vote(post_id: int, vote_direction):
|
||||||
def comment_vote(comment_id, vote_direction):
|
def comment_vote(comment_id, vote_direction):
|
||||||
comment = PostReply.query.get_or_404(comment_id)
|
comment = PostReply.query.get_or_404(comment_id)
|
||||||
existing_vote = PostReplyVote.query.filter_by(user_id=current_user.id, post_reply_id=comment.id).first()
|
existing_vote = PostReplyVote.query.filter_by(user_id=current_user.id, post_reply_id=comment.id).first()
|
||||||
|
undo = None
|
||||||
if existing_vote:
|
if existing_vote:
|
||||||
if existing_vote.effect > 0: # previous vote was up
|
if existing_vote.effect > 0: # previous vote was up
|
||||||
if vote_direction == 'upvote': # new vote is also up, so remove it
|
if vote_direction == 'upvote': # new vote is also up, so remove it
|
||||||
db.session.delete(existing_vote)
|
db.session.delete(existing_vote)
|
||||||
comment.up_votes -= 1
|
comment.up_votes -= 1
|
||||||
comment.score -= 1
|
comment.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
|
||||||
comment.up_votes -= 1
|
comment.up_votes -= 1
|
||||||
comment.down_votes += 1
|
comment.down_votes += 1
|
||||||
comment.score -= 2
|
comment.score -= 2
|
||||||
downvoted_class = 'voted_down'
|
|
||||||
else: # previous vote was down
|
else: # previous vote was down
|
||||||
if vote_direction == 'downvote': # new vote is also down, so remove it
|
if vote_direction == 'downvote': # new vote is also down, so remove it
|
||||||
db.session.delete(existing_vote)
|
db.session.delete(existing_vote)
|
||||||
comment.down_votes -= 1
|
comment.down_votes -= 1
|
||||||
comment.score += 1
|
comment.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
|
||||||
comment.up_votes += 1
|
comment.up_votes += 1
|
||||||
comment.down_votes -= 1
|
comment.down_votes -= 1
|
||||||
comment.score += 2
|
comment.score += 2
|
||||||
upvoted_class = 'voted_up'
|
|
||||||
else:
|
else:
|
||||||
if vote_direction == 'upvote':
|
if vote_direction == 'upvote':
|
||||||
effect = 1
|
effect = 1
|
||||||
comment.up_votes += 1
|
comment.up_votes += 1
|
||||||
comment.score += 1
|
comment.score += 1
|
||||||
upvoted_class = 'voted_up'
|
|
||||||
else:
|
else:
|
||||||
effect = -1
|
effect = -1
|
||||||
comment.down_votes += 1
|
comment.down_votes += 1
|
||||||
comment.score -= 1
|
comment.score -= 1
|
||||||
downvoted_class = 'voted_down'
|
|
||||||
vote = PostReplyVote(user_id=current_user.id, post_reply_id=comment_id, author_id=comment.author.id, effect=effect)
|
vote = PostReplyVote(user_id=current_user.id, post_reply_id=comment_id, author_id=comment.author.id, effect=effect)
|
||||||
comment.author.reputation += effect
|
comment.author.reputation += effect
|
||||||
db.session.add(vote)
|
db.session.add(vote)
|
||||||
|
|
||||||
if comment.community.is_local():
|
if not comment.community.local_only:
|
||||||
...
|
if undo:
|
||||||
# todo: federate vote
|
action_json = {
|
||||||
else:
|
'actor': current_user.profile_id(),
|
||||||
if not comment.community.local_only:
|
'type': 'Undo',
|
||||||
action_type = 'Like' if vote_direction == 'upvote' else 'Dislike'
|
'id': f"https://{current_app.config['SERVER_NAME']}/activities/undo/{gibberish(15)}",
|
||||||
action_json = {
|
'audience': comment.community.profile_id(),
|
||||||
|
'object': {
|
||||||
'actor': current_user.profile_id(),
|
'actor': current_user.profile_id(),
|
||||||
'object': comment.profile_id(),
|
'object': comment.profile_id(),
|
||||||
'type': action_type,
|
'type': undo,
|
||||||
'id': f"https://{current_app.config['SERVER_NAME']}/activities/{action_type.lower()}/{gibberish(15)}",
|
'id': f"https://{current_app.config['SERVER_NAME']}/activities/{undo.lower()}/{gibberish(15)}",
|
||||||
'audience': comment.community.profile_id()
|
'audience': comment.community.profile_id()
|
||||||
}
|
}
|
||||||
success = post_request(comment.community.ap_inbox_url, action_json, current_user.private_key,
|
}
|
||||||
current_user.ap_profile_id + '#main-key')
|
else:
|
||||||
if not success:
|
action_type = 'Like' if vote_direction == 'upvote' else 'Dislike'
|
||||||
flash('Failed to send vote', 'error')
|
action_json = {
|
||||||
|
'actor': current_user.profile_id(),
|
||||||
|
'object': comment.profile_id(),
|
||||||
|
'type': action_type,
|
||||||
|
'id': f"https://{current_app.config['SERVER_NAME']}/activities/{action_type.lower()}/{gibberish(15)}",
|
||||||
|
'audience': comment.community.profile_id()
|
||||||
|
}
|
||||||
|
if comment.community.is_local():
|
||||||
|
announce = {
|
||||||
|
"id": f"https://{current_app.config['SERVER_NAME']}/activities/announce/{gibberish(15)}",
|
||||||
|
"type": 'Announce',
|
||||||
|
"to": [
|
||||||
|
"https://www.w3.org/ns/activitystreams#Public"
|
||||||
|
],
|
||||||
|
"actor": comment.community.ap_profile_id,
|
||||||
|
"cc": [
|
||||||
|
comment.community.ap_followers_url
|
||||||
|
],
|
||||||
|
'@context': default_context(),
|
||||||
|
'object': action_json
|
||||||
|
}
|
||||||
|
for instance in comment.community.following_instances():
|
||||||
|
if instance.inbox and not current_user.has_blocked_instance(instance.id) and not instance_banned(instance.domain):
|
||||||
|
send_to_remote_instance(instance.id, comment.community.id, announce)
|
||||||
|
else:
|
||||||
|
success = post_request(comment.community.ap_inbox_url, action_json, current_user.private_key,
|
||||||
|
current_user.ap_profile_id + '#main-key')
|
||||||
|
if not success:
|
||||||
|
flash('Failed to send vote', 'warning')
|
||||||
|
|
||||||
current_user.last_seen = utcnow()
|
current_user.last_seen = utcnow()
|
||||||
current_user.ip_address = ip_address()
|
current_user.ip_address = ip_address()
|
||||||
|
@ -478,9 +506,9 @@ def comment_vote(comment_id, vote_direction):
|
||||||
|
|
||||||
recently_upvoted = []
|
recently_upvoted = []
|
||||||
recently_downvoted = []
|
recently_downvoted = []
|
||||||
if vote_direction == 'upvote':
|
if vote_direction == 'upvote' and undo is None:
|
||||||
recently_upvoted = [comment_id]
|
recently_upvoted = [comment_id]
|
||||||
elif vote_direction == 'downvote':
|
elif vote_direction == 'downvote' and undo is None:
|
||||||
recently_downvoted = [comment_id]
|
recently_downvoted = [comment_id]
|
||||||
cache.delete_memoized(recently_upvoted_post_replies, current_user.id)
|
cache.delete_memoized(recently_upvoted_post_replies, current_user.id)
|
||||||
cache.delete_memoized(recently_downvoted_post_replies, current_user.id)
|
cache.delete_memoized(recently_downvoted_post_replies, current_user.id)
|
||||||
|
|
Loading…
Add table
Reference in a new issue