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):
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):

View file

@ -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')

View file

@ -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):