示例#1
0
def send_notification_to_all(message: str):
    if socketio is None:
        raise Exception('Tried to emit but socketio is not initialized!')

    profiles = Profile.select().where(~Profile.is_admin).execute()
    notifications = [(profile.id, message) for profile in profiles]
    for batch in chunked(notifications, 100):
        Notification.insert_many(
            batch, fields=[Notification.profile,
                           Notification.content]).execute()

    # LOOKATME: Seems slow
    for profile in profiles:
        if not profile.socket_id:
            continue
        socketio.emit('notification', message, room=profile.socket_id)
示例#2
0
def broadcast_in_a_game(game: Game,
                        topic: str,
                        text: str,
                        as_notification=False):
    if socketio is None:
        raise Exception('Tried to emit but socketio is not initialized!')

    profiles = Profile.select().join(GameProfile).where(
        GameProfile.game == game.id).execute()

    if as_notification:
        notifications = [(profile.id, text) for profile in profiles]
        for batch in chunked(notifications, 100):
            Notification.insert_many(
                batch, fields=[Notification.profile,
                               Notification.content]).execute()

    # LOOKATME: Seems slow
    for profile in profiles:
        if not profile.socket_id:
            continue
        if as_notification:
            socketio.emit('notification', text, room=profile.socket_id)
        socketio.emit(topic, text, room=profile.socket_id)
示例#3
0
def get_users_except_admins():
    users = Profile.select().where(~Profile.is_admin,
                                   ~Profile.is_banned).execute()
    return users
示例#4
0
def json_and_response_code(request):
    if "username" not in request.json or "password" not in request.json:
        return NULL_MESSAGE.dual

    account = attempt_login(request.json["username"], request.json["password"])
    if account is None:
        return INVALID_CREDENTIALS.dual

    client_token: ClientToken
    if "clientToken" not in request.json:
        client_token = ClientToken(account=account)
        client_token_string = client_token.uuid.hex
    else:
        try:
            client_token_uuid = UUID(request.json["clientToken"])
        except ValueError:
            return INVALID_TOKEN.dual

        client_token = ClientToken.get(uuid=client_token_uuid)
        if client_token is not None and client_token.account != account:
            client_token.account = account

        elif client_token is None:  # there's no client token with requested UUID
            client_token = ClientToken(account=account, uuid=client_token_uuid)

        else:  # requested clientToken exists and owned by authorized account
            client_token.refresh()

        client_token_string = request.json["clientToken"]

    optional_profile = {}
    if "agent" in request.json:
        available_profiles = list(
            Profile.select(lambda p: p.agent == request.json["agent"]["name"]
                           and p.account == account))
        if len(available_profiles) == 1:
            optional_profile = {"profile": available_profiles[0]}

    access_token = AccessToken(client_token=client_token, **optional_profile)

    response_data = {
        "accessToken": jwt_encode(access_token.format(), key="").decode(),
        "clientToken": client_token_string
    }

    if "requestUser" in request.json and request.json["requestUser"]:
        response_data["user"] = {
            "username": account.username,
            "id": account.uuid.hex
        }

    response_data["availableProfiles"] = []
    if "agent" in request.json:
        for profile in available_profiles:
            response_data["availableProfiles"].append({
                "name": profile.name,
                "id": profile.uuid.hex,
            })

        if len(available_profiles) == 1:
            response_data["selectedProfile"] = response_data[
                "availableProfiles"][0]

    for token in AccessToken \
            .select(lambda candidate: candidate.client_token.account == account and candidate != access_token):
        token.authentication_valid = False

    return jsonify(response_data), 200
def get_user_name_by_profile_id(profile_id):
    return Profile.select(username).where(Profile.id == profile_id)