diff --git a/app/community/routes.py b/app/community/routes.py index 89857667..0e4811e3 100644 --- a/app/community/routes.py +++ b/app/community/routes.py @@ -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() diff --git a/app/community/util.py b/app/community/util.py index b865d48c..59376fd7 100644 --- a/app/community/util.py +++ b/app/community/util.py @@ -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 diff --git a/app/models.py b/app/models.py index 88de3df8..8587e190 100644 --- a/app/models.py +++ b/app/models.py @@ -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)