adding a way to pass a user to do_subscribe

This commit is contained in:
Alan Roberts 2024-09-13 11:26:00 -04:00
parent cf5bb65a9f
commit 6b72b9cb64
2 changed files with 26 additions and 14 deletions

View file

@ -279,6 +279,8 @@ def admin_federation():
community_urls_to_join.append(parsed_communities_sorted[i]['url']) community_urls_to_join.append(parsed_communities_sorted[i]['url'])
# loop through the list and send off the follow requests # loop through the list and send off the follow requests
# use User #1, the first instance admin
user = User.query.get(1)
pre_load_messages = [] pre_load_messages = []
for c in community_urls_to_join: for c in community_urls_to_join:
# get the relevant url bits # get the relevant url bits
@ -289,10 +291,10 @@ def admin_federation():
# capture the messages returned by do_subscibe # capture the messages returned by do_subscibe
# and show to user if instance is in debug mode # and show to user if instance is in debug mode
if current_app.debug: if current_app.debug:
message = do_subscribe(new_community.ap_id, main_user_name=False) message = do_subscribe(new_community.ap_id, user, main_user_name=False)
pre_load_messages.append(message) pre_load_messages.append(message)
else: else:
message_we_wont_do_anything_with = do_subscribe.delay(new_community.ap_id, main_user_name=False) message_we_wont_do_anything_with = do_subscribe.delay(new_community.ap_id, user, main_user_name=False)
if current_app.debug: if current_app.debug:
flash(_(f'Results: {pre_load_messages}')) flash(_(f'Results: {pre_load_messages}'))

View file

@ -390,12 +390,12 @@ def show_community_rss(actor):
@login_required @login_required
@validation_required @validation_required
def subscribe(actor): def subscribe(actor):
do_subscribe(actor) do_subscribe(actor, current_user)
# this is separated out from the route, so it can be used by the # this is separated out from the route, so it can be used by the
# admin.admin_federation.preload_form as well # admin.admin_federation.preload_form as well
@celery.task @celery.task
def do_subscribe(actor, main_user_name=True): def do_subscribe(actor, user, main_user_name=True):
remote = False remote = False
actor = actor.strip() actor = actor.strip()
pre_load_message = {} pre_load_message = {}
@ -407,13 +407,16 @@ def do_subscribe(actor, main_user_name=True):
if community is not None: if community is not None:
pre_load_message['community'] = community.ap_id pre_load_message['community'] = community.ap_id
if community.id in communities_banned_from(current_user.id): # if community.id in communities_banned_from(current_user.id):
if community.id in communities_banned_from(user.id):
if main_user_name: if main_user_name:
abort(401) abort(401)
else: else:
pre_load_message['user_banned'] = True pre_load_message['user_banned'] = True
if community_membership(current_user, community) != SUBSCRIPTION_MEMBER and community_membership(current_user, community) != SUBSCRIPTION_PENDING: # if community_membership(current_user, community) != SUBSCRIPTION_MEMBER and community_membership(current_user, community) != SUBSCRIPTION_PENDING:
banned = CommunityBan.query.filter_by(user_id=current_user.id, community_id=community.id).first() if community_membership(user, community) != SUBSCRIPTION_MEMBER and community_membership(user, community) != SUBSCRIPTION_PENDING:
# banned = CommunityBan.query.filter_by(user_id=current_user.id, community_id=community.id).first()
banned = CommunityBan.query.filter_by(user_id=user.id, community_id=community.id).first()
if banned: if banned:
if main_user_name: if main_user_name:
flash(_('You cannot join this community')) flash(_('You cannot join this community'))
@ -422,19 +425,23 @@ def do_subscribe(actor, main_user_name=True):
success = True 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)
join_request = CommunityJoinRequest(user_id=user.id, community_id=community.id)
db.session.add(join_request) db.session.add(join_request)
db.session.commit() db.session.commit()
if community.instance.online(): if community.instance.online():
follow = { follow = {
"actor": current_user.public_url(main_user_name=main_user_name), # "actor": current_user.public_url(main_user_name=main_user_name),
"actor": user.public_url(main_user_name=main_user_name),
"to": [community.public_url()], "to": [community.public_url()],
"object": community.public_url(), "object": community.public_url(),
"type": "Follow", "type": "Follow",
"id": f"https://{current_app.config['SERVER_NAME']}/activities/follow/{join_request.id}" "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 = 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)
success = post_request(community.ap_inbox_url, follow, user.private_key,
user.public_url(main_user_name=main_user_name) + '#main-key', timeout=10)
if success is False or isinstance(success, str): if success is False or isinstance(success, str):
if 'is not in allowlist' in success: if 'is not in allowlist' in success:
msg_to_user = f'{community.instance.domain} does not allow us to join their communities.' msg_to_user = f'{community.instance.domain} does not allow us to join their communities.'
@ -450,7 +457,8 @@ def do_subscribe(actor, main_user_name=True):
pre_load_message['status'] = msg_to_user pre_load_message['status'] = msg_to_user
# 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)
member = CommunityMember(user_id=user.id, community_id=community.id)
db.session.add(member) db.session.add(member)
db.session.commit() db.session.commit()
if success is True: if success is True:
@ -466,8 +474,10 @@ def do_subscribe(actor, main_user_name=True):
pre_load_message['status'] = 'already subscribed, or subsciption pending' pre_load_message['status'] = 'already subscribed, or subsciption pending'
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)
cache.delete_memoized(joined_communities, current_user.id) # cache.delete_memoized(joined_communities, current_user.id)
cache.delete_memoized(community_membership, user, community)
cache.delete_memoized(joined_communities, user.id)
if main_user_name: if main_user_name:
if referrer is not None: if referrer is not None:
return redirect(referrer) return redirect(referrer)