Don't send subscription activity to communities on dead instances

This commit is contained in:
freamon 2024-08-20 20:32:16 +00:00
parent 3ff54c54da
commit 96cc0704e0
3 changed files with 34 additions and 36 deletions

View file

@ -55,6 +55,9 @@ def unsubscribe_from_everything_then_delete_task(user_id):
def unsubscribe_from_community(community, user): 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) undo_id = f"https://{current_app.config['SERVER_NAME']}/activities/undo/" + gibberish(15)
follow = { follow = {
"actor": user.public_url(), "actor": user.public_url(),
@ -70,13 +73,7 @@ def unsubscribe_from_community(community, user):
'id': undo_id, 'id': undo_id,
'object': follow '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') 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): def send_newsletter(form):

View file

@ -403,14 +403,16 @@ def subscribe(actor):
join_request = CommunityJoinRequest(user_id=current_user.id, community_id=community.id) join_request = CommunityJoinRequest(user_id=current_user.id, community_id=community.id)
db.session.add(join_request) db.session.add(join_request)
db.session.commit() db.session.commit()
follow = { success = True
"actor": current_user.public_url(), if not community.instance.gone_forever:
"to": [community.public_url()], follow = {
"object": community.public_url(), "actor": current_user.public_url(),
"type": "Follow", "to": [community.public_url()],
"id": f"https://{current_app.config['SERVER_NAME']}/activities/follow/{join_request.id}" "object": community.public_url(),
} "type": "Follow",
success = post_request(community.ap_inbox_url, follow, current_user.private_key, "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) current_user.public_url() + '#main-key', timeout=10)
if not success: 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') 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 proceed = True
# Undo the Follow # Undo the Follow
if '@' in actor: # this is a remote community, so activitypub is needed 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) success = True
follow = { if not community.instance.gone_forever:
"actor": current_user.public_url(), undo_id = f"https://{current_app.config['SERVER_NAME']}/activities/undo/" + gibberish(15)
"to": [community.public_url()], follow = {
"object": community.public_url(), "actor": current_user.public_url(),
"type": "Follow", "to": [community.public_url()],
"id": f"https://{current_app.config['SERVER_NAME']}/activities/follow/{gibberish(15)}" "object": community.public_url(),
} "type": "Follow",
undo = { "id": f"https://{current_app.config['SERVER_NAME']}/activities/follow/{gibberish(15)}"
'actor': current_user.public_url(), }
'to': [community.public_url()], undo = {
'type': 'Undo', 'actor': current_user.public_url(),
'id': undo_id, 'to': [community.public_url()],
'object': follow 'type': 'Undo',
} 'id': undo_id,
success = post_request(community.ap_inbox_url, undo, current_user.private_key, 'object': follow
}
success = post_request(community.ap_inbox_url, undo, current_user.private_key,
current_user.public_url() + '#main-key', timeout=10) current_user.public_url() + '#main-key', timeout=10)
if not success: if not success:
flash('There was a problem while trying to unsubscribe', 'error') flash('There was a problem while trying to unsubscribe', 'error')

View file

@ -91,6 +91,9 @@ def purge_user_then_delete_task(user_id):
def unsubscribe_from_community(community, user): 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) undo_id = f"https://{current_app.config['SERVER_NAME']}/activities/undo/" + gibberish(15)
follow = { follow = {
"actor": user.public_url(), "actor": user.public_url(),
@ -106,13 +109,7 @@ def unsubscribe_from_community(community, user):
'id': undo_id, 'id': undo_id,
'object': follow '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') 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): def search_for_user(address: str):