diff options
author | lonkaars <l.leblansch@gmail.com> | 2021-03-12 15:24:25 +0100 |
---|---|---|
committer | lonkaars <l.leblansch@gmail.com> | 2021-03-12 15:24:25 +0100 |
commit | a69b5df8beddb4a9b492e29ba32747c368666239 (patch) | |
tree | e1d0a5b9a6cc9f5e3291759eb74539422a672050 /api | |
parent | 3d36847211e37c5aed0a7fdda5c2a00399fdc66e (diff) |
friend request api endpoints
Diffstat (limited to 'api')
-rw-r--r-- | api/dynamic_import.py | 14 | ||||
-rw-r--r-- | api/social/create_relation.py | 49 | ||||
-rwxr-xr-x | api/tests.sh | 34 |
3 files changed, 85 insertions, 12 deletions
diff --git a/api/dynamic_import.py b/api/dynamic_import.py index 3b05764..76281c2 100644 --- a/api/dynamic_import.py +++ b/api/dynamic_import.py @@ -13,10 +13,16 @@ files = [str(filename) .replace(".py", '') for filename in files] +def route(dynamic_route): + app.register_blueprint(dynamic_route[1], url_prefix=dynamic_route[0]) + path = (dynamic_route[0] + "/" + dynamic_route[1].name).replace('//', '/') + log.info(f"dynamically routing {path}") + for file in files: mod = importlib.import_module(file) - if not hasattr(mod, "dynamic_route"): continue - app.register_blueprint(mod.dynamic_route[1], url_prefix=mod.dynamic_route[0]) - path = (mod.dynamic_route[0] + "/" + mod.dynamic_route[1].name).replace('//', '/') - log.info(f"dynamically routing {path}") + if hasattr(mod, "dynamic_route"): + route(mod.dynamic_route) + elif hasattr(mod, "dynamic_routes"): + for dynamic_route in mod.dynamic_routes: + route(dynamic_route) diff --git a/api/social/create_relation.py b/api/social/create_relation.py new file mode 100644 index 0000000..7451b6d --- /dev/null +++ b/api/social/create_relation.py @@ -0,0 +1,49 @@ +from flask import Blueprint, request +from db import cursor, connection +from auth.login_token import token_login +import time + +def create_relation(user_1_id, user_2_id, relation_type): + remove_relation(user_1_id, user_2_id) + remove_relation(user_2_id, user_1_id) + timestamp = int( time.time() * 1000 ) + cursor.execute("insert into social values (?, ?, ?, ?)", + [user_1_id, user_2_id, relation_type, timestamp]) + connection.commit() + +def remove_relation(user_1_id, user_2_id): + cursor.execute("delete from social where user_1_id = ? and user_2_id = ?", + [user_1_id, user_2_id]) + connection.commit + +def create_relation_route(relation_type): + def route(): + data = request.get_json() + + user_2_id = data.get("id") or "" + token = request.cookies.get("token") or "" + + if not token: return "", 401 + user_1_id = token_login(token) or "" + + if not user_1_id or \ + not user_2_id: + return "", 403 + + create_relation(user_1_id, user_2_id, relation_type) + return "", 200 + return route + + +friend_request = Blueprint('friend_request', __name__) +friend_request.add_url_rule('/request', 'route', create_relation_route("outgoing"), methods = ["POST"]) + +block = Blueprint('block', __name__) +block.add_url_rule('/block', 'route', create_relation_route("block"), methods = ["POST"]) + +dynamic_routes = [ + ["/social", friend_request], + ["/social", block] + ] + + diff --git a/api/tests.sh b/api/tests.sh index 8ee9d68..3e4dae2 100755 --- a/api/tests.sh +++ b/api/tests.sh @@ -100,12 +100,30 @@ # search loekaars -games () { - curl -X POST \ - -H "Content-Type: application/json" \ - -d "{ \"id\": \"4577c119-c768-4ad5-afec-b53a5c19baf4\" }" \ - localhost:2080/api/user/games -} - -games +# games () { +# curl -X POST \ +# -H "Content-Type: application/json" \ +# -d "{ \"id\": \"4577c119-c768-4ad5-afec-b53a5c19baf4\" }" \ +# localhost:2080/api/user/games +# } + +# games + +curl -X POST \ + -H "Content-Type: application/json" \ + --cookie "token= 40183c739ae198cee3718c81c72b1bbd56ff83d9fcdbb9badb9ecef3684f98cf8df391aa31a8c1c8cfa55d1161a847fd60040c5b28104892e20b2d7e6eaf1cfc79f3bb288b50718c015834f3c162e1d3c771afc23d53b316b20ab20922244c0ddec789d3427b6bbaba766dee34f77b792cce2a1cd8e65ae69b16289d285d93e3" \ + -d '{ + "id": "a651f66c-a769-40a7-a962-0a4e1bd38d42" + }' \ + localhost:2080/api/social/block + +sleep 10 + +curl -X POST \ + -H "Content-Type: application/json" \ + --cookie "token= 40183c739ae198cee3718c81c72b1bbd56ff83d9fcdbb9badb9ecef3684f98cf8df391aa31a8c1c8cfa55d1161a847fd60040c5b28104892e20b2d7e6eaf1cfc79f3bb288b50718c015834f3c162e1d3c771afc23d53b316b20ab20922244c0ddec789d3427b6bbaba766dee34f77b792cce2a1cd8e65ae69b16289d285d93e3" \ + -d '{ + "id": "a651f66c-a769-40a7-a962-0a4e1bd38d42" + }' \ + localhost:2080/api/social/request |