diff options
Diffstat (limited to 'api')
-rw-r--r-- | api/auth/signup.py | 10 | ||||
-rw-r--r-- | api/game/accept.py | 7 | ||||
-rw-r--r-- | api/game/socket.py | 4 | ||||
-rw-r--r-- | api/hierarchy.py | 2 | ||||
-rw-r--r-- | api/rating.py | 6 | ||||
-rw-r--r-- | api/readme.md | 105 | ||||
-rw-r--r-- | api/user/avatar.py | 2 | ||||
-rw-r--r-- | api/user/info.py | 4 | ||||
-rw-r--r-- | api/user/password.py | 2 |
9 files changed, 89 insertions, 53 deletions
diff --git a/api/auth/signup.py b/api/auth/signup.py index 5e74076..98c07a7 100644 --- a/api/auth/signup.py +++ b/api/auth/signup.py @@ -16,7 +16,7 @@ def validate_username(username): def validate_email(email): #TODO: use node_modules/email-validator/index.js return len(email) > 1 and \ - "@" in email + "@" in email # checks if the password is safe (regex explanation in pages/register.tsx) @@ -40,14 +40,14 @@ def index(): # return 400 (malformed request) if any of the required data is missing if not username or \ - not email or \ - not password: + not email or \ + not password: return "", 400 # return 403 (forbidden) if any of the required data is invalid if not validate_username(username) or \ - not validate_email(email) or \ - not validate_password(password): + not validate_email(email) or \ + not validate_password(password): return {"error": "form_data_invalid"}, 403 # check if username is taken diff --git a/api/game/accept.py b/api/game/accept.py index 1f73fd1..3510ffd 100644 --- a/api/game/accept.py +++ b/api/game/accept.py @@ -5,7 +5,7 @@ import time import json import random from game.socket import game, games -from hierarchy import auth_required +from hierarchy import game_id_with_viewer from socket_io import io from game.new import start_game @@ -14,8 +14,9 @@ join_game = Blueprint('game_accept', __name__) # join a game by game_id (public or private) @join_game.route('/accept', methods=['POST']) -@auth_required("user") #FIXME: this doesn't work haha i don't know when i wrote this -def index(game_id): +@game_id_with_viewer +def index(game_id, user_id): + if not user_id: return "", 400 if cursor.execute("select status from games where game_id = ?", [game_id]).fetchone()[0] != "wait_for_opponent": return "", 403 diff --git a/api/game/socket.py b/api/game/socket.py index 69dc07e..ca28346 100644 --- a/api/game/socket.py +++ b/api/game/socket.py @@ -22,12 +22,12 @@ def participants_only(func): game_id = data["game_id"] if not game_id or \ - not game_id in games: + not game_id in games: return game = games[game_id] if game.player_1_id != user_id and \ - game.player_2_id != user_id: + game.player_2_id != user_id: return return func(data, user_id, game) diff --git a/api/hierarchy.py b/api/hierarchy.py index 206e27c..c50aa81 100644 --- a/api/hierarchy.py +++ b/api/hierarchy.py @@ -29,7 +29,7 @@ def util_two_id(type="user"): # if there's an explicit_id, validate it using `type` if explicit_id and \ - not valid.validate(explicit_id, type): + not valid.validate(explicit_id, type): explicit_id = None return func(token_id, explicit_id) diff --git a/api/rating.py b/api/rating.py index 5295f0d..406969b 100644 --- a/api/rating.py +++ b/api/rating.py @@ -13,9 +13,9 @@ def rating_v1(won_games): # python is a garbage language def get_all_games(user_id): return cursor.execute("select player_1_id, player_2_id, outcome " + \ - "from games " + \ - "where (player_1_id = ? or player_2_id = ?) " + \ - "and status = \"finished\" or status = \"resign\"", [user_id, user_id]).fetchall() + "from games " + \ + "where (player_1_id = ? or player_2_id = ?) " + \ + "and status = \"finished\" or status = \"resign\"", [user_id, user_id]).fetchall() # simple rating function that doesn't use game analysis diff --git a/api/readme.md b/api/readme.md index f46c812..648171a 100644 --- a/api/readme.md +++ b/api/readme.md @@ -79,7 +79,9 @@ API return type classes are mostly defined in api/api.ts <td> ```ts -{ id: userID } +{ + id: userID; +} ``` </td> @@ -87,7 +89,9 @@ API return type classes are mostly defined in api/api.ts <td> ```ts -{ userInfo } +{ + userInfo; +} ``` </td> @@ -102,7 +106,9 @@ API return type classes are mostly defined in api/api.ts <td> ```ts -{ userInfo } +{ + userInfo; +} ``` </td> @@ -115,7 +121,9 @@ API return type classes are mostly defined in api/api.ts <td> ```ts -{ id: userID } +{ + id: userID; +} ``` </td> @@ -181,7 +189,9 @@ returns error when image is not .png or larger than 256x256 <td> ```ts -{ image: base64PNG; } +{ + image: base64PNG; +} ``` </td> @@ -198,7 +208,9 @@ returns error when image is not .png or larger than 256x256 <td> ```ts -{ preferences: userPreferences; } +{ + preferences: userPreferences; +} ``` </td> @@ -211,7 +223,9 @@ returns error when image is not .png or larger than 256x256 <td> ```ts -{ newPreferences: userPreferences; } +{ + newPreferences: userPreferences; +} ``` </td> @@ -280,7 +294,9 @@ returns error when image is not .png or larger than 256x256 <td> ```ts -{ status: string; } +{ + status: string; +} ``` </td> @@ -295,7 +311,9 @@ returns error when image is not .png or larger than 256x256 <td> ```ts -{ id: userID } +{ + id: userID; +} ``` </td> @@ -310,7 +328,9 @@ returns error when image is not .png or larger than 256x256 <td> ```ts -{ id: userID } +{ + id: userID; +} ``` </td> @@ -325,7 +345,9 @@ returns error when image is not .png or larger than 256x256 <td> ```ts -{ id: userID } +{ + id: userID; +} ``` </td> @@ -340,7 +362,9 @@ returns error when image is not .png or larger than 256x256 <td> ```ts -{ query: string } +{ + query: string; +} ``` </td> @@ -361,7 +385,9 @@ returns error when image is not .png or larger than 256x256 <td> ```ts -{ id: userID } +{ + id: userID; +} ``` </td> @@ -376,7 +402,9 @@ returns error when image is not .png or larger than 256x256 <td> ```ts -{ id: userID } +{ + id: userID; +} ``` </td> @@ -474,7 +502,9 @@ returns error when image is not .png or larger than 256x256 <td> ```ts -{ id: gameID } +{ + id: gameID; +} ``` </td> @@ -482,7 +512,9 @@ returns error when image is not .png or larger than 256x256 <td> ```ts -{ gameInfo } +{ + gameInfo; +} ``` </td> @@ -495,7 +527,9 @@ returns error when image is not .png or larger than 256x256 <td> ```ts -{ id: gameID } +{ + id: gameID; +} ``` </td> @@ -520,7 +554,9 @@ returns error when image is not .png or larger than 256x256 <td> ```ts -{ id: gameID } +{ + id: gameID; +} ``` </td> @@ -657,7 +693,7 @@ example = Blueprint('example', __name__) @example.route('/example') def index(): - # python dictionaries are automatically converted to JSON by flask + # python dictionaries are automatically converted to JSON by flask return {"hello": "world"}, 200 # flask returns http code 200 by default if no code is explicitly defined # define a `dynamic_route` variable at the end of your endpoint definition file @@ -676,19 +712,18 @@ dynamic_route = ["/tests", status] ## Handy utility functions and where to find them All of the paths are defined relative to this (api/) directory. Most of these -functions/decorators should also have docstrings for editor autocompletion -help, but not all of them do. - -utility | description | file ------------------------------------|--------------------------------------------------------------------------|------------- -`@util_two_id(type)` | exposes (token_id, explicit_id) to the endpoint | hierarchy.py -`@two_person` | exposes (user_1_id, user_2_id) to the endpoint | hierarchy.py -`@one_person` | exposes (user_id) to the endpoint | hierarchy.py -`@game_id_with_viewer` | exposes (game_id, viewer?) to the endpoint | hierarchy.py -`@auth_required(level)` | checks if user is authorized and expose (user_id) to the endpoint | hierarchy.py -`@io_auth_required(level)` | same as @auth_required but for socket.io event listeners | hierarchy.py -`all_def([ ... ])` | checks if all items of the list are truthy | util.py -`all_notdef([ ... ])` | checks if all items of the list are falsy | util.py -`format_user(user_id, viewer_id?)` | format a user to /api/user/info format with optional viewer for relation | user/info.py -`format_game(game_id, viewer_id?)` | format a game to /api/game/info format with optional viewer for opponent | game/info.py - +functions/decorators should also have docstrings for editor autocompletion help, +but not all of them do. + +| utility | description | file | +| ---------------------------------- | ------------------------------------------------------------------------ | ------------ | +| `@util_two_id(type)` | exposes (token_id, explicit_id) to the endpoint | hierarchy.py | +| `@two_person` | exposes (user_1_id, user_2_id) to the endpoint | hierarchy.py | +| `@one_person` | exposes (user_id) to the endpoint | hierarchy.py | +| `@game_id_with_viewer` | exposes (game_id, viewer?) to the endpoint | hierarchy.py | +| `@auth_required(level)` | checks if user is authorized and expose (user_id) to the endpoint | hierarchy.py | +| `@io_auth_required(level)` | same as @auth_required but for socket.io event listeners | hierarchy.py | +| `all_def([ ... ])` | checks if all items of the list are truthy | util.py | +| `all_notdef([ ... ])` | checks if all items of the list are falsy | util.py | +| `format_user(user_id, viewer_id?)` | format a user to /api/user/info format with optional viewer for relation | user/info.py | +| `format_game(game_id, viewer_id?)` | format a game to /api/game/info format with optional viewer for opponent | game/info.py | diff --git a/api/user/avatar.py b/api/user/avatar.py index f55db4a..bfbc097 100644 --- a/api/user/avatar.py +++ b/api/user/avatar.py @@ -31,7 +31,7 @@ def update_avatar(user_id): if not request.data: return "", 400 open(f"database/avatars/{user_id}.png", "wb") \ - .write(decode(request.data, "base64")) + .write(decode(request.data, "base64")) return "", 200 diff --git a/api/user/info.py b/api/user/info.py index 3711371..a5e2aac 100644 --- a/api/user/info.py +++ b/api/user/info.py @@ -8,8 +8,8 @@ import json # get relation to user_2_id from user_1_id's perspective def get_relation_to(user_1_id, user_2_id): relation = cursor.execute("select * from social where " + \ - "(user_1_id = ? and user_2_id = ?) or " + \ - "(user_1_id = ? and user_2_id = ?)", [user_1_id, user_2_id, user_2_id, user_1_id]).fetchone() + "(user_1_id = ? and user_2_id = ?) or " + \ + "(user_1_id = ? and user_2_id = ?)", [user_1_id, user_2_id, user_2_id, user_1_id]).fetchone() if not relation: return "none" if relation[2] == "friendship": return "friends" if relation[2] == "outgoing" and relation[0] == user_1_id: diff --git a/api/user/password.py b/api/user/password.py index b77067f..51ab6db 100644 --- a/api/user/password.py +++ b/api/user/password.py @@ -10,7 +10,7 @@ def index(): data = request.get_json() if not data["password"] or \ - not data["newPassword"]: + not data["newPassword"]: return "", 400 return {}, 200 |