diff options
author | lonkaars <l.leblansch@gmail.com> | 2021-04-16 16:57:26 +0200 |
---|---|---|
committer | lonkaars <l.leblansch@gmail.com> | 2021-04-16 16:57:26 +0200 |
commit | 07c2b124e4348b15f1e5ec18c6cdfd77248c6bc8 (patch) | |
tree | e4a29123d3ebedc1d25500390c904c66b3b02489 /api/social | |
parent | aa2c999702dadba2afbcf2be9f597f890aafcc87 (diff) |
spaces > tabs in python :(
Diffstat (limited to 'api/social')
-rw-r--r-- | api/social/create_relation.py | 44 | ||||
-rw-r--r-- | api/social/destroy_relation.py | 20 | ||||
-rw-r--r-- | api/social/friend_accept.py | 16 | ||||
-rw-r--r-- | api/social/request_list.py | 20 | ||||
-rw-r--r-- | api/social/search.py | 40 |
5 files changed, 70 insertions, 70 deletions
diff --git a/api/social/create_relation.py b/api/social/create_relation.py index 5367ac5..01ad0eb 100644 --- a/api/social/create_relation.py +++ b/api/social/create_relation.py @@ -6,46 +6,46 @@ 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() + 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 - def route(user_1_id, user_2_id): - create_relation(user_1_id, user_2_id, relation_type) + @two_person + 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 "", 200 - return route + return route friend_request = Blueprint('friend_request', __name__) friend_request.add_url_rule( - '/request', 'route', create_relation_route("outgoing"), methods=["POST"] + '/request', 'route', create_relation_route("outgoing"), methods=["POST"] ) block = Blueprint('block', __name__) block.add_url_rule( - '/block', 'route', create_relation_route("block"), methods=["POST"] + '/block', 'route', create_relation_route("block"), methods=["POST"] ) dynamic_routes = [["/social", friend_request], ["/social", block]] diff --git a/api/social/destroy_relation.py b/api/social/destroy_relation.py index 2aa793b..d2e4388 100644 --- a/api/social/destroy_relation.py +++ b/api/social/destroy_relation.py @@ -12,16 +12,16 @@ remove = Blueprint('remove', __name__) @remove.route('/remove', methods=['POST']) @two_person def index(user_1_id, user_2_id): - relation = get_relation_to(user_1_id, user_2_id) - if relation == "none": return "", 403 + relation = get_relation_to(user_1_id, user_2_id) + if relation == "none": return "", 403 - remove_relation(user_1_id, user_2_id) - remove_relation(user_2_id, user_1_id) + remove_relation(user_1_id, user_2_id) + remove_relation(user_2_id, user_1_id) - io.emit("changedRelation", {"id": user_2_id}, room="user-" + user_1_id) - io.emit("changedRelation", {"id": user_1_id}, room="user-" + user_2_id) + io.emit("changedRelation", {"id": user_2_id}, room="user-" + user_1_id) + io.emit("changedRelation", {"id": user_1_id}, room="user-" + user_2_id) - return "", 200 + return "", 200 unblock = Blueprint('unblock', __name__) @@ -30,10 +30,10 @@ unblock = Blueprint('unblock', __name__) @unblock.route('/unblock', methods=['POST']) @two_person def index(user_1_id, user_2_id): - if get_relation_to(user_1_id, user_2_id) != "blocked": return "", 403 + if get_relation_to(user_1_id, user_2_id) != "blocked": return "", 403 - remove_relation(user_1_id, user_2_id) - return "", 200 + remove_relation(user_1_id, user_2_id) + return "", 200 dynamic_routes = [["/social", remove], ["/social", unblock]] diff --git a/api/social/friend_accept.py b/api/social/friend_accept.py index b434272..45edc72 100644 --- a/api/social/friend_accept.py +++ b/api/social/friend_accept.py @@ -10,16 +10,16 @@ accept = Blueprint('accept', __name__) @accept.route("/accept", methods=['POST']) @two_person def route(user_1_id, user_2_id): - cursor.execute( - "update social set type = \"friendship\" where user_1_id = ? and user_2_id = ?", - [user_2_id, user_1_id] - ) - connection.commit() + cursor.execute( + "update social set type = \"friendship\" where user_1_id = ? and user_2_id = ?", + [user_2_id, user_1_id] + ) + connection.commit() - io.emit("changedRelation", {"id": user_2_id}, room="user-" + user_1_id) - io.emit("changedRelation", {"id": user_1_id}, room="user-" + user_2_id) + io.emit("changedRelation", {"id": user_2_id}, room="user-" + user_1_id) + io.emit("changedRelation", {"id": user_1_id}, room="user-" + user_2_id) - return "", 200 + return "", 200 dynamic_route = ["/social", accept] diff --git a/api/social/request_list.py b/api/social/request_list.py index 9b79203..f991ce2 100644 --- a/api/social/request_list.py +++ b/api/social/request_list.py @@ -10,18 +10,18 @@ requests = Blueprint('requests', __name__) @requests.route("/requests") @auth_required("user") def route(user_2_id): - # get a list of friend requests - request_list = cursor.execute( - "select user_1_id from social where user_2_id = ? and type = \"outgoing\"", - [user_2_id] - ).fetchall() + # get a list of friend requests + request_list = cursor.execute( + "select user_1_id from social where user_2_id = ? and type = \"outgoing\"", + [user_2_id] + ).fetchall() - # get user_id for each result to prevent repeat user/info requests - formatted_request_list = [] - for user_1_id in [q[0] for q in request_list]: - formatted_request_list.append(format_user(user_1_id)) + # get user_id for each result to prevent repeat user/info requests + formatted_request_list = [] + for user_1_id in [q[0] for q in request_list]: + formatted_request_list.append(format_user(user_1_id)) - return {"requests": formatted_request_list}, 200 + return {"requests": formatted_request_list}, 200 dynamic_route = ["/social/list", requests] diff --git a/api/social/search.py b/api/social/search.py index f0ce8a2..c0ef312 100644 --- a/api/social/search.py +++ b/api/social/search.py @@ -8,26 +8,26 @@ search = Blueprint('search', __name__) @search.route('/search', methods=['POST']) def index(): - data_string = request.data or "{}" - data = json.loads(data_string) - query = data.get("query") or "" - if not query: return "", 400 - if len(query) < 3: return "", 403 - - # use levenshtein with max distance 3 to search for users - #TODO: use mysql and sort by best match - results = cursor.execute( - "select user_id from users where levenshtein(lower(username), lower(?), 3)", - [query] - ).fetchmany(20) - - formatted = {"results": []} - - # get user_id for each result to prevent repeat user/info requests - for user in results: - formatted["results"].append(format_user(user[0])) - - return formatted, 200 + data_string = request.data or "{}" + data = json.loads(data_string) + query = data.get("query") or "" + if not query: return "", 400 + if len(query) < 3: return "", 403 + + # use levenshtein with max distance 3 to search for users + #TODO: use mysql and sort by best match + results = cursor.execute( + "select user_id from users where levenshtein(lower(username), lower(?), 3)", + [query] + ).fetchmany(20) + + formatted = {"results": []} + + # get user_id for each result to prevent repeat user/info requests + for user in results: + formatted["results"].append(format_user(user[0])) + + return formatted, 200 dynamic_route = ["/social", search] |