accept Posts and Comments from a.gup.pe #31

This commit is contained in:
rimu 2024-07-15 20:46:48 +08:00
parent 85cc1595fd
commit 1ea3c925ea
3 changed files with 22 additions and 17 deletions

View file

@ -654,13 +654,13 @@ def process_inbox_request(request_json, activitypublog_id, ip_address):
# Announce is new content and votes that happened on a remote server. # Announce is new content and votes that happened on a remote server.
if request_json['type'] == 'Announce': if request_json['type'] == 'Announce':
if isinstance(request_json['object'], str): if isinstance(request_json['object'], str): # Mastodon, PeerTube, A.gup.pe
activity_log.activity_json = json.dumps(request_json) activity_log.activity_json = json.dumps(request_json)
activity_log.exception_message = 'invalid json?' activity_log.exception_message = 'invalid json?'
if 'actor' in request_json: if 'actor' in request_json:
community = find_actor_or_create(request_json['actor'], community_only=True, create_if_not_found=False) community = find_actor_or_create(request_json['actor'], community_only=True, create_if_not_found=False)
if community: if community:
resolve_remote_post(request_json['object'], community.id, request_json['actor']) post = resolve_remote_post(request_json['object'], community.id, request_json['actor'])
elif request_json['object']['type'] == 'Create' or request_json['object']['type'] == 'Update': elif request_json['object']['type'] == 'Create' or request_json['object']['type'] == 'Update':
activity_log.activity_type = request_json['object']['type'] activity_log.activity_type = request_json['object']['type']
if 'object' in request_json and 'object' in request_json['object']: if 'object' in request_json and 'object' in request_json['object']:
@ -1176,7 +1176,7 @@ def process_inbox_request(request_json, activitypublog_id, ip_address):
if activity_log.exception_message is not None and activity_log.result == 'processing': if activity_log.exception_message is not None and activity_log.result == 'processing':
activity_log.result = 'failure' activity_log.result = 'failure'
# Don't log successful json - save space # Don't log successful json - save space
if site.log_activitypub_json and activity_log.result == 'success': if site.log_activitypub_json and activity_log.result == 'success' and not current_app.debug:
activity_log.activity_json = '' activity_log.activity_json = ''
db.session.commit() db.session.commit()

View file

@ -1222,7 +1222,7 @@ def new_instance_profile_task(instance_id: int):
InstanceRole.instance_id == instance.id, InstanceRole.instance_id == instance.id,
InstanceRole.role == 'admin').delete() InstanceRole.role == 'admin').delete()
db.session.commit() db.session.commit()
elif instance_data.status_code == 406: # Mastodon and PeerTube do this elif instance_data.status_code == 406 or instance_data.status_code == 404: # Mastodon and PeerTube do 406, a.gup.pe does 404
instance.inbox = f"https://{instance.domain}/inbox" instance.inbox = f"https://{instance.domain}/inbox"
instance.updated_at = utcnow() instance.updated_at = utcnow()
db.session.commit() db.session.commit()
@ -2413,7 +2413,7 @@ def resolve_remote_post(uri: str, community_id: int, announce_actor=None) -> Uni
if announce_actor: if announce_actor:
parsed_url = urlparse(announce_actor) parsed_url = urlparse(announce_actor)
announce_actor_domain = parsed_url.netloc announce_actor_domain = parsed_url.netloc
if announce_actor_domain != uri_domain: if announce_actor_domain != 'a.gup.pe' and announce_actor_domain != uri_domain:
return None return None
actor_domain = None actor_domain = None
actor = None actor = None
@ -2490,13 +2490,22 @@ def resolve_remote_post(uri: str, community_id: int, announce_actor=None) -> Uni
'id': f"https://{uri_domain}/activities/create/gibberish(15)", 'id': f"https://{uri_domain}/activities/create/gibberish(15)",
'object': post_data 'object': post_data
} }
post = create_post(activity_log, community, request_json, user) if 'inReplyTo' in request_json['object'] and request_json['object']['inReplyTo']:
if post: post_reply = create_post_reply(activity_log, community, request_json['object']['inReplyTo'], request_json, user)
if 'published' in post_data: if post_reply:
post.posted_at=post_data['published'] if 'published' in post_data:
post.last_active=post_data['published'] post_reply.posted_at = post_data['published']
db.session.commit() post_reply.post.last_active = post_data['published']
return post db.session.commit()
return post_reply
else:
post = create_post(activity_log, community, request_json, user)
if post:
if 'published' in post_data:
post.posted_at=post_data['published']
post.last_active=post_data['published']
db.session.commit()
return post
return None return None

View file

@ -5,14 +5,11 @@ from random import randint
from time import sleep from time import sleep
import flask import flask
import markdown2
import requests
from flask_caching import CachedResponse from flask_caching import CachedResponse
from sqlalchemy.sql.operators import or_, and_ from sqlalchemy.sql.operators import or_, and_
from app import db, cache from app import db, cache
from app.activitypub.util import make_image_sizes_async, refresh_user_profile, find_actor_or_create, \ from app.activitypub.util import users_total, active_month, local_posts, local_communities
refresh_community_profile_task, users_total, active_month, local_posts, local_communities, local_comments
from app.activitypub.signature import default_context from app.activitypub.signature import default_context
from app.constants import SUBSCRIPTION_PENDING, SUBSCRIPTION_MEMBER, POST_TYPE_IMAGE, POST_TYPE_LINK, \ from app.constants import SUBSCRIPTION_PENDING, SUBSCRIPTION_MEMBER, POST_TYPE_IMAGE, POST_TYPE_LINK, \
SUBSCRIPTION_OWNER, SUBSCRIPTION_MODERATOR, POST_TYPE_VIDEO, POST_TYPE_POLL SUBSCRIPTION_OWNER, SUBSCRIPTION_MODERATOR, POST_TYPE_VIDEO, POST_TYPE_POLL
@ -24,7 +21,6 @@ from flask_moment import moment
from flask_login import current_user, login_required from flask_login import current_user, login_required
from flask_babel import _, get_locale from flask_babel import _, get_locale
from sqlalchemy import select, desc, text from sqlalchemy import select, desc, text
from sqlalchemy_searchable import search
from app.utils import render_template, get_setting, gibberish, request_etag_matches, return_304, blocked_domains, \ from app.utils import render_template, get_setting, gibberish, request_etag_matches, return_304, blocked_domains, \
ap_datetime, ip_address, retrieve_block_list, shorten_string, markdown_to_text, user_filters_home, \ ap_datetime, ip_address, retrieve_block_list, shorten_string, markdown_to_text, user_filters_home, \
joined_communities, moderating_communities, parse_page, theme_list, get_request, markdown_to_html, allowlist_html, \ joined_communities, moderating_communities, parse_page, theme_list, get_request, markdown_to_html, allowlist_html, \