diff --git a/app/activitypub/util.py b/app/activitypub/util.py index 047d617f..ba92da49 100644 --- a/app/activitypub/util.py +++ b/app/activitypub/util.py @@ -1654,17 +1654,19 @@ def create_post(activity_log: ActivityPubLog, community: Community, request_json activity_log.exception_message = 'Community is local only, post discarded' activity_log.result = 'ignored' return None + microblog = False if 'name' not in request_json['object']: # Microblog posts if 'content' in request_json['object'] and request_json['object']['content'] is not None: - name = "[Microblog]" + title = "[Microblog]" + microblog = True else: return None else: - name = request_json['object']['name'] + title = request_json['object']['name'] - nsfl_in_title = '[NSFL]' in name.upper() or '(NSFL)' in name.upper() + nsfl_in_title = '[NSFL]' in title.upper() or '(NSFL)' in title.upper() post = Post(user_id=user.id, community_id=community.id, - title=html.unescape(name), + title=html.unescape(title), comments_enabled=request_json['object']['commentsEnabled'] if 'commentsEnabled' in request_json['object'] else True, sticky=request_json['object']['stickied'] if 'stickied' in request_json['object'] else False, nsfw=request_json['object']['sensitive'] if 'sensitive' in request_json['object'] else False, @@ -1677,7 +1679,8 @@ def create_post(activity_log: ActivityPubLog, community: Community, request_json from_bot=user.bot, score=instance_weight(user.ap_domain), instance_id=user.instance_id, - indexable=user.indexable + indexable=user.indexable, + microblog=microblog ) # Get post content. Lemmy and Kbin put this in different places. if 'source' in request_json['object'] and isinstance(request_json['object']['source'], dict) and request_json['object']['source']['mediaType'] == 'text/markdown': # Lemmy @@ -1693,11 +1696,17 @@ def create_post(activity_log: ActivityPubLog, community: Community, request_json else: post.body_html = allowlist_html(request_json['object']['content']) post.body = html_to_text(post.body_html) - if name == "[Microblog]": - name += ' ' + microblog_content_to_title(post.body_html) - if '[NSFL]' in name.upper() or '(NSFL)' in name.upper(): + if microblog: + autogenerated_title = microblog_content_to_title(post.body_html) + if len(autogenerated_title) < 20: + title = '[Microblog] ' + autogenerated_title + else: + title = autogenerated_title + if '[NSFL]' in title.upper() or '(NSFL)' in title.upper(): post.nsfl = True - post.title = name + if '[NSFW]' in title.upper() or '(NSFW)' in title.upper(): + post.nsfw = True + post.title = title # Discard post if it contains certain phrases. Good for stopping spam floods. blocked_phrases_list = blocked_phrases() for blocked_phrase in blocked_phrases_list: @@ -1934,7 +1943,11 @@ def update_post_from_activity(post: Post, request_json: dict): post.body_html = allowlist_html(request_json['object']['content']) post.body = html_to_text(post.body_html) if name == "[Microblog]": - name += ' ' + microblog_content_to_title(post.body_html) + autogenerated_title = microblog_content_to_title(post.body_html) + if len(autogenerated_title) < 20: + name += ' ' + autogenerated_title + else: + name = autogenerated_title nsfl_in_title = '[NSFL]' in name.upper() or '(NSFL)' in name.upper() post.title = name # Language diff --git a/app/models.py b/app/models.py index ac0ef598..89adca95 100644 --- a/app/models.py +++ b/app/models.py @@ -980,6 +980,7 @@ class Post(db.Model): body = db.Column(db.Text) body_html = db.Column(db.Text) type = db.Column(db.Integer) + microblog = db.Column(db.Boolean, default=False) comments_enabled = db.Column(db.Boolean, default=True) deleted = db.Column(db.Boolean, default=False, index=True) mea_culpa = db.Column(db.Boolean, default=False) diff --git a/app/utils.py b/app/utils.py index 073c75e8..6fa1b058 100644 --- a/app/utils.py +++ b/app/utils.py @@ -342,7 +342,7 @@ def microblog_content_to_title(html: str) -> str: title = shorten_string(title, 197) else: title = '(content in post body)' - return title + return title.strip() if end_index != -1: if question_index != -1 and question_index == end_index: @@ -357,7 +357,7 @@ def microblog_content_to_title(html: str) -> str: break title = title[:i] + ' ...' if i > 0 else '' - return title + return title.strip() def community_link_to_href(link: str) -> str: diff --git a/migrations/versions/f6d6bd92cf88_post_microblog_flag.py b/migrations/versions/f6d6bd92cf88_post_microblog_flag.py new file mode 100644 index 00000000..5daa17df --- /dev/null +++ b/migrations/versions/f6d6bd92cf88_post_microblog_flag.py @@ -0,0 +1,34 @@ +"""post microblog flag + +Revision ID: f6d6bd92cf88 +Revises: 5bf49b8b7b79 +Create Date: 2024-08-08 18:41:20.321882 + +""" +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = 'f6d6bd92cf88' +down_revision = '5bf49b8b7b79' +branch_labels = None +depends_on = None + + +def upgrade(): + # ### commands auto generated by Alembic - please adjust! ### + with op.batch_alter_table('post', schema=None) as batch_op: + batch_op.add_column(sa.Column('microblog', sa.Boolean(), nullable=True)) + + # ### end Alembic commands ### + + op.execute(sa.DDL("UPDATE \"post\" SET microblog = true WHERE starts_with(title, '[Microblog]')")) + + +def downgrade(): + # ### commands auto generated by Alembic - please adjust! ### + with op.batch_alter_table('post', schema=None) as batch_op: + batch_op.drop_column('microblog') + + # ### end Alembic commands ###