aboutsummaryrefslogtreecommitdiff
path: root/api/social/create_relation.py
diff options
context:
space:
mode:
authorlonkaars <l.leblansch@gmail.com>2021-04-11 17:50:58 +0200
committerlonkaars <l.leblansch@gmail.com>2021-04-11 17:50:58 +0200
commit28f104de9ae9abe4b42abafbf3865ede5687996c (patch)
tree65e651f09d8fbf81380384692e45803cb4f9d61c /api/social/create_relation.py
parent7b4859059b3bbabf4139ccdf3270a82c094f5d8e (diff)
dprint yapf python formatting
Diffstat (limited to 'api/social/create_relation.py')
-rw-r--r--api/social/create_relation.py78
1 files changed, 44 insertions, 34 deletions
diff --git a/api/social/create_relation.py b/api/social/create_relation.py
index 7e7c466..f58e105 100644
--- a/api/social/create_relation.py
+++ b/api/social/create_relation.py
@@ -4,56 +4,66 @@ from hierarchy import auth_required
from socket_io import io
import time
+
# @two_person_endpoint decorator
# defines (user_1_id, user_2_id) in endpoint handler function arguments
def two_person_endpoint(func):
- @auth_required("user")
- def wrapper(user_1_id):
- data = request.get_json()
- user_2_id = data.get("id") or ""
+ @auth_required("user")
+ def wrapper(user_1_id):
+ data = request.get_json()
+ user_2_id = data.get("id") or ""
+
+ if not user_1_id or \
+ not user_2_id:
+ return "", 403
+
+ return func(user_1_id, user_2_id)
- if not user_1_id or \
- not user_2_id:
- return "", 403
+ wrapper.__name__ = func.__name__
+ return wrapper
- return func(user_1_id, user_2_id)
- wrapper.__name__ = func.__name__
- return wrapper
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()
+ 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()
+
# remove relation between user_1_id and user_2_id (one-way)
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()
+ 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):
- @two_person_endpoint
- def route(user_1_id, user_2_id):
- create_relation(user_1_id, user_2_id, relation_type)
+ @two_person_endpoint
+ def route(user_1_id, user_2_id):
+ create_relation(user_1_id, user_2_id, relation_type)
- if relation_type == "outgoing":
- io.emit("incomingFriendRequest", room="user-"+user_2_id)
+ if relation_type == "outgoing":
+ io.emit("incomingFriendRequest", room="user-" + user_2_id)
- return "", 200
- return route
+ return "", 200
-friend_request = Blueprint('friend_request', __name__)
-friend_request.add_url_rule('/request', 'route', create_relation_route("outgoing"), methods = ["POST"])
+ return route
-block = Blueprint('block', __name__)
-block.add_url_rule('/block', 'route', create_relation_route("block"), methods = ["POST"])
-dynamic_routes = [
- ["/social", friend_request],
- ["/social", block]
- ]
+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]]