2024-03-09 14:59:05 +13:00
|
|
|
from collections import namedtuple
|
2024-04-02 18:01:24 +01:00
|
|
|
from datetime import datetime, timedelta
|
2024-01-19 15:08:39 +13:00
|
|
|
from random import randint
|
2023-11-30 20:57:51 +13:00
|
|
|
|
2023-12-30 19:22:22 +13:00
|
|
|
from flask import redirect, url_for, flash, current_app, abort, request, g, make_response
|
2024-07-07 15:01:52 +08:00
|
|
|
from flask_login import logout_user, current_user, login_required
|
2023-11-30 06:36:08 +13:00
|
|
|
from flask_babel import _
|
|
|
|
from sqlalchemy import or_, desc
|
2024-05-18 19:41:20 +12:00
|
|
|
from wtforms import SelectField, RadioField
|
2023-11-30 06:36:08 +13:00
|
|
|
|
2024-08-16 16:10:09 +12:00
|
|
|
from app import db, constants, cache, celery
|
2024-05-06 19:49:51 +12:00
|
|
|
from app.activitypub.signature import HttpSignature, post_request, default_context, post_request_in_background
|
2024-05-31 22:06:52 +01:00
|
|
|
from app.activitypub.util import notify_about_post_reply, inform_followers_of_post_update
|
2023-12-26 21:39:52 +13:00
|
|
|
from app.community.util import save_post, send_to_remote_instance
|
2024-01-19 15:08:39 +13:00
|
|
|
from app.inoculation import inoculation
|
2023-12-14 21:22:46 +13:00
|
|
|
from app.post.forms import NewReplyForm, ReportPostForm, MeaCulpaForm
|
2024-08-16 16:35:16 -04:00
|
|
|
from app.community.forms import CreateLinkForm, CreateImageForm, CreateDiscussionForm, CreateVideoForm, CreatePollForm, EditImageForm
|
2024-09-13 11:08:04 +12:00
|
|
|
from app.post.util import post_replies, get_comment_branch, get_post_reply_count, tags_to_string, url_needs_archive, \
|
2024-06-16 21:21:58 +08:00
|
|
|
generate_archive_link, body_has_no_archive_link
|
2024-04-29 21:43:37 +12:00
|
|
|
from app.constants import SUBSCRIPTION_MEMBER, SUBSCRIPTION_OWNER, SUBSCRIPTION_MODERATOR, POST_TYPE_LINK, \
|
|
|
|
POST_TYPE_IMAGE, \
|
2024-05-18 19:41:20 +12:00
|
|
|
POST_TYPE_ARTICLE, POST_TYPE_VIDEO, NOTIF_REPLY, NOTIF_POST, POST_TYPE_POLL
|
2023-11-30 06:36:08 +13:00
|
|
|
from app.models import Post, PostReply, \
|
2024-03-15 14:24:45 +13:00
|
|
|
PostReplyVote, PostVote, Notification, utcnow, UserBlock, DomainBlock, InstanceBlock, Report, Site, Community, \
|
2024-06-20 21:51:43 +08:00
|
|
|
Topic, User, Instance, NotificationSubscription, UserFollower, Poll, PollChoice, PollChoiceVote, PostBookmark, \
|
2024-08-12 20:54:10 +12:00
|
|
|
PostReplyBookmark, CommunityBlock
|
2023-11-30 06:36:08 +13:00
|
|
|
from app.post import bp
|
|
|
|
from app.utils import get_setting, render_template, allowlist_html, markdown_to_html, validation_required, \
|
2024-01-25 20:16:08 +13:00
|
|
|
shorten_string, markdown_to_text, gibberish, ap_datetime, return_304, \
|
2024-01-06 14:54:10 +13:00
|
|
|
request_etag_matches, ip_address, user_ip_banned, instance_banned, can_downvote, can_upvote, post_ranking, \
|
2024-03-12 20:06:24 +13:00
|
|
|
reply_already_exists, reply_is_just_link_to_gif_reaction, confidence, moderating_communities, joined_communities, \
|
2024-04-11 14:04:57 +12:00
|
|
|
blocked_instances, blocked_domains, community_moderators, blocked_phrases, show_ban_message, recently_upvoted_posts, \
|
2024-05-09 17:54:30 +12:00
|
|
|
recently_downvoted_posts, recently_upvoted_post_replies, recently_downvoted_post_replies, reply_is_stupid, \
|
2024-09-24 09:28:06 +12:00
|
|
|
languages_for_form, menu_topics, add_to_modlog, blocked_communities, piefed_markdown_to_lemmy_markdown, \
|
|
|
|
permission_required
|
2023-11-30 06:36:08 +13:00
|
|
|
|
|
|
|
|
|
|
|
def show_post(post_id: int):
|
|
|
|
post = Post.query.get_or_404(post_id)
|
2024-01-03 16:29:58 +13:00
|
|
|
community: Community = post.community
|
2023-12-10 15:10:09 +13:00
|
|
|
|
2024-06-02 16:45:21 +12:00
|
|
|
if community.banned or post.deleted:
|
2024-07-07 15:01:52 +08:00
|
|
|
if current_user.is_anonymous or not (current_user.is_authenticated and (current_user.is_admin() or current_user.is_staff())):
|
|
|
|
abort(404)
|
|
|
|
else:
|
|
|
|
flash(_('This post has been deleted and is only visible to staff and admins.'), 'warning')
|
2024-01-01 11:38:24 +13:00
|
|
|
|
2024-01-08 10:48:05 +13:00
|
|
|
sort = request.args.get('sort', 'hot')
|
2024-01-07 22:28:13 +13:00
|
|
|
|
2023-12-10 15:10:09 +13:00
|
|
|
# If nothing has changed since their last visit, return HTTP 304
|
2024-01-07 22:28:13 +13:00
|
|
|
current_etag = f"{post.id}{sort}_{hash(post.last_active)}"
|
2023-12-11 20:46:38 +13:00
|
|
|
if current_user.is_anonymous and request_etag_matches(current_etag):
|
2023-12-10 15:10:09 +13:00
|
|
|
return return_304(current_etag)
|
|
|
|
|
2023-12-14 21:22:46 +13:00
|
|
|
if post.mea_culpa:
|
|
|
|
flash(_('%(name)s has indicated they made a mistake in this post.', name=post.author.user_name), 'warning')
|
|
|
|
|
2024-03-13 16:40:20 +13:00
|
|
|
mods = community_moderators(community.id)
|
2024-07-17 22:11:31 +08:00
|
|
|
is_moderator = community.is_moderator()
|
2023-12-09 22:14:16 +13:00
|
|
|
|
2024-03-22 01:23:26 +00:00
|
|
|
if community.private_mods:
|
|
|
|
mod_list = []
|
|
|
|
else:
|
|
|
|
mod_user_ids = [mod.user_id for mod in mods]
|
|
|
|
mod_list = User.query.filter(User.id.in_(mod_user_ids)).all()
|
|
|
|
|
2023-12-09 22:14:16 +13:00
|
|
|
# handle top-level comments/replies
|
2023-11-30 06:36:08 +13:00
|
|
|
form = NewReplyForm()
|
2024-05-09 17:54:30 +12:00
|
|
|
form.language_id.choices = languages_for_form()
|
2023-11-30 06:36:08 +13:00
|
|
|
if current_user.is_authenticated and current_user.verified and form.validate_on_submit():
|
2023-12-14 21:22:46 +13:00
|
|
|
|
|
|
|
if not post.comments_enabled:
|
|
|
|
flash('Comments have been disabled.', 'warning')
|
|
|
|
return redirect(url_for('activitypub.post_ap', post_id=post_id))
|
|
|
|
|
2023-12-30 19:22:22 +13:00
|
|
|
if current_user.banned:
|
|
|
|
flash('You have been banned.', 'error')
|
|
|
|
logout_user()
|
|
|
|
resp = make_response(redirect(url_for('main.index')))
|
|
|
|
resp.set_cookie('sesion', '17489047567495', expires=datetime(year=2099, month=12, day=30))
|
|
|
|
return resp
|
|
|
|
|
2024-01-06 11:01:44 +13:00
|
|
|
if post.author.has_blocked_user(current_user.id):
|
|
|
|
flash(_('You cannot reply to %(name)s', name=post.author.display_name()))
|
|
|
|
return redirect(url_for('activitypub.post_ap', post_id=post_id))
|
|
|
|
|
2024-01-06 14:54:10 +13:00
|
|
|
# avoid duplicate replies
|
|
|
|
if reply_already_exists(user_id=current_user.id, post_id=post.id, parent_id=None, body=form.body.data):
|
|
|
|
return redirect(url_for('activitypub.post_ap', post_id=post_id))
|
|
|
|
|
|
|
|
# disallow low-effort gif reaction posts
|
|
|
|
if reply_is_just_link_to_gif_reaction(form.body.data):
|
|
|
|
current_user.reputation -= 1
|
|
|
|
flash(_('This type of comment is not accepted, sorry.'), 'error')
|
|
|
|
return redirect(url_for('activitypub.post_ap', post_id=post_id))
|
|
|
|
|
2024-04-22 07:48:20 +01:00
|
|
|
# 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))
|
|
|
|
|
2024-09-22 13:42:02 +00:00
|
|
|
reply = PostReply(user_id=current_user.id, post_id=post.id, community_id=community.id, body=piefed_markdown_to_lemmy_markdown(form.body.data),
|
2023-11-30 06:36:08 +13:00
|
|
|
body_html=markdown_to_html(form.body.data), body_html_safe=True,
|
2024-03-05 02:17:01 +00:00
|
|
|
from_bot=current_user.bot, nsfw=post.nsfw, nsfl=post.nsfl,
|
2024-05-09 17:54:30 +12:00
|
|
|
notify_author=form.notify_author.data, language_id=form.language_id.data, instance_id=1)
|
2024-04-29 21:43:37 +12:00
|
|
|
|
2024-01-03 16:29:58 +13:00
|
|
|
post.last_active = community.last_active = utcnow()
|
2023-12-09 22:14:16 +13:00
|
|
|
post.reply_count += 1
|
2024-01-03 16:29:58 +13:00
|
|
|
community.post_reply_count += 1
|
2024-09-13 11:08:04 +12:00
|
|
|
current_user.post_reply_count += 1
|
2024-05-09 17:54:30 +12:00
|
|
|
current_user.language_id = form.language_id.data
|
2023-12-10 15:10:09 +13:00
|
|
|
|
2023-11-30 06:36:08 +13:00
|
|
|
db.session.add(reply)
|
|
|
|
db.session.commit()
|
2024-04-22 16:14:32 +01:00
|
|
|
|
2024-04-29 21:43:37 +12:00
|
|
|
notify_about_post_reply(None, reply)
|
|
|
|
|
|
|
|
# Subscribe to own comment
|
|
|
|
if form.notify_author.data:
|
|
|
|
new_notification = NotificationSubscription(name=shorten_string(_('Replies to my comment on %(post_title)s',
|
|
|
|
post_title=post.title), 50),
|
|
|
|
user_id=current_user.id, entity_id=reply.id,
|
|
|
|
type=NOTIF_REPLY)
|
|
|
|
db.session.add(new_notification)
|
|
|
|
db.session.commit()
|
|
|
|
|
2024-04-22 16:14:32 +01:00
|
|
|
# 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)
|
|
|
|
|
2023-12-09 22:14:16 +13:00
|
|
|
reply.ap_id = reply.profile_id()
|
2024-01-02 16:07:41 +13:00
|
|
|
if current_user.reputation > 100:
|
|
|
|
reply.up_votes += 1
|
|
|
|
reply.score += 1
|
|
|
|
reply.ranking += 1
|
|
|
|
elif current_user.reputation < -100:
|
|
|
|
reply.score -= 1
|
|
|
|
reply.ranking -= 1
|
2023-11-30 06:36:08 +13:00
|
|
|
db.session.commit()
|
|
|
|
form.body.data = ''
|
|
|
|
flash('Your comment has been added.')
|
2023-12-10 15:10:09 +13:00
|
|
|
|
2023-12-09 22:14:16 +13:00
|
|
|
# federation
|
2023-12-26 21:39:52 +13:00
|
|
|
reply_json = {
|
|
|
|
'type': 'Note',
|
2024-06-05 13:21:41 +12:00
|
|
|
'id': reply.public_url(),
|
2024-03-24 03:20:45 +00:00
|
|
|
'attributedTo': current_user.public_url(),
|
2023-12-26 21:39:52 +13:00
|
|
|
'to': [
|
|
|
|
'https://www.w3.org/ns/activitystreams#Public'
|
|
|
|
],
|
|
|
|
'cc': [
|
2024-03-24 03:20:45 +00:00
|
|
|
community.public_url(), post.author.public_url()
|
2023-12-26 21:39:52 +13:00
|
|
|
],
|
|
|
|
'content': reply.body_html,
|
2024-06-05 16:23:31 +12:00
|
|
|
'inReplyTo': post.profile_id(),
|
2023-12-26 21:39:52 +13:00
|
|
|
'mediaType': 'text/html',
|
2024-09-22 13:42:02 +00:00
|
|
|
'source': {'content': reply.body, 'mediaType': 'text/markdown'},
|
2023-12-26 21:39:52 +13:00
|
|
|
'published': ap_datetime(utcnow()),
|
|
|
|
'distinguished': False,
|
2024-03-24 03:20:45 +00:00
|
|
|
'audience': community.public_url(),
|
|
|
|
'tag': [{
|
|
|
|
'href': post.author.public_url(),
|
|
|
|
'name': post.author.mention_tag(),
|
|
|
|
'type': 'Mention'
|
2024-05-09 17:54:30 +12:00
|
|
|
}],
|
|
|
|
'language': {
|
|
|
|
'identifier': reply.language_code(),
|
|
|
|
'name': reply.language_name()
|
|
|
|
}
|
2023-12-26 21:39:52 +13:00
|
|
|
}
|
|
|
|
create_json = {
|
|
|
|
'type': 'Create',
|
2024-03-24 03:20:45 +00:00
|
|
|
'actor': current_user.public_url(),
|
|
|
|
'audience': community.public_url(),
|
2023-12-26 21:39:52 +13:00
|
|
|
'to': [
|
|
|
|
'https://www.w3.org/ns/activitystreams#Public'
|
|
|
|
],
|
|
|
|
'cc': [
|
2024-03-24 03:20:45 +00:00
|
|
|
community.public_url(), post.author.public_url()
|
2023-12-26 21:39:52 +13:00
|
|
|
],
|
|
|
|
'object': reply_json,
|
2024-03-24 03:20:45 +00:00
|
|
|
'id': f"https://{current_app.config['SERVER_NAME']}/activities/create/{gibberish(15)}",
|
|
|
|
'tag': [{
|
|
|
|
'href': post.author.public_url(),
|
|
|
|
'name': post.author.mention_tag(),
|
|
|
|
'type': 'Mention'
|
|
|
|
}]
|
2023-12-26 21:39:52 +13:00
|
|
|
}
|
2024-01-03 16:29:58 +13:00
|
|
|
if not community.is_local(): # this is a remote community, send it to the instance that hosts it
|
|
|
|
success = post_request(community.ap_inbox_url, create_json, current_user.private_key,
|
2024-06-05 13:21:41 +12:00
|
|
|
current_user.public_url() + '#main-key')
|
2024-08-30 17:25:37 +12:00
|
|
|
if success is False or isinstance(success, str):
|
2023-12-22 15:34:45 +13:00
|
|
|
flash('Failed to send to remote instance', 'error')
|
2023-12-09 22:14:16 +13:00
|
|
|
else: # local community - send it to followers on remote instances
|
2023-12-26 21:39:52 +13:00
|
|
|
announce = {
|
|
|
|
"id": f"https://{current_app.config['SERVER_NAME']}/activities/announce/{gibberish(15)}",
|
|
|
|
"type": 'Announce',
|
|
|
|
"to": [
|
|
|
|
"https://www.w3.org/ns/activitystreams#Public"
|
|
|
|
],
|
2024-03-24 03:20:45 +00:00
|
|
|
"actor": community.public_url(),
|
2023-12-26 21:39:52 +13:00
|
|
|
"cc": [
|
2024-01-03 16:29:58 +13:00
|
|
|
community.ap_followers_url
|
2023-12-26 21:39:52 +13:00
|
|
|
],
|
|
|
|
'@context': default_context(),
|
|
|
|
'object': create_json
|
|
|
|
}
|
|
|
|
|
2024-01-03 16:29:58 +13:00
|
|
|
for instance in 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, community.id, announce)
|
2023-12-09 22:14:16 +13:00
|
|
|
|
2024-03-24 16:38:20 +00:00
|
|
|
# send copy of Note to post author (who won't otherwise get it if no-one else on their instance is subscribed to the community)
|
|
|
|
if not post.author.is_local() and post.author.ap_domain != community.ap_domain:
|
|
|
|
if not community.is_local() or (community.is_local and not community.has_followers_from_domain(post.author.ap_domain)):
|
|
|
|
success = post_request(post.author.ap_inbox_url, create_json, current_user.private_key,
|
2024-06-05 13:21:41 +12:00
|
|
|
current_user.public_url() + '#main-key')
|
2024-08-30 17:25:37 +12:00
|
|
|
if success is False or isinstance(success, str):
|
2024-03-24 16:38:20 +00:00
|
|
|
# sending to shared inbox is good enough for Mastodon, but Lemmy will reject it the local community has no followers
|
|
|
|
personal_inbox = post.author.public_url() + '/inbox'
|
|
|
|
post_request(personal_inbox, create_json, current_user.private_key,
|
2024-06-05 13:21:41 +12:00
|
|
|
current_user.public_url() + '#main-key')
|
2024-03-24 04:13:51 +00:00
|
|
|
|
2024-07-07 13:27:48 +08:00
|
|
|
return redirect(url_for('activitypub.post_ap', post_id=post_id, _anchor=f'comment_{reply.id}'))
|
2023-11-30 06:36:08 +13:00
|
|
|
else:
|
2024-01-07 22:28:13 +13:00
|
|
|
replies = post_replies(post.id, sort)
|
2023-11-30 23:21:37 +13:00
|
|
|
form.notify_author.data = True
|
2023-11-30 06:36:08 +13:00
|
|
|
|
|
|
|
og_image = post.image.source_url if post.image_id else None
|
|
|
|
description = shorten_string(markdown_to_text(post.body), 150) if post.body else None
|
|
|
|
|
2024-06-02 16:45:21 +12:00
|
|
|
# Breadcrumbs
|
2024-03-09 14:59:05 +13:00
|
|
|
breadcrumbs = []
|
|
|
|
breadcrumb = namedtuple("Breadcrumb", ['text', 'url'])
|
|
|
|
breadcrumb.text = _('Home')
|
|
|
|
breadcrumb.url = '/'
|
|
|
|
breadcrumbs.append(breadcrumb)
|
|
|
|
|
|
|
|
if community.topic_id:
|
|
|
|
related_communities = Community.query.filter_by(topic_id=community.topic_id).\
|
|
|
|
filter(Community.id != community.id, Community.banned == False).order_by(Community.name)
|
|
|
|
topics = []
|
|
|
|
previous_topic = Topic.query.get(community.topic_id)
|
|
|
|
topics.append(previous_topic)
|
|
|
|
while previous_topic.parent_id:
|
|
|
|
topic = Topic.query.get(previous_topic.parent_id)
|
|
|
|
topics.append(topic)
|
|
|
|
previous_topic = topic
|
|
|
|
topics = list(reversed(topics))
|
|
|
|
|
|
|
|
breadcrumb = namedtuple("Breadcrumb", ['text', 'url'])
|
|
|
|
breadcrumb.text = _('Topics')
|
|
|
|
breadcrumb.url = '/topics'
|
|
|
|
breadcrumbs.append(breadcrumb)
|
|
|
|
|
|
|
|
existing_url = '/topic'
|
|
|
|
for topic in topics:
|
|
|
|
breadcrumb = namedtuple("Breadcrumb", ['text', 'url'])
|
|
|
|
breadcrumb.text = topic.name
|
|
|
|
breadcrumb.url = f"{existing_url}/{topic.machine_name}"
|
|
|
|
breadcrumbs.append(breadcrumb)
|
|
|
|
existing_url = breadcrumb.url
|
|
|
|
else:
|
|
|
|
related_communities = []
|
|
|
|
breadcrumb = namedtuple("Breadcrumb", ['text', 'url'])
|
|
|
|
breadcrumb.text = _('Communities')
|
|
|
|
breadcrumb.url = '/communities'
|
|
|
|
breadcrumbs.append(breadcrumb)
|
|
|
|
|
2024-04-11 14:04:57 +12:00
|
|
|
# Voting history
|
|
|
|
if current_user.is_authenticated:
|
|
|
|
recently_upvoted = recently_upvoted_posts(current_user.id)
|
|
|
|
recently_downvoted = recently_downvoted_posts(current_user.id)
|
|
|
|
recently_upvoted_replies = recently_upvoted_post_replies(current_user.id)
|
|
|
|
recently_downvoted_replies = recently_downvoted_post_replies(current_user.id)
|
2024-06-28 18:59:43 +08:00
|
|
|
reply_collapse_threshold = current_user.reply_collapse_threshold if current_user.reply_collapse_threshold else -1000
|
2024-04-11 14:04:57 +12:00
|
|
|
else:
|
|
|
|
recently_upvoted = []
|
|
|
|
recently_downvoted = []
|
|
|
|
recently_upvoted_replies = []
|
|
|
|
recently_downvoted_replies = []
|
2024-06-28 18:34:54 +08:00
|
|
|
reply_collapse_threshold = -10
|
2024-04-11 14:04:57 +12:00
|
|
|
|
2024-05-18 19:41:20 +12:00
|
|
|
# Polls
|
|
|
|
poll_form = False
|
|
|
|
poll_results = False
|
|
|
|
poll_choices = []
|
|
|
|
poll_data = None
|
|
|
|
poll_total_votes = 0
|
|
|
|
if post.type == POST_TYPE_POLL:
|
|
|
|
poll_data = Poll.query.get(post.id)
|
2024-06-02 16:45:21 +12:00
|
|
|
if poll_data:
|
|
|
|
poll_choices = PollChoice.query.filter_by(post_id=post.id).order_by(PollChoice.sort_order).all()
|
|
|
|
poll_total_votes = poll_data.total_votes()
|
|
|
|
# Show poll results to everyone after the poll finishes, to the poll creator and to those who have voted
|
|
|
|
if (current_user.is_authenticated and (poll_data.has_voted(current_user.id))) \
|
|
|
|
or poll_data.end_poll < datetime.utcnow():
|
|
|
|
poll_results = True
|
|
|
|
else:
|
|
|
|
poll_form = True
|
2024-05-18 19:41:20 +12:00
|
|
|
|
2024-06-16 21:21:58 +08:00
|
|
|
# Archive.ph link
|
|
|
|
archive_link = None
|
|
|
|
if post.type == POST_TYPE_LINK and body_has_no_archive_link(post.body_html) and url_needs_archive(post.url):
|
|
|
|
archive_link = generate_archive_link(post.url)
|
2024-06-16 17:55:08 +08:00
|
|
|
|
2024-07-17 22:11:31 +08:00
|
|
|
response = render_template('post/post.html', title=post.title, post=post, is_moderator=is_moderator, is_owner=community.is_owner(),
|
|
|
|
community=post.community,
|
2024-03-22 01:23:26 +00:00
|
|
|
breadcrumbs=breadcrumbs, related_communities=related_communities, mods=mod_list,
|
2024-05-18 19:41:20 +12:00
|
|
|
poll_form=poll_form, poll_results=poll_results, poll_data=poll_data, poll_choices=poll_choices, poll_total_votes=poll_total_votes,
|
2023-11-30 06:36:08 +13:00
|
|
|
canonical=post.ap_id, form=form, replies=replies, THREAD_CUTOFF_DEPTH=constants.THREAD_CUTOFF_DEPTH,
|
2024-07-17 22:11:31 +08:00
|
|
|
description=description, og_image=og_image,
|
2024-06-16 21:21:58 +08:00
|
|
|
autoplay=request.args.get('autoplay', False), archive_link=archive_link,
|
2024-04-24 10:29:14 +12:00
|
|
|
noindex=not post.author.indexable, preconnect=post.url if post.url else None,
|
2024-04-11 14:04:57 +12:00
|
|
|
recently_upvoted=recently_upvoted, recently_downvoted=recently_downvoted,
|
|
|
|
recently_upvoted_replies=recently_upvoted_replies, recently_downvoted_replies=recently_downvoted_replies,
|
2024-06-28 18:34:54 +08:00
|
|
|
reply_collapse_threshold=reply_collapse_threshold,
|
2024-02-27 05:24:38 +13:00
|
|
|
etag=f"{post.id}{sort}_{hash(post.last_active)}", markdown_editor=current_user.is_authenticated and current_user.markdown_editor,
|
2024-07-17 22:11:31 +08:00
|
|
|
low_bandwidth=request.cookies.get('low_bandwidth', '0') == '1',
|
2024-01-13 10:45:39 +13:00
|
|
|
moderating_communities=moderating_communities(current_user.get_id()),
|
2024-01-19 15:08:39 +13:00
|
|
|
joined_communities=joined_communities(current_user.get_id()),
|
2024-05-30 21:54:25 +12:00
|
|
|
menu_topics=menu_topics(), site=g.site,
|
2024-07-12 19:56:57 +08:00
|
|
|
inoculation=inoculation[randint(0, len(inoculation) - 1)] if g.site.show_inoculation_block else None
|
2024-01-13 10:45:39 +13:00
|
|
|
)
|
2024-03-17 20:46:33 +13:00
|
|
|
response.headers.set('Vary', 'Accept, Cookie, Accept-Language')
|
2024-02-28 20:45:39 +13:00
|
|
|
return response
|
2023-11-30 06:36:08 +13:00
|
|
|
|
|
|
|
|
|
|
|
@bp.route('/post/<int:post_id>/<vote_direction>', methods=['GET', 'POST'])
|
|
|
|
@login_required
|
|
|
|
@validation_required
|
|
|
|
def post_vote(post_id: int, vote_direction):
|
|
|
|
post = Post.query.get_or_404(post_id)
|
2024-09-13 16:39:42 +12:00
|
|
|
undo = post.vote(current_user, vote_direction)
|
2023-12-17 20:33:27 +13:00
|
|
|
|
2024-04-16 18:40:48 +01:00
|
|
|
if not post.community.local_only:
|
|
|
|
if undo:
|
|
|
|
action_json = {
|
2024-08-20 07:03:08 +12:00
|
|
|
'actor': current_user.public_url(not(post.community.instance.votes_are_public() and current_user.vote_privately())),
|
2024-04-16 18:40:48 +01:00
|
|
|
'type': 'Undo',
|
|
|
|
'id': f"https://{current_app.config['SERVER_NAME']}/activities/undo/{gibberish(15)}",
|
2024-06-05 13:21:41 +12:00
|
|
|
'audience': post.community.public_url(),
|
2024-04-16 18:40:48 +01:00
|
|
|
'object': {
|
2024-08-20 07:03:08 +12:00
|
|
|
'actor': current_user.public_url(not(post.community.instance.votes_are_public() and current_user.vote_privately())),
|
2024-06-05 13:21:41 +12:00
|
|
|
'object': post.public_url(),
|
2024-04-16 18:40:48 +01:00
|
|
|
'type': undo,
|
|
|
|
'id': f"https://{current_app.config['SERVER_NAME']}/activities/{undo.lower()}/{gibberish(15)}",
|
2024-06-05 13:21:41 +12:00
|
|
|
'audience': post.community.public_url()
|
2024-04-16 18:40:48 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else:
|
2024-01-27 12:22:35 +13:00
|
|
|
action_type = 'Like' if vote_direction == 'upvote' else 'Dislike'
|
|
|
|
action_json = {
|
2024-08-20 07:03:08 +12:00
|
|
|
'actor': current_user.public_url(not(post.community.instance.votes_are_public() and current_user.vote_privately())),
|
2024-06-05 16:23:31 +12:00
|
|
|
'object': post.profile_id(),
|
2024-01-27 12:22:35 +13:00
|
|
|
'type': action_type,
|
|
|
|
'id': f"https://{current_app.config['SERVER_NAME']}/activities/{action_type.lower()}/{gibberish(15)}",
|
2024-06-05 13:21:41 +12:00
|
|
|
'audience': post.community.public_url()
|
2023-12-17 20:33:27 +13:00
|
|
|
}
|
2024-04-16 18:40:48 +01:00
|
|
|
if post.community.is_local():
|
|
|
|
announce = {
|
2024-01-27 12:22:35 +13:00
|
|
|
"id": f"https://{current_app.config['SERVER_NAME']}/activities/announce/{gibberish(15)}",
|
|
|
|
"type": 'Announce',
|
|
|
|
"to": [
|
|
|
|
"https://www.w3.org/ns/activitystreams#Public"
|
|
|
|
],
|
2024-06-05 13:21:41 +12:00
|
|
|
"actor": post.community.public_url(),
|
2024-01-27 12:22:35 +13:00
|
|
|
"cc": [
|
|
|
|
post.community.ap_followers_url
|
|
|
|
],
|
|
|
|
'@context': default_context(),
|
|
|
|
'object': action_json
|
2024-04-16 18:40:48 +01:00
|
|
|
}
|
|
|
|
for instance in post.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, post.community.id, announce)
|
|
|
|
else:
|
2024-08-30 17:25:37 +12:00
|
|
|
post_request_in_background(post.community.ap_inbox_url, action_json, current_user.private_key,
|
|
|
|
current_user.public_url(not(post.community.instance.votes_are_public() and current_user.vote_privately())) + '#main-key')
|
2023-12-17 20:33:27 +13:00
|
|
|
|
2024-04-11 14:04:57 +12:00
|
|
|
recently_upvoted = []
|
|
|
|
recently_downvoted = []
|
2024-04-16 18:40:48 +01:00
|
|
|
if vote_direction == 'upvote' and undo is None:
|
2024-04-11 14:04:57 +12:00
|
|
|
recently_upvoted = [post_id]
|
2024-04-16 18:40:48 +01:00
|
|
|
elif vote_direction == 'downvote' and undo is None:
|
2024-04-11 14:04:57 +12:00
|
|
|
recently_downvoted = [post_id]
|
|
|
|
cache.delete_memoized(recently_upvoted_posts, current_user.id)
|
|
|
|
cache.delete_memoized(recently_downvoted_posts, current_user.id)
|
|
|
|
|
2024-01-31 20:16:46 +13:00
|
|
|
template = 'post/_post_voting_buttons.html' if request.args.get('style', '') == '' else 'post/_post_voting_buttons_masonry.html'
|
2024-04-11 14:04:57 +12:00
|
|
|
return render_template(template, post=post, community=post.community, recently_upvoted=recently_upvoted,
|
|
|
|
recently_downvoted=recently_downvoted)
|
2023-11-30 06:36:08 +13:00
|
|
|
|
|
|
|
|
|
|
|
@bp.route('/comment/<int:comment_id>/<vote_direction>', methods=['POST'])
|
|
|
|
@login_required
|
|
|
|
@validation_required
|
|
|
|
def comment_vote(comment_id, vote_direction):
|
|
|
|
comment = PostReply.query.get_or_404(comment_id)
|
2024-09-13 16:39:42 +12:00
|
|
|
undo = comment.vote(current_user, vote_direction)
|
2023-12-17 20:33:27 +13:00
|
|
|
|
2024-04-17 03:21:00 +01:00
|
|
|
if not comment.community.local_only:
|
|
|
|
if undo:
|
|
|
|
action_json = {
|
2024-08-20 01:33:33 +00:00
|
|
|
'actor': current_user.public_url(not(comment.community.instance.votes_are_public() and current_user.vote_privately())),
|
2024-04-17 03:21:00 +01:00
|
|
|
'type': 'Undo',
|
|
|
|
'id': f"https://{current_app.config['SERVER_NAME']}/activities/undo/{gibberish(15)}",
|
2024-06-05 13:21:41 +12:00
|
|
|
'audience': comment.community.public_url(),
|
2024-04-17 03:21:00 +01:00
|
|
|
'object': {
|
2024-08-20 07:03:08 +12:00
|
|
|
'actor': current_user.public_url(not(comment.community.instance.votes_are_public() and current_user.vote_privately())),
|
2024-06-05 13:21:41 +12:00
|
|
|
'object': comment.public_url(),
|
2024-04-17 03:21:00 +01:00
|
|
|
'type': undo,
|
|
|
|
'id': f"https://{current_app.config['SERVER_NAME']}/activities/{undo.lower()}/{gibberish(15)}",
|
2024-06-05 13:21:41 +12:00
|
|
|
'audience': comment.community.public_url()
|
2024-01-27 12:22:35 +13:00
|
|
|
}
|
2024-04-17 03:21:00 +01:00
|
|
|
}
|
|
|
|
else:
|
|
|
|
action_type = 'Like' if vote_direction == 'upvote' else 'Dislike'
|
|
|
|
action_json = {
|
2024-08-20 07:03:08 +12:00
|
|
|
'actor': current_user.public_url(not(comment.community.instance.votes_are_public() and current_user.vote_privately())),
|
2024-06-05 13:21:41 +12:00
|
|
|
'object': comment.public_url(),
|
2024-04-17 03:21:00 +01:00
|
|
|
'type': action_type,
|
|
|
|
'id': f"https://{current_app.config['SERVER_NAME']}/activities/{action_type.lower()}/{gibberish(15)}",
|
2024-06-05 13:21:41 +12:00
|
|
|
'audience': comment.community.public_url()
|
2024-04-17 03:21:00 +01:00
|
|
|
}
|
|
|
|
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:
|
2024-08-20 07:03:08 +12:00
|
|
|
post_request_in_background(comment.community.ap_inbox_url, action_json, current_user.private_key,
|
|
|
|
current_user.public_url(not(comment.community.instance.votes_are_public() and current_user.vote_privately())) + '#main-key')
|
2023-12-22 15:34:45 +13:00
|
|
|
|
2024-04-11 14:04:57 +12:00
|
|
|
recently_upvoted = []
|
|
|
|
recently_downvoted = []
|
2024-04-17 03:21:00 +01:00
|
|
|
if vote_direction == 'upvote' and undo is None:
|
2024-04-11 14:04:57 +12:00
|
|
|
recently_upvoted = [comment_id]
|
2024-04-17 03:21:00 +01:00
|
|
|
elif vote_direction == 'downvote' and undo is None:
|
2024-04-11 14:04:57 +12:00
|
|
|
recently_downvoted = [comment_id]
|
|
|
|
cache.delete_memoized(recently_upvoted_post_replies, current_user.id)
|
|
|
|
cache.delete_memoized(recently_downvoted_post_replies, current_user.id)
|
|
|
|
|
2024-01-16 21:39:10 +13:00
|
|
|
return render_template('post/_comment_voting_buttons.html', comment=comment,
|
2024-04-11 14:04:57 +12:00
|
|
|
recently_upvoted_replies=recently_upvoted,
|
|
|
|
recently_downvoted_replies=recently_downvoted,
|
|
|
|
community=comment.community)
|
2023-11-30 06:36:08 +13:00
|
|
|
|
|
|
|
|
2024-05-18 19:41:20 +12:00
|
|
|
@bp.route('/poll/<int:post_id>/vote', methods=['POST'])
|
|
|
|
@login_required
|
|
|
|
@validation_required
|
|
|
|
def poll_vote(post_id):
|
|
|
|
poll_data = Poll.query.get_or_404(post_id)
|
|
|
|
if poll_data.mode == 'single':
|
|
|
|
choice_id = int(request.form.get('poll_choice'))
|
|
|
|
poll_data.vote_for_choice(choice_id, current_user.id)
|
|
|
|
else:
|
|
|
|
for choice_id in request.form.getlist('poll_choice[]'):
|
|
|
|
poll_data.vote_for_choice(int(choice_id), current_user.id)
|
|
|
|
flash(_('Vote has been cast.'))
|
2024-05-31 22:06:52 +01:00
|
|
|
|
|
|
|
post = Post.query.get(post_id)
|
|
|
|
if post:
|
|
|
|
poll_votes = PollChoice.query.join(PollChoiceVote, PollChoiceVote.choice_id == PollChoice.id).filter(PollChoiceVote.post_id == post.id, PollChoiceVote.user_id == current_user.id).all()
|
|
|
|
for pv in poll_votes:
|
|
|
|
if post.author.is_local():
|
2024-06-01 19:52:17 +12:00
|
|
|
inform_followers_of_post_update(post.id, 1)
|
2024-05-31 22:06:52 +01:00
|
|
|
else:
|
|
|
|
pollvote_json = {
|
|
|
|
'@context': default_context(),
|
|
|
|
'actor': current_user.public_url(),
|
|
|
|
'id': f"https://{current_app.config['SERVER_NAME']}/activities/create/{gibberish(15)}",
|
|
|
|
'object': {
|
|
|
|
'attributedTo': current_user.public_url(),
|
|
|
|
'id': f"https://{current_app.config['SERVER_NAME']}/activities/vote/{gibberish(15)}",
|
2024-06-05 16:23:31 +12:00
|
|
|
'inReplyTo': post.profile_id(),
|
2024-05-31 22:06:52 +01:00
|
|
|
'name': pv.choice_text,
|
|
|
|
'to': post.author.public_url(),
|
|
|
|
'type': 'Note'
|
|
|
|
},
|
|
|
|
'to': post.author.public_url(),
|
|
|
|
'type': 'Create'
|
|
|
|
}
|
|
|
|
try:
|
|
|
|
post_request(post.author.ap_inbox_url, pollvote_json, current_user.private_key,
|
2024-06-05 13:21:41 +12:00
|
|
|
current_user.public_url() + '#main-key')
|
2024-05-31 22:06:52 +01:00
|
|
|
except Exception:
|
|
|
|
pass
|
|
|
|
|
2024-05-18 19:41:20 +12:00
|
|
|
return redirect(url_for('activitypub.post_ap', post_id=post_id))
|
|
|
|
|
|
|
|
|
2023-11-30 06:36:08 +13:00
|
|
|
@bp.route('/post/<int:post_id>/comment/<int:comment_id>')
|
|
|
|
def continue_discussion(post_id, comment_id):
|
|
|
|
post = Post.query.get_or_404(post_id)
|
|
|
|
comment = PostReply.query.get_or_404(comment_id)
|
2024-06-02 16:45:21 +12:00
|
|
|
if post.community.banned or post.deleted or comment.deleted:
|
2024-01-01 11:38:24 +13:00
|
|
|
abort(404)
|
2023-11-30 06:36:08 +13:00
|
|
|
mods = post.community.moderators()
|
|
|
|
is_moderator = current_user.is_authenticated and any(mod.user_id == current_user.id for mod in mods)
|
2024-03-22 01:23:26 +00:00
|
|
|
if post.community.private_mods:
|
|
|
|
mod_list = []
|
|
|
|
else:
|
|
|
|
mod_user_ids = [mod.user_id for mod in mods]
|
|
|
|
mod_list = User.query.filter(User.id.in_(mod_user_ids)).all()
|
2023-11-30 06:36:08 +13:00
|
|
|
replies = get_comment_branch(post.id, comment.id, 'top')
|
|
|
|
|
2024-03-22 01:23:26 +00:00
|
|
|
response = render_template('post/continue_discussion.html', title=_('Discussing %(title)s', title=post.title), post=post, mods=mod_list,
|
2024-02-27 05:24:38 +13:00
|
|
|
is_moderator=is_moderator, comment=comment, replies=replies, markdown_editor=current_user.is_authenticated and current_user.markdown_editor,
|
2024-02-26 21:26:19 +13:00
|
|
|
moderating_communities=moderating_communities(current_user.get_id()),
|
2024-05-30 21:54:25 +12:00
|
|
|
joined_communities=joined_communities(current_user.get_id()),
|
|
|
|
menu_topics=menu_topics(), site=g.site,
|
|
|
|
community=post.community,
|
2024-04-15 12:54:27 +01:00
|
|
|
SUBSCRIPTION_OWNER=SUBSCRIPTION_OWNER, SUBSCRIPTION_MODERATOR=SUBSCRIPTION_MODERATOR,
|
2024-07-12 19:56:57 +08:00
|
|
|
inoculation=inoculation[randint(0, len(inoculation) - 1)] if g.site.show_inoculation_block else None)
|
2024-03-17 20:46:33 +13:00
|
|
|
response.headers.set('Vary', 'Accept, Cookie, Accept-Language')
|
2024-02-28 21:34:19 +13:00
|
|
|
return response
|
2023-11-30 06:36:08 +13:00
|
|
|
|
|
|
|
|
|
|
|
@bp.route('/post/<int:post_id>/comment/<int:comment_id>/reply', methods=['GET', 'POST'])
|
|
|
|
@login_required
|
|
|
|
def add_reply(post_id: int, comment_id: int):
|
2023-12-30 19:22:22 +13:00
|
|
|
if current_user.banned:
|
2024-04-03 20:48:39 +13:00
|
|
|
return show_ban_message()
|
2023-11-30 06:36:08 +13:00
|
|
|
post = Post.query.get_or_404(post_id)
|
2023-12-14 21:22:46 +13:00
|
|
|
|
|
|
|
if not post.comments_enabled:
|
2024-02-24 11:07:06 +13:00
|
|
|
flash('Comments have been disabled.', 'warning')
|
2023-12-14 21:22:46 +13:00
|
|
|
return redirect(url_for('activitypub.post_ap', post_id=post_id))
|
|
|
|
|
2023-12-09 22:14:16 +13:00
|
|
|
in_reply_to = PostReply.query.get_or_404(comment_id)
|
2023-11-30 06:36:08 +13:00
|
|
|
mods = post.community.moderators()
|
|
|
|
is_moderator = current_user.is_authenticated and any(mod.user_id == current_user.id for mod in mods)
|
2024-03-22 01:23:26 +00:00
|
|
|
if post.community.private_mods:
|
|
|
|
mod_list = []
|
|
|
|
else:
|
|
|
|
mod_user_ids = [mod.user_id for mod in mods]
|
|
|
|
mod_list = User.query.filter(User.id.in_(mod_user_ids)).all()
|
2024-01-06 11:01:44 +13:00
|
|
|
|
|
|
|
if in_reply_to.author.has_blocked_user(current_user.id):
|
|
|
|
flash(_('You cannot reply to %(name)s', name=in_reply_to.author.display_name()))
|
|
|
|
return redirect(url_for('activitypub.post_ap', post_id=post_id))
|
|
|
|
|
2023-11-30 06:36:08 +13:00
|
|
|
form = NewReplyForm()
|
2024-05-09 17:54:30 +12:00
|
|
|
form.language_id.choices = languages_for_form()
|
2023-11-30 06:36:08 +13:00
|
|
|
if form.validate_on_submit():
|
2024-01-06 14:54:10 +13:00
|
|
|
if reply_already_exists(user_id=current_user.id, post_id=post.id, parent_id=in_reply_to.id, body=form.body.data):
|
|
|
|
if in_reply_to.depth <= constants.THREAD_CUTOFF_DEPTH:
|
|
|
|
return redirect(url_for('activitypub.post_ap', post_id=post_id, _anchor=f'comment_{in_reply_to.id}'))
|
|
|
|
else:
|
|
|
|
return redirect(url_for('post.continue_discussion', post_id=post_id, comment_id=in_reply_to.parent_id))
|
|
|
|
|
|
|
|
if reply_is_just_link_to_gif_reaction(form.body.data):
|
|
|
|
current_user.reputation -= 1
|
|
|
|
flash(_('This type of comment is not accepted, sorry.'), 'error')
|
|
|
|
if in_reply_to.depth <= constants.THREAD_CUTOFF_DEPTH:
|
|
|
|
return redirect(url_for('activitypub.post_ap', post_id=post_id, _anchor=f'comment_{in_reply_to.id}'))
|
|
|
|
else:
|
|
|
|
return redirect(url_for('post.continue_discussion', post_id=post_id, comment_id=in_reply_to.parent_id))
|
|
|
|
|
2024-04-22 15:25:37 +12:00
|
|
|
if reply_is_stupid(form.body.data):
|
|
|
|
existing_vote = PostReplyVote.query.filter_by(user_id=current_user.id, post_reply_id=in_reply_to.id).first()
|
|
|
|
if existing_vote is None:
|
|
|
|
flash(_('We have upvoted the comment for you.'), 'warning')
|
|
|
|
comment_vote(in_reply_to.id, 'upvote')
|
|
|
|
else:
|
|
|
|
flash(_('You have already upvoted the comment, you do not need to say "this" also.'), 'error')
|
|
|
|
if in_reply_to.depth <= constants.THREAD_CUTOFF_DEPTH:
|
|
|
|
return redirect(url_for('activitypub.post_ap', post_id=post_id))
|
|
|
|
else:
|
|
|
|
return redirect(url_for('post.continue_discussion', post_id=post_id, comment_id=in_reply_to.parent_id))
|
|
|
|
|
2023-12-12 08:53:35 +13:00
|
|
|
current_user.last_seen = utcnow()
|
2023-12-30 19:03:44 +13:00
|
|
|
current_user.ip_address = ip_address()
|
2024-05-09 17:54:30 +12:00
|
|
|
current_user.language_id = form.language_id.data
|
2023-12-09 22:14:16 +13:00
|
|
|
reply = PostReply(user_id=current_user.id, post_id=post.id, parent_id=in_reply_to.id, depth=in_reply_to.depth + 1,
|
2024-09-22 13:42:02 +00:00
|
|
|
community_id=post.community.id, body=piefed_markdown_to_lemmy_markdown(form.body.data),
|
2023-11-30 06:36:08 +13:00
|
|
|
body_html=markdown_to_html(form.body.data), body_html_safe=True,
|
2024-03-05 02:17:01 +00:00
|
|
|
from_bot=current_user.bot, nsfw=post.nsfw, nsfl=post.nsfl,
|
2024-05-09 17:54:30 +12:00
|
|
|
notify_author=form.notify_author.data, instance_id=1, language_id=form.language_id.data)
|
2024-03-22 12:22:19 +13:00
|
|
|
if reply.body:
|
|
|
|
for blocked_phrase in blocked_phrases():
|
|
|
|
if blocked_phrase in reply.body:
|
|
|
|
abort(401)
|
2023-11-30 06:36:08 +13:00
|
|
|
db.session.add(reply)
|
|
|
|
db.session.commit()
|
2024-04-22 16:14:32 +01:00
|
|
|
|
2024-04-29 21:43:37 +12:00
|
|
|
# Notify subscribers
|
|
|
|
notify_about_post_reply(in_reply_to, reply)
|
|
|
|
|
|
|
|
# Subscribe to own comment
|
|
|
|
if form.notify_author.data:
|
|
|
|
new_notification = NotificationSubscription(name=shorten_string(_('Replies to my comment on %(post_title)s',
|
|
|
|
post_title=post.title), 50),
|
|
|
|
user_id=current_user.id, entity_id=reply.id,
|
|
|
|
type=NOTIF_REPLY)
|
|
|
|
db.session.add(new_notification)
|
|
|
|
|
2024-04-22 16:14:32 +01:00
|
|
|
# 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)
|
|
|
|
|
2023-12-09 22:14:16 +13:00
|
|
|
reply.ap_id = reply.profile_id()
|
2024-01-02 16:07:41 +13:00
|
|
|
if current_user.reputation > 100:
|
|
|
|
reply.up_votes += 1
|
|
|
|
reply.score += 1
|
|
|
|
reply.ranking += 1
|
|
|
|
elif current_user.reputation < -100:
|
|
|
|
reply.score -= 1
|
|
|
|
reply.ranking -= 1
|
2024-09-13 11:08:04 +12:00
|
|
|
post.reply_count = get_post_reply_count(post.id)
|
2023-12-12 08:53:35 +13:00
|
|
|
post.last_active = post.community.last_active = utcnow()
|
2024-09-13 11:08:04 +12:00
|
|
|
current_user.post_reply_count += 1
|
2023-11-30 06:36:08 +13:00
|
|
|
db.session.commit()
|
|
|
|
form.body.data = ''
|
|
|
|
flash('Your comment has been added.')
|
2023-12-10 15:10:09 +13:00
|
|
|
|
2023-12-09 22:14:16 +13:00
|
|
|
# federation
|
2024-01-27 12:22:35 +13:00
|
|
|
if not post.community.local_only:
|
|
|
|
reply_json = {
|
|
|
|
'type': 'Note',
|
2024-06-05 13:21:41 +12:00
|
|
|
'id': reply.public_url(),
|
2024-03-26 23:17:19 +00:00
|
|
|
'attributedTo': current_user.public_url(),
|
2024-01-27 12:22:35 +13:00
|
|
|
'to': [
|
2024-03-26 23:17:19 +00:00
|
|
|
'https://www.w3.org/ns/activitystreams#Public'
|
2023-12-26 21:39:52 +13:00
|
|
|
],
|
2024-01-27 12:22:35 +13:00
|
|
|
'cc': [
|
2024-03-26 23:17:19 +00:00
|
|
|
post.community.public_url(),
|
|
|
|
in_reply_to.author.public_url()
|
2023-12-26 21:39:52 +13:00
|
|
|
],
|
2024-01-27 12:22:35 +13:00
|
|
|
'content': reply.body_html,
|
2024-06-05 16:23:31 +12:00
|
|
|
'inReplyTo': in_reply_to.profile_id(),
|
|
|
|
'url': reply.profile_id(),
|
2024-01-27 12:22:35 +13:00
|
|
|
'mediaType': 'text/html',
|
2024-09-22 13:42:02 +00:00
|
|
|
'source': {'content': reply.body, 'mediaType': 'text/markdown'},
|
2024-01-27 12:22:35 +13:00
|
|
|
'published': ap_datetime(utcnow()),
|
|
|
|
'distinguished': False,
|
2024-03-26 23:17:19 +00:00
|
|
|
'audience': post.community.public_url(),
|
2024-01-27 12:22:35 +13:00
|
|
|
'contentMap': {
|
|
|
|
'en': reply.body_html
|
|
|
|
}
|
|
|
|
}
|
|
|
|
create_json = {
|
2023-12-26 21:39:52 +13:00
|
|
|
'@context': default_context(),
|
2024-01-27 12:22:35 +13:00
|
|
|
'type': 'Create',
|
2024-03-26 23:17:19 +00:00
|
|
|
'actor': current_user.public_url(),
|
|
|
|
'audience': post.community.public_url(),
|
2024-01-27 12:22:35 +13:00
|
|
|
'to': [
|
2024-03-26 23:17:19 +00:00
|
|
|
'https://www.w3.org/ns/activitystreams#Public'
|
2024-01-27 12:22:35 +13:00
|
|
|
],
|
|
|
|
'cc': [
|
2024-03-26 23:17:19 +00:00
|
|
|
post.community.public_url(),
|
|
|
|
in_reply_to.author.public_url()
|
2024-01-27 12:22:35 +13:00
|
|
|
],
|
|
|
|
'object': reply_json,
|
|
|
|
'id': f"https://{current_app.config['SERVER_NAME']}/activities/create/{gibberish(15)}"
|
2023-12-26 21:39:52 +13:00
|
|
|
}
|
2024-01-27 12:22:35 +13:00
|
|
|
if in_reply_to.notify_author and in_reply_to.author.ap_id is not None:
|
|
|
|
reply_json['tag'] = [
|
|
|
|
{
|
2024-03-26 23:17:19 +00:00
|
|
|
'href': in_reply_to.author.public_url(),
|
|
|
|
'name': in_reply_to.author.mention_tag(),
|
|
|
|
'type': 'Mention'
|
|
|
|
}
|
|
|
|
]
|
|
|
|
create_json['tag'] = [
|
|
|
|
{
|
|
|
|
'href': in_reply_to.author.public_url(),
|
|
|
|
'name': in_reply_to.author.mention_tag(),
|
2024-01-27 12:22:35 +13:00
|
|
|
'type': 'Mention'
|
|
|
|
}
|
|
|
|
]
|
|
|
|
if not post.community.is_local(): # this is a remote community, send it to the instance that hosts it
|
|
|
|
success = post_request(post.community.ap_inbox_url, create_json, current_user.private_key,
|
2024-06-05 13:21:41 +12:00
|
|
|
current_user.public_url() + '#main-key')
|
2024-08-30 17:25:37 +12:00
|
|
|
if success is False or isinstance(success, str):
|
2024-01-27 12:22:35 +13:00
|
|
|
flash('Failed to send reply', 'error')
|
|
|
|
else: # local community - send it to followers on remote instances
|
|
|
|
announce = {
|
|
|
|
"id": f"https://{current_app.config['SERVER_NAME']}/activities/announce/{gibberish(15)}",
|
|
|
|
"type": 'Announce',
|
|
|
|
"to": [
|
|
|
|
"https://www.w3.org/ns/activitystreams#Public"
|
|
|
|
],
|
2024-03-26 23:17:19 +00:00
|
|
|
"actor": post.community.public_url(),
|
2024-01-27 12:22:35 +13:00
|
|
|
"cc": [
|
|
|
|
post.community.ap_followers_url
|
|
|
|
],
|
|
|
|
'@context': default_context(),
|
|
|
|
'object': create_json
|
|
|
|
}
|
2023-12-26 21:39:52 +13:00
|
|
|
|
2024-01-27 12:22:35 +13:00
|
|
|
for instance in post.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, post.community.id, announce)
|
2023-12-26 21:39:52 +13:00
|
|
|
|
2024-03-26 23:17:19 +00:00
|
|
|
# send copy of Note to comment author (who won't otherwise get it if no-one else on their instance is subscribed to the community)
|
|
|
|
if not in_reply_to.author.is_local() and in_reply_to.author.ap_domain != reply.community.ap_domain:
|
|
|
|
if not post.community.is_local() or (post.community.is_local and not post.community.has_followers_from_domain(in_reply_to.author.ap_domain)):
|
|
|
|
success = post_request(in_reply_to.author.ap_inbox_url, create_json, current_user.private_key,
|
2024-06-05 13:21:41 +12:00
|
|
|
current_user.public_url() + '#main-key')
|
2024-08-30 17:25:37 +12:00
|
|
|
if success is False or isinstance(success, str):
|
2024-03-26 23:17:19 +00:00
|
|
|
# sending to shared inbox is good enough for Mastodon, but Lemmy will reject it the local community has no followers
|
|
|
|
personal_inbox = in_reply_to.author.public_url() + '/inbox'
|
|
|
|
post_request(personal_inbox, create_json, current_user.private_key,
|
2024-06-05 13:21:41 +12:00
|
|
|
current_user.public_url() + '#main-key')
|
2024-03-26 23:17:19 +00:00
|
|
|
|
2023-11-30 06:36:08 +13:00
|
|
|
if reply.depth <= constants.THREAD_CUTOFF_DEPTH:
|
2024-01-06 14:54:10 +13:00
|
|
|
return redirect(url_for('activitypub.post_ap', post_id=post_id, _anchor=f'comment_{reply.id}'))
|
2023-11-30 06:36:08 +13:00
|
|
|
else:
|
|
|
|
return redirect(url_for('post.continue_discussion', post_id=post_id, comment_id=reply.parent_id))
|
|
|
|
else:
|
2023-11-30 23:21:37 +13:00
|
|
|
form.notify_author.data = True
|
2024-04-22 16:14:32 +01:00
|
|
|
|
2023-11-30 06:36:08 +13:00
|
|
|
return render_template('post/add_reply.html', title=_('Discussing %(title)s', title=post.title), post=post,
|
2024-02-27 05:24:38 +13:00
|
|
|
is_moderator=is_moderator, form=form, comment=in_reply_to, markdown_editor=current_user.is_authenticated and current_user.markdown_editor,
|
2024-03-22 01:23:26 +00:00
|
|
|
moderating_communities=moderating_communities(current_user.get_id()), mods=mod_list,
|
2024-04-15 12:54:27 +01:00
|
|
|
joined_communities = joined_communities(current_user.id), community=post.community,
|
|
|
|
SUBSCRIPTION_OWNER=SUBSCRIPTION_OWNER, SUBSCRIPTION_MODERATOR=SUBSCRIPTION_MODERATOR,
|
2024-07-12 19:56:57 +08:00
|
|
|
inoculation=inoculation[randint(0, len(inoculation) - 1)] if g.site.show_inoculation_block else None)
|
2023-11-30 06:36:08 +13:00
|
|
|
|
2023-11-30 07:12:17 +13:00
|
|
|
|
2023-11-30 20:57:51 +13:00
|
|
|
@bp.route('/post/<int:post_id>/options', methods=['GET'])
|
2023-11-30 06:36:08 +13:00
|
|
|
def post_options(post_id: int):
|
|
|
|
post = Post.query.get_or_404(post_id)
|
2024-06-02 16:45:21 +12:00
|
|
|
if current_user.is_anonymous or not current_user.is_admin():
|
|
|
|
if post.deleted:
|
|
|
|
abort(404)
|
2024-06-21 17:28:49 +08:00
|
|
|
|
|
|
|
existing_bookmark = []
|
|
|
|
if current_user.is_authenticated:
|
|
|
|
existing_bookmark = PostBookmark.query.filter(PostBookmark.post_id == post_id, PostBookmark.user_id == current_user.id).first()
|
2024-06-02 16:45:21 +12:00
|
|
|
|
2024-06-21 17:28:49 +08:00
|
|
|
return render_template('post/post_options.html', post=post, existing_bookmark=existing_bookmark,
|
2024-03-15 14:24:45 +13:00
|
|
|
moderating_communities=moderating_communities(current_user.get_id()),
|
2024-05-30 21:54:25 +12:00
|
|
|
joined_communities=joined_communities(current_user.get_id()),
|
|
|
|
menu_topics=menu_topics(), site=g.site)
|
2023-11-30 06:36:08 +13:00
|
|
|
|
2023-12-26 21:39:52 +13:00
|
|
|
|
2023-12-28 20:00:07 +13:00
|
|
|
@bp.route('/post/<int:post_id>/comment/<int:comment_id>/options', methods=['GET'])
|
|
|
|
def post_reply_options(post_id: int, comment_id: int):
|
|
|
|
post = Post.query.get_or_404(post_id)
|
|
|
|
post_reply = PostReply.query.get_or_404(comment_id)
|
2024-06-02 16:45:21 +12:00
|
|
|
if current_user.is_anonymous or not current_user.is_admin():
|
|
|
|
if post.deleted or post_reply.deleted:
|
|
|
|
abort(404)
|
2024-06-21 17:28:49 +08:00
|
|
|
|
|
|
|
existing_bookmark = []
|
|
|
|
if current_user.is_authenticated:
|
|
|
|
existing_bookmark = PostReplyBookmark.query.filter(PostReplyBookmark.post_reply_id == comment_id,
|
|
|
|
PostReplyBookmark.user_id == current_user.id).first()
|
|
|
|
|
2024-01-12 12:34:08 +13:00
|
|
|
return render_template('post/post_reply_options.html', post=post, post_reply=post_reply,
|
2024-06-21 17:28:49 +08:00
|
|
|
existing_bookmark=existing_bookmark,
|
2024-01-12 12:34:08 +13:00
|
|
|
moderating_communities=moderating_communities(current_user.get_id()),
|
2024-05-30 21:54:25 +12:00
|
|
|
joined_communities=joined_communities(current_user.get_id()),
|
|
|
|
menu_topics=menu_topics(), site=g.site
|
2024-01-12 12:34:08 +13:00
|
|
|
)
|
2023-12-28 20:00:07 +13:00
|
|
|
|
|
|
|
|
2024-07-08 15:19:49 +02:00
|
|
|
@bp.route('/post/<int:post_id>/edit', methods=['GET', 'POST'])
|
2023-12-26 21:39:52 +13:00
|
|
|
@login_required
|
2023-11-30 20:57:51 +13:00
|
|
|
def post_edit(post_id: int):
|
|
|
|
post = Post.query.get_or_404(post_id)
|
2024-04-07 11:15:04 +12:00
|
|
|
if post.type == POST_TYPE_ARTICLE:
|
2024-07-08 15:19:49 +02:00
|
|
|
form = CreateDiscussionForm()
|
2024-04-07 11:15:04 +12:00
|
|
|
elif post.type == POST_TYPE_LINK:
|
2024-07-08 15:19:49 +02:00
|
|
|
form = CreateLinkForm()
|
2024-04-07 11:15:04 +12:00
|
|
|
elif post.type == POST_TYPE_IMAGE:
|
2024-08-16 16:35:16 -04:00
|
|
|
form = EditImageForm()
|
2024-04-16 20:59:58 +12:00
|
|
|
elif post.type == POST_TYPE_VIDEO:
|
2024-07-08 15:19:49 +02:00
|
|
|
form = CreateVideoForm()
|
2024-05-18 20:17:05 +12:00
|
|
|
elif post.type == POST_TYPE_POLL:
|
2024-07-08 15:19:49 +02:00
|
|
|
form = CreatePollForm()
|
|
|
|
poll = Poll.query.filter_by(post_id=post_id).first()
|
|
|
|
del form.finish_in
|
2024-04-07 11:15:04 +12:00
|
|
|
else:
|
|
|
|
abort(404)
|
2024-07-08 15:19:49 +02:00
|
|
|
|
2024-03-12 21:34:10 +13:00
|
|
|
del form.communities
|
2024-03-22 01:23:26 +00:00
|
|
|
|
|
|
|
mods = post.community.moderators()
|
|
|
|
if post.community.private_mods:
|
|
|
|
mod_list = []
|
|
|
|
else:
|
|
|
|
mod_user_ids = [mod.user_id for mod in mods]
|
|
|
|
mod_list = User.query.filter(User.id.in_(mod_user_ids)).all()
|
|
|
|
|
2024-03-14 22:22:24 +13:00
|
|
|
if post.user_id == current_user.id or post.community.is_moderator() or current_user.is_admin():
|
2024-02-16 06:24:28 +13:00
|
|
|
if g.site.enable_nsfl is False:
|
2023-11-30 20:57:51 +13:00
|
|
|
form.nsfl.render_kw = {'disabled': True}
|
2024-02-16 06:24:28 +13:00
|
|
|
if post.community.nsfw:
|
|
|
|
form.nsfw.data = True
|
|
|
|
form.nsfw.render_kw = {'disabled': True}
|
|
|
|
if post.community.nsfl:
|
|
|
|
form.nsfl.data = True
|
|
|
|
form.nsfw.render_kw = {'disabled': True}
|
2023-11-30 20:57:51 +13:00
|
|
|
|
2024-04-02 18:01:24 +01:00
|
|
|
old_url = post.url
|
2023-11-30 20:57:51 +13:00
|
|
|
|
2024-05-09 17:54:30 +12:00
|
|
|
form.language_id.choices = languages_for_form()
|
|
|
|
|
2023-11-30 20:57:51 +13:00
|
|
|
if form.validate_on_submit():
|
2024-07-08 15:19:49 +02:00
|
|
|
save_post(form, post, post.type)
|
2023-12-12 08:53:35 +13:00
|
|
|
post.community.last_active = utcnow()
|
|
|
|
post.edited_at = utcnow()
|
2024-04-02 18:01:24 +01:00
|
|
|
|
|
|
|
if post.url != old_url:
|
|
|
|
if post.cross_posts is not None:
|
|
|
|
old_cross_posts = Post.query.filter(Post.id.in_(post.cross_posts)).all()
|
|
|
|
post.cross_posts.clear()
|
|
|
|
for ocp in old_cross_posts:
|
|
|
|
if ocp.cross_posts is not None:
|
|
|
|
ocp.cross_posts.remove(post.id)
|
|
|
|
|
2024-09-27 10:07:41 +00:00
|
|
|
new_cross_posts = Post.query.filter(Post.id != post.id, Post.url == post.url, Post.deleted == False,
|
2024-04-02 18:01:24 +01:00
|
|
|
Post.posted_at > post.edited_at - timedelta(days=6)).all()
|
|
|
|
for ncp in new_cross_posts:
|
|
|
|
if ncp.cross_posts is None:
|
|
|
|
ncp.cross_posts = [post.id]
|
|
|
|
else:
|
|
|
|
ncp.cross_posts.append(post.id)
|
|
|
|
if post.cross_posts is None:
|
|
|
|
post.cross_posts = [ncp.id]
|
|
|
|
else:
|
|
|
|
post.cross_posts.append(ncp.id)
|
|
|
|
|
2024-04-07 11:15:04 +12:00
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
flash(_('Your changes have been saved.'), 'success')
|
2024-04-16 20:59:58 +12:00
|
|
|
|
|
|
|
# federate edit
|
|
|
|
if not post.community.local_only:
|
|
|
|
federate_post_update(post)
|
2024-05-10 16:36:15 +01:00
|
|
|
federate_post_edit_to_user_followers(post)
|
2024-04-16 20:59:58 +12:00
|
|
|
|
|
|
|
return redirect(url_for('activitypub.post_ap', post_id=post.id))
|
|
|
|
else:
|
2024-07-08 13:21:02 +02:00
|
|
|
form.title.data = post.title
|
|
|
|
form.body.data = post.body
|
2024-04-16 20:59:58 +12:00
|
|
|
form.notify_author.data = post.notify_author
|
|
|
|
form.nsfw.data = post.nsfw
|
|
|
|
form.nsfl.data = post.nsfl
|
|
|
|
form.sticky.data = post.sticky
|
2024-05-09 17:54:30 +12:00
|
|
|
form.language_id.data = post.language_id
|
2024-05-12 13:02:45 +12:00
|
|
|
form.tags.data = tags_to_string(post)
|
2024-07-08 15:19:49 +02:00
|
|
|
if post.type == POST_TYPE_LINK:
|
|
|
|
form.link_url.data = post.url
|
|
|
|
elif post.type == POST_TYPE_IMAGE:
|
2024-08-16 16:35:16 -04:00
|
|
|
# existing_image = True
|
2024-07-08 15:19:49 +02:00
|
|
|
form.image_alt_text.data = post.image.alt_text
|
|
|
|
elif post.type == POST_TYPE_VIDEO:
|
|
|
|
form.video_url.data = post.url
|
|
|
|
elif post.type == POST_TYPE_POLL:
|
|
|
|
poll = Poll.query.filter_by(post_id=post.id).first()
|
|
|
|
form.mode.data = poll.mode
|
|
|
|
form.local_only.data = poll.local_only
|
|
|
|
i = 1
|
|
|
|
for choice in PollChoice.query.filter_by(post_id=post.id).order_by(PollChoice.sort_order).all():
|
|
|
|
form_field = getattr(form, f"choice_{i}")
|
|
|
|
form_field.data = choice.choice_text
|
|
|
|
i += 1
|
2024-04-16 20:59:58 +12:00
|
|
|
|
2024-05-18 20:17:05 +12:00
|
|
|
if not (post.community.is_moderator() or post.community.is_owner() or current_user.is_admin()):
|
|
|
|
form.sticky.render_kw = {'disabled': True}
|
2024-07-08 17:25:49 +02:00
|
|
|
return render_template('post/post_edit.html', title=_('Edit post'), form=form,
|
2024-08-16 16:35:16 -04:00
|
|
|
post_type=post.type, community=post.community, post=post,
|
2024-05-18 20:17:05 +12:00
|
|
|
markdown_editor=current_user.markdown_editor, mods=mod_list,
|
|
|
|
moderating_communities=moderating_communities(current_user.get_id()),
|
|
|
|
joined_communities=joined_communities(current_user.get_id()),
|
2024-05-30 21:54:25 +12:00
|
|
|
menu_topics=menu_topics(), site=g.site,
|
2024-07-12 19:56:57 +08:00
|
|
|
inoculation=inoculation[randint(0, len(inoculation) - 1)] if g.site.show_inoculation_block else None
|
2024-05-18 20:17:05 +12:00
|
|
|
)
|
|
|
|
else:
|
|
|
|
abort(401)
|
2024-05-18 21:06:57 +12:00
|
|
|
|
|
|
|
|
2024-04-07 11:15:04 +12:00
|
|
|
def federate_post_update(post):
|
|
|
|
page_json = {
|
|
|
|
'type': 'Page',
|
|
|
|
'id': post.ap_id,
|
2024-08-23 21:23:58 +00:00
|
|
|
'attributedTo': current_user.public_url(),
|
2024-04-07 11:15:04 +12:00
|
|
|
'to': [
|
2024-08-23 21:23:58 +00:00
|
|
|
post.community.public_url(),
|
2024-04-07 11:15:04 +12:00
|
|
|
'https://www.w3.org/ns/activitystreams#Public'
|
|
|
|
],
|
|
|
|
'name': post.title,
|
|
|
|
'cc': [],
|
|
|
|
'content': post.body_html if post.body_html else '',
|
|
|
|
'mediaType': 'text/html',
|
2024-09-22 13:42:02 +00:00
|
|
|
'source': {'content': post.body if post.body else '', 'mediaType': 'text/markdown'},
|
2024-04-07 11:15:04 +12:00
|
|
|
'attachment': [],
|
|
|
|
'commentsEnabled': post.comments_enabled,
|
|
|
|
'sensitive': post.nsfw,
|
|
|
|
'nsfl': post.nsfl,
|
|
|
|
'stickied': post.sticky,
|
|
|
|
'published': ap_datetime(post.posted_at),
|
|
|
|
'updated': ap_datetime(post.edited_at),
|
2024-08-23 21:23:58 +00:00
|
|
|
'audience': post.community.public_url(),
|
2024-05-09 17:54:30 +12:00
|
|
|
'language': {
|
|
|
|
'identifier': post.language_code(),
|
|
|
|
'name': post.language_name()
|
2024-05-12 13:02:45 +12:00
|
|
|
},
|
|
|
|
'tag': post.tags_for_activitypub()
|
2024-04-07 11:15:04 +12:00
|
|
|
}
|
|
|
|
update_json = {
|
|
|
|
'id': f"https://{current_app.config['SERVER_NAME']}/activities/update/{gibberish(15)}",
|
|
|
|
'type': 'Update',
|
2024-06-05 13:21:41 +12:00
|
|
|
'actor': current_user.public_url(),
|
|
|
|
'audience': post.community.public_url(),
|
|
|
|
'to': [post.community.public_url(), 'https://www.w3.org/ns/activitystreams#Public'],
|
2024-04-07 11:15:04 +12:00
|
|
|
'published': ap_datetime(utcnow()),
|
|
|
|
'cc': [
|
|
|
|
current_user.followers_url()
|
|
|
|
],
|
|
|
|
'object': page_json,
|
|
|
|
}
|
2024-04-16 20:59:58 +12:00
|
|
|
if post.type == POST_TYPE_LINK or post.type == POST_TYPE_VIDEO:
|
2024-04-07 11:15:04 +12:00
|
|
|
page_json['attachment'] = [{'href': post.url, 'type': 'Link'}]
|
|
|
|
elif post.image_id:
|
|
|
|
if post.image.file_path:
|
|
|
|
image_url = post.image.file_path.replace('app/static/',
|
|
|
|
f"https://{current_app.config['SERVER_NAME']}/static/")
|
|
|
|
elif post.image.thumbnail_path:
|
|
|
|
image_url = post.image.thumbnail_path.replace('app/static/',
|
|
|
|
f"https://{current_app.config['SERVER_NAME']}/static/")
|
|
|
|
else:
|
|
|
|
image_url = post.image.source_url
|
|
|
|
# NB image is a dict while attachment is a list of dicts (usually just one dict in the list)
|
|
|
|
page_json['image'] = {'type': 'Image', 'url': image_url}
|
|
|
|
if post.type == POST_TYPE_IMAGE:
|
2024-09-01 19:14:05 +01:00
|
|
|
page_json['attachment'] = [{'type': 'Image',
|
|
|
|
'url': post.image.source_url, # source_url is always a https link, no need for .replace() as done above
|
|
|
|
'name': post.image.alt_text}]
|
2024-05-18 21:06:57 +12:00
|
|
|
if post.type == POST_TYPE_POLL:
|
|
|
|
poll = Poll.query.filter_by(post_id=post.id).first()
|
|
|
|
page_json['type'] = 'Question'
|
|
|
|
page_json['endTime'] = ap_datetime(poll.end_poll)
|
|
|
|
page_json['votersCount'] = 0
|
|
|
|
choices = []
|
|
|
|
for choice in PollChoice.query.filter_by(post_id=post.id).all():
|
|
|
|
choices.append({
|
|
|
|
"type": "Note",
|
|
|
|
"name": choice.choice_text,
|
|
|
|
"replies": {
|
|
|
|
"type": "Collection",
|
|
|
|
"totalItems": 0
|
|
|
|
}
|
|
|
|
})
|
|
|
|
page_json['oneOf' if poll.mode == 'single' else 'anyOf'] = choices
|
|
|
|
|
2024-04-07 11:15:04 +12:00
|
|
|
if not post.community.is_local(): # this is a remote community, send it to the instance that hosts it
|
|
|
|
success = post_request(post.community.ap_inbox_url, update_json, current_user.private_key,
|
2024-06-05 13:21:41 +12:00
|
|
|
current_user.public_url() + '#main-key')
|
2024-08-30 17:25:37 +12:00
|
|
|
if success is False or isinstance(success, str):
|
2024-04-07 11:15:04 +12:00
|
|
|
flash('Failed to send edit to remote server', 'error')
|
|
|
|
else: # local community - send it to followers on remote instances
|
|
|
|
announce = {
|
|
|
|
"id": f"https://{current_app.config['SERVER_NAME']}/activities/announce/{gibberish(15)}",
|
|
|
|
"type": 'Announce',
|
|
|
|
"to": [
|
|
|
|
"https://www.w3.org/ns/activitystreams#Public"
|
|
|
|
],
|
|
|
|
"actor": post.community.ap_profile_id,
|
|
|
|
"cc": [
|
|
|
|
post.community.ap_followers_url
|
|
|
|
],
|
|
|
|
'@context': default_context(),
|
|
|
|
'object': update_json
|
|
|
|
}
|
|
|
|
|
|
|
|
for instance in post.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, post.community.id, announce)
|
|
|
|
|
|
|
|
|
2024-05-10 16:36:15 +01:00
|
|
|
def federate_post_edit_to_user_followers(post):
|
|
|
|
followers = UserFollower.query.filter_by(local_user_id=post.user_id)
|
|
|
|
if not followers:
|
|
|
|
return
|
|
|
|
|
|
|
|
note = {
|
|
|
|
'type': 'Note',
|
|
|
|
'id': post.ap_id,
|
|
|
|
'inReplyTo': None,
|
2024-09-01 17:49:46 +01:00
|
|
|
'attributedTo': current_user.public_url(),
|
2024-05-10 16:36:15 +01:00
|
|
|
'to': [
|
|
|
|
'https://www.w3.org/ns/activitystreams#Public'
|
|
|
|
],
|
|
|
|
'cc': [
|
2024-09-01 17:49:46 +01:00
|
|
|
current_user.followers_url()
|
2024-05-10 16:36:15 +01:00
|
|
|
],
|
|
|
|
'content': '',
|
|
|
|
'mediaType': 'text/html',
|
2024-09-22 13:42:02 +00:00
|
|
|
'source': {'content': post.body if post.body else '', 'mediaType': 'text/markdown'},
|
2024-05-10 16:36:15 +01:00
|
|
|
'attachment': [],
|
|
|
|
'commentsEnabled': post.comments_enabled,
|
|
|
|
'sensitive': post.nsfw,
|
|
|
|
'nsfl': post.nsfl,
|
|
|
|
'stickied': post.sticky,
|
|
|
|
'published': ap_datetime(utcnow()),
|
|
|
|
'updated': ap_datetime(post.edited_at),
|
|
|
|
'language': {
|
|
|
|
'identifier': post.language_code(),
|
|
|
|
'name': post.language_name()
|
2024-05-12 13:02:45 +12:00
|
|
|
},
|
|
|
|
'tag': post.tags_for_activitypub()
|
2024-05-10 16:36:15 +01:00
|
|
|
}
|
|
|
|
update = {
|
|
|
|
"id": f"https://{current_app.config['SERVER_NAME']}/activities/create/{gibberish(15)}",
|
2024-09-01 17:49:46 +01:00
|
|
|
"actor": current_user.public_url(),
|
2024-05-10 16:36:15 +01:00
|
|
|
"to": [
|
|
|
|
"https://www.w3.org/ns/activitystreams#Public"
|
|
|
|
],
|
|
|
|
"cc": [
|
2024-09-01 17:49:46 +01:00
|
|
|
current_user.followers_url()
|
2024-05-10 16:36:15 +01:00
|
|
|
],
|
|
|
|
"type": "Update",
|
|
|
|
"object": note,
|
|
|
|
'@context': default_context()
|
|
|
|
}
|
|
|
|
if post.type == POST_TYPE_ARTICLE:
|
|
|
|
note['content'] = '<p>' + post.title + '</p>'
|
|
|
|
elif post.type == POST_TYPE_LINK or post.type == POST_TYPE_VIDEO:
|
|
|
|
note['content'] = '<p><a href=' + post.url + '>' + post.title + '</a></p>'
|
|
|
|
elif post.type == POST_TYPE_IMAGE:
|
|
|
|
note['content'] = '<p>' + post.title + '</p>'
|
2024-06-25 21:11:57 +08:00
|
|
|
if post.image_id and post.image.source_url:
|
2024-09-01 19:14:05 +01:00
|
|
|
note['attachment'] = [{'type': 'Image', 'url': post.image.source_url, 'name': post.image.alt_text}]
|
2024-05-18 21:06:57 +12:00
|
|
|
elif post.type == POST_TYPE_POLL:
|
|
|
|
poll = Poll.query.filter_by(post_id=post.id).first()
|
|
|
|
note['type'] = 'Question'
|
|
|
|
note['endTime'] = ap_datetime(poll.end_poll)
|
|
|
|
note['votersCount'] = 0
|
|
|
|
choices = []
|
|
|
|
for choice in PollChoice.query.filter_by(post_id=post.id).all():
|
|
|
|
choices.append({
|
|
|
|
"type": "Note",
|
|
|
|
"name": choice.choice_text,
|
|
|
|
"replies": {
|
|
|
|
"type": "Collection",
|
|
|
|
"totalItems": 0
|
|
|
|
}
|
|
|
|
})
|
|
|
|
note['oneOf' if poll.mode == 'single' else 'anyOf'] = choices
|
2024-05-10 16:36:15 +01:00
|
|
|
|
|
|
|
if post.body_html:
|
|
|
|
note['content'] = note['content'] + '<p>' + post.body_html + '</p>'
|
|
|
|
|
|
|
|
instances = Instance.query.join(User, User.instance_id == Instance.id).join(UserFollower, UserFollower.remote_user_id == User.id)
|
|
|
|
instances = instances.filter(UserFollower.local_user_id == post.user_id)
|
2024-07-24 21:00:36 +08:00
|
|
|
for instance in instances:
|
|
|
|
if instance.inbox and not instance_banned(instance.domain):
|
|
|
|
post_request_in_background(instance.inbox, update, current_user.private_key, current_user.public_url() + '#main-key')
|
2024-05-10 16:36:15 +01:00
|
|
|
|
|
|
|
|
2023-11-30 20:57:51 +13:00
|
|
|
@bp.route('/post/<int:post_id>/delete', methods=['GET', 'POST'])
|
2023-12-26 21:39:52 +13:00
|
|
|
@login_required
|
2023-11-30 20:57:51 +13:00
|
|
|
def post_delete(post_id: int):
|
|
|
|
post = Post.query.get_or_404(post_id)
|
|
|
|
community = post.community
|
2024-01-08 22:43:38 +13:00
|
|
|
if post.user_id == current_user.id or community.is_moderator() or current_user.is_admin():
|
2024-08-16 16:10:09 +12:00
|
|
|
post_delete_post(community, post, current_user.id)
|
|
|
|
return redirect(url_for('activitypub.community_profile', actor=community.ap_id if community.ap_id is not None else community.name))
|
2024-01-27 12:22:35 +13:00
|
|
|
|
2024-01-03 16:29:58 +13:00
|
|
|
|
2024-08-16 16:19:23 +12:00
|
|
|
def post_delete_post(community: Community, post: Post, user_id: int, federate_all_communities=True):
|
2024-08-16 16:10:09 +12:00
|
|
|
user: User = User.query.get(user_id)
|
|
|
|
if post.url:
|
|
|
|
if post.cross_posts is not None:
|
|
|
|
old_cross_posts = Post.query.filter(Post.id.in_(post.cross_posts)).all()
|
|
|
|
post.cross_posts.clear()
|
|
|
|
for ocp in old_cross_posts:
|
|
|
|
if ocp.cross_posts is not None:
|
|
|
|
ocp.cross_posts.remove(post.id)
|
|
|
|
post.delete_dependencies()
|
|
|
|
post.deleted = True
|
2024-09-13 11:08:04 +12:00
|
|
|
post.author.post_count -= 1
|
|
|
|
community.post_count -= 1
|
2024-08-16 16:10:09 +12:00
|
|
|
if hasattr(g, 'site'): # g.site is invalid when running from cli
|
|
|
|
g.site.last_active = community.last_active = utcnow()
|
|
|
|
flash(_('Post deleted.'))
|
|
|
|
db.session.commit()
|
2024-05-10 17:10:44 +01:00
|
|
|
|
2024-08-16 16:10:09 +12:00
|
|
|
delete_json = {
|
|
|
|
'id': f"https://{current_app.config['SERVER_NAME']}/activities/delete/{gibberish(15)}",
|
|
|
|
'type': 'Delete',
|
|
|
|
'actor': user.public_url(),
|
|
|
|
'audience': post.community.public_url(),
|
|
|
|
'to': [post.community.public_url(), 'https://www.w3.org/ns/activitystreams#Public'],
|
|
|
|
'published': ap_datetime(utcnow()),
|
|
|
|
'cc': [
|
|
|
|
user.followers_url()
|
|
|
|
],
|
|
|
|
'object': post.ap_id,
|
|
|
|
'uri': post.ap_id
|
|
|
|
}
|
|
|
|
if post.user_id != user.id:
|
|
|
|
delete_json['summary'] = 'Deleted by mod'
|
|
|
|
|
|
|
|
# Federation
|
2024-08-16 16:19:23 +12:00
|
|
|
if not community.local_only: # local_only communities do not federate
|
|
|
|
# if this is a remote community and we are a mod of that community
|
|
|
|
if not post.community.is_local() and user.is_local() and (community.is_moderator(user) or community.is_owner(user)):
|
2024-08-16 16:10:09 +12:00
|
|
|
post_request(post.community.ap_inbox_url, delete_json, user.private_key, user.public_url() + '#main-key')
|
2024-08-16 16:19:23 +12:00
|
|
|
elif post.community.is_local(): # if this is a local community - Announce it to followers on remote instances
|
2024-08-16 16:10:09 +12:00
|
|
|
announce = {
|
|
|
|
"id": f"https://{current_app.config['SERVER_NAME']}/activities/announce/{gibberish(15)}",
|
|
|
|
"type": 'Announce',
|
|
|
|
"to": [
|
|
|
|
"https://www.w3.org/ns/activitystreams#Public"
|
|
|
|
],
|
|
|
|
"actor": post.community.ap_profile_id,
|
|
|
|
"cc": [
|
|
|
|
post.community.ap_followers_url
|
|
|
|
],
|
|
|
|
'@context': default_context(),
|
|
|
|
'object': delete_json
|
|
|
|
}
|
|
|
|
for instance in post.community.following_instances():
|
|
|
|
if instance.inbox and not user.has_blocked_instance(instance.id) and not instance_banned(instance.domain):
|
|
|
|
send_to_remote_instance(instance.id, post.community.id, announce)
|
2024-07-07 15:01:52 +08:00
|
|
|
|
2024-08-16 16:10:09 +12:00
|
|
|
# Federate to microblog followers
|
|
|
|
followers = UserFollower.query.filter_by(local_user_id=post.user_id)
|
|
|
|
if followers:
|
|
|
|
instances = Instance.query.join(User, User.instance_id == Instance.id).join(UserFollower,
|
|
|
|
UserFollower.remote_user_id == User.id)
|
|
|
|
instances = instances.filter(UserFollower.local_user_id == post.user_id)
|
|
|
|
for instance in instances:
|
2024-08-17 10:26:19 +12:00
|
|
|
if instance.inbox and not user.has_blocked_instance(instance.id) and not instance_banned(instance.domain) and instance.online():
|
2024-08-16 16:10:09 +12:00
|
|
|
post_request_in_background(instance.inbox, delete_json, user.private_key, user.public_url() + '#main-key')
|
|
|
|
|
|
|
|
if post.user_id != user.id:
|
|
|
|
add_to_modlog('delete_post', community_id=community.id, link_text=shorten_string(post.title),
|
|
|
|
link=f'post/{post.id}')
|
2023-11-30 20:57:51 +13:00
|
|
|
|
|
|
|
|
2024-06-02 16:45:21 +12:00
|
|
|
@bp.route('/post/<int:post_id>/restore', methods=['GET', 'POST'])
|
|
|
|
@login_required
|
|
|
|
def post_restore(post_id: int):
|
|
|
|
post = Post.query.get_or_404(post_id)
|
|
|
|
if post.community.is_moderator() or post.community.is_owner() or current_user.is_admin():
|
|
|
|
post.deleted = False
|
2024-09-13 11:08:04 +12:00
|
|
|
post.author.post_count += 1
|
|
|
|
post.community.post_count += 1
|
2024-06-02 16:45:21 +12:00
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
# Federate un-delete
|
|
|
|
if post.is_local():
|
|
|
|
delete_json = {
|
2024-06-05 13:21:41 +12:00
|
|
|
"actor": current_user.public_url(),
|
2024-06-02 16:45:21 +12:00
|
|
|
"to": ["https://www.w3.org/ns/activitystreams#Public"],
|
|
|
|
"object": {
|
|
|
|
'id': f"https://{current_app.config['SERVER_NAME']}/activities/delete/{gibberish(15)}",
|
|
|
|
'type': 'Delete',
|
2024-06-05 13:21:41 +12:00
|
|
|
'actor': current_user.public_url(),
|
|
|
|
'audience': post.community.public_url(),
|
|
|
|
'to': [post.community.public_url(), 'https://www.w3.org/ns/activitystreams#Public'],
|
2024-06-02 16:45:21 +12:00
|
|
|
'published': ap_datetime(utcnow()),
|
|
|
|
'cc': [
|
|
|
|
current_user.followers_url()
|
|
|
|
],
|
|
|
|
'object': post.ap_id,
|
|
|
|
'uri': post.ap_id,
|
|
|
|
"summary": "bad post",
|
|
|
|
},
|
2024-06-05 13:21:41 +12:00
|
|
|
"cc": [post.community.public_url()],
|
|
|
|
"audience": post.author.public_url(),
|
2024-06-02 16:45:21 +12:00
|
|
|
"type": "Undo",
|
|
|
|
"id": f"https://{current_app.config['SERVER_NAME']}/activities/undo/{gibberish(15)}"
|
|
|
|
}
|
|
|
|
|
|
|
|
announce = {
|
|
|
|
"id": f"https://{current_app.config['SERVER_NAME']}/activities/announce/{gibberish(15)}",
|
|
|
|
"type": 'Announce',
|
|
|
|
"to": [
|
|
|
|
"https://www.w3.org/ns/activitystreams#Public"
|
|
|
|
],
|
2024-06-05 13:21:41 +12:00
|
|
|
"actor": post.community.public_url(),
|
2024-06-02 16:45:21 +12:00
|
|
|
"cc": [
|
|
|
|
post.community.ap_followers_url
|
|
|
|
],
|
|
|
|
'@context': default_context(),
|
|
|
|
'object': delete_json
|
|
|
|
}
|
|
|
|
|
|
|
|
for instance in post.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, post.community.id, announce)
|
|
|
|
|
2024-07-07 15:01:52 +08:00
|
|
|
if post.user_id != current_user.id:
|
|
|
|
add_to_modlog('restore_post', community_id=post.community.id, link_text=shorten_string(post.title),
|
|
|
|
link=f'post/{post.id}')
|
|
|
|
|
2024-06-02 16:45:21 +12:00
|
|
|
flash(_('Post has been restored.'))
|
|
|
|
return redirect(url_for('activitypub.post_ap', post_id=post.id))
|
|
|
|
|
|
|
|
|
2024-06-20 21:51:43 +08:00
|
|
|
@bp.route('/post/<int:post_id>/bookmark', methods=['GET', 'POST'])
|
|
|
|
@login_required
|
|
|
|
def post_bookmark(post_id: int):
|
|
|
|
post = Post.query.get_or_404(post_id)
|
|
|
|
if post.deleted:
|
|
|
|
abort(404)
|
|
|
|
existing_bookmark = PostBookmark.query.filter(PostBookmark.post_id == post_id, PostBookmark.user_id == current_user.id).first()
|
|
|
|
if not existing_bookmark:
|
|
|
|
db.session.add(PostBookmark(post_id=post_id, user_id=current_user.id))
|
|
|
|
db.session.commit()
|
|
|
|
flash(_('Bookmark added.'))
|
|
|
|
else:
|
|
|
|
flash(_('This post has already been bookmarked.'))
|
|
|
|
return redirect(url_for('activitypub.post_ap', post_id=post.id))
|
|
|
|
|
|
|
|
|
2024-06-21 17:28:49 +08:00
|
|
|
@bp.route('/post/<int:post_id>/remove_bookmark', methods=['GET', 'POST'])
|
|
|
|
@login_required
|
|
|
|
def post_remove_bookmark(post_id: int):
|
|
|
|
post = Post.query.get_or_404(post_id)
|
|
|
|
if post.deleted:
|
|
|
|
abort(404)
|
|
|
|
existing_bookmark = PostBookmark.query.filter(PostBookmark.post_id == post_id, PostBookmark.user_id == current_user.id).first()
|
|
|
|
if existing_bookmark:
|
|
|
|
db.session.delete(existing_bookmark)
|
|
|
|
db.session.commit()
|
|
|
|
flash(_('Bookmark has been removed.'))
|
|
|
|
return redirect(url_for('activitypub.post_ap', post_id=post.id))
|
|
|
|
|
|
|
|
|
|
|
|
@bp.route('/post/<int:post_id>/comment/<int:comment_id>/remove_bookmark', methods=['GET', 'POST'])
|
|
|
|
@login_required
|
|
|
|
def post_reply_remove_bookmark(post_id: int, comment_id: int):
|
|
|
|
post = Post.query.get_or_404(post_id)
|
|
|
|
post_reply = PostReply.query.get_or_404(comment_id)
|
|
|
|
|
|
|
|
if post.deleted or post_reply.deleted:
|
|
|
|
abort(404)
|
|
|
|
existing_bookmark = PostReplyBookmark.query.filter(PostReplyBookmark.post_reply_id == comment_id, PostReplyBookmark.user_id == current_user.id).first()
|
|
|
|
if existing_bookmark:
|
|
|
|
db.session.delete(existing_bookmark)
|
|
|
|
db.session.commit()
|
|
|
|
flash(_('Bookmark has been removed.'))
|
|
|
|
return redirect(url_for('activitypub.post_ap', post_id=post.id))
|
|
|
|
|
|
|
|
|
2023-11-30 20:57:51 +13:00
|
|
|
@bp.route('/post/<int:post_id>/report', methods=['GET', 'POST'])
|
2023-12-26 21:39:52 +13:00
|
|
|
@login_required
|
2023-11-30 20:57:51 +13:00
|
|
|
def post_report(post_id: int):
|
2023-12-13 21:04:11 +13:00
|
|
|
post = Post.query.get_or_404(post_id)
|
|
|
|
form = ReportPostForm()
|
2024-04-06 14:10:23 +13:00
|
|
|
if post.reports == -1: # When a mod decides to ignore future reports, post.reports is set to -1
|
|
|
|
flash(_('Moderators have already assessed reports regarding this post, no further reports are necessary.'), 'warning')
|
2023-12-13 21:04:11 +13:00
|
|
|
if form.validate_on_submit():
|
2024-04-06 14:10:23 +13:00
|
|
|
if post.reports == -1:
|
|
|
|
flash(_('Post has already been reported, thank you!'))
|
|
|
|
return redirect(post.community.local_url())
|
2023-12-13 21:04:11 +13:00
|
|
|
report = Report(reasons=form.reasons_to_string(form.reasons.data), description=form.description.data,
|
2023-12-28 20:00:07 +13:00
|
|
|
type=1, reporter_id=current_user.id, suspect_user_id=post.author.id, suspect_post_id=post.id,
|
2024-03-26 22:18:05 +13:00
|
|
|
suspect_community_id=post.community.id, in_community_id=post.community.id, source_instance_id=1)
|
2023-12-13 21:04:11 +13:00
|
|
|
db.session.add(report)
|
|
|
|
|
|
|
|
# Notify moderators
|
2024-01-01 16:26:57 +13:00
|
|
|
already_notified = set()
|
2023-12-13 21:04:11 +13:00
|
|
|
for mod in post.community.moderators():
|
|
|
|
notification = Notification(user_id=mod.user_id, title=_('A post has been reported'),
|
|
|
|
url=f"https://{current_app.config['SERVER_NAME']}/post/{post.id}",
|
|
|
|
author_id=current_user.id)
|
|
|
|
db.session.add(notification)
|
2024-02-23 20:23:59 +13:00
|
|
|
already_notified.add(mod.user_id)
|
2023-12-17 00:12:49 +13:00
|
|
|
post.reports += 1
|
2024-01-01 16:26:57 +13:00
|
|
|
# todo: only notify admins for certain types of report
|
|
|
|
for admin in Site.admins():
|
|
|
|
if admin.id not in already_notified:
|
2024-02-17 20:05:57 +13:00
|
|
|
notify = Notification(title='Suspicious content', url='/admin/reports', user_id=admin.id, author_id=current_user.id)
|
2024-01-01 16:26:57 +13:00
|
|
|
db.session.add(notify)
|
2024-02-05 16:22:17 +13:00
|
|
|
admin.unread_notifications += 1
|
2023-12-13 21:04:11 +13:00
|
|
|
db.session.commit()
|
|
|
|
|
2024-04-06 15:24:59 +13:00
|
|
|
# federate report to community instance
|
2023-12-13 21:04:11 +13:00
|
|
|
if not post.community.is_local() and form.report_remote.data:
|
2024-04-06 15:24:59 +13:00
|
|
|
summary = form.reasons_to_string(form.reasons.data)
|
|
|
|
if form.description.data:
|
|
|
|
summary += ' - ' + form.description.data
|
|
|
|
report_json = {
|
2024-06-05 13:21:41 +12:00
|
|
|
"actor": current_user.public_url(),
|
|
|
|
"audience": post.community.public_url(),
|
2024-04-06 15:24:59 +13:00
|
|
|
"content": None,
|
|
|
|
"id": f"https://{current_app.config['SERVER_NAME']}/activities/flag/{gibberish(15)}",
|
|
|
|
"object": post.ap_id,
|
|
|
|
"summary": summary,
|
|
|
|
"to": [
|
2024-06-05 13:21:41 +12:00
|
|
|
post.community.public_url()
|
2024-04-06 15:24:59 +13:00
|
|
|
],
|
|
|
|
"type": "Flag"
|
|
|
|
}
|
|
|
|
instance = Instance.query.get(post.community.instance_id)
|
|
|
|
if post.community.ap_inbox_url and not current_user.has_blocked_instance(instance.id) and not instance_banned(instance.domain):
|
|
|
|
success = post_request(post.community.ap_inbox_url, report_json, current_user.private_key,
|
2024-06-05 13:21:41 +12:00
|
|
|
current_user.public_url() + '#main-key')
|
2024-08-30 17:25:37 +12:00
|
|
|
if success is False or isinstance(success, str):
|
2024-04-06 15:24:59 +13:00
|
|
|
flash('Failed to send report to remote server', 'error')
|
2023-12-13 21:04:11 +13:00
|
|
|
|
|
|
|
flash(_('Post has been reported, thank you!'))
|
|
|
|
return redirect(post.community.local_url())
|
2023-12-15 17:35:11 +13:00
|
|
|
elif request.method == 'GET':
|
|
|
|
form.report_remote.data = True
|
2023-12-13 21:04:11 +13:00
|
|
|
|
2024-01-12 12:34:08 +13:00
|
|
|
return render_template('post/post_report.html', title=_('Report post'), form=form, post=post,
|
|
|
|
moderating_communities=moderating_communities(current_user.get_id()),
|
2024-05-30 21:54:25 +12:00
|
|
|
joined_communities=joined_communities(current_user.get_id()),
|
|
|
|
menu_topics=menu_topics(), site=g.site
|
2024-01-12 12:34:08 +13:00
|
|
|
)
|
2023-12-13 21:04:11 +13:00
|
|
|
|
|
|
|
|
|
|
|
@bp.route('/post/<int:post_id>/block_user', methods=['GET', 'POST'])
|
2023-12-26 21:39:52 +13:00
|
|
|
@login_required
|
2023-12-13 21:04:11 +13:00
|
|
|
def post_block_user(post_id: int):
|
|
|
|
post = Post.query.get_or_404(post_id)
|
|
|
|
existing = UserBlock.query.filter_by(blocker_id=current_user.id, blocked_id=post.author.id).first()
|
|
|
|
if not existing:
|
|
|
|
db.session.add(UserBlock(blocker_id=current_user.id, blocked_id=post.author.id))
|
|
|
|
db.session.commit()
|
|
|
|
flash(_('%(name)s has been blocked.', name=post.author.user_name))
|
|
|
|
|
|
|
|
# todo: federate block to post author instance
|
|
|
|
|
|
|
|
return redirect(post.community.local_url())
|
|
|
|
|
|
|
|
|
|
|
|
@bp.route('/post/<int:post_id>/block_domain', methods=['GET', 'POST'])
|
2023-12-26 21:39:52 +13:00
|
|
|
@login_required
|
2023-12-13 21:04:11 +13:00
|
|
|
def post_block_domain(post_id: int):
|
|
|
|
post = Post.query.get_or_404(post_id)
|
|
|
|
existing = DomainBlock.query.filter_by(user_id=current_user.id, domain_id=post.domain_id).first()
|
|
|
|
if not existing:
|
|
|
|
db.session.add(DomainBlock(user_id=current_user.id, domain_id=post.domain_id))
|
|
|
|
db.session.commit()
|
2024-03-12 20:06:24 +13:00
|
|
|
cache.delete_memoized(blocked_domains, current_user.id)
|
2023-12-13 21:04:11 +13:00
|
|
|
flash(_('Posts linking to %(name)s will be hidden.', name=post.domain.name))
|
|
|
|
return redirect(post.community.local_url())
|
|
|
|
|
2023-11-30 20:57:51 +13:00
|
|
|
|
2024-08-12 20:54:10 +12:00
|
|
|
@bp.route('/post/<int:post_id>/block_community', methods=['GET', 'POST'])
|
|
|
|
@login_required
|
|
|
|
def post_block_community(post_id: int):
|
|
|
|
post = Post.query.get_or_404(post_id)
|
|
|
|
existing = CommunityBlock.query.filter_by(user_id=current_user.id, community_id=post.community_id).first()
|
|
|
|
if not existing:
|
|
|
|
db.session.add(CommunityBlock(user_id=current_user.id, community_id=post.community_id))
|
|
|
|
db.session.commit()
|
|
|
|
cache.delete_memoized(blocked_communities, current_user.id)
|
|
|
|
flash(_('Posts in %(name)s will be hidden.', name=post.community.display_name()))
|
|
|
|
return redirect(post.community.local_url())
|
|
|
|
|
|
|
|
|
2023-12-13 21:04:11 +13:00
|
|
|
@bp.route('/post/<int:post_id>/block_instance', methods=['GET', 'POST'])
|
2023-12-26 21:39:52 +13:00
|
|
|
@login_required
|
2023-12-13 21:04:11 +13:00
|
|
|
def post_block_instance(post_id: int):
|
|
|
|
post = Post.query.get_or_404(post_id)
|
|
|
|
existing = InstanceBlock.query.filter_by(user_id=current_user.id, instance_id=post.instance_id).first()
|
|
|
|
if not existing:
|
|
|
|
db.session.add(InstanceBlock(user_id=current_user.id, instance_id=post.instance_id))
|
|
|
|
db.session.commit()
|
2024-03-12 20:06:24 +13:00
|
|
|
cache.delete_memoized(blocked_instances, current_user.id)
|
2023-12-13 21:04:11 +13:00
|
|
|
flash(_('Content from %(name)s will be hidden.', name=post.instance.domain))
|
|
|
|
return redirect(post.community.local_url())
|
2023-12-14 21:22:46 +13:00
|
|
|
|
|
|
|
|
|
|
|
@bp.route('/post/<int:post_id>/mea_culpa', methods=['GET', 'POST'])
|
2023-12-26 21:39:52 +13:00
|
|
|
@login_required
|
2023-12-14 21:22:46 +13:00
|
|
|
def post_mea_culpa(post_id: int):
|
|
|
|
post = Post.query.get_or_404(post_id)
|
|
|
|
form = MeaCulpaForm()
|
|
|
|
if form.validate_on_submit():
|
|
|
|
post.comments_enabled = False
|
|
|
|
post.mea_culpa = True
|
|
|
|
post.community.last_active = utcnow()
|
|
|
|
post.last_active = utcnow()
|
|
|
|
db.session.commit()
|
|
|
|
return redirect(url_for('activitypub.post_ap', post_id=post.id))
|
|
|
|
|
2024-01-12 12:34:08 +13:00
|
|
|
return render_template('post/post_mea_culpa.html', title=_('I changed my mind'), form=form, post=post,
|
|
|
|
moderating_communities=moderating_communities(current_user.get_id()),
|
2024-05-30 21:54:25 +12:00
|
|
|
joined_communities=joined_communities(current_user.get_id()),
|
|
|
|
menu_topics=menu_topics(), site=g.site
|
2024-01-12 12:34:08 +13:00
|
|
|
)
|
2023-12-28 20:00:07 +13:00
|
|
|
|
|
|
|
|
|
|
|
@bp.route('/post/<int:post_id>/comment/<int:comment_id>/report', methods=['GET', 'POST'])
|
|
|
|
@login_required
|
|
|
|
def post_reply_report(post_id: int, comment_id: int):
|
|
|
|
post = Post.query.get_or_404(post_id)
|
|
|
|
post_reply = PostReply.query.get_or_404(comment_id)
|
|
|
|
form = ReportPostForm()
|
2024-04-06 14:10:23 +13:00
|
|
|
|
|
|
|
if post_reply.reports == -1: # When a mod decides to ignore future reports, post_reply.reports is set to -1
|
|
|
|
flash(_('Moderators have already assessed reports regarding this comment, no further reports are necessary.'), 'warning')
|
|
|
|
|
2023-12-28 20:00:07 +13:00
|
|
|
if form.validate_on_submit():
|
2024-04-06 14:10:23 +13:00
|
|
|
|
|
|
|
if post_reply.reports == -1:
|
|
|
|
flash(_('Comment has already been reported, thank you!'))
|
|
|
|
return redirect(post.community.local_url())
|
|
|
|
|
2023-12-28 20:00:07 +13:00
|
|
|
report = Report(reasons=form.reasons_to_string(form.reasons.data), description=form.description.data,
|
2023-12-28 20:39:26 +13:00
|
|
|
type=2, reporter_id=current_user.id, suspect_post_id=post.id, suspect_community_id=post.community.id,
|
2024-03-26 22:18:05 +13:00
|
|
|
suspect_user_id=post_reply.author.id, suspect_post_reply_id=post_reply.id, in_community_id=post.community.id,
|
|
|
|
source_instance_id=1)
|
2023-12-28 20:00:07 +13:00
|
|
|
db.session.add(report)
|
|
|
|
|
|
|
|
# Notify moderators
|
2024-01-01 16:26:57 +13:00
|
|
|
already_notified = set()
|
2023-12-28 20:00:07 +13:00
|
|
|
for mod in post.community.moderators():
|
|
|
|
notification = Notification(user_id=mod.user_id, title=_('A comment has been reported'),
|
|
|
|
url=f"https://{current_app.config['SERVER_NAME']}/comment/{post_reply.id}",
|
|
|
|
author_id=current_user.id)
|
|
|
|
db.session.add(notification)
|
2024-04-06 16:29:47 +13:00
|
|
|
already_notified.add(mod.user_id)
|
2023-12-28 20:00:07 +13:00
|
|
|
post_reply.reports += 1
|
2024-01-01 16:26:57 +13:00
|
|
|
# todo: only notify admins for certain types of report
|
|
|
|
for admin in Site.admins():
|
|
|
|
if admin.id not in already_notified:
|
2024-02-17 20:05:57 +13:00
|
|
|
notify = Notification(title='Suspicious content', url='/admin/reports', user_id=admin.id, author_id=current_user.id)
|
2024-01-01 16:26:57 +13:00
|
|
|
db.session.add(notify)
|
2024-02-05 16:22:17 +13:00
|
|
|
admin.unread_notifications += 1
|
2023-12-28 20:00:07 +13:00
|
|
|
db.session.commit()
|
|
|
|
|
2024-04-06 15:24:59 +13:00
|
|
|
# federate report to originating instance
|
2023-12-28 20:00:07 +13:00
|
|
|
if not post.community.is_local() and form.report_remote.data:
|
2024-04-06 15:24:59 +13:00
|
|
|
summary = form.reasons_to_string(form.reasons.data)
|
|
|
|
if form.description.data:
|
|
|
|
summary += ' - ' + form.description.data
|
|
|
|
report_json = {
|
2024-06-05 13:21:41 +12:00
|
|
|
"actor": current_user.public_url(),
|
|
|
|
"audience": post.community.public_url(),
|
2024-04-06 15:24:59 +13:00
|
|
|
"content": None,
|
|
|
|
"id": f"https://{current_app.config['SERVER_NAME']}/activities/flag/{gibberish(15)}",
|
|
|
|
"object": post_reply.ap_id,
|
|
|
|
"summary": summary,
|
|
|
|
"to": [
|
2024-06-05 13:21:41 +12:00
|
|
|
post.community.public_url()
|
2024-04-06 15:24:59 +13:00
|
|
|
],
|
|
|
|
"type": "Flag"
|
|
|
|
}
|
|
|
|
instance = Instance.query.get(post.community.instance_id)
|
|
|
|
if post.community.ap_inbox_url and not current_user.has_blocked_instance(
|
|
|
|
instance.id) and not instance_banned(instance.domain):
|
|
|
|
success = post_request(post.community.ap_inbox_url, report_json, current_user.private_key,
|
2024-06-05 13:21:41 +12:00
|
|
|
current_user.public_url() + '#main-key')
|
2024-08-30 17:25:37 +12:00
|
|
|
if success is False or isinstance(success, str):
|
2024-04-06 15:24:59 +13:00
|
|
|
flash('Failed to send report to remote server', 'error')
|
2023-12-28 20:00:07 +13:00
|
|
|
|
|
|
|
flash(_('Comment has been reported, thank you!'))
|
2024-06-20 21:51:43 +08:00
|
|
|
return redirect(url_for('activitypub.post_ap', post_id=post.id))
|
2023-12-28 20:00:07 +13:00
|
|
|
elif request.method == 'GET':
|
|
|
|
form.report_remote.data = True
|
|
|
|
|
2024-01-12 12:34:08 +13:00
|
|
|
return render_template('post/post_reply_report.html', title=_('Report comment'), form=form, post=post, post_reply=post_reply,
|
|
|
|
moderating_communities=moderating_communities(current_user.get_id()),
|
2024-05-30 21:54:25 +12:00
|
|
|
joined_communities=joined_communities(current_user.get_id()),
|
|
|
|
menu_topics=menu_topics(), site=g.site
|
2024-01-12 12:34:08 +13:00
|
|
|
)
|
2023-12-28 20:00:07 +13:00
|
|
|
|
|
|
|
|
2024-06-20 21:51:43 +08:00
|
|
|
@bp.route('/post/<int:post_id>/comment/<int:comment_id>/bookmark', methods=['GET'])
|
|
|
|
@login_required
|
|
|
|
def post_reply_bookmark(post_id: int, comment_id: int):
|
|
|
|
post = Post.query.get_or_404(post_id)
|
|
|
|
post_reply = PostReply.query.get_or_404(comment_id)
|
|
|
|
|
|
|
|
if post.deleted or post_reply.deleted:
|
|
|
|
abort(404)
|
|
|
|
existing_bookmark = PostReplyBookmark.query.filter(PostReplyBookmark.post_reply_id == comment_id,
|
|
|
|
PostReplyBookmark.user_id == current_user.id).first()
|
|
|
|
if not existing_bookmark:
|
|
|
|
db.session.add(PostReplyBookmark(post_reply_id=comment_id, user_id=current_user.id))
|
|
|
|
db.session.commit()
|
|
|
|
flash(_('Bookmark added.'))
|
|
|
|
else:
|
|
|
|
flash(_('This comment has already been bookmarked.'))
|
|
|
|
return redirect(url_for('activitypub.post_ap', post_id=post.id, _anchor=f'comment_{comment_id}'))
|
|
|
|
|
|
|
|
|
2023-12-28 20:00:07 +13:00
|
|
|
@bp.route('/post/<int:post_id>/comment/<int:comment_id>/block_user', methods=['GET', 'POST'])
|
|
|
|
@login_required
|
|
|
|
def post_reply_block_user(post_id: int, comment_id: int):
|
|
|
|
post = Post.query.get_or_404(post_id)
|
|
|
|
post_reply = PostReply.query.get_or_404(comment_id)
|
|
|
|
existing = UserBlock.query.filter_by(blocker_id=current_user.id, blocked_id=post_reply.author.id).first()
|
|
|
|
if not existing:
|
|
|
|
db.session.add(UserBlock(blocker_id=current_user.id, blocked_id=post_reply.author.id))
|
|
|
|
db.session.commit()
|
|
|
|
flash(_('%(name)s has been blocked.', name=post_reply.author.user_name))
|
|
|
|
|
|
|
|
# todo: federate block to post_reply author instance
|
|
|
|
|
|
|
|
return redirect(url_for('activitypub.post_ap', post_id=post.id))
|
|
|
|
|
|
|
|
|
|
|
|
@bp.route('/post/<int:post_id>/comment/<int:comment_id>/block_instance', methods=['GET', 'POST'])
|
|
|
|
@login_required
|
|
|
|
def post_reply_block_instance(post_id: int, comment_id: int):
|
|
|
|
post = Post.query.get_or_404(post_id)
|
|
|
|
post_reply = PostReply.query.get_or_404(comment_id)
|
|
|
|
existing = InstanceBlock.query.filter_by(user_id=current_user.id, instance_id=post_reply.instance_id).first()
|
|
|
|
if not existing:
|
|
|
|
db.session.add(InstanceBlock(user_id=current_user.id, instance_id=post_reply.instance_id))
|
|
|
|
db.session.commit()
|
|
|
|
flash(_('Content from %(name)s will be hidden.', name=post_reply.instance.domain))
|
|
|
|
return redirect(url_for('activitypub.post_ap', post_id=post.id))
|
|
|
|
|
|
|
|
|
|
|
|
@bp.route('/post/<int:post_id>/comment/<int:comment_id>/edit', methods=['GET', 'POST'])
|
|
|
|
@login_required
|
|
|
|
def post_reply_edit(post_id: int, comment_id: int):
|
|
|
|
post = Post.query.get_or_404(post_id)
|
|
|
|
post_reply = PostReply.query.get_or_404(comment_id)
|
|
|
|
if post_reply.parent_id:
|
|
|
|
comment = PostReply.query.get_or_404(post_reply.parent_id)
|
|
|
|
else:
|
|
|
|
comment = None
|
|
|
|
form = NewReplyForm()
|
2024-05-09 17:54:30 +12:00
|
|
|
form.language_id.choices = languages_for_form()
|
2023-12-28 20:00:07 +13:00
|
|
|
if post_reply.user_id == current_user.id or post.community.is_moderator():
|
|
|
|
if form.validate_on_submit():
|
2024-09-22 13:42:02 +00:00
|
|
|
post_reply.body = piefed_markdown_to_lemmy_markdown(form.body.data)
|
2023-12-28 20:00:07 +13:00
|
|
|
post_reply.body_html = markdown_to_html(form.body.data)
|
|
|
|
post_reply.notify_author = form.notify_author.data
|
|
|
|
post.community.last_active = utcnow()
|
|
|
|
post_reply.edited_at = utcnow()
|
2024-05-09 17:54:30 +12:00
|
|
|
post_reply.language_id = form.language_id.data
|
2023-12-28 20:00:07 +13:00
|
|
|
db.session.commit()
|
|
|
|
flash(_('Your changes have been saved.'), 'success')
|
2024-01-17 20:48:35 +13:00
|
|
|
|
|
|
|
if post_reply.parent_id:
|
|
|
|
in_reply_to = PostReply.query.get(post_reply.parent_id)
|
|
|
|
else:
|
|
|
|
in_reply_to = post
|
|
|
|
# federate edit
|
2024-01-27 12:22:35 +13:00
|
|
|
if not post.community.local_only:
|
|
|
|
reply_json = {
|
|
|
|
'type': 'Note',
|
2024-06-05 13:21:41 +12:00
|
|
|
'id': post_reply.public_url(),
|
2024-03-26 23:17:19 +00:00
|
|
|
'attributedTo': current_user.public_url(),
|
2024-01-27 12:22:35 +13:00
|
|
|
'to': [
|
2024-03-26 23:17:19 +00:00
|
|
|
'https://www.w3.org/ns/activitystreams#Public'
|
2024-01-17 20:48:35 +13:00
|
|
|
],
|
2024-01-27 12:22:35 +13:00
|
|
|
'cc': [
|
2024-03-26 23:17:19 +00:00
|
|
|
post.community.public_url(),
|
|
|
|
in_reply_to.author.public_url()
|
2024-01-17 20:48:35 +13:00
|
|
|
],
|
2024-01-27 12:22:35 +13:00
|
|
|
'content': post_reply.body_html,
|
2024-06-05 16:23:31 +12:00
|
|
|
'inReplyTo': in_reply_to.profile_id(),
|
2024-06-05 13:21:41 +12:00
|
|
|
'url': post_reply.public_url(),
|
2024-01-27 12:22:35 +13:00
|
|
|
'mediaType': 'text/html',
|
2024-09-22 13:42:02 +00:00
|
|
|
'source': {'content': post_reply.body, 'mediaType': 'text/markdown'},
|
2024-01-27 12:22:35 +13:00
|
|
|
'published': ap_datetime(post_reply.posted_at),
|
|
|
|
'updated': ap_datetime(post_reply.edited_at),
|
|
|
|
'distinguished': False,
|
2024-03-26 23:17:19 +00:00
|
|
|
'audience': post.community.public_url(),
|
2024-01-27 12:22:35 +13:00
|
|
|
'contentMap': {
|
|
|
|
'en': post_reply.body_html
|
2024-05-09 17:54:30 +12:00
|
|
|
},
|
|
|
|
'language': {
|
|
|
|
'identifier': post_reply.language_code(),
|
|
|
|
'name': post_reply.language_name()
|
2024-01-27 12:22:35 +13:00
|
|
|
}
|
|
|
|
}
|
|
|
|
update_json = {
|
2024-03-26 23:17:19 +00:00
|
|
|
'@context': default_context(),
|
2024-01-27 12:22:35 +13:00
|
|
|
'type': 'Update',
|
2024-03-26 23:17:19 +00:00
|
|
|
'actor': current_user.public_url(),
|
|
|
|
'audience': post.community.public_url(),
|
|
|
|
'to': [
|
|
|
|
'https://www.w3.org/ns/activitystreams#Public'
|
|
|
|
],
|
2024-01-27 12:22:35 +13:00
|
|
|
'cc': [
|
2024-03-26 23:17:19 +00:00
|
|
|
post.community.public_url(),
|
|
|
|
in_reply_to.author.public_url()
|
2024-01-27 12:22:35 +13:00
|
|
|
],
|
|
|
|
'object': reply_json,
|
2024-03-26 23:17:19 +00:00
|
|
|
'id': f"https://{current_app.config['SERVER_NAME']}/activities/update/{gibberish(15)}"
|
2024-01-17 20:48:35 +13:00
|
|
|
}
|
2024-03-26 23:17:19 +00:00
|
|
|
if in_reply_to.notify_author and in_reply_to.author.ap_id is not None:
|
|
|
|
reply_json['tag'] = [
|
|
|
|
{
|
|
|
|
'href': in_reply_to.author.public_url(),
|
|
|
|
'name': in_reply_to.author.mention_tag(),
|
|
|
|
'type': 'Mention'
|
|
|
|
}
|
|
|
|
]
|
|
|
|
update_json['tag'] = [
|
|
|
|
{
|
|
|
|
'href': in_reply_to.author.public_url(),
|
|
|
|
'name': in_reply_to.author.mention_tag(),
|
|
|
|
'type': 'Mention'
|
|
|
|
}
|
|
|
|
]
|
|
|
|
if not post.community.is_local(): # this is a remote community, send it to the instance that hosts it
|
2024-01-27 12:22:35 +13:00
|
|
|
success = post_request(post.community.ap_inbox_url, update_json, current_user.private_key,
|
2024-06-05 13:21:41 +12:00
|
|
|
current_user.public_url() + '#main-key')
|
2024-08-30 17:25:37 +12:00
|
|
|
if success is False or isinstance(success, str):
|
2024-03-26 23:17:19 +00:00
|
|
|
flash('Failed to send send edit to remote server', 'error')
|
|
|
|
else: # local community - send it to followers on remote instances
|
2024-01-27 12:22:35 +13:00
|
|
|
announce = {
|
|
|
|
"id": f"https://{current_app.config['SERVER_NAME']}/activities/announce/{gibberish(15)}",
|
|
|
|
"type": 'Announce',
|
|
|
|
"to": [
|
|
|
|
"https://www.w3.org/ns/activitystreams#Public"
|
|
|
|
],
|
2024-03-26 23:17:19 +00:00
|
|
|
"actor": post.community.public_url(),
|
2024-01-27 12:22:35 +13:00
|
|
|
"cc": [
|
|
|
|
post.community.ap_followers_url
|
|
|
|
],
|
|
|
|
'@context': default_context(),
|
|
|
|
'object': update_json
|
|
|
|
}
|
|
|
|
|
|
|
|
for instance in post.community.following_instances():
|
2024-03-26 23:17:19 +00:00
|
|
|
if instance.inbox and not current_user.has_blocked_instance(instance.id) and not instance_banned(instance.domain):
|
2024-01-27 12:22:35 +13:00
|
|
|
send_to_remote_instance(instance.id, post.community.id, announce)
|
2024-03-26 23:17:19 +00:00
|
|
|
|
|
|
|
# send copy of Note to post author (who won't otherwise get it if no-one else on their instance is subscribed to the community)
|
|
|
|
if not in_reply_to.author.is_local() and in_reply_to.author.ap_domain != post_reply.community.ap_domain:
|
|
|
|
if not post.community.is_local() or (post.community.is_local and not post.community.has_followers_from_domain(in_reply_to.author.ap_domain)):
|
|
|
|
success = post_request(in_reply_to.author.ap_inbox_url, update_json, current_user.private_key,
|
2024-06-05 13:21:41 +12:00
|
|
|
current_user.public_url() + '#main-key')
|
2024-08-30 17:25:37 +12:00
|
|
|
if success is False or isinstance(success, str):
|
2024-03-26 23:17:19 +00:00
|
|
|
# sending to shared inbox is good enough for Mastodon, but Lemmy will reject it the local community has no followers
|
|
|
|
personal_inbox = in_reply_to.author.public_url() + '/inbox'
|
|
|
|
post_request(personal_inbox, update_json, current_user.private_key,
|
2024-06-05 13:21:41 +12:00
|
|
|
current_user.public_url() + '#main-key')
|
2024-03-26 23:17:19 +00:00
|
|
|
|
2023-12-28 20:00:07 +13:00
|
|
|
return redirect(url_for('activitypub.post_ap', post_id=post.id))
|
|
|
|
else:
|
|
|
|
form.body.data = post_reply.body
|
2024-01-14 12:47:49 +13:00
|
|
|
form.notify_author.data = post_reply.notify_author
|
2024-05-09 17:54:30 +12:00
|
|
|
form.language_id.data = post_reply.language_id
|
2023-12-28 20:00:07 +13:00
|
|
|
return render_template('post/post_reply_edit.html', title=_('Edit comment'), form=form, post=post, post_reply=post_reply,
|
2024-02-26 21:26:19 +13:00
|
|
|
comment=comment, markdown_editor=current_user.markdown_editor, moderating_communities=moderating_communities(current_user.get_id()),
|
2024-05-30 21:54:25 +12:00
|
|
|
joined_communities=joined_communities(current_user.get_id()), menu_topics=menu_topics(),
|
|
|
|
community=post.community, site=g.site,
|
2024-04-15 12:54:27 +01:00
|
|
|
SUBSCRIPTION_OWNER=SUBSCRIPTION_OWNER, SUBSCRIPTION_MODERATOR=SUBSCRIPTION_MODERATOR,
|
2024-07-12 19:56:57 +08:00
|
|
|
inoculation=inoculation[randint(0, len(inoculation) - 1)] if g.site.show_inoculation_block else None)
|
2023-12-28 20:00:07 +13:00
|
|
|
else:
|
|
|
|
abort(401)
|
|
|
|
|
|
|
|
|
|
|
|
@bp.route('/post/<int:post_id>/comment/<int:comment_id>/delete', methods=['GET', 'POST'])
|
|
|
|
@login_required
|
|
|
|
def post_reply_delete(post_id: int, comment_id: int):
|
|
|
|
post = Post.query.get_or_404(post_id)
|
|
|
|
post_reply = PostReply.query.get_or_404(comment_id)
|
|
|
|
community = post.community
|
2024-04-15 12:16:29 +12:00
|
|
|
if post_reply.user_id == current_user.id or community.is_moderator() or current_user.is_admin():
|
2023-12-28 20:00:07 +13:00
|
|
|
if post_reply.has_replies():
|
|
|
|
post_reply.body = 'Deleted by author' if post_reply.author.id == current_user.id else 'Deleted by moderator'
|
|
|
|
post_reply.body_html = markdown_to_html(post_reply.body)
|
|
|
|
else:
|
|
|
|
post_reply.delete_dependencies()
|
2024-06-02 16:45:21 +12:00
|
|
|
post_reply.deleted = True
|
2023-12-28 20:00:07 +13:00
|
|
|
g.site.last_active = community.last_active = utcnow()
|
2024-09-20 23:14:57 +00:00
|
|
|
if not post_reply.author.bot:
|
|
|
|
post.reply_count -= 1
|
2024-09-13 11:08:04 +12:00
|
|
|
post_reply.author.post_reply_count -= 1
|
2023-12-28 20:00:07 +13:00
|
|
|
db.session.commit()
|
|
|
|
flash(_('Comment deleted.'))
|
2024-01-17 21:02:28 +13:00
|
|
|
# federate delete
|
2024-01-27 12:22:35 +13:00
|
|
|
if not post.community.local_only:
|
|
|
|
delete_json = {
|
|
|
|
'id': f"https://{current_app.config['SERVER_NAME']}/activities/delete/{gibberish(15)}",
|
|
|
|
'type': 'Delete',
|
2024-06-05 13:21:41 +12:00
|
|
|
'actor': current_user.public_url(),
|
|
|
|
'audience': post.community.public_url(),
|
|
|
|
'to': [post.community.public_url(), 'https://www.w3.org/ns/activitystreams#Public'],
|
2024-01-27 12:22:35 +13:00
|
|
|
'published': ap_datetime(utcnow()),
|
|
|
|
'cc': [
|
|
|
|
current_user.followers_url()
|
2024-01-17 21:02:28 +13:00
|
|
|
],
|
2024-01-27 12:22:35 +13:00
|
|
|
'object': post_reply.ap_id,
|
2024-01-17 21:02:28 +13:00
|
|
|
}
|
2024-04-20 20:09:52 +12:00
|
|
|
if post_reply.user_id != current_user.id:
|
|
|
|
delete_json['summary'] = 'Deleted by mod'
|
2024-01-17 21:02:28 +13:00
|
|
|
|
2024-01-27 12:22:35 +13:00
|
|
|
if not post.community.is_local(): # this is a remote community, send it to the instance that hosts it
|
|
|
|
success = post_request(post.community.ap_inbox_url, delete_json, current_user.private_key,
|
2024-06-05 13:21:41 +12:00
|
|
|
current_user.public_url() + '#main-key')
|
2024-08-30 17:25:37 +12:00
|
|
|
if success is False or isinstance(success, str):
|
2024-01-27 12:22:35 +13:00
|
|
|
flash('Failed to send delete to remote server', 'error')
|
|
|
|
else: # local community - send it to followers on remote instances
|
|
|
|
announce = {
|
|
|
|
"id": f"https://{current_app.config['SERVER_NAME']}/activities/announce/{gibberish(15)}",
|
|
|
|
"type": 'Announce',
|
|
|
|
"to": [
|
|
|
|
"https://www.w3.org/ns/activitystreams#Public"
|
|
|
|
],
|
|
|
|
"actor": post.community.ap_profile_id,
|
|
|
|
"cc": [
|
|
|
|
post.community.ap_followers_url
|
|
|
|
],
|
|
|
|
'@context': default_context(),
|
|
|
|
'object': delete_json
|
|
|
|
}
|
|
|
|
|
|
|
|
for instance in post.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, post.community.id, announce)
|
2024-01-17 21:02:28 +13:00
|
|
|
|
2024-07-07 15:01:52 +08:00
|
|
|
if post_reply.user_id != current_user.id:
|
|
|
|
add_to_modlog('delete_post_reply', community_id=post.community.id, link_text=f'comment on {shorten_string(post.title)}',
|
|
|
|
link=f'post/{post.id}#comment_{post_reply.id}')
|
|
|
|
|
2024-01-10 09:34:58 +13:00
|
|
|
return redirect(url_for('activitypub.post_ap', post_id=post.id))
|
|
|
|
|
|
|
|
|
|
|
|
@bp.route('/post/<int:post_id>/notification', methods=['GET', 'POST'])
|
|
|
|
@login_required
|
|
|
|
def post_notification(post_id: int):
|
2024-04-29 21:43:37 +12:00
|
|
|
# Toggle whether the current user is subscribed to notifications about top-level replies to this post or not
|
2024-01-10 09:34:58 +13:00
|
|
|
post = Post.query.get_or_404(post_id)
|
2024-04-29 21:43:37 +12:00
|
|
|
existing_notification = NotificationSubscription.query.filter(NotificationSubscription.entity_id == post.id,
|
|
|
|
NotificationSubscription.user_id == current_user.id,
|
|
|
|
NotificationSubscription.type == NOTIF_POST).first()
|
|
|
|
if existing_notification:
|
|
|
|
db.session.delete(existing_notification)
|
|
|
|
db.session.commit()
|
|
|
|
else: # no subscription yet, so make one
|
|
|
|
new_notification = NotificationSubscription(name=shorten_string(_('Replies to my post %(post_title)s',
|
|
|
|
post_title=post.title)),
|
|
|
|
user_id=current_user.id, entity_id=post.id,
|
|
|
|
type=NOTIF_POST)
|
|
|
|
db.session.add(new_notification)
|
2024-01-10 09:34:58 +13:00
|
|
|
db.session.commit()
|
2024-04-29 21:43:37 +12:00
|
|
|
|
2024-01-10 09:34:58 +13:00
|
|
|
return render_template('post/_post_notification_toggle.html', post=post)
|
|
|
|
|
|
|
|
|
|
|
|
@bp.route('/post_reply/<int:post_reply_id>/notification', methods=['GET', 'POST'])
|
|
|
|
@login_required
|
|
|
|
def post_reply_notification(post_reply_id: int):
|
2024-04-29 21:43:37 +12:00
|
|
|
# Toggle whether the current user is subscribed to notifications about replies to this reply or not
|
2024-01-10 09:34:58 +13:00
|
|
|
post_reply = PostReply.query.get_or_404(post_reply_id)
|
2024-04-29 21:43:37 +12:00
|
|
|
existing_notification = NotificationSubscription.query.filter(NotificationSubscription.entity_id == post_reply.id,
|
|
|
|
NotificationSubscription.user_id == current_user.id,
|
|
|
|
NotificationSubscription.type == NOTIF_REPLY).first()
|
|
|
|
if existing_notification:
|
|
|
|
db.session.delete(existing_notification)
|
|
|
|
db.session.commit()
|
|
|
|
else: # no subscription yet, so make one
|
|
|
|
new_notification = NotificationSubscription(name=shorten_string(_('Replies to my comment on %(post_title)s',
|
|
|
|
post_title=post_reply.post.title)), user_id=current_user.id, entity_id=post_reply.id,
|
|
|
|
type=NOTIF_REPLY)
|
|
|
|
db.session.add(new_notification)
|
2024-01-10 09:34:58 +13:00
|
|
|
db.session.commit()
|
2024-04-29 21:43:37 +12:00
|
|
|
|
2024-01-10 09:34:58 +13:00
|
|
|
return render_template('post/_reply_notification_toggle.html', comment={'comment': post_reply})
|
2024-04-02 04:37:48 +01:00
|
|
|
|
|
|
|
|
|
|
|
@bp.route('/post/<int:post_id>/cross_posts', methods=['GET'])
|
|
|
|
def post_cross_posts(post_id: int):
|
|
|
|
post = Post.query.get_or_404(post_id)
|
|
|
|
cross_posts = Post.query.filter(Post.id.in_(post.cross_posts)).all()
|
|
|
|
return render_template('post/post_cross_posts.html', post=post, cross_posts=cross_posts)
|
2024-09-22 22:32:42 +00:00
|
|
|
|
|
|
|
|
|
|
|
@bp.route('/post/<int:post_id>/voting_activity', methods=['GET'])
|
|
|
|
@login_required
|
2024-09-24 09:28:06 +12:00
|
|
|
@permission_required('change instance settings')
|
2024-09-22 22:32:42 +00:00
|
|
|
def post_view_voting_activity(post_id: int):
|
|
|
|
post = Post.query.get_or_404(post_id)
|
|
|
|
|
|
|
|
post_title=post.title
|
|
|
|
upvoters = User.query.join(PostVote, PostVote.user_id == User.id).filter_by(post_id=post_id, effect=1.0).order_by(User.ap_domain, User.user_name)
|
|
|
|
downvoters = User.query.join(PostVote, PostVote.user_id == User.id).filter_by(post_id=post_id, effect=-1.0).order_by(User.ap_domain, User.user_name)
|
|
|
|
|
|
|
|
# local users will be at the bottom of each list as ap_domain is empty for those.
|
|
|
|
|
|
|
|
return render_template('post/post_voting_activity.html', title=_('Voting Activity'),
|
|
|
|
post_title=post_title, upvoters=upvoters, downvoters=downvoters,
|
|
|
|
moderating_communities=moderating_communities(current_user.get_id()),
|
|
|
|
joined_communities=joined_communities(current_user.get_id()),
|
|
|
|
menu_topics=menu_topics(), site=g.site
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@bp.route('/comment/<int:comment_id>/voting_activity', methods=['GET'])
|
|
|
|
@login_required
|
2024-09-24 09:28:06 +12:00
|
|
|
@permission_required('change instance settings')
|
2024-09-22 22:32:42 +00:00
|
|
|
def post_reply_view_voting_activity(comment_id: int):
|
|
|
|
post_reply = PostReply.query.get_or_404(comment_id)
|
|
|
|
|
|
|
|
reply_text=post_reply.body
|
|
|
|
upvoters = User.query.join(PostReplyVote, PostReplyVote.user_id == User.id).filter_by(post_reply_id=comment_id, effect=1.0).order_by(User.ap_domain, User.user_name)
|
|
|
|
downvoters = User.query.join(PostReplyVote, PostReplyVote.user_id == User.id).filter_by(post_reply_id=comment_id, effect=-1.0).order_by(User.ap_domain, User.user_name)
|
|
|
|
|
|
|
|
# local users will be at the bottom of each list as ap_domain is empty for those.
|
|
|
|
|
|
|
|
return render_template('post/post_reply_voting_activity.html', title=_('Voting Activity'),
|
|
|
|
reply_text=reply_text, upvoters=upvoters, downvoters=downvoters,
|
|
|
|
moderating_communities=moderating_communities(current_user.get_id()),
|
|
|
|
joined_communities=joined_communities(current_user.get_id()),
|
|
|
|
menu_topics=menu_topics(), site=g.site
|
|
|
|
)
|