mirror of
https://codeberg.org/rimu/pyfedi
synced 2025-01-23 19:36:56 -08:00
debug lemmy vote federation
This commit is contained in:
parent
d73eb55d72
commit
3038cb7118
4 changed files with 138 additions and 89 deletions
|
@ -568,7 +568,7 @@ def shared_inbox():
|
||||||
db.session.delete(join_request)
|
db.session.delete(join_request)
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
activity_log.result = 'success'
|
activity_log.result = 'success'
|
||||||
elif request_json['object']['type'] == 'Like': # Undoing an upvote
|
elif request_json['object']['type'] == 'Like': # Undoing an upvote or downvote
|
||||||
activity_log.activity_type = request_json['object']['type']
|
activity_log.activity_type = request_json['object']['type']
|
||||||
user_ap_id = request_json['actor']
|
user_ap_id = request_json['actor']
|
||||||
user = find_actor_or_create(user_ap_id)
|
user = find_actor_or_create(user_ap_id)
|
||||||
|
@ -583,19 +583,23 @@ def shared_inbox():
|
||||||
existing_vote = PostVote.query.filter_by(user_id=user.id, post_id=post.id).first()
|
existing_vote = PostVote.query.filter_by(user_id=user.id, post_id=post.id).first()
|
||||||
if existing_vote:
|
if existing_vote:
|
||||||
post.author.reputation -= existing_vote.effect
|
post.author.reputation -= existing_vote.effect
|
||||||
|
if existing_vote.effect < 0: # Lemmy sends 'like' for upvote and 'dislike' for down votes. Cool! When it undoes an upvote it sends an 'Undo Like'. Fine. When it undoes a downvote it sends an 'Undo Like' - not 'Undo Dislike'?!
|
||||||
|
post.down_votes -= 1
|
||||||
|
else:
|
||||||
post.up_votes -= 1
|
post.up_votes -= 1
|
||||||
post.score -= 1
|
post.score -= existing_vote.effect
|
||||||
db.session.delete(existing_vote)
|
db.session.delete(existing_vote)
|
||||||
|
activity_log.result = 'success'
|
||||||
if user and comment:
|
if user and comment:
|
||||||
existing_vote = PostReplyVote.query.filter_by(user_id=user.id, post_reply_id=comment.id).first()
|
existing_vote = PostReplyVote.query.filter_by(user_id=user.id, post_reply_id=comment.id).first()
|
||||||
if existing_vote:
|
if existing_vote:
|
||||||
comment.author.reputation -= existing_vote.effect
|
comment.author.reputation -= existing_vote.effect
|
||||||
comment.up_votes -= 1
|
comment.up_votes -= 1
|
||||||
comment.score -= 1
|
comment.score -= existing_vote.effect
|
||||||
db.session.delete(existing_vote)
|
db.session.delete(existing_vote)
|
||||||
activity_log.result = 'success'
|
activity_log.result = 'success'
|
||||||
|
|
||||||
elif request_json['object']['type'] == 'Dislike': # Undoing a downvote
|
elif request_json['object']['type'] == 'Dislike': # Undoing a downvote - probably unused
|
||||||
activity_log.activity_type = request_json['object']['type']
|
activity_log.activity_type = request_json['object']['type']
|
||||||
user_ap_id = request_json['actor']
|
user_ap_id = request_json['actor']
|
||||||
user = find_actor_or_create(user_ap_id)
|
user = find_actor_or_create(user_ap_id)
|
||||||
|
@ -609,17 +613,18 @@ def shared_inbox():
|
||||||
if user and post:
|
if user and post:
|
||||||
existing_vote = PostVote.query.filter_by(user_id=user.id, post_id=post.id).first()
|
existing_vote = PostVote.query.filter_by(user_id=user.id, post_id=post.id).first()
|
||||||
if existing_vote:
|
if existing_vote:
|
||||||
post.author.reputation += existing_vote.effect
|
post.author.reputation -= existing_vote.effect
|
||||||
post.up_votes += 1
|
post.down_votes -= 1
|
||||||
post.score += 1
|
post.score -= existing_vote.effect
|
||||||
db.session.delete(existing_vote)
|
db.session.delete(existing_vote)
|
||||||
|
activity_log.result = 'success'
|
||||||
if user and comment:
|
if user and comment:
|
||||||
existing_vote = PostReplyVote.query.filter_by(user_id=user.id,
|
existing_vote = PostReplyVote.query.filter_by(user_id=user.id,
|
||||||
post_reply_id=comment.id).first()
|
post_reply_id=comment.id).first()
|
||||||
if existing_vote:
|
if existing_vote:
|
||||||
comment.author.reputation += existing_vote.effect
|
comment.author.reputation -= existing_vote.effect
|
||||||
comment.up_votes += 1
|
comment.down_votes -= 1
|
||||||
comment.score += 1
|
comment.score -= existing_vote.effect
|
||||||
db.session.delete(existing_vote)
|
db.session.delete(existing_vote)
|
||||||
activity_log.result = 'success'
|
activity_log.result = 'success'
|
||||||
|
|
||||||
|
@ -673,30 +678,74 @@ def shared_inbox():
|
||||||
target_ap_id = request_json['object']
|
target_ap_id = request_json['object']
|
||||||
post = None
|
post = None
|
||||||
comment = None
|
comment = None
|
||||||
|
effect = 1.0
|
||||||
if '/comment/' in target_ap_id:
|
if '/comment/' in target_ap_id:
|
||||||
comment = PostReply.query.filter_by(ap_id=target_ap_id).first()
|
comment = PostReply.query.filter_by(ap_id=target_ap_id).first()
|
||||||
if '/post/' in target_ap_id:
|
if '/post/' in target_ap_id:
|
||||||
post = Post.query.filter_by(ap_id=target_ap_id).first()
|
post = Post.query.filter_by(ap_id=target_ap_id).first()
|
||||||
|
if user.ap_domain:
|
||||||
|
# alter the effect of upvotes based on their instance
|
||||||
|
instance = Instance.query.filter_by(domain=user.ap_domain).first()
|
||||||
|
if instance:
|
||||||
|
effect = instance.vote_weight
|
||||||
if user and post:
|
if user and post:
|
||||||
effect = 1
|
existing_vote = PostVote.query.filter_by(user_id=user.id, post_id=post.id).first()
|
||||||
|
if not existing_vote:
|
||||||
post.up_votes += 1
|
post.up_votes += 1
|
||||||
post.score += 1
|
post.score += effect
|
||||||
|
vote = PostVote(user_id=user.id, post_id=post.id, author_id=post.author.id,
|
||||||
|
effect=effect)
|
||||||
|
post.author.reputation += effect
|
||||||
|
db.session.add(vote)
|
||||||
|
else:
|
||||||
|
# remove previous cast downvote
|
||||||
|
if existing_vote.effect < 0:
|
||||||
|
post.author.reputation -= existing_vote.effect
|
||||||
|
post.down_votes -= 1
|
||||||
|
post.score -= existing_vote.effect
|
||||||
|
db.session.delete(existing_vote)
|
||||||
|
|
||||||
|
# apply up vote
|
||||||
|
post.up_votes += 1
|
||||||
|
post.score += effect
|
||||||
vote = PostVote(user_id=user.id, post_id=post.id, author_id=post.author.id,
|
vote = PostVote(user_id=user.id, post_id=post.id, author_id=post.author.id,
|
||||||
effect=effect)
|
effect=effect)
|
||||||
post.author.reputation += effect
|
post.author.reputation += effect
|
||||||
db.session.add(vote)
|
db.session.add(vote)
|
||||||
activity_log.result = 'success'
|
activity_log.result = 'success'
|
||||||
elif user and comment:
|
elif user and comment:
|
||||||
effect = 1
|
existing_vote = PostReplyVote.query.filter_by(user_id=user.id,
|
||||||
|
post_reply_id=comment.id).first()
|
||||||
|
if not existing_vote:
|
||||||
comment.up_votes += 1
|
comment.up_votes += 1
|
||||||
comment.score += 1
|
comment.score += effect
|
||||||
vote = PostReplyVote(user_id=user.id, post_reply_id=comment.id,
|
vote = PostReplyVote(user_id=user.id, post_reply_id=comment.id,
|
||||||
author_id=comment.author.id, effect=effect)
|
author_id=comment.author.id, effect=effect)
|
||||||
comment.author.reputation += effect
|
comment.author.reputation += effect
|
||||||
db.session.add(vote)
|
db.session.add(vote)
|
||||||
|
else:
|
||||||
|
# remove previously cast downvote
|
||||||
|
if existing_vote.effect < 0:
|
||||||
|
comment.author.reputation -= existing_vote.effect
|
||||||
|
comment.down_votes -= 1
|
||||||
|
comment.score -= existing_vote.effect
|
||||||
|
db.session.delete(existing_vote)
|
||||||
|
|
||||||
|
# apply up vote
|
||||||
|
comment.up_votes += 1
|
||||||
|
comment.score += effect
|
||||||
|
vote = PostReplyVote(user_id=user.id, post_reply_id=comment.id,
|
||||||
|
author_id=comment.author.id, effect=effect)
|
||||||
|
comment.author.reputation += effect
|
||||||
|
db.session.add(vote)
|
||||||
|
else:
|
||||||
|
pass # they have already upvoted this reply
|
||||||
activity_log.result = 'success'
|
activity_log.result = 'success'
|
||||||
|
|
||||||
elif request_json['type'] == 'Dislike':
|
elif request_json['type'] == 'Dislike':
|
||||||
|
if get_setting('allow_dislike', True) is False:
|
||||||
|
activity_log.exception_message = 'Dislike ignored because of allow_dislike setting'
|
||||||
|
else:
|
||||||
activity_log.activity_type = request_json['type']
|
activity_log.activity_type = request_json['type']
|
||||||
user_ap_id = request_json['actor']
|
user_ap_id = request_json['actor']
|
||||||
user = find_actor_or_create(user_ap_id)
|
user = find_actor_or_create(user_ap_id)
|
||||||
|
@ -708,13 +757,12 @@ def shared_inbox():
|
||||||
if '/post/' in target_ap_id:
|
if '/post/' in target_ap_id:
|
||||||
post = Post.query.filter_by(ap_id=target_ap_id).first()
|
post = Post.query.filter_by(ap_id=target_ap_id).first()
|
||||||
if user and comment:
|
if user and comment:
|
||||||
#
|
|
||||||
existing_vote = PostReplyVote.query.filter_by(user_id=user.id,
|
existing_vote = PostReplyVote.query.filter_by(user_id=user.id,
|
||||||
post_reply_id=comment.id).first()
|
post_reply_id=comment.id).first()
|
||||||
if not existing_vote:
|
if not existing_vote:
|
||||||
effect = -1
|
effect = -1.0
|
||||||
comment.down_votes += 1
|
comment.down_votes += 1
|
||||||
comment.score -= 1
|
comment.score -= 1.0
|
||||||
vote = PostReplyVote(user_id=user.id, post_reply_id=comment.id,
|
vote = PostReplyVote(user_id=user.id, post_reply_id=comment.id,
|
||||||
author_id=comment.author.id, effect=effect)
|
author_id=comment.author.id, effect=effect)
|
||||||
comment.author.reputation += effect
|
comment.author.reputation += effect
|
||||||
|
@ -724,13 +772,13 @@ def shared_inbox():
|
||||||
if existing_vote.effect > 0:
|
if existing_vote.effect > 0:
|
||||||
comment.author.reputation -= existing_vote.effect
|
comment.author.reputation -= existing_vote.effect
|
||||||
comment.up_votes -= 1
|
comment.up_votes -= 1
|
||||||
comment.score -= 1
|
comment.score -= existing_vote.effect
|
||||||
db.session.delete(existing_vote)
|
db.session.delete(existing_vote)
|
||||||
|
|
||||||
# apply down vote
|
# apply down vote
|
||||||
effect = -1
|
effect = -1.0
|
||||||
comment.down_votes += 1
|
comment.down_votes += 1
|
||||||
comment.score -= 1
|
comment.score -= 1.0
|
||||||
vote = PostReplyVote(user_id=user.id, post_reply_id=comment.id,
|
vote = PostReplyVote(user_id=user.id, post_reply_id=comment.id,
|
||||||
author_id=comment.author.id, effect=effect)
|
author_id=comment.author.id, effect=effect)
|
||||||
comment.author.reputation += effect
|
comment.author.reputation += effect
|
||||||
|
@ -741,9 +789,9 @@ def shared_inbox():
|
||||||
elif user and post:
|
elif user and post:
|
||||||
existing_vote = PostVote.query.filter_by(user_id=user.id, post_id=post.id).first()
|
existing_vote = PostVote.query.filter_by(user_id=user.id, post_id=post.id).first()
|
||||||
if not existing_vote:
|
if not existing_vote:
|
||||||
effect = -1
|
effect = -1.0
|
||||||
post.down_votes += 1
|
post.down_votes += 1
|
||||||
post.score -= 1
|
post.score -= 1.0
|
||||||
vote = PostVote(user_id=user.id, post_id=post.id, author_id=post.author.id,
|
vote = PostVote(user_id=user.id, post_id=post.id, author_id=post.author.id,
|
||||||
effect=effect)
|
effect=effect)
|
||||||
post.author.reputation += effect
|
post.author.reputation += effect
|
||||||
|
@ -753,13 +801,13 @@ def shared_inbox():
|
||||||
if existing_vote.effect > 0:
|
if existing_vote.effect > 0:
|
||||||
post.author.reputation -= existing_vote.effect
|
post.author.reputation -= existing_vote.effect
|
||||||
post.up_votes -= 1
|
post.up_votes -= 1
|
||||||
post.score -= 1
|
post.score -= existing_vote.effect
|
||||||
db.session.delete(existing_vote)
|
db.session.delete(existing_vote)
|
||||||
|
|
||||||
# apply down vote
|
# apply down vote
|
||||||
effect = -1
|
effect = -1.0
|
||||||
post.down_votes += 1
|
post.down_votes += 1
|
||||||
post.score -= 1
|
post.score -= 1.0
|
||||||
vote = PostVote(user_id=user.id, post_id=post.id, author_id=post.author.id,
|
vote = PostVote(user_id=user.id, post_id=post.id, author_id=post.author.id,
|
||||||
effect=effect)
|
effect=effect)
|
||||||
post.author.reputation += effect
|
post.author.reputation += effect
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
{% if user.deleted %}
|
{% if user.deleted %}
|
||||||
[deleted]
|
[deleted]
|
||||||
{% else %}
|
{% else %}
|
||||||
<a href="/u/{{ user.link() }}">{{ user.user_name if user.ap_id == none else user.ap_id }}</a>
|
<a href="/u/{{ user.link() }}" title="{{ user.ap_id if user.ap_id != none else user.user_name }}">{{ user.user_name }}</a>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
{% endmacro %}
|
{% endmacro %}
|
||||||
<!doctype html>
|
<!doctype html>
|
||||||
|
|
|
@ -26,7 +26,7 @@
|
||||||
<a href="{{ url_for('community.show_post', post_id=post.id, _anchor='replies') }}"><span class="fe fe-reply"></span></a>
|
<a href="{{ url_for('community.show_post', post_id=post.id, _anchor='replies') }}"><span class="fe fe-reply"></span></a>
|
||||||
<a href="{{ url_for('community.show_post', post_id=post.id, _anchor='replies') }}">{{ post.reply_count }}</a>
|
<a href="{{ url_for('community.show_post', post_id=post.id, _anchor='replies') }}">{{ post.reply_count }}</a>
|
||||||
</div>
|
</div>
|
||||||
<div class="col-2">...</div>
|
<div class="col-2">{{ post.score }}</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div class="col col-md-2">
|
<div class="col col-md-2">
|
||||||
|
|
|
@ -4,7 +4,7 @@ from flask_babel import get_locale
|
||||||
|
|
||||||
from app import create_app, db, cli
|
from app import create_app, db, cli
|
||||||
import os, click
|
import os, click
|
||||||
from flask import session, g
|
from flask import session, g, json
|
||||||
from app.constants import POST_TYPE_LINK, POST_TYPE_IMAGE, POST_TYPE_ARTICLE
|
from app.constants import POST_TYPE_LINK, POST_TYPE_IMAGE, POST_TYPE_ARTICLE
|
||||||
from app.utils import getmtime, gibberish, shorten_string, shorten_url, digits, user_access
|
from app.utils import getmtime, gibberish, shorten_string, shorten_url, digits, user_access
|
||||||
|
|
||||||
|
@ -29,6 +29,7 @@ with app.app_context():
|
||||||
app.jinja_env.globals['len'] = len
|
app.jinja_env.globals['len'] = len
|
||||||
app.jinja_env.globals['digits'] = digits
|
app.jinja_env.globals['digits'] = digits
|
||||||
app.jinja_env.globals['str'] = str
|
app.jinja_env.globals['str'] = str
|
||||||
|
app.jinja_env.globals['json_loads'] = json.loads
|
||||||
app.jinja_env.globals['user_access'] = user_access
|
app.jinja_env.globals['user_access'] = user_access
|
||||||
app.jinja_env.filters['shorten'] = shorten_string
|
app.jinja_env.filters['shorten'] = shorten_string
|
||||||
app.jinja_env.filters['shorten_url'] = shorten_url
|
app.jinja_env.filters['shorten_url'] = shorten_url
|
||||||
|
|
Loading…
Reference in a new issue