MarseyWorld/files/routes/groups.py

191 lines
6.6 KiB
Python
Raw Normal View History

2023-02-24 06:31:06 +00:00
from files.classes import *
from files.helpers.alerts import *
from files.helpers.regex import *
from files.helpers.get import *
from files.routes.wrappers import *
from files.__main__ import app, limiter
@app.get("/ping_groups")
@limiter.limit(DEFAULT_RATELIMIT)
@limiter.limit(DEFAULT_RATELIMIT, key_func=get_ID)
2023-02-24 06:31:06 +00:00
@auth_required
def ping_groups(v:User):
2023-03-16 06:27:58 +00:00
groups = g.db.query(Group).order_by(Group.created_utc).all()
2023-02-24 06:31:06 +00:00
return render_template('groups.html', v=v, groups=groups, cost=GROUP_COST, msg=get_msg(), error=get_error())
@app.post("/create_group")
2023-02-27 05:33:45 +00:00
@limiter.limit('1/second', scope=rpath)
@limiter.limit('1/second', scope=rpath, key_func=get_ID)
@limiter.limit(DEFAULT_RATELIMIT)
@limiter.limit(DEFAULT_RATELIMIT, key_func=get_ID)
2023-02-24 06:31:06 +00:00
@is_not_permabanned
def create_group(v):
name = request.values.get('name')
if not name: abort(400)
name = name.strip().lower()
if name.startswith('slots') or name.startswith('remindme'):
2023-03-04 21:45:12 +00:00
return redirect(f"/ping_groups?error=You can't make a group with that name!")
2023-02-24 06:31:06 +00:00
if not valid_sub_regex.fullmatch(name):
return redirect(f"/ping_groups?error=Name does not match the required format!")
2023-03-16 06:27:58 +00:00
if name == 'everyone' or g.db.get(Group, name):
2023-03-02 19:21:38 +00:00
return redirect(f"/ping_groups?error=This group already exists!")
if not v.charge_account('combined', GROUP_COST)[0]:
2023-03-07 05:06:17 +00:00
return redirect(f"/ping_groups?error=You don't have enough coins or marseybux!")
2023-03-02 19:21:38 +00:00
2023-03-16 06:27:58 +00:00
g.db.add(v)
2023-03-02 19:21:38 +00:00
if v.shadowbanned: abort(500)
group = Group(name=name)
2023-03-16 06:27:58 +00:00
g.db.add(group)
g.db.flush()
2023-03-02 19:21:38 +00:00
group_membership = GroupMembership(
user_id=v.id,
group_name=group.name,
created_utc=time.time(),
approved_utc=time.time()
)
2023-03-16 06:27:58 +00:00
g.db.add(group_membership)
2023-03-02 19:21:38 +00:00
2023-03-16 06:27:58 +00:00
admins = [x[0] for x in g.db.query(User.id).filter(User.admin_level >= PERMS['NOTIFICATIONS_HOLE_CREATION'], User.id != v.id).all()]
2023-03-02 19:21:38 +00:00
for admin in admins:
send_repeatable_notification(admin, f":!marseyparty: !{group} has been created by @{v.username} :marseyparty:")
2023-02-24 06:31:06 +00:00
return redirect(f'/ping_groups?msg=!{group} created successfully!')
2023-02-24 19:29:07 +00:00
@app.post("/!<group_name>/apply")
2023-02-27 05:33:45 +00:00
@limiter.limit('1/second', scope=rpath)
@limiter.limit('1/second', scope=rpath, key_func=get_ID)
@limiter.limit(DEFAULT_RATELIMIT)
@limiter.limit(DEFAULT_RATELIMIT, key_func=get_ID)
2023-02-24 06:31:06 +00:00
@auth_required
def join_group(v:User, group_name):
2023-03-07 18:55:17 +00:00
group_name = group_name.strip().lower()
2023-03-16 06:27:58 +00:00
group = g.db.get(Group, group_name)
2023-02-24 06:31:06 +00:00
if not group: abort(404)
2023-03-16 06:27:58 +00:00
existing = g.db.query(GroupMembership).filter_by(user_id=v.id, group_name=group_name).one_or_none()
2023-02-24 06:31:06 +00:00
if not existing:
join = GroupMembership(user_id=v.id, group_name=group_name)
2023-03-16 06:27:58 +00:00
g.db.add(join)
2023-02-25 22:06:43 +00:00
send_notification(group.owner.id, f"@{v.username} has applied to join !{group}. You can approve or reject the application [here](/!{group}).")
2023-02-24 06:31:06 +00:00
2023-02-24 19:29:07 +00:00
return {"message": f"Application submitted to !{group}'s owner (@{group.owner.username}) successfully!"}
2023-02-24 06:31:06 +00:00
2023-02-24 19:29:07 +00:00
@app.post("/!<group_name>/leave")
2023-02-27 05:33:45 +00:00
@limiter.limit('1/second', scope=rpath)
@limiter.limit('1/second', scope=rpath, key_func=get_ID)
@limiter.limit(DEFAULT_RATELIMIT)
@limiter.limit(DEFAULT_RATELIMIT, key_func=get_ID)
2023-02-24 06:31:06 +00:00
@auth_required
2023-02-24 19:29:07 +00:00
def leave_group(v:User, group_name):
2023-03-07 18:55:17 +00:00
group_name = group_name.strip().lower()
2023-02-28 20:07:31 +00:00
if group_name == 'jannies':
abort(403, "You can't leave !jannies")
2023-03-16 06:27:58 +00:00
group = g.db.get(Group, group_name)
2023-02-24 06:31:06 +00:00
if not group: abort(404)
2023-03-16 06:27:58 +00:00
existing = g.db.query(GroupMembership).filter_by(user_id=v.id, group_name=group_name).one_or_none()
2023-02-24 19:29:07 +00:00
if existing:
if existing.approved_utc:
text = f"@{v.username} has left !{group}"
msg = f"You have left !{group} successfully!"
else:
text = f"@{v.username} has cancelled their application to !{group}"
msg = f"You have cancelled your application to !{group} successfully!"
send_notification(group.owner.id, text)
2023-03-16 06:27:58 +00:00
g.db.delete(existing)
2023-02-25 21:44:02 +00:00
return {"message": msg}
2023-02-24 19:29:07 +00:00
2023-02-24 19:43:27 +00:00
return {"message": ''}
2023-02-24 19:29:07 +00:00
2023-02-25 22:06:43 +00:00
@app.get("/!<group_name>")
@limiter.limit(DEFAULT_RATELIMIT)
@limiter.limit(DEFAULT_RATELIMIT, key_func=get_ID)
2023-02-24 19:29:07 +00:00
@auth_required
def memberships(v:User, group_name):
2023-03-07 18:55:17 +00:00
group_name = group_name.strip().lower()
2023-03-16 06:27:58 +00:00
group = g.db.get(Group, group_name)
2023-02-24 19:29:07 +00:00
if not group: abort(404)
2023-02-24 19:53:40 +00:00
2023-03-16 06:27:58 +00:00
members = g.db.query(GroupMembership).filter(
2023-02-25 21:44:02 +00:00
GroupMembership.group_name == group_name,
GroupMembership.approved_utc != None
).order_by(GroupMembership.approved_utc).all()
2023-02-24 19:53:40 +00:00
2023-03-16 06:27:58 +00:00
applications = g.db.query(GroupMembership).filter(
2023-02-25 21:44:02 +00:00
GroupMembership.group_name == group_name,
GroupMembership.approved_utc == None
).order_by(GroupMembership.created_utc).all()
return render_template('group_memberships.html', v=v, group=group, members=members, applications=applications)
2023-02-24 06:31:06 +00:00
@app.post("/!<group_name>/<user_id>/approve")
2023-02-27 05:33:45 +00:00
@limiter.limit('1/second', scope=rpath)
@limiter.limit('1/second', scope=rpath, key_func=get_ID)
@limiter.limit(DEFAULT_RATELIMIT)
@limiter.limit(DEFAULT_RATELIMIT, key_func=get_ID)
2023-02-24 06:31:06 +00:00
@auth_required
def group_approve(v:User, group_name, user_id):
2023-03-07 18:55:17 +00:00
group_name = group_name.strip().lower()
2023-03-16 06:27:58 +00:00
group = g.db.get(Group, group_name)
2023-02-24 06:31:06 +00:00
if not group: abort(404)
2023-03-10 02:10:17 +00:00
if v.id != group.owner.id and v.admin_level < PERMS['MODS_EVERY_GROUP']:
2023-02-24 06:31:06 +00:00
abort(403, f"Only the group owner (@{group.owner.username}) can approve applications!")
2023-03-16 06:27:58 +00:00
application = g.db.query(GroupMembership).filter_by(user_id=user_id, group_name=group.name).one_or_none()
2023-02-24 06:31:06 +00:00
if not application:
abort(404, "There is no application to approve!")
if not application.approved_utc:
application.approved_utc = time.time()
2023-03-16 06:27:58 +00:00
g.db.add(application)
2023-02-24 06:31:06 +00:00
send_repeatable_notification(application.user_id, f"@{v.username} (!{group}'s owner) has approved your application!")
return {"message": f'You have approved @{application.user.username} successfully!'}
2023-02-24 06:31:06 +00:00
@app.post("/!<group_name>/<user_id>/reject")
2023-02-27 05:33:45 +00:00
@limiter.limit('1/second', scope=rpath)
@limiter.limit('1/second', scope=rpath, key_func=get_ID)
@limiter.limit(DEFAULT_RATELIMIT)
@limiter.limit(DEFAULT_RATELIMIT, key_func=get_ID)
2023-02-24 06:31:06 +00:00
@auth_required
def group_reject(v:User, group_name, user_id):
2023-03-07 18:55:17 +00:00
group_name = group_name.strip().lower()
2023-03-16 06:27:58 +00:00
group = g.db.get(Group, group_name)
2023-02-24 06:31:06 +00:00
if not group: abort(404)
2023-03-10 02:10:17 +00:00
if v.id != group.owner.id and v.admin_level < PERMS['MODS_EVERY_GROUP']:
abort(403, f"Only the group owner (@{group.owner.username}) can reject memberships!")
2023-03-16 06:27:58 +00:00
membership = g.db.query(GroupMembership).filter_by(user_id=user_id, group_name=group.name).one_or_none()
if not membership:
abort(404, "There is no membership to reject!")
2023-02-24 06:31:06 +00:00
if membership.approved_utc:
2023-03-25 23:44:48 +00:00
text = f"@{v.username} (!{group}'s owner) has kicked you from the group!"
msg = f"You have kicked @{membership.user.username} successfully!"
else:
text = f"@{v.username} (!{group}'s owner) has rejected your application!"
msg = f"You have rejected @{membership.user.username} successfully!"
2023-02-24 06:31:06 +00:00
send_repeatable_notification(membership.user_id, text)
2023-03-16 06:27:58 +00:00
g.db.delete(membership)
2023-02-24 19:43:27 +00:00
return {"message": msg}