Add ActivityPub logging to outbox processing

This commit is contained in:
freamon 2024-03-15 22:32:48 +00:00
parent 32adbbc414
commit c5f0d5175a
2 changed files with 13 additions and 4 deletions

View file

@ -589,7 +589,7 @@ def actor_json_to_model(activity_json, address, server):
return community return community
def post_json_to_model(post_json, user, community) -> Post: def post_json_to_model(activity_log, post_json, user, community) -> Post:
try: try:
post = Post(user_id=user.id, community_id=community.id, post = Post(user_id=user.id, community_id=community.id,
title=html.unescape(post_json['name']), title=html.unescape(post_json['name']),
@ -637,6 +637,7 @@ def post_json_to_model(post_json, user, community) -> Post:
admin.unread_notifications += 1 admin.unread_notifications += 1
if domain.banned: if domain.banned:
post = None post = None
activity_log.exception_message = domain.name + ' is blocked by admin'
if not domain.banned: if not domain.banned:
domain.post_count += 1 domain.post_count += 1
post.domain = domain post.domain = domain
@ -648,6 +649,7 @@ def post_json_to_model(post_json, user, community) -> Post:
if post is not None: if post is not None:
db.session.add(post) db.session.add(post)
community.post_count += 1 community.post_count += 1
activity_log.result = 'success'
db.session.commit() db.session.commit()
return post return post
except KeyError as e: except KeyError as e:

View file

@ -4,7 +4,7 @@ from time import sleep
from typing import List from typing import List
import requests import requests
from PIL import Image, ImageOps from PIL import Image, ImageOps
from flask import request, abort, g, current_app from flask import request, abort, g, current_app, json
from flask_login import current_user from flask_login import current_user
from pillow_heif import register_heif_opener from pillow_heif import register_heif_opener
@ -13,7 +13,7 @@ from app.activitypub.signature import post_request
from app.activitypub.util import find_actor_or_create, actor_json_to_model, post_json_to_model, default_context from app.activitypub.util import find_actor_or_create, actor_json_to_model, post_json_to_model, default_context
from app.constants import POST_TYPE_ARTICLE, POST_TYPE_LINK, POST_TYPE_IMAGE from app.constants import POST_TYPE_ARTICLE, POST_TYPE_LINK, POST_TYPE_IMAGE
from app.models import Community, File, BannedInstances, PostReply, PostVote, Post, utcnow, CommunityMember, Site, \ from app.models import Community, File, BannedInstances, PostReply, PostVote, Post, utcnow, CommunityMember, Site, \
Instance, Notification, User Instance, Notification, User, ActivityPubLog
from app.utils import get_request, gibberish, markdown_to_html, domain_from_url, allowlist_html, \ from app.utils import get_request, gibberish, markdown_to_html, domain_from_url, allowlist_html, \
html_to_markdown, is_image_url, ensure_directory_exists, inbox_domain, post_ranking, shorten_string, parse_page, \ html_to_markdown, is_image_url, ensure_directory_exists, inbox_domain, post_ranking, shorten_string, parse_page, \
remove_tracking_from_link, ap_datetime, instance_banned remove_tracking_from_link, ap_datetime, instance_banned
@ -100,12 +100,19 @@ def retrieve_mods_and_backfill(community_id: int):
activities_processed = 0 activities_processed = 0
for activity in outbox_data['orderedItems']: for activity in outbox_data['orderedItems']:
user = find_actor_or_create(activity['object']['actor']) user = find_actor_or_create(activity['object']['actor'])
activity_log = ActivityPubLog(direction='in', activity_id=activity['id'], activity_type='Announce', result='failure')
if site.log_activitypub_json:
activity_log.activity_json = json.dumps(activity)
db.session.add(activity_log)
if user: if user:
post = post_json_to_model(activity['object']['object'], user, community) post = post_json_to_model(activity_log, activity['object']['object'], user, community)
post.ap_create_id = activity['object']['id'] post.ap_create_id = activity['object']['id']
post.ap_announce_id = activity['id'] post.ap_announce_id = activity['id']
post.ranking = post_ranking(post.score, post.posted_at) post.ranking = post_ranking(post.score, post.posted_at)
db.session.commit() db.session.commit()
else:
activity_log.exception_message = 'Could not find or create actor'
db.session.commit()
activities_processed += 1 activities_processed += 1
if activities_processed >= 50: if activities_processed >= 50: