def get_collection_with_private_check(coll_id, user): coll = WorkshopCollection.from_id(coll_id) if coll.publish_state == PublicationState.PRIVATE \ and (user is None or not (coll.is_owner(int(user.id)) or coll.is_editor(int(user.id)))): raise Error(403, "This collection is private.") return coll
def remove_editor(user, coll_id, editor_id: int): coll = WorkshopCollection.from_id(coll_id) if not (coll.is_owner(int(user.id)) or editor_id == int(user.id)): return error(403, "you do not have permission to remove editors from this collection") coll.remove_editor(editor_id) return success(get_editors(coll), 200)
def delete_collection(user, coll_id): coll = WorkshopCollection.from_id(coll_id) if not coll.is_owner(int(user.id)): return error(403, "you do not have permission to delete this collection") coll.delete() return success(f"Deleted {coll.name}", 200)
def moderator_set_collection_state(_, body, coll_id): coll = WorkshopCollection.from_id(coll_id) try: coll.set_state(body['state'], run_checks=False) except ValueError as e: # invalid publication state return error(400, str(e)) return success(coll.to_dict(js=True), 200)
def set_state(user, body, coll_id): coll = WorkshopCollection.from_id(coll_id) if not coll.is_owner(int(user.id)): return error(403, "you do not have permission to change the state of this collection") try: coll.set_state(body['state']) except ValueError as e: # invalid publication state return error(400, str(e)) return success(coll.to_dict(js=True), 200)
def add_editor(user, coll_id, editor_id: int): coll = WorkshopCollection.from_id(coll_id) if not coll.is_owner(int(user.id)): return error(403, "You do not have permission to add editors to this collection.") if coll.is_owner(editor_id): return error(409, "You are already the owner of this collection.") try: coll.add_editor(editor_id) except NotAllowed as e: return error(409, str(e)) return success(get_editors(coll), 200)
def personal_subscribe(user, body, coll_id): coll = WorkshopCollection.from_id(coll_id) if body is None: alias_bindings = snippet_bindings = None else: alias_bindings = body['alias_bindings'] _bindings_check(coll, alias_bindings) snippet_bindings = body['snippet_bindings'] _bindings_check(coll, snippet_bindings) bindings = coll.subscribe(int(user.id), alias_bindings, snippet_bindings) return success(bindings, 200)
def guild_subscribe(user, body, coll_id, guild_id): guild_permissions_check(user, guild_id) coll = WorkshopCollection.from_id(coll_id) if body is None: alias_bindings = snippet_bindings = None else: alias_bindings = body['alias_bindings'] _bindings_check(coll, alias_bindings) snippet_bindings = body['snippet_bindings'] _bindings_check(coll, snippet_bindings) bindings = coll.set_server_active(guild_id, alias_bindings, snippet_bindings, invoker_id=int(user.id)) return success(bindings, 200)
def moderator_delete_collection(_, coll_id): coll = WorkshopCollection.from_id(coll_id) coll.delete(run_checks=False) return success(f"Deleted {coll.name}", 200)
def get_guild_subscription(coll_id, guild_id): coll = WorkshopCollection.from_id(coll_id) sub = coll.guild_sub(guild_id) if sub is None: return error(404, "You are not subscribed to this collection") return success(sub, 200)
def guild_unsubscribe(user, coll_id, guild_id): guild_permissions_check(user, guild_id) coll = WorkshopCollection.from_id(coll_id) coll.unset_server_active(guild_id, int(user.id)) return success(f"Unsubscribed from {coll.name}", 200)
def get_personal_subscription(user, coll_id): coll = WorkshopCollection.from_id(coll_id) sub = coll.my_sub(int(user.id)) if sub is None: return error(404, "You are not subscribed to this collection") return success(sub, 200)
def personal_unsubscribe(user, coll_id): coll = WorkshopCollection.from_id(coll_id) coll.unsubscribe(int(user.id)) return success(f"Unsubscribed from {coll.name}", 200)
def get_collection_with_editor_check(coll_id, user): coll = WorkshopCollection.from_id(coll_id) if not (coll.is_owner(int(user.id)) or coll.is_editor(int(user.id))): raise Error(403, "you do not have permission to edit this collection") return coll
def route_get_editors(_, coll_id): coll = WorkshopCollection.from_id(coll_id) return success(get_editors(coll), 200)