Exemplo n.º 1
0
def user_create(json: Dict[str, Any]) -> Dict[str, Any]:
    params = json["params"]
    params["password"] = get_hash(str(params["password"]))
    null_user = _database.user_get_id_by_email(params["email"])
    if null_user:
        raise jsonrpc.JSONRPCError({
            "code":
            -1002,
            "message":
            "Email '%s' already used" % params["email"],
        })
    null_user = _database.user_get_id_by_email(params["phone"])
    if null_user:
        raise jsonrpc.JSONRPCError({
            "code":
            -1003,
            "message":
            "Phone number '%s' already used" % params["phone"],
        })
    user = models.User.create_in_database(  # type: ignore
        _database,  # type: ignore
        **params,  # type: ignore
        role=constants.Roles.User,  # type: ignore
        status=constants.Status.Active,  # type: ignore
        points=0,  # type: ignore
    )  # type: ignore
    return jsonrpc.create_json_response(json, {"user_id": user.user_id})
Exemplo n.º 2
0
    def _callMethod(self, request_dict):
        """
        Here we actually find and call the method.

        @type request_dict: dict
        @param request_dict: Dict with details about the method

        @rtype: Deferred
        @return: Deferred, that will eventually fire with the method's result.

        @raise JSONRPCError: When method not found.
        """

        function = getattr(self, 'jsonrpc_%s' % request_dict['method'], None)
        if callable(function):

            if 'params' in request_dict:
                if isinstance(request_dict['params'], dict):
                    d = maybeDeferred(function, **request_dict['params'])
                else:
                    d = maybeDeferred(function, *request_dict['params'])
            else:
                d = maybeDeferred(function)

            return d

        else:
            msg = 'Method %s not found' % request_dict['method']
            raise jsonrpc.JSONRPCError(msg,
                                       jsonrpc.METHOD_NOT_FOUND,
                                       id_=request_dict['id'],
                                       version=request_dict['jsonrpc'])
Exemplo n.º 3
0
def _chat_exists(chat_id: int) -> None:
    current_chat_id = _database.single_chat_meta_info_get_by_chat_id(chat_id)
    if not current_chat_id:
        raise jsonrpc.JSONRPCError({
            "code": -1007,
            "message": "Chat does not exists"
        })
Exemplo n.º 4
0
def _authentication(user: interfaces.User,
                    expected_role: constants.Roles) -> None:
    if user.role < expected_role:
        raise jsonrpc.JSONRPCError({
            "code": -1005,
            "message": "Incorrect role"
        })
Exemplo n.º 5
0
def _get_user_id_by_session(session: int) -> int:
    user_id = _database.user_get_id_online_by_session(session)
    if not user_id:
        raise jsonrpc.JSONRPCError({
            "code": -1004,
            "message": "Incorrect session"
        })
    return user_id[0]["user_id"]
Exemplo n.º 6
0
def _check_blocks(user: interfaces.User, block_list: List[int]) -> None:
    if user.user_id in block_list:
        raise jsonrpc.JSONRPCError({
            "code":
            -1006,
            "message":
            "User '%d' was blocked by someone" % user.user_id,
        })
Exemplo n.º 7
0
def _check_user_in_chat(user: interfaces.User, chat: interfaces.Chat) -> None:
    for u in chat.users:
        if u.user_id == user.user_id:
            break
    else:
        raise jsonrpc.JSONRPCError({
            "code": -1008,
            "message": "Incorrect chat_id"
        })
Exemplo n.º 8
0
def _action_exists(action_id: int) -> None:
    result = _database.action_get(action_id)
    if not result:
        raise jsonrpc.JSONRPCError({
            "code":
            -1012,
            "message":
            "Action id '%d' is unknown" % action_id,
        })
Exemplo n.º 9
0
def _user_in_friends(source_user: interfaces.User,
                     target_user: interfaces.User) -> None:
    if source_user.user_id not in _get_friend_ids(target_user):
        raise jsonrpc.JSONRPCError({
            "code":
            -1011,
            "message":
            "User '%d' not in your friends" % source_user.user_id,
        })
Exemplo n.º 10
0
def _blocked_by_user(blocker: interfaces.User, user: interfaces.User) -> None:
    blocked_by_user = _get_blocked_user_ids(blocker)
    if user.user_id in blocked_by_user:
        raise jsonrpc.JSONRPCError({
            "code":
            -1009,
            "message":
            "User '%d' was blocked by user '%d'. Need to unblock" %
            (user.user_id, blocker.user_id),
        })
Exemplo n.º 11
0
    def checkAuthError(self, response):
        """
        Check for authentication error.

        @type response: t.w.c.Response
        @param response: Response object from the call

        @raise JSONRPCError: If the call failed with authorization error

        @rtype: t.w.c.Response
        @return If there was no error, just return the response
        """

        if response.code == 401:
            raise jsonrpc.JSONRPCError('Unauthorized', jsonrpc.INVALID_REQUEST)
        return response
Exemplo n.º 12
0
def _user_exists(user_data: List[Dict[str, Any]]) -> None:
    if not user_data:
        raise jsonrpc.JSONRPCError({"code": -1000, "message": "Unknown user."})
Exemplo n.º 13
0
def _check_password(user: interfaces.User, password: str) -> None:
    if user.password != get_hash(password):
        raise jsonrpc.JSONRPCError({
            "code": -1001,
            "message": "Incorrect password"
        })