This commit is contained in:
rimu 2023-12-27 19:51:07 +13:00
parent f6d3468c98
commit 52a4d7250f
6 changed files with 42 additions and 1 deletions

View file

@ -647,6 +647,7 @@ def activity_already_ingested(ap_id):
def downvote_post(post, user):
user.last_seen = utcnow()
user.recalculate_attitude()
existing_vote = PostVote.query.filter_by(user_id=user.id, post_id=post.id).first()
if not existing_vote:
effect = -1.0
@ -678,6 +679,7 @@ def downvote_post(post, user):
def downvote_post_reply(comment, user):
user.last_seen = utcnow()
user.recalculate_attitude()
existing_vote = PostReplyVote.query.filter_by(user_id=user.id,
post_reply_id=comment.id).first()
if not existing_vote:
@ -710,6 +712,7 @@ def downvote_post_reply(comment, user):
def upvote_post_reply(comment, user):
user.last_seen = utcnow()
user.recalculate_attitude()
effect = instance_weight(user.ap_domain)
existing_vote = PostReplyVote.query.filter_by(user_id=user.id,
post_reply_id=comment.id).first()
@ -745,6 +748,7 @@ def upvote_post_reply(comment, user):
def upvote_post(post, user):
user.last_seen = utcnow()
user.recalculate_attitude()
effect = instance_weight(user.ap_domain)
existing_vote = PostVote.query.filter_by(user_id=user.id, post_id=post.id).first()
if not existing_vote:

View file

@ -386,6 +386,34 @@ class User(UserMixin, db.Model):
return True
return self.expires < datetime(2019, 9, 1)
def recalculate_attitude(self):
upvotes = db.session.execute(text('SELECT COUNT(id) as c FROM "post_vote" WHERE user_id = :user_id AND effect > 0'),
{'user_id': self.id}).scalar()
downvotes = db.session.execute(text('SELECT COUNT(id) as c FROM "post_vote" WHERE user_id = :user_id AND effect < 0'),
{'user_id': self.id}).scalar()
if upvotes is None:
upvotes = 0
if downvotes is None:
downvotes = 0
comment_upvotes = db.session.execute(text('SELECT COUNT(id) as c FROM "post_reply_vote" WHERE user_id = :user_id AND effect > 0'),
{'user_id': self.id}).scalar()
comment_downvotes = db.session.execute(text('SELECT COUNT(id) as c FROM "post_reply_vote" WHERE user_id = :user_id AND effect < 0'),
{'user_id': self.id}).scalar()
if comment_upvotes is None:
comment_upvotes = 0
if comment_downvotes is None:
comment_downvotes = 0
total_upvotes = upvotes + comment_upvotes
total_downvotes = downvotes + comment_downvotes
if total_downvotes == 0: # guard against division by zero
self.attitude = 1.0
else:
self.attitude = (total_upvotes - total_downvotes) / (total_upvotes + total_downvotes)
def subscribed(self, community_id: int) -> int:
if community_id is None:
return False

View file

@ -226,6 +226,8 @@ def post_vote(post_id: int, vote_direction):
current_user.last_seen = utcnow()
db.session.commit()
current_user.recalculate_attitude()
db.session.commit()
post.flush_cache()
return render_template('post/_post_voting_buttons.html', post=post,
upvoted_class=upvoted_class,
@ -279,6 +281,7 @@ def comment_vote(comment_id, vote_direction):
if comment.community.is_local():
...
# todo: federate vote
else:
action_type = 'Like' if vote_direction == 'upvote' else 'Dislike'
action_json = {
@ -295,6 +298,9 @@ def comment_vote(comment_id, vote_direction):
current_user.last_seen = utcnow()
db.session.commit()
current_user.recalculate_attitude(vote_direction)
db.session.commit()
comment.post.flush_cache()
return render_template('post/_voting_buttons.html', comment=comment,
upvoted_class=upvoted_class,

View file

@ -424,6 +424,7 @@ nav.navbar {
}
.main_pane .url_thumbnail img {
width: 100%;
height: auto;
}
.community_icon {

View file

@ -108,6 +108,7 @@ nav.navbar {
img {
width: 100%;
height: auto;
}
}
}

View file

@ -35,7 +35,8 @@
</nav>
<h1 class="mt-2">{{ user.user_name if user.ap_id == none else user.ap_id }}</h1>
{% endif %}
<p class="small">{{ _('Joined') }}: {{ moment(user.created).fromNow(refresh=True) }}
<p class="small">{{ _('Joined') }}: {{ moment(user.created).fromNow(refresh=True) }}<br />
{{ _('Attitude') }}: <span title="{{ _('Ratio of upvotes cast to downvotes cast. Higher is more positive.') }}">{{ (user.attitude * 100) | round | int }}%</span></p>
{{ user.about_html|safe }}
{% if posts %}
<h2 class="mt-4">Posts</h2>