display error message when joining is denied due to allowlist

This commit is contained in:
rimu 2024-08-30 17:25:37 +12:00
parent b3e2cab577
commit 11ce66443e
5 changed files with 28 additions and 23 deletions

View file

@ -126,7 +126,10 @@ def post_request(uri: str, body: dict | None, private_key: str, key_id: str, con
log.result = 'success' log.result = 'success'
db.session.commit() 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", def signed_get_request(uri: str, private_key: str, key_id: str, content_type: str = "application/activity+json",

View file

@ -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, success = post_request(recipient.ap_inbox_url, reply_json, current_user.private_key,
current_user.public_url() + '#main-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 failed to send to %(name)s.', name=recipient.link()), 'error')
flash(_('Message sent.')) flash(_('Message sent.'))

View file

@ -398,13 +398,13 @@ def subscribe(actor):
banned = CommunityBan.query.filter_by(user_id=current_user.id, community_id=community.id).first() banned = CommunityBan.query.filter_by(user_id=current_user.id, community_id=community.id).first()
if banned: if banned:
flash(_('You cannot join this community')) flash(_('You cannot join this community'))
success = True
if remote: if remote:
# send ActivityPub message to remote community, asking to follow. Accept message will be sent to our shared inbox # 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) 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()
success = True if community.instance.online():
if not community.instance.gone_forever:
follow = { follow = {
"actor": current_user.public_url(), "actor": current_user.public_url(),
"to": [community.public_url()], "to": [community.public_url()],
@ -414,12 +414,16 @@ def subscribe(actor):
} }
success = post_request(community.ap_inbox_url, follow, current_user.private_key, 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 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') 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 # for local communities, joining is instant
member = CommunityMember(user_id=current_user.id, community_id=community.id) member = CommunityMember(user_id=current_user.id, community_id=community.id)
db.session.add(member) db.session.add(member)
db.session.commit() db.session.commit()
if success is True:
flash('You joined ' + community.title) flash('You joined ' + community.title)
referrer = request.headers.get('Referer', None) referrer = request.headers.get('Referer', None)
cache.delete_memoized(community_membership, current_user, community) cache.delete_memoized(community_membership, current_user, community)
@ -463,7 +467,7 @@ def unsubscribe(actor):
} }
success = post_request(community.ap_inbox_url, undo, current_user.private_key, 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 success is False or isinstance(success, str):
flash('There was a problem while trying to unsubscribe', 'error') flash('There was a problem while trying to unsubscribe', 'error')
if proceed: if proceed:

View file

@ -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 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, success = post_request(community.ap_inbox_url, create_json, current_user.private_key,
current_user.public_url() + '#main-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') flash('Failed to send to remote instance', 'error')
else: # local community - send it to followers on remote instances else: # local community - send it to followers on remote instances
announce = { 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)): 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, success = post_request(post.author.ap_inbox_url, create_json, current_user.private_key,
current_user.public_url() + '#main-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 # 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' personal_inbox = post.author.public_url() + '/inbox'
post_request(personal_inbox, create_json, current_user.private_key, 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): 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) send_to_remote_instance(instance.id, post.community.id, announce)
else: else:
success = post_request_in_background(post.community.ap_inbox_url, action_json, current_user.private_key, 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.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')
current_user.last_seen = utcnow() current_user.last_seen = utcnow()
current_user.ip_address = ip_address() 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 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, success = post_request(post.community.ap_inbox_url, create_json, current_user.private_key,
current_user.public_url() + '#main-key') current_user.public_url() + '#main-key')
if not success: if success is False or isinstance(success, str):
flash('Failed to send reply', 'error') flash('Failed to send reply', 'error')
else: # local community - send it to followers on remote instances else: # local community - send it to followers on remote instances
announce = { 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)): 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, success = post_request(in_reply_to.author.ap_inbox_url, create_json, current_user.private_key,
current_user.public_url() + '#main-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 # 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' personal_inbox = in_reply_to.author.public_url() + '/inbox'
post_request(personal_inbox, create_json, current_user.private_key, 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 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, success = post_request(post.community.ap_inbox_url, update_json, current_user.private_key,
current_user.public_url() + '#main-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') flash('Failed to send edit to remote server', 'error')
else: # local community - send it to followers on remote instances else: # local community - send it to followers on remote instances
announce = { 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): 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, success = post_request(post.community.ap_inbox_url, report_json, current_user.private_key,
current_user.public_url() + '#main-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('Failed to send report to remote server', 'error')
flash(_('Post has been reported, thank you!')) 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): instance.id) and not instance_banned(instance.domain):
success = post_request(post.community.ap_inbox_url, report_json, current_user.private_key, success = post_request(post.community.ap_inbox_url, report_json, current_user.private_key,
current_user.public_url() + '#main-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('Failed to send report to remote server', 'error')
flash(_('Comment has been reported, thank you!')) 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 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, success = post_request(post.community.ap_inbox_url, update_json, current_user.private_key,
current_user.public_url() + '#main-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') flash('Failed to send send edit to remote server', 'error')
else: # local community - send it to followers on remote instances else: # local community - send it to followers on remote instances
announce = { 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)): 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, success = post_request(in_reply_to.author.ap_inbox_url, update_json, current_user.private_key,
current_user.public_url() + '#main-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 # 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' personal_inbox = in_reply_to.author.public_url() + '/inbox'
post_request(personal_inbox, update_json, current_user.private_key, 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 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, success = post_request(post.community.ap_inbox_url, delete_json, current_user.private_key,
current_user.public_url() + '#main-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') flash('Failed to send delete to remote server', 'error')
else: # local community - send it to followers on remote instances else: # local community - send it to followers on remote instances
announce = { announce = {

View file

@ -745,7 +745,7 @@ def import_settings_task(user_id, filename):
} }
success = post_request(community.ap_inbox_url, follow, user.private_key, success = post_request(community.ap_inbox_url, follow, user.private_key,
user.public_url() + '#main-key') user.public_url() + '#main-key')
if not success: if success is False or isinstance(success, str):
sleep(5) # give them a rest sleep(5) # give them a rest
else: # for local communities, joining is instant else: # for local communities, joining is instant
banned = CommunityBan.query.filter_by(user_id=user.id, community_id=community.id).first() banned = CommunityBan.query.filter_by(user_id=user.id, community_id=community.id).first()