mirror of
https://codeberg.org/rimu/pyfedi
synced 2025-01-23 19:36:56 -08:00
adding a way to pass a user to do_subscribe
This commit is contained in:
parent
cf5bb65a9f
commit
6b72b9cb64
2 changed files with 26 additions and 14 deletions
|
@ -279,6 +279,8 @@ def admin_federation():
|
|||
community_urls_to_join.append(parsed_communities_sorted[i]['url'])
|
||||
|
||||
# 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 = []
|
||||
for c in community_urls_to_join:
|
||||
# get the relevant url bits
|
||||
|
@ -289,10 +291,10 @@ def admin_federation():
|
|||
# capture the messages returned by do_subscibe
|
||||
# and show to user if instance is in debug mode
|
||||
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)
|
||||
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:
|
||||
flash(_(f'Results: {pre_load_messages}'))
|
||||
|
|
|
@ -390,12 +390,12 @@ def show_community_rss(actor):
|
|||
@login_required
|
||||
@validation_required
|
||||
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
|
||||
# admin.admin_federation.preload_form as well
|
||||
@celery.task
|
||||
def do_subscribe(actor, main_user_name=True):
|
||||
def do_subscribe(actor, user, main_user_name=True):
|
||||
remote = False
|
||||
actor = actor.strip()
|
||||
pre_load_message = {}
|
||||
|
@ -407,13 +407,16 @@ def do_subscribe(actor, main_user_name=True):
|
|||
|
||||
if community is not None:
|
||||
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:
|
||||
abort(401)
|
||||
else:
|
||||
pre_load_message['user_banned'] = True
|
||||
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(current_user, community) != SUBSCRIPTION_MEMBER and community_membership(current_user, community) != SUBSCRIPTION_PENDING:
|
||||
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 main_user_name:
|
||||
flash(_('You cannot join this community'))
|
||||
|
@ -422,19 +425,23 @@ def do_subscribe(actor, main_user_name=True):
|
|||
success = True
|
||||
if remote:
|
||||
# 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.commit()
|
||||
if community.instance.online():
|
||||
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()],
|
||||
"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)
|
||||
# success = post_request(community.ap_inbox_url, follow, current_user.private_key,
|
||||
# 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 'is not in allowlist' in success:
|
||||
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
|
||||
|
||||
# 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.commit()
|
||||
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'
|
||||
|
||||
referrer = request.headers.get('Referer', None)
|
||||
cache.delete_memoized(community_membership, current_user, community)
|
||||
cache.delete_memoized(joined_communities, current_user.id)
|
||||
# cache.delete_memoized(community_membership, current_user, community)
|
||||
# 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 referrer is not None:
|
||||
return redirect(referrer)
|
||||
|
|
Loading…
Add table
Reference in a new issue