refactor post-new wip

This commit is contained in:
rimu 2024-10-16 21:42:30 +13:00
parent cec844ec4f
commit feca5992af
3 changed files with 28 additions and 6 deletions

View file

@ -21,7 +21,7 @@ from app.community.forms import SearchRemoteCommunity, CreateDiscussionForm, Cre
EditCommunityWikiPageForm
from app.community.util import search_for_community, actor_to_community, \
save_post, save_icon_file, save_banner_file, send_to_remote_instance, \
delete_post_from_community, delete_post_reply_from_community, community_in_list, find_local_users
delete_post_from_community, delete_post_reply_from_community, community_in_list, find_local_users, tags_from_string
from app.constants import SUBSCRIPTION_MEMBER, SUBSCRIPTION_OWNER, POST_TYPE_LINK, POST_TYPE_ARTICLE, POST_TYPE_IMAGE, \
SUBSCRIPTION_PENDING, SUBSCRIPTION_MODERATOR, REPORT_STATE_NEW, REPORT_STATE_ESCALATED, REPORT_STATE_RESOLVED, \
REPORT_STATE_DISCARDED, POST_TYPE_VIDEO, NOTIF_COMMUNITY, POST_TYPE_POLL, MICROBLOG_APPS
@ -624,6 +624,8 @@ def add_post(actor, type):
if not can_create_post(current_user, community):
abort(401)
language = Language.query.get(form.language_id.data)
request_json = {
'id': None,
'object': {
@ -634,13 +636,33 @@ def add_post(actor, type):
'id': gibberish(), # this will be updated once we have the post.id
'mediaType': 'text/markdown',
'content': form.body.data,
'tag': tags_from_string(form.tags.data),
'language': {'identifier': language.code, 'name': language.name}
}
}
if type == 'link':
request_json['object']['attachment'] = {'type': 'Link', 'href': form.link_url.data}
elif type == 'image':
request_json['object']['attachment'] = {'type': 'Image', 'url': image_url, 'name': form.image_alt_text}
elif type == 'video':
request_json['object']['attachment'] = {'type': 'Document', 'url': form.video_url.data}
elif type == 'poll':
request_json['object']['type'] = 'Question'
choices = [form.choice_1, form.choice_2, form.choice_3, form.choice_4, form.choice_5,
form.choice_6, form.choice_7, form.choice_8, form.choice_9, form.choice_10]
key = 'oneOf' if form.mode.data == 'single' else 'anyOf'
request_json['object'][key] = []
for choice in choices:
choice_data = choice.data.strip()
if choice_data:
request_json['object'][key].append({'name': choice_data})
# todo: add try..except
post = Post.new(current_user, community, request_json)
community.post_count += 1
current_user.post_count += 1
current_user.language_id = form.language_id.data
community.last_active = g.site.last_active = utcnow()
post.ap_id = f"https://{current_app.config['SERVER_NAME']}/post/{post.id}"
db.session.commit()

View file

@ -484,7 +484,7 @@ def end_poll_date(end_choice):
raise ValueError("Invalid choice")
def tags_from_string(tags: str) -> List[Tag]:
def tags_from_string(tags: str) -> List[dict]:
return_value = []
tags = tags.strip()
if tags == '':
@ -496,7 +496,7 @@ def tags_from_string(tags: str) -> List[Tag]:
tag = tag[1:]
tag_to_append = find_hashtag_or_create(tag)
if tag_to_append:
return_value.append(tag_to_append)
return_value.append({'type': 'Hashtag', 'name': tag_to_append.name})
return return_value

View file

@ -635,7 +635,7 @@ class User(UserMixin, db.Model):
verification_token = db.Column(db.String(16), index=True)
banned = db.Column(db.Boolean, default=False)
deleted = db.Column(db.Boolean, default=False)
deleted_by = db.Column(db.Integer, db.ForeignKey('user.id'), index=True)
deleted_by = db.Column(db.Integer, index=True)
about = db.Column(db.Text) # markdown
about_html = db.Column(db.Text) # html
keywords = db.Column(db.String(256))
@ -1079,7 +1079,7 @@ class Post(db.Model):
microblog = db.Column(db.Boolean, default=False)
comments_enabled = db.Column(db.Boolean, default=True)
deleted = db.Column(db.Boolean, default=False, index=True)
deleted_by = db.Column(db.Integer, db.ForeignKey('user.id'), index=True)
deleted_by = db.Column(db.Integer, index=True)
mea_culpa = db.Column(db.Boolean, default=False)
has_embed = db.Column(db.Boolean, default=False)
reply_count = db.Column(db.Integer, default=0)
@ -1602,7 +1602,7 @@ class PostReply(db.Model):
created_at = db.Column(db.DateTime, index=True, default=utcnow)
posted_at = db.Column(db.DateTime, index=True, default=utcnow)
deleted = db.Column(db.Boolean, default=False, index=True)
deleted_by = db.Column(db.Integer, db.ForeignKey('user.id'), index=True)
deleted_by = db.Column(db.Integer, index=True)
ip = db.Column(db.String(50))
from_bot = db.Column(db.Boolean, default=False)
up_votes = db.Column(db.Integer, default=0)