diff options
| -rw-r--r-- | api/social/create_relation.py | 32 | ||||
| -rw-r--r-- | api/social/destroy_relation.py | 33 | 
2 files changed, 23 insertions, 42 deletions
| diff --git a/api/social/create_relation.py b/api/social/create_relation.py index 49bb4bc..eb38978 100644 --- a/api/social/create_relation.py +++ b/api/social/create_relation.py @@ -1,9 +1,23 @@  from flask import Blueprint, request  from db import cursor, connection -from auth.login_token import token_login +from hierarchy import auth_required  from socket_io import io  import time +def two_person_endpoint(func): +    @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) +    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) @@ -18,20 +32,10 @@ def remove_relation(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 - +    @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) diff --git a/api/social/destroy_relation.py b/api/social/destroy_relation.py index a08175f..4b61ecd 100644 --- a/api/social/destroy_relation.py +++ b/api/social/destroy_relation.py @@ -1,7 +1,6 @@  from flask import Blueprint, request  from db import cursor -from auth.login_token import token_login -from social.create_relation import remove_relation +from social.create_relation import remove_relation, two_person_endpoint  from user.info import get_relation_to  from socket_io import io  import time @@ -9,19 +8,8 @@ import time  remove = Blueprint('remove', __name__)  @remove.route('/remove', methods = ['POST']) -def index(): -    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 - +@two_person_endpoint +def index(user_1_id, user_2_id):      relation = get_relation_to(user_1_id, user_2_id)      if relation == "none": return "", 403 @@ -36,19 +24,8 @@ def index():  unblock = Blueprint('unblock', __name__)  @unblock.route('/unblock', methods = ['POST']) -def index(): -    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 - +@two_person_endpoint +def index(user_1_id, user_2_id):      if get_relation_to(user_1_id, user_2_id) != "blocked": return "", 403      remove_relation(user_1_id, user_2_id) |