Merge pull request 'Auto-upvote own post/comment, and handle top-level 'this' reply' (#167) from freamon/pyfedi:2204 into main

Reviewed-on: https://codeberg.org/rimu/pyfedi/pulls/167
This commit is contained in:
rimu 2024-04-23 09:11:49 +00:00
commit 1c5fb460c2
2 changed files with 42 additions and 2 deletions

View file

@ -33,7 +33,7 @@ from app.utils import get_setting, render_template, allowlist_html, markdown_to_
request_etag_matches, return_304, instance_banned, can_create_post, can_upvote, can_downvote, user_filters_posts, \ request_etag_matches, return_304, instance_banned, can_create_post, can_upvote, can_downvote, user_filters_posts, \
joined_communities, moderating_communities, blocked_domains, mimetype_from_url, blocked_instances, \ joined_communities, moderating_communities, blocked_domains, mimetype_from_url, blocked_instances, \
community_moderators, communities_banned_from, show_ban_message, recently_upvoted_posts, recently_downvoted_posts, \ community_moderators, communities_banned_from, show_ban_message, recently_upvoted_posts, recently_downvoted_posts, \
blocked_users blocked_users, post_ranking
from feedgen.feed import FeedGenerator from feedgen.feed import FeedGenerator
from datetime import timezone, timedelta from datetime import timezone, timedelta
@ -496,6 +496,7 @@ def add_discussion_post(actor):
post.ap_id = f"https://{current_app.config['SERVER_NAME']}/post/{post.id}" post.ap_id = f"https://{current_app.config['SERVER_NAME']}/post/{post.id}"
db.session.commit() db.session.commit()
upvote_own_post(post)
notify_about_post(post) notify_about_post(post)
if not community.local_only: if not community.local_only:
@ -571,6 +572,7 @@ def add_image_post(actor):
post.cross_posts.append(op.id) post.cross_posts.append(op.id)
db.session.commit() db.session.commit()
upvote_own_post(post)
notify_about_post(post) notify_about_post(post)
if not community.local_only: if not community.local_only:
@ -644,6 +646,7 @@ def add_link_post(actor):
post.cross_posts.append(op.id) post.cross_posts.append(op.id)
db.session.commit() db.session.commit()
upvote_own_post(post)
notify_about_post(post) notify_about_post(post)
if not community.local_only: if not community.local_only:
@ -717,6 +720,7 @@ def add_video_post(actor):
post.cross_posts.append(op.id) post.cross_posts.append(op.id)
db.session.commit() db.session.commit()
upvote_own_post(post)
notify_about_post(post) notify_about_post(post)
if not community.local_only: if not community.local_only:
@ -1406,3 +1410,11 @@ def lookup(community, domain):
return redirect('/') return redirect('/')
def upvote_own_post(post):
post.score = 1
post.up_votes = 1
post.ranking = post_ranking(post.score, utcnow())
vote = PostVote(user_id=current_user.id, post_id=post.id, author_id=current_user.id, effect=1)
db.session.add(vote)
db.session.commit()
cache.delete_memoized(recently_upvoted_posts, current_user.id)

View file

@ -84,6 +84,16 @@ def show_post(post_id: int):
flash(_('This type of comment is not accepted, sorry.'), 'error') flash(_('This type of comment is not accepted, sorry.'), 'error')
return redirect(url_for('activitypub.post_ap', post_id=post_id)) return redirect(url_for('activitypub.post_ap', post_id=post_id))
# respond to comments that are just variants of 'this'
if reply_is_stupid(form.body.data):
existing_vote = PostVote.query.filter_by(user_id=current_user.id, post_id=post.id).first()
if existing_vote is None:
flash(_('We have upvoted the post for you.'), 'warning')
post_vote(post.id, 'upvote')
else:
flash(_('You have already upvoted the post, you do not need to say "this" also.'), 'error')
return redirect(url_for('activitypub.post_ap', post_id=post_id))
reply = PostReply(user_id=current_user.id, post_id=post.id, community_id=community.id, body=form.body.data, reply = PostReply(user_id=current_user.id, post_id=post.id, community_id=community.id, body=form.body.data,
body_html=markdown_to_html(form.body.data), body_html_safe=True, body_html=markdown_to_html(form.body.data), body_html_safe=True,
from_bot=current_user.bot, nsfw=post.nsfw, nsfl=post.nsfl, from_bot=current_user.bot, nsfw=post.nsfw, nsfl=post.nsfl,
@ -102,6 +112,15 @@ def show_post(post_id: int):
db.session.add(reply) db.session.add(reply)
db.session.commit() db.session.commit()
# upvote own reply
reply.score = 1
reply.up_votes = 1
reply.ranking = confidence(1, 0)
vote = PostReplyVote(user_id=current_user.id, post_reply_id=reply.id, author_id=current_user.id, effect=1)
db.session.add(vote)
cache.delete_memoized(recently_upvoted_post_replies, current_user.id)
reply.ap_id = reply.profile_id() reply.ap_id = reply.profile_id()
if current_user.reputation > 100: if current_user.reputation > 100:
reply.up_votes += 1 reply.up_votes += 1
@ -612,8 +631,16 @@ def add_reply(post_id: int, comment_id: int):
db.session.add(notification) db.session.add(notification)
in_reply_to.author.unread_notifications += 1 in_reply_to.author.unread_notifications += 1
db.session.commit() db.session.commit()
# upvote own reply
reply.score = 1
reply.up_votes = 1
reply.ranking = confidence(1, 0)
vote = PostReplyVote(user_id=current_user.id, post_reply_id=reply.id, author_id=current_user.id, effect=1)
db.session.add(vote)
cache.delete_memoized(recently_upvoted_post_replies, current_user.id)
reply.ap_id = reply.profile_id() reply.ap_id = reply.profile_id()
db.session.commit()
if current_user.reputation > 100: if current_user.reputation > 100:
reply.up_votes += 1 reply.up_votes += 1
reply.score += 1 reply.score += 1
@ -726,6 +753,7 @@ def add_reply(post_id: int, comment_id: int):
return redirect(url_for('post.continue_discussion', post_id=post_id, comment_id=reply.parent_id)) return redirect(url_for('post.continue_discussion', post_id=post_id, comment_id=reply.parent_id))
else: else:
form.notify_author.data = True form.notify_author.data = True
return render_template('post/add_reply.html', title=_('Discussing %(title)s', title=post.title), post=post, return render_template('post/add_reply.html', title=_('Discussing %(title)s', title=post.title), post=post,
is_moderator=is_moderator, form=form, comment=in_reply_to, markdown_editor=current_user.is_authenticated and current_user.markdown_editor, is_moderator=is_moderator, form=form, comment=in_reply_to, markdown_editor=current_user.is_authenticated and current_user.markdown_editor,
moderating_communities=moderating_communities(current_user.get_id()), mods=mod_list, moderating_communities=moderating_communities(current_user.get_id()), mods=mod_list,