From 96cc0704e0488b8fdfb26d2ec43f4cfa81d8a11f Mon Sep 17 00:00:00 2001 From: freamon Date: Tue, 20 Aug 2024 20:32:16 +0000 Subject: [PATCH] Don't send subscription activity to communities on dead instances --- app/admin/util.py | 9 +++---- app/community/routes.py | 52 ++++++++++++++++++++++------------------- app/user/utils.py | 9 +++---- 3 files changed, 34 insertions(+), 36 deletions(-) diff --git a/app/admin/util.py b/app/admin/util.py index dd456554..c3c068ad 100644 --- a/app/admin/util.py +++ b/app/admin/util.py @@ -55,6 +55,9 @@ def unsubscribe_from_everything_then_delete_task(user_id): def unsubscribe_from_community(community, user): + if community.instance.gone_forever: + return + undo_id = f"https://{current_app.config['SERVER_NAME']}/activities/undo/" + gibberish(15) follow = { "actor": user.public_url(), @@ -70,13 +73,7 @@ def unsubscribe_from_community(community, user): 'id': undo_id, 'object': follow } - activity = ActivityPubLog(direction='out', activity_id=undo_id, activity_type='Undo', - activity_json=json.dumps(undo), result='processing') - db.session.add(activity) - db.session.commit() post_request(community.ap_inbox_url, undo, user.private_key, user.public_url() + '#main-key') - activity.result = 'success' - db.session.commit() def send_newsletter(form): diff --git a/app/community/routes.py b/app/community/routes.py index a59e34d3..5308f84a 100644 --- a/app/community/routes.py +++ b/app/community/routes.py @@ -403,14 +403,16 @@ def subscribe(actor): join_request = CommunityJoinRequest(user_id=current_user.id, community_id=community.id) db.session.add(join_request) db.session.commit() - follow = { - "actor": current_user.public_url(), - "to": [community.public_url()], - "object": community.public_url(), - "type": "Follow", - "id": f"https://{current_app.config['SERVER_NAME']}/activities/follow/{join_request.id}" - } - success = post_request(community.ap_inbox_url, follow, current_user.private_key, + success = True + if not community.instance.gone_forever: + follow = { + "actor": current_user.public_url(), + "to": [community.public_url()], + "object": community.public_url(), + "type": "Follow", + "id": f"https://{current_app.config['SERVER_NAME']}/activities/follow/{join_request.id}" + } + 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') @@ -442,22 +444,24 @@ def unsubscribe(actor): proceed = True # Undo the Follow if '@' in actor: # this is a remote community, so activitypub is needed - undo_id = f"https://{current_app.config['SERVER_NAME']}/activities/undo/" + gibberish(15) - follow = { - "actor": current_user.public_url(), - "to": [community.public_url()], - "object": community.public_url(), - "type": "Follow", - "id": f"https://{current_app.config['SERVER_NAME']}/activities/follow/{gibberish(15)}" - } - undo = { - 'actor': current_user.public_url(), - 'to': [community.public_url()], - 'type': 'Undo', - 'id': undo_id, - 'object': follow - } - success = post_request(community.ap_inbox_url, undo, current_user.private_key, + success = True + if not community.instance.gone_forever: + undo_id = f"https://{current_app.config['SERVER_NAME']}/activities/undo/" + gibberish(15) + follow = { + "actor": current_user.public_url(), + "to": [community.public_url()], + "object": community.public_url(), + "type": "Follow", + "id": f"https://{current_app.config['SERVER_NAME']}/activities/follow/{gibberish(15)}" + } + undo = { + 'actor': current_user.public_url(), + 'to': [community.public_url()], + 'type': 'Undo', + 'id': undo_id, + 'object': follow + } + success = post_request(community.ap_inbox_url, undo, current_user.private_key, current_user.public_url() + '#main-key', timeout=10) if not success: flash('There was a problem while trying to unsubscribe', 'error') diff --git a/app/user/utils.py b/app/user/utils.py index 01aa9bcf..50bbe305 100644 --- a/app/user/utils.py +++ b/app/user/utils.py @@ -91,6 +91,9 @@ def purge_user_then_delete_task(user_id): def unsubscribe_from_community(community, user): + if community.instance.gone_forever: + return + undo_id = f"https://{current_app.config['SERVER_NAME']}/activities/undo/" + gibberish(15) follow = { "actor": user.public_url(), @@ -106,13 +109,7 @@ def unsubscribe_from_community(community, user): 'id': undo_id, 'object': follow } - activity = ActivityPubLog(direction='out', activity_id=undo_id, activity_type='Undo', - activity_json=json.dumps(undo), result='processing') - db.session.add(activity) - db.session.commit() post_request(community.ap_inbox_url, undo, user.private_key, user.public_url() + '#main-key') - activity.result = 'success' - db.session.commit() def search_for_user(address: str):