only prepend [Microblog] when autogenerated title is too short. fixes #266

This commit is contained in:
rimu 2024-08-08 19:04:50 +12:00
parent d25232b128
commit 9611a27749
4 changed files with 60 additions and 12 deletions

View file

@ -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.exception_message = 'Community is local only, post discarded'
activity_log.result = 'ignored' activity_log.result = 'ignored'
return None return None
microblog = False
if 'name' not in request_json['object']: # Microblog posts if 'name' not in request_json['object']: # Microblog posts
if 'content' in request_json['object'] and request_json['object']['content'] is not None: if 'content' in request_json['object'] and request_json['object']['content'] is not None:
name = "[Microblog]" title = "[Microblog]"
microblog = True
else: else:
return None return None
else: 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, 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, 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, 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, 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, from_bot=user.bot,
score=instance_weight(user.ap_domain), score=instance_weight(user.ap_domain),
instance_id=user.instance_id, instance_id=user.instance_id,
indexable=user.indexable indexable=user.indexable,
microblog=microblog
) )
# Get post content. Lemmy and Kbin put this in different places. # 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 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: else:
post.body_html = allowlist_html(request_json['object']['content']) post.body_html = allowlist_html(request_json['object']['content'])
post.body = html_to_text(post.body_html) post.body = html_to_text(post.body_html)
if name == "[Microblog]": if microblog:
name += ' ' + microblog_content_to_title(post.body_html) autogenerated_title = microblog_content_to_title(post.body_html)
if '[NSFL]' in name.upper() or '(NSFL)' in name.upper(): 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.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. # Discard post if it contains certain phrases. Good for stopping spam floods.
blocked_phrases_list = blocked_phrases() blocked_phrases_list = blocked_phrases()
for blocked_phrase in blocked_phrases_list: 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 = allowlist_html(request_json['object']['content'])
post.body = html_to_text(post.body_html) post.body = html_to_text(post.body_html)
if name == "[Microblog]": 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() nsfl_in_title = '[NSFL]' in name.upper() or '(NSFL)' in name.upper()
post.title = name post.title = name
# Language # Language

View file

@ -980,6 +980,7 @@ class Post(db.Model):
body = db.Column(db.Text) body = db.Column(db.Text)
body_html = db.Column(db.Text) body_html = db.Column(db.Text)
type = db.Column(db.Integer) type = db.Column(db.Integer)
microblog = db.Column(db.Boolean, default=False)
comments_enabled = db.Column(db.Boolean, default=True) comments_enabled = db.Column(db.Boolean, default=True)
deleted = db.Column(db.Boolean, default=False, index=True) deleted = db.Column(db.Boolean, default=False, index=True)
mea_culpa = db.Column(db.Boolean, default=False) mea_culpa = db.Column(db.Boolean, default=False)

View file

@ -342,7 +342,7 @@ def microblog_content_to_title(html: str) -> str:
title = shorten_string(title, 197) title = shorten_string(title, 197)
else: else:
title = '(content in post body)' title = '(content in post body)'
return title return title.strip()
if end_index != -1: if end_index != -1:
if question_index != -1 and question_index == end_index: if question_index != -1 and question_index == end_index:
@ -357,7 +357,7 @@ def microblog_content_to_title(html: str) -> str:
break break
title = title[:i] + ' ...' if i > 0 else '' title = title[:i] + ' ...' if i > 0 else ''
return title return title.strip()
def community_link_to_href(link: str) -> str: def community_link_to_href(link: str) -> str:

View file

@ -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 ###