pyfedi/app/shared/community.py

103 lines
3.3 KiB
Python
Raw Permalink Normal View History

from app import db, cache
from app.constants import *
2024-11-02 16:56:56 -07:00
from app.models import CommunityBlock, CommunityMember
from app.shared.tasks import task_selector
from app.utils import authorise_api_user, blocked_communities
2024-11-02 16:56:56 -07:00
from flask import current_app, flash
from flask_babel import _
from flask_login import current_user
2024-11-02 16:56:56 -07:00
# would be in app/constants.py
SRC_WEB = 1
SRC_PUB = 2
SRC_API = 3
2024-11-02 16:56:56 -07:00
SRC_PLD = 4 # admin preload form to seed communities
# function can be shared between WEB and API (only API calls it for now)
# call from admin.federation not tested
2024-11-02 16:56:56 -07:00
def join_community(community_id: int, src, auth=None, user_id=None):
if src == SRC_API:
2024-11-02 16:56:56 -07:00
user_id = authorise_api_user(auth)
2024-11-02 16:56:56 -07:00
send_async = not (current_app.debug or src == SRC_WEB) # False if using a browser
2024-11-02 16:56:56 -07:00
sync_retval = task_selector('join_community', send_async, user_id=user_id, community_id=community_id, src=src)
if send_async or sync_retval is True:
member = CommunityMember(user_id=user_id, community_id=community_id)
db.session.add(member)
db.session.commit()
if src == SRC_API:
return user_id
elif src == SRC_PLD:
return sync_retval
else:
return
# function can be shared between WEB and API (only API calls it for now)
def leave_community(community_id: int, src, auth=None):
2024-11-02 16:56:56 -07:00
user_id = authorise_api_user(auth) if src == SRC_API else current_user.id
cm = CommunityMember.query.filter_by(user_id=user_id, community_id=community_id).one()
if not cm.is_owner:
task_selector('leave_community', user_id=user_id, community_id=community_id)
db.session.query(CommunityMember).filter_by(user_id=user_id, community_id=community_id).delete()
db.session.commit()
if src == SRC_WEB:
flash('You have left the community')
else:
2024-11-02 16:56:56 -07:00
# todo: community deletion
if src == SRC_API:
raise Exception('need_to_make_someone_else_owner')
else:
2024-11-02 16:56:56 -07:00
flash('You need to make someone else the owner before unsubscribing.', 'warning')
return
if src == SRC_API:
2024-11-02 16:56:56 -07:00
return user_id
else:
# let calling function handle redirect
return
2024-10-07 06:57:19 -07:00
def block_community(community_id, src, auth=None):
if src == SRC_API:
user_id = authorise_api_user(auth)
2024-10-07 06:57:19 -07:00
else:
user_id = current_user.id
2024-10-07 06:57:19 -07:00
existing = CommunityBlock.query.filter_by(user_id=user_id, community_id=community_id).first()
if not existing:
db.session.add(CommunityBlock(user_id=user_id, community_id=community_id))
db.session.commit()
cache.delete_memoized(blocked_communities, user_id)
2024-10-07 06:57:19 -07:00
if src == SRC_API:
return user_id
else:
return # let calling function handle confirmation flash message and redirect
2024-10-07 06:57:19 -07:00
def unblock_community(community_id, src, auth=None):
if src == SRC_API:
user_id = authorise_api_user(auth)
2024-10-07 06:57:19 -07:00
else:
user_id = current_user.id
2024-10-07 06:57:19 -07:00
existing_block = CommunityBlock.query.filter_by(user_id=user_id, community_id=community_id).first()
if existing_block:
db.session.delete(existing_block)
db.session.commit()
cache.delete_memoized(blocked_communities, user_id)
2024-10-07 06:57:19 -07:00
if src == SRC_API:
return user_id
else:
return # let calling function handle confirmation flash message and redirect