2025-01-17 17:20:15 +00:00
|
|
|
from app import db, cache
|
|
|
|
from app.activitypub.util import make_image_sizes
|
2024-09-26 16:01:55 +00:00
|
|
|
from app.constants import *
|
2025-01-17 17:20:15 +00:00
|
|
|
from app.community.util import tags_from_string_old, end_poll_date
|
|
|
|
from app.models import File, Language, NotificationSubscription, Poll, PollChoice, Post, PostBookmark, PostVote, utcnow
|
2024-11-02 23:56:56 +00:00
|
|
|
from app.shared.tasks import task_selector
|
2025-01-15 01:26:27 +00:00
|
|
|
from app.utils import render_template, authorise_api_user, shorten_string, gibberish, ensure_directory_exists, \
|
|
|
|
piefed_markdown_to_lemmy_markdown, markdown_to_html, remove_tracking_from_link, domain_from_url, \
|
2025-01-17 17:20:15 +00:00
|
|
|
opengraph_parse, url_to_thumbnail_file, can_create_post, is_video_hosting_site, recently_upvoted_posts, \
|
|
|
|
is_image_url, is_video_hosting_site
|
2024-09-20 16:06:08 +00:00
|
|
|
|
2025-01-15 01:26:27 +00:00
|
|
|
from flask import abort, flash, redirect, request, url_for, current_app, g
|
2024-09-23 12:40:27 +00:00
|
|
|
from flask_babel import _
|
2024-09-20 16:06:08 +00:00
|
|
|
from flask_login import current_user
|
|
|
|
|
2025-01-14 13:54:17 +00:00
|
|
|
from pillow_heif import register_heif_opener
|
|
|
|
from PIL import Image, ImageOps
|
|
|
|
|
2025-01-15 01:26:27 +00:00
|
|
|
from sqlalchemy import text
|
|
|
|
|
2025-01-14 13:54:17 +00:00
|
|
|
import os
|
2024-09-20 16:06:08 +00:00
|
|
|
|
|
|
|
# would be in app/constants.py
|
|
|
|
SRC_WEB = 1
|
|
|
|
SRC_PUB = 2
|
|
|
|
SRC_API = 3
|
|
|
|
|
|
|
|
# function can be shared between WEB and API (only API calls it for now)
|
|
|
|
# post_vote in app/post/routes would just need to do 'return vote_for_post(post_id, vote_direction, SRC_WEB)'
|
|
|
|
|
|
|
|
def vote_for_post(post_id: int, vote_direction, src, auth=None):
|
2024-10-05 05:53:53 +00:00
|
|
|
if src == SRC_API:
|
2024-10-27 13:36:17 +00:00
|
|
|
post = Post.query.filter_by(id=post_id).one()
|
2024-10-08 03:26:40 +00:00
|
|
|
user = authorise_api_user(auth, return_type='model')
|
2024-09-20 16:06:08 +00:00
|
|
|
else:
|
|
|
|
post = Post.query.get_or_404(post_id)
|
|
|
|
user = current_user
|
|
|
|
|
|
|
|
undo = post.vote(user, vote_direction)
|
|
|
|
|
2024-11-02 23:56:56 +00:00
|
|
|
task_selector('vote_for_post', user_id=user.id, post_id=post_id, vote_to_undo=undo, vote_direction=vote_direction)
|
2024-09-20 16:06:08 +00:00
|
|
|
|
|
|
|
if src == SRC_API:
|
|
|
|
return user.id
|
|
|
|
else:
|
|
|
|
recently_upvoted = []
|
|
|
|
recently_downvoted = []
|
|
|
|
if vote_direction == 'upvote' and undo is None:
|
|
|
|
recently_upvoted = [post_id]
|
|
|
|
elif vote_direction == 'downvote' and undo is None:
|
|
|
|
recently_downvoted = [post_id]
|
|
|
|
|
|
|
|
template = 'post/_post_voting_buttons.html' if request.args.get('style', '') == '' else 'post/_post_voting_buttons_masonry.html'
|
|
|
|
return render_template(template, post=post, community=post.community, recently_upvoted=recently_upvoted,
|
|
|
|
recently_downvoted=recently_downvoted)
|
2024-09-23 12:40:27 +00:00
|
|
|
|
|
|
|
|
|
|
|
# function can be shared between WEB and API (only API calls it for now)
|
|
|
|
# post_bookmark in app/post/routes would just need to do 'return bookmark_the_post(post_id, SRC_WEB)'
|
|
|
|
def bookmark_the_post(post_id: int, src, auth=None):
|
2024-10-05 05:53:53 +00:00
|
|
|
if src == SRC_API:
|
2024-10-27 13:36:17 +00:00
|
|
|
post = Post.query.filter_by(id=post_id, deleted=False).one()
|
2024-10-08 03:26:40 +00:00
|
|
|
user_id = authorise_api_user(auth)
|
2024-09-23 12:40:27 +00:00
|
|
|
else:
|
|
|
|
post = Post.query.get_or_404(post_id)
|
|
|
|
if post.deleted:
|
|
|
|
abort(404)
|
|
|
|
user_id = current_user.id
|
|
|
|
|
|
|
|
existing_bookmark = PostBookmark.query.filter(PostBookmark.post_id == post_id, PostBookmark.user_id == user_id).first()
|
|
|
|
if not existing_bookmark:
|
|
|
|
db.session.add(PostBookmark(post_id=post_id, user_id=user_id))
|
|
|
|
db.session.commit()
|
|
|
|
if src == SRC_WEB:
|
|
|
|
flash(_('Bookmark added.'))
|
|
|
|
else:
|
|
|
|
if src == SRC_WEB:
|
|
|
|
flash(_('This post has already been bookmarked.'))
|
|
|
|
|
|
|
|
if src == SRC_API:
|
|
|
|
return user_id
|
|
|
|
else:
|
|
|
|
return redirect(url_for('activitypub.post_ap', post_id=post.id))
|
|
|
|
|
|
|
|
|
|
|
|
# function can be shared between WEB and API (only API calls it for now)
|
|
|
|
# post_remove_bookmark in app/post/routes would just need to do 'return remove_the_bookmark_from_post(post_id, SRC_WEB)'
|
|
|
|
def remove_the_bookmark_from_post(post_id: int, src, auth=None):
|
2024-10-05 05:53:53 +00:00
|
|
|
if src == SRC_API:
|
2024-10-27 13:36:17 +00:00
|
|
|
post = Post.query.filter_by(id=post_id, deleted=False).one()
|
2024-10-08 03:26:40 +00:00
|
|
|
user_id = authorise_api_user(auth)
|
2024-09-23 12:40:27 +00:00
|
|
|
else:
|
|
|
|
post = Post.query.get_or_404(post_id)
|
|
|
|
if post.deleted:
|
|
|
|
abort(404)
|
|
|
|
user_id = current_user.id
|
|
|
|
|
|
|
|
existing_bookmark = PostBookmark.query.filter(PostBookmark.post_id == post_id, PostBookmark.user_id == user_id).first()
|
|
|
|
if existing_bookmark:
|
|
|
|
db.session.delete(existing_bookmark)
|
|
|
|
db.session.commit()
|
|
|
|
if src == SRC_WEB:
|
|
|
|
flash(_('Bookmark has been removed.'))
|
|
|
|
|
|
|
|
if src == SRC_API:
|
|
|
|
return user_id
|
|
|
|
else:
|
|
|
|
return redirect(url_for('activitypub.post_ap', post_id=post.id))
|
|
|
|
|
|
|
|
|
2024-09-26 16:01:55 +00:00
|
|
|
|
|
|
|
# function can be shared between WEB and API (only API calls it for now)
|
|
|
|
# post_notification in app/post/routes would just need to do 'return toggle_post_notification(post_id, SRC_WEB)'
|
|
|
|
def toggle_post_notification(post_id: int, src, auth=None):
|
|
|
|
# Toggle whether the current user is subscribed to notifications about top-level replies to this post or not
|
2024-10-05 05:53:53 +00:00
|
|
|
if src == SRC_API:
|
2024-10-27 13:36:17 +00:00
|
|
|
post = Post.query.filter_by(id=post_id, deleted=False).one()
|
2024-10-08 03:26:40 +00:00
|
|
|
user_id = authorise_api_user(auth)
|
2024-09-26 16:01:55 +00:00
|
|
|
else:
|
|
|
|
post = Post.query.get_or_404(post_id)
|
|
|
|
if post.deleted:
|
|
|
|
abort(404)
|
|
|
|
user_id = current_user.id
|
|
|
|
|
|
|
|
existing_notification = NotificationSubscription.query.filter(NotificationSubscription.entity_id == post.id,
|
|
|
|
NotificationSubscription.user_id == 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
|
2024-10-27 13:36:17 +00:00
|
|
|
new_notification = NotificationSubscription(name=shorten_string(_('Replies to my post %(post_title)s', post_title=post.title)),
|
|
|
|
user_id=user_id, entity_id=post.id, type=NOTIF_POST)
|
2024-09-26 16:01:55 +00:00
|
|
|
db.session.add(new_notification)
|
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
if src == SRC_API:
|
|
|
|
return user_id
|
|
|
|
else:
|
|
|
|
return render_template('post/_post_notification_toggle.html', post=post)
|
2025-01-14 13:54:17 +00:00
|
|
|
|
|
|
|
|
|
|
|
def make_post(input, community, type, src, auth=None, uploaded_file=None):
|
|
|
|
if src == SRC_API:
|
|
|
|
user = authorise_api_user(auth, return_type='model')
|
|
|
|
#if not basic_rate_limit_check(user):
|
|
|
|
# raise Exception('rate_limited')
|
|
|
|
title = input['title']
|
|
|
|
url = input['url']
|
|
|
|
language_id = input['language_id']
|
|
|
|
else:
|
|
|
|
user = current_user
|
2025-01-17 17:20:15 +00:00
|
|
|
title = input.title.data.strip()
|
|
|
|
if type == POST_TYPE_LINK:
|
|
|
|
url = input.link_url.data.strip()
|
|
|
|
elif type == POST_TYPE_VIDEO:
|
|
|
|
url = input.video_url.data.strip()
|
|
|
|
else:
|
|
|
|
url = None
|
2025-01-14 13:54:17 +00:00
|
|
|
language_id = input.language_id.data
|
|
|
|
|
2025-01-17 17:20:15 +00:00
|
|
|
# taking values from one JSON to put in another JSON to put in a DB to put in another JSON feels bad
|
|
|
|
# instead, make_post shares code with edit_post
|
|
|
|
# ideally, a similar change could be made for incoming activitypub (create_post() and update_post_from_activity() could share code)
|
|
|
|
# once this happens, and post.new() just does the minimum before being passed off to an update function, post.new() can be used here again.
|
2025-01-14 13:54:17 +00:00
|
|
|
|
2025-01-17 17:20:15 +00:00
|
|
|
if not can_create_post(user, community):
|
|
|
|
raise Exception('You are not permitted to make posts in this community')
|
2025-01-14 13:54:17 +00:00
|
|
|
|
2025-01-17 17:20:15 +00:00
|
|
|
if url:
|
|
|
|
domain = domain_from_url(url)
|
|
|
|
if domain:
|
|
|
|
if domain.banned or domain.name.endswith('.pages.dev'):
|
|
|
|
raise Exception(domain.name + ' is blocked by admin')
|
2025-01-14 13:54:17 +00:00
|
|
|
|
2025-01-17 17:20:15 +00:00
|
|
|
if uploaded_file and uploaded_file.filename != '':
|
|
|
|
# check if this is an allowed type of file
|
|
|
|
allowed_extensions = ['.gif', '.jpg', '.jpeg', '.png', '.webp', '.heic', '.mpo', '.avif', '.svg']
|
|
|
|
file_ext = os.path.splitext(uploaded_file.filename)[1]
|
|
|
|
if file_ext.lower() not in allowed_extensions:
|
|
|
|
raise Exception('filetype not allowed')
|
2025-01-14 13:54:17 +00:00
|
|
|
|
2025-01-17 17:20:15 +00:00
|
|
|
post = Post(user_id=user.id, community_id=community.id, instance_id=user.instance_id, posted_at=utcnow(),
|
|
|
|
ap_id=gibberish(), title=title, language_id=language_id)
|
|
|
|
db.session.add(post)
|
|
|
|
db.session.commit()
|
2025-01-14 13:54:17 +00:00
|
|
|
|
2025-01-17 17:20:15 +00:00
|
|
|
post.up_votes = 1
|
|
|
|
if user.reputation > 100:
|
|
|
|
post.up_votes += 1
|
|
|
|
effect = user.instance.vote_weight
|
|
|
|
post.score = post.up_votes * effect
|
|
|
|
post.ranking = post.post_ranking(post.score, post.posted_at)
|
|
|
|
cache.delete_memoized(recently_upvoted_posts, user.id)
|
|
|
|
|
|
|
|
community.post_count += 1
|
|
|
|
community.last_active = g.site.last_active = utcnow()
|
|
|
|
user.post_count += 1
|
2025-01-14 13:54:17 +00:00
|
|
|
|
|
|
|
post.ap_id = f"https://{current_app.config['SERVER_NAME']}/post/{post.id}"
|
2025-01-17 17:20:15 +00:00
|
|
|
vote = PostVote(user_id=user.id, post_id=post.id, author_id=user.id, effect=1)
|
|
|
|
db.session.add(vote)
|
2025-01-14 13:54:17 +00:00
|
|
|
db.session.commit()
|
|
|
|
|
2025-01-17 17:20:15 +00:00
|
|
|
post = edit_post(input, post, type, src, user, auth, uploaded_file, from_scratch=True)
|
2025-01-14 13:54:17 +00:00
|
|
|
|
|
|
|
if src == SRC_API:
|
|
|
|
return user.id, post
|
|
|
|
else:
|
|
|
|
return post
|
|
|
|
|
|
|
|
|
2025-01-17 17:20:15 +00:00
|
|
|
# 'from_scratch == True' means that it's not really a user edit, we're just re-using code for make_post()
|
|
|
|
def edit_post(input, post, type, src, user=None, auth=None, uploaded_file=None, from_scratch=False):
|
2025-01-15 01:26:27 +00:00
|
|
|
if src == SRC_API:
|
2025-01-17 17:20:15 +00:00
|
|
|
if not user:
|
|
|
|
user = authorise_api_user(auth, return_type='model')
|
2025-01-15 01:26:27 +00:00
|
|
|
title = input['title']
|
|
|
|
body = input['body']
|
|
|
|
url = input['url']
|
|
|
|
nsfw = input['nsfw']
|
|
|
|
notify_author = input['notify_author']
|
|
|
|
language_id = input['language_id']
|
|
|
|
tags = []
|
|
|
|
else:
|
2025-01-17 17:20:15 +00:00
|
|
|
if not user:
|
|
|
|
user = current_user
|
|
|
|
title = input.title.data.strip()
|
2025-01-15 01:26:27 +00:00
|
|
|
body = input.body.data
|
2025-01-17 17:20:15 +00:00
|
|
|
if type == POST_TYPE_LINK:
|
|
|
|
url = input.link_url.data.strip()
|
|
|
|
elif type == POST_TYPE_VIDEO:
|
|
|
|
url = input.video_url.data.strip()
|
|
|
|
elif type == POST_TYPE_IMAGE and not from_scratch:
|
|
|
|
url = post.url
|
|
|
|
else:
|
|
|
|
url = None
|
2025-01-15 01:26:27 +00:00
|
|
|
nsfw = input.nsfw.data
|
|
|
|
notify_author = input.notify_author.data
|
|
|
|
language_id = input.language_id.data
|
2025-01-17 17:20:15 +00:00
|
|
|
tags = tags_from_string_old(input.tags.data)
|
2025-01-15 01:26:27 +00:00
|
|
|
|
|
|
|
post.indexable = user.indexable
|
|
|
|
post.sticky = False if src == SRC_API else input.sticky.data
|
|
|
|
post.nsfw = nsfw
|
|
|
|
post.nsfl = False if src == SRC_API else input.nsfl.data
|
|
|
|
post.notify_author = notify_author
|
|
|
|
post.language_id = language_id
|
|
|
|
user.language_id = language_id
|
2025-01-17 17:20:15 +00:00
|
|
|
post.title = title
|
2025-01-15 01:26:27 +00:00
|
|
|
post.body = piefed_markdown_to_lemmy_markdown(body)
|
|
|
|
post.body_html = markdown_to_html(post.body)
|
2025-01-17 17:20:15 +00:00
|
|
|
post.type = type
|
2025-01-15 01:26:27 +00:00
|
|
|
|
2025-01-17 17:20:15 +00:00
|
|
|
url_changed = False
|
2025-01-15 01:26:27 +00:00
|
|
|
|
2025-01-17 17:20:15 +00:00
|
|
|
if not from_scratch:
|
|
|
|
# Remove any subscription that currently exists
|
|
|
|
existing_notification = NotificationSubscription.query.filter(NotificationSubscription.entity_id == post.id,
|
|
|
|
NotificationSubscription.user_id == user.id,
|
|
|
|
NotificationSubscription.type == NOTIF_POST).first()
|
|
|
|
if existing_notification:
|
|
|
|
db.session.delete(existing_notification)
|
|
|
|
|
|
|
|
# Remove any poll votes that currently exists
|
|
|
|
# Partially because it's easier to code but also to stop malicious alterations to polls after people have already voted
|
|
|
|
db.session.execute(text('DELETE FROM "poll_choice_vote" WHERE post_id = :post_id'), {'post_id': post.id})
|
|
|
|
db.session.execute(text('DELETE FROM "poll_choice" WHERE post_id = :post_id'), {'post_id': post.id})
|
2025-01-15 01:26:27 +00:00
|
|
|
|
2025-01-17 17:20:15 +00:00
|
|
|
# Remove any old images, and set url_changed
|
|
|
|
if url != post.url or uploaded_file:
|
|
|
|
url_changed = True
|
2025-01-15 01:26:27 +00:00
|
|
|
if post.image_id:
|
|
|
|
remove_file = File.query.get(post.image_id)
|
2025-01-17 17:20:15 +00:00
|
|
|
if remove_file:
|
|
|
|
remove_file.delete_from_disk()
|
2025-01-15 01:26:27 +00:00
|
|
|
post.image_id = None
|
2025-01-18 00:35:20 +00:00
|
|
|
if post.url:
|
|
|
|
domain = domain_from_url(post.url)
|
|
|
|
if domain:
|
|
|
|
domain.post_count -= 1
|
2025-01-15 01:26:27 +00:00
|
|
|
|
2025-01-17 17:20:15 +00:00
|
|
|
# remove any old tags
|
|
|
|
db.session.execute(text('DELETE FROM "post_tag" WHERE post_id = :post_id'), {'post_id': post.id})
|
2025-01-15 01:26:27 +00:00
|
|
|
|
2025-01-17 17:20:15 +00:00
|
|
|
post.edited_at = utcnow()
|
2025-01-15 01:26:27 +00:00
|
|
|
|
2025-01-17 17:20:15 +00:00
|
|
|
db.session.commit()
|
2025-01-15 01:26:27 +00:00
|
|
|
|
2025-01-17 17:20:15 +00:00
|
|
|
if uploaded_file and uploaded_file.filename != '':
|
|
|
|
# check if this is an allowed type of file
|
|
|
|
allowed_extensions = ['.gif', '.jpg', '.jpeg', '.png', '.webp', '.heic', '.mpo', '.avif', '.svg']
|
|
|
|
file_ext = os.path.splitext(uploaded_file.filename)[1]
|
|
|
|
if file_ext.lower() not in allowed_extensions:
|
|
|
|
raise Exception('filetype not allowed')
|
2025-01-15 01:26:27 +00:00
|
|
|
|
2025-01-17 17:20:15 +00:00
|
|
|
new_filename = gibberish(15)
|
|
|
|
# set up the storage directory
|
|
|
|
directory = 'app/static/media/posts/' + new_filename[0:2] + '/' + new_filename[2:4]
|
|
|
|
ensure_directory_exists(directory)
|
2025-01-15 01:26:27 +00:00
|
|
|
|
2025-01-17 17:20:15 +00:00
|
|
|
# save the file
|
|
|
|
final_place = os.path.join(directory, new_filename + file_ext)
|
|
|
|
uploaded_file.seek(0)
|
|
|
|
uploaded_file.save(final_place)
|
2025-01-15 01:26:27 +00:00
|
|
|
|
2025-01-17 17:20:15 +00:00
|
|
|
if file_ext.lower() == '.heic':
|
|
|
|
register_heif_opener()
|
|
|
|
if file_ext.lower() == '.avif':
|
|
|
|
import pillow_avif
|
|
|
|
|
|
|
|
Image.MAX_IMAGE_PIXELS = 89478485
|
2025-01-15 01:26:27 +00:00
|
|
|
|
2025-01-17 17:20:15 +00:00
|
|
|
# limit full sized version to 2000px
|
|
|
|
if not final_place.endswith('.svg'):
|
2025-01-15 01:26:27 +00:00
|
|
|
img = Image.open(final_place)
|
|
|
|
if '.' + img.format.lower() in allowed_extensions:
|
|
|
|
img = ImageOps.exif_transpose(img)
|
|
|
|
|
|
|
|
img.thumbnail((2000, 2000))
|
|
|
|
img.save(final_place)
|
|
|
|
|
2025-01-17 17:20:15 +00:00
|
|
|
url = f"https://{current_app.config['SERVER_NAME']}/{final_place.replace('app/', '')}"
|
|
|
|
else:
|
|
|
|
raise Exception('filetype not allowed')
|
2025-01-15 01:26:27 +00:00
|
|
|
|
2025-01-17 17:20:15 +00:00
|
|
|
if url and (from_scratch or url_changed):
|
|
|
|
domain = domain_from_url(url)
|
|
|
|
if domain:
|
|
|
|
if domain.banned or domain.name.endswith('.pages.dev'):
|
|
|
|
raise Exception(domain.name + ' is blocked by admin')
|
|
|
|
post.domain = domain
|
|
|
|
domain.post_count += 1
|
|
|
|
already_notified = set() # often admins and mods are the same people - avoid notifying them twice
|
|
|
|
if domain.notify_mods:
|
|
|
|
for community_member in post.community.moderators():
|
|
|
|
if community_member.is_local():
|
|
|
|
notify = Notification(title='Suspicious content', url=post.ap_id, user_id=community_member.user_id, author_id=user.id)
|
|
|
|
db.session.add(notify)
|
|
|
|
already_notified.add(community_member.user_id)
|
|
|
|
if domain.notify_admins:
|
|
|
|
for admin in Site.admins():
|
|
|
|
if admin.id not in already_notified:
|
|
|
|
notify = Notification(title='Suspicious content', url=post.ap_id, user_id=admin.id, author_id=user.id)
|
|
|
|
db.session.add(notify)
|
|
|
|
if is_image_url(url):
|
|
|
|
file = File(source_url=url)
|
|
|
|
if uploaded_file and type == POST_TYPE_IMAGE:
|
|
|
|
# change this line when uploaded_file is supported in API
|
|
|
|
file.alt_text = input.image_alt_text.data if input.image_alt_text.data else title
|
|
|
|
db.session.add(file)
|
|
|
|
db.session.commit()
|
|
|
|
post.image_id = file.id
|
|
|
|
make_image_sizes(post.image_id, 170, 512, 'posts', post.community.low_quality)
|
|
|
|
post.url = url
|
|
|
|
post.type = POST_TYPE_IMAGE
|
|
|
|
else:
|
|
|
|
opengraph = opengraph_parse(url)
|
|
|
|
if opengraph and (opengraph.get('og:image', '') != '' or opengraph.get('og:image:url', '') != ''):
|
|
|
|
filename = opengraph.get('og:image') or opengraph.get('og:image:url')
|
|
|
|
if not filename.startswith('/'):
|
|
|
|
file = url_to_thumbnail_file(filename)
|
|
|
|
if file:
|
|
|
|
file.alt_text = shorten_string(opengraph.get('og:title'), 295)
|
|
|
|
post.image = file
|
|
|
|
db.session.add(file)
|
|
|
|
|
|
|
|
post.url = remove_tracking_from_link(url)
|
|
|
|
|
|
|
|
if url.endswith('.mp4') or url.endswith('.webm') or is_video_hosting_site(url):
|
|
|
|
post.type = POST_TYPE_VIDEO
|
2025-01-15 01:26:27 +00:00
|
|
|
else:
|
2025-01-17 17:20:15 +00:00
|
|
|
post.type = POST_TYPE_LINK
|
|
|
|
|
|
|
|
post.calculate_cross_posts(url_changed=url_changed)
|
2025-01-15 01:26:27 +00:00
|
|
|
|
2025-01-17 17:20:15 +00:00
|
|
|
federate = True
|
2025-01-15 01:26:27 +00:00
|
|
|
if type == POST_TYPE_POLL:
|
2025-01-17 17:20:15 +00:00
|
|
|
post.type = POST_TYPE_POLL
|
2025-01-15 01:26:27 +00:00
|
|
|
for i in range(1, 10):
|
2025-01-17 17:20:15 +00:00
|
|
|
# change this line when polls are supported in API
|
2025-01-15 01:26:27 +00:00
|
|
|
choice_data = getattr(input, f"choice_{i}").data.strip()
|
|
|
|
if choice_data != '':
|
|
|
|
db.session.add(PollChoice(post_id=post.id, choice_text=choice_data, sort_order=i))
|
|
|
|
|
|
|
|
poll = Poll.query.filter_by(post_id=post.id).first()
|
|
|
|
if poll is None:
|
|
|
|
poll = Poll(post_id=post.id)
|
|
|
|
db.session.add(poll)
|
|
|
|
poll.mode = input.mode.data
|
|
|
|
if input.finish_in:
|
|
|
|
poll.end_poll = end_poll_date(input.finish_in.data)
|
|
|
|
poll.local_only = input.local_only.data
|
|
|
|
poll.latest_vote = None
|
|
|
|
db.session.commit()
|
|
|
|
|
2025-01-17 17:20:15 +00:00
|
|
|
if poll.local_only:
|
|
|
|
federate = False
|
|
|
|
|
|
|
|
# add tags
|
|
|
|
post.tags = tags
|
2025-01-15 01:26:27 +00:00
|
|
|
|
|
|
|
# Add subscription if necessary
|
|
|
|
if notify_author:
|
2025-01-17 17:20:15 +00:00
|
|
|
new_notification = NotificationSubscription(name=post.title, user_id=user.id, entity_id=post.id, type=NOTIF_POST)
|
2025-01-15 01:26:27 +00:00
|
|
|
db.session.add(new_notification)
|
|
|
|
|
|
|
|
db.session.commit()
|
|
|
|
|
2025-01-17 17:20:15 +00:00
|
|
|
if from_scratch:
|
|
|
|
if federate:
|
|
|
|
task_selector('make_post', user_id=user.id, post_id=post.id)
|
|
|
|
elif federate:
|
|
|
|
task_selector('edit_post', user_id=user.id, post_id=post.id)
|
2025-01-15 01:26:27 +00:00
|
|
|
|
|
|
|
if src == SRC_API:
|
2025-01-17 17:20:15 +00:00
|
|
|
if from_scratch:
|
|
|
|
return post
|
|
|
|
else:
|
|
|
|
return user.id, post
|
|
|
|
elif from_scratch:
|
|
|
|
return post
|
|
|
|
|