parse mastodon urls in extra fields

This commit is contained in:
rimu 2025-01-01 15:39:39 +13:00
parent 6573d25dd3
commit ddccb7859b
5 changed files with 54 additions and 3 deletions

View file

@ -33,7 +33,8 @@ from app.utils import get_request, allowlist_html, get_setting, ap_datetime, mar
microblog_content_to_title, is_video_url, \
notification_subscribers, communities_banned_from, actor_contains_blocked_words, \
html_to_text, add_to_modlog_activitypub, joined_communities, \
moderating_communities, get_task_session, is_video_hosting_site, opengraph_parse, instance_banned
moderating_communities, get_task_session, is_video_hosting_site, opengraph_parse, instance_banned, \
mastodon_extra_field_link
from sqlalchemy import or_
@ -517,6 +518,8 @@ def refresh_user_profile_task(user_id):
user.extra_fields = []
for field_data in activity_json['attachment']:
if field_data['type'] == 'PropertyValue':
if '<a ' in field_data['value']:
field_data['value'] = mastodon_extra_field_link(field_data['value'])
user.extra_fields.append(UserExtraField(label=field_data['name'].strip(), text=field_data['value'].strip()))
if 'type' in activity_json:
user.bot = True if activity_json['type'] == 'Service' else False
@ -769,6 +772,8 @@ def actor_json_to_model(activity_json, address, server):
user.extra_fields = []
for field_data in activity_json['attachment']:
if field_data['type'] == 'PropertyValue':
if '<a ' in field_data['value']:
field_data['value'] = mastodon_extra_field_link(field_data['value'])
user.extra_fields.append(UserExtraField(label=field_data['name'].strip(), text=field_data['value'].strip()))
try:
db.session.add(user)

View file

@ -21,7 +21,7 @@ from app.utils import render_template, get_setting, request_etag_matches, return
ap_datetime, shorten_string, markdown_to_text, user_filters_home, \
joined_communities, moderating_communities, markdown_to_html, allowlist_html, \
blocked_instances, communities_banned_from, topic_tree, recently_upvoted_posts, recently_downvoted_posts, \
blocked_users, menu_topics, blocked_communities, get_request
blocked_users, menu_topics, blocked_communities, get_request, mastodon_extra_field_link
from app.models import Community, CommunityMember, Post, Site, User, utcnow, Topic, Instance, \
Notification, Language, community_language, ModLog, read_posts
@ -509,6 +509,8 @@ def replay_inbox():
@bp.route('/test')
def test():
return mastodon_extra_field_link('<a href="https://www.brammeehan.com" target="_blank" rel="nofollow noopener me" translate="no"><span class="invisible">https://www.</span><span class="">brammeehan.com</span><span class="invisible"></span></a>')
response = get_request('https://rimu.geek.nz')
x = ''
if response.status_code == 200:

View file

@ -2067,7 +2067,7 @@ class UserExtraField(db.Model):
id = db.Column(db.Integer, primary_key=True)
user_id = db.Column(db.Integer, db.ForeignKey('user.id'), index=True)
label = db.Column(db.String(50))
text = db.Column(db.String(256))
text = db.Column(db.String(1024))
class UserBlock(db.Model):

View file

@ -384,6 +384,12 @@ def html_to_text(html) -> str:
return soup.get_text()
def mastodon_extra_field_link(extra_field: str) -> str:
soup = BeautifulSoup(extra_field, 'html.parser')
for tag in soup.find_all('a'):
return tag['href']
def microblog_content_to_title(html: str) -> str:
title = ''
if '<p>' in html:

View file

@ -0,0 +1,38 @@
"""extra field length
Revision ID: 9df13396fd54
Revises: 8758c0a94f82
Create Date: 2025-01-01 15:30:42.165900
"""
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision = '9df13396fd54'
down_revision = '8758c0a94f82'
branch_labels = None
depends_on = None
def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
with op.batch_alter_table('user_extra_field', schema=None) as batch_op:
batch_op.alter_column('text',
existing_type=sa.VARCHAR(length=256),
type_=sa.String(length=1024),
existing_nullable=True)
# ### end Alembic commands ###
def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
with op.batch_alter_table('user_extra_field', schema=None) as batch_op:
batch_op.alter_column('text',
existing_type=sa.String(length=1024),
type_=sa.VARCHAR(length=256),
existing_nullable=True)
# ### end Alembic commands ###