logging on post_json_to_model

This commit is contained in:
rimu 2024-03-09 12:15:58 +13:00
parent 690bb34eff
commit bacc0c81be

View file

@ -578,66 +578,70 @@ def actor_json_to_model(activity_json, address, server):
def post_json_to_model(post_json, user, community) -> Post: def post_json_to_model(post_json, user, community) -> Post:
post = Post(user_id=user.id, community_id=community.id, try:
title=html.unescape(post_json['name']), post = Post(user_id=user.id, community_id=community.id,
comments_enabled=post_json['commentsEnabled'], title=html.unescape(post_json['name']),
sticky=post_json['stickied'] if 'stickied' in post_json else False, comments_enabled=post_json['commentsEnabled'],
nsfw=post_json['sensitive'], sticky=post_json['stickied'] if 'stickied' in post_json else False,
nsfl=post_json['nsfl'] if 'nsfl' in post_json else False, nsfw=post_json['sensitive'],
ap_id=post_json['id'], nsfl=post_json['nsfl'] if 'nsfl' in post_json else False,
type=constants.POST_TYPE_ARTICLE, ap_id=post_json['id'],
posted_at=post_json['published'], type=constants.POST_TYPE_ARTICLE,
last_active=post_json['published'], posted_at=post_json['published'],
instance_id=user.instance_id last_active=post_json['published'],
) instance_id=user.instance_id
if 'source' in post_json and \ )
post_json['source']['mediaType'] == 'text/markdown': if 'source' in post_json and \
post.body = post_json['source']['content'] post_json['source']['mediaType'] == 'text/markdown':
post.body_html = markdown_to_html(post.body) post.body = post_json['source']['content']
elif 'content' in post_json: post.body_html = markdown_to_html(post.body)
post.body_html = allowlist_html(post_json['content']) elif 'content' in post_json:
post.body = html_to_markdown(post.body_html) post.body_html = allowlist_html(post_json['content'])
if 'attachment' in post_json and \ post.body = html_to_markdown(post.body_html)
len(post_json['attachment']) > 0 and \ if 'attachment' in post_json and \
'type' in post_json['attachment'][0]: len(post_json['attachment']) > 0 and \
if post_json['attachment'][0]['type'] == 'Link': 'type' in post_json['attachment'][0]:
post.url = post_json['attachment'][0]['href'] if post_json['attachment'][0]['type'] == 'Link':
if is_image_url(post.url): post.url = post_json['attachment'][0]['href']
post.type = POST_TYPE_IMAGE if is_image_url(post.url):
else: post.type = POST_TYPE_IMAGE
post.type = POST_TYPE_LINK else:
post.type = POST_TYPE_LINK
domain = domain_from_url(post.url) domain = domain_from_url(post.url)
# notify about links to banned websites. # notify about links to banned websites.
already_notified = set() # often admins and mods are the same people - avoid notifying them twice already_notified = set() # often admins and mods are the same people - avoid notifying them twice
if domain: if domain:
if domain.notify_mods: if domain.notify_mods:
for community_member in post.community.moderators(): for community_member in post.community.moderators():
notify = Notification(title='Suspicious content', url=post.ap_id, user_id=community_member.user_id, author_id=user.id) notify = Notification(title='Suspicious content', url=post.ap_id, user_id=community_member.user_id, author_id=user.id)
db.session.add(notify)
already_notified.add(community_member.user_id)
if domain.notify_admins:
for admin in Site.admins():
if admin.id not in already_notified:
notify = Notification(title='Suspicious content', url=post.ap_id, user_id=admin.id, author_id=user.id)
db.session.add(notify) db.session.add(notify)
admin.unread_notifications += 1 already_notified.add(community_member.user_id)
if domain.banned:
post = None
if not domain.banned:
domain.post_count += 1
post.domain = domain
if 'image' in post_json and post:
image = File(source_url=post_json['image']['url'])
db.session.add(image)
post.image = image
if post is not None: if domain.notify_admins:
db.session.add(post) for admin in Site.admins():
community.post_count += 1 if admin.id not in already_notified:
db.session.commit() notify = Notification(title='Suspicious content', url=post.ap_id, user_id=admin.id, author_id=user.id)
return post db.session.add(notify)
admin.unread_notifications += 1
if domain.banned:
post = None
if not domain.banned:
domain.post_count += 1
post.domain = domain
if 'image' in post_json and post:
image = File(source_url=post_json['image']['url'])
db.session.add(image)
post.image = image
if post is not None:
db.session.add(post)
community.post_count += 1
db.session.commit()
return post
except KeyError as e:
current_app.logger.error(f'KeyError in post_json_to_model: ' + str(post_json))
return None
# Save two different versions of a File, after downloading it from file.source_url. Set a width parameter to None to avoid generating one of that size # Save two different versions of a File, after downloading it from file.source_url. Set a width parameter to None to avoid generating one of that size