diff --git a/app/activitypub/signature.py b/app/activitypub/signature.py index 9fefb305..991b5e15 100644 --- a/app/activitypub/signature.py +++ b/app/activitypub/signature.py @@ -126,7 +126,10 @@ def post_request(uri: str, body: dict | None, private_key: str, key_id: str, con log.result = 'success' db.session.commit() - return log.result != 'failure' + if log.result != 'failure': + return True + else: + return log.exception_message def signed_get_request(uri: str, private_key: str, key_id: str, content_type: str = "application/activity+json", diff --git a/app/chat/util.py b/app/chat/util.py index 8d63a3c1..7adc5828 100644 --- a/app/chat/util.py +++ b/app/chat/util.py @@ -50,7 +50,7 @@ def send_message(message: str, conversation_id: int) -> ChatMessage: } success = post_request(recipient.ap_inbox_url, reply_json, current_user.private_key, current_user.public_url() + '#main-key') - if not success: + if success is False or isinstance(success, str): flash(_('Message failed to send to %(name)s.', name=recipient.link()), 'error') flash(_('Message sent.')) diff --git a/app/community/routes.py b/app/community/routes.py index 7e992725..42e4080a 100644 --- a/app/community/routes.py +++ b/app/community/routes.py @@ -398,13 +398,13 @@ def subscribe(actor): banned = CommunityBan.query.filter_by(user_id=current_user.id, community_id=community.id).first() if banned: flash(_('You cannot join this community')) + success = True if remote: # send ActivityPub message to remote community, asking to follow. Accept message will be sent to our shared inbox join_request = CommunityJoinRequest(user_id=current_user.id, community_id=community.id) db.session.add(join_request) db.session.commit() - success = True - if not community.instance.gone_forever: + if community.instance.online(): follow = { "actor": current_user.public_url(), "to": [community.public_url()], @@ -414,13 +414,17 @@ def subscribe(actor): } success = post_request(community.ap_inbox_url, follow, current_user.private_key, current_user.public_url() + '#main-key', timeout=10) - if not success: - flash(_("There was a problem while trying to communicate with remote server. If other people have already joined this community it won't matter."), 'error') + if success is False or isinstance(success, str): + if 'is not in allowlist' in success: + flash(_('%(name)s does not allow us to join their communities.', name=community.instance.domain), 'error') + else: + flash(_("There was a problem while trying to communicate with remote server. If other people have already joined this community it won't matter."), 'error') # for local communities, joining is instant member = CommunityMember(user_id=current_user.id, community_id=community.id) db.session.add(member) db.session.commit() - flash('You joined ' + community.title) + if success is True: + flash('You joined ' + community.title) referrer = request.headers.get('Referer', None) cache.delete_memoized(community_membership, current_user, community) cache.delete_memoized(joined_communities, current_user.id) @@ -463,7 +467,7 @@ def unsubscribe(actor): } success = post_request(community.ap_inbox_url, undo, current_user.private_key, current_user.public_url() + '#main-key', timeout=10) - if not success: + if success is False or isinstance(success, str): flash('There was a problem while trying to unsubscribe', 'error') if proceed: diff --git a/app/post/routes.py b/app/post/routes.py index 96035a14..ae34fed5 100644 --- a/app/post/routes.py +++ b/app/post/routes.py @@ -195,7 +195,7 @@ def show_post(post_id: int): if not community.is_local(): # this is a remote community, send it to the instance that hosts it success = post_request(community.ap_inbox_url, create_json, current_user.private_key, current_user.public_url() + '#main-key') - if not success: + if success is False or isinstance(success, str): flash('Failed to send to remote instance', 'error') else: # local community - send it to followers on remote instances announce = { @@ -221,7 +221,7 @@ def show_post(post_id: int): if not community.is_local() or (community.is_local and not community.has_followers_from_domain(post.author.ap_domain)): success = post_request(post.author.ap_inbox_url, create_json, current_user.private_key, current_user.public_url() + '#main-key') - if not success: + if success is False or isinstance(success, str): # sending to shared inbox is good enough for Mastodon, but Lemmy will reject it the local community has no followers personal_inbox = post.author.public_url() + '/inbox' post_request(personal_inbox, create_json, current_user.private_key, @@ -436,10 +436,8 @@ def post_vote(post_id: int, vote_direction): if instance.inbox and not current_user.has_blocked_instance(instance.id) and not instance_banned(instance.domain): send_to_remote_instance(instance.id, post.community.id, announce) else: - success = post_request_in_background(post.community.ap_inbox_url, action_json, current_user.private_key, - current_user.public_url(not(post.community.instance.votes_are_public() and current_user.vote_privately())) + '#main-key') - if not success: - flash('Failed to send vote', 'warning') + post_request_in_background(post.community.ap_inbox_url, action_json, current_user.private_key, + current_user.public_url(not(post.community.instance.votes_are_public() and current_user.vote_privately())) + '#main-key') current_user.last_seen = utcnow() current_user.ip_address = ip_address() @@ -802,7 +800,7 @@ def add_reply(post_id: int, comment_id: int): if not post.community.is_local(): # this is a remote community, send it to the instance that hosts it success = post_request(post.community.ap_inbox_url, create_json, current_user.private_key, current_user.public_url() + '#main-key') - if not success: + if success is False or isinstance(success, str): flash('Failed to send reply', 'error') else: # local community - send it to followers on remote instances announce = { @@ -828,7 +826,7 @@ def add_reply(post_id: int, comment_id: int): if not post.community.is_local() or (post.community.is_local and not post.community.has_followers_from_domain(in_reply_to.author.ap_domain)): success = post_request(in_reply_to.author.ap_inbox_url, create_json, current_user.private_key, current_user.public_url() + '#main-key') - if not success: + if success is False or isinstance(success, str): # sending to shared inbox is good enough for Mastodon, but Lemmy will reject it the local community has no followers personal_inbox = in_reply_to.author.public_url() + '/inbox' post_request(personal_inbox, create_json, current_user.private_key, @@ -1079,7 +1077,7 @@ def federate_post_update(post): if not post.community.is_local(): # this is a remote community, send it to the instance that hosts it success = post_request(post.community.ap_inbox_url, update_json, current_user.private_key, current_user.public_url() + '#main-key') - if not success: + if success is False or isinstance(success, str): flash('Failed to send edit to remote server', 'error') else: # local community - send it to followers on remote instances announce = { @@ -1423,7 +1421,7 @@ def post_report(post_id: int): if post.community.ap_inbox_url and not current_user.has_blocked_instance(instance.id) and not instance_banned(instance.domain): success = post_request(post.community.ap_inbox_url, report_json, current_user.private_key, current_user.public_url() + '#main-key') - if not success: + if success is False or isinstance(success, str): flash('Failed to send report to remote server', 'error') flash(_('Post has been reported, thank you!')) @@ -1573,7 +1571,7 @@ def post_reply_report(post_id: int, comment_id: int): instance.id) and not instance_banned(instance.domain): success = post_request(post.community.ap_inbox_url, report_json, current_user.private_key, current_user.public_url() + '#main-key') - if not success: + if success is False or isinstance(success, str): flash('Failed to send report to remote server', 'error') flash(_('Comment has been reported, thank you!')) @@ -1724,7 +1722,7 @@ def post_reply_edit(post_id: int, comment_id: int): if not post.community.is_local(): # this is a remote community, send it to the instance that hosts it success = post_request(post.community.ap_inbox_url, update_json, current_user.private_key, current_user.public_url() + '#main-key') - if not success: + if success is False or isinstance(success, str): flash('Failed to send send edit to remote server', 'error') else: # local community - send it to followers on remote instances announce = { @@ -1750,7 +1748,7 @@ def post_reply_edit(post_id: int, comment_id: int): if not post.community.is_local() or (post.community.is_local and not post.community.has_followers_from_domain(in_reply_to.author.ap_domain)): success = post_request(in_reply_to.author.ap_inbox_url, update_json, current_user.private_key, current_user.public_url() + '#main-key') - if not success: + if success is False or isinstance(success, str): # sending to shared inbox is good enough for Mastodon, but Lemmy will reject it the local community has no followers personal_inbox = in_reply_to.author.public_url() + '/inbox' post_request(personal_inbox, update_json, current_user.private_key, @@ -1807,7 +1805,7 @@ def post_reply_delete(post_id: int, comment_id: int): if not post.community.is_local(): # this is a remote community, send it to the instance that hosts it success = post_request(post.community.ap_inbox_url, delete_json, current_user.private_key, current_user.public_url() + '#main-key') - if not success: + if success is False or isinstance(success, str): flash('Failed to send delete to remote server', 'error') else: # local community - send it to followers on remote instances announce = { diff --git a/app/user/routes.py b/app/user/routes.py index 55efe891..95bc9f02 100644 --- a/app/user/routes.py +++ b/app/user/routes.py @@ -745,7 +745,7 @@ def import_settings_task(user_id, filename): } success = post_request(community.ap_inbox_url, follow, user.private_key, user.public_url() + '#main-key') - if not success: + if success is False or isinstance(success, str): sleep(5) # give them a rest else: # for local communities, joining is instant banned = CommunityBan.query.filter_by(user_id=user.id, community_id=community.id).first()