-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi_following.py
More file actions
187 lines (154 loc) · 5.81 KB
/
api_following.py
File metadata and controls
187 lines (154 loc) · 5.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
"""
This module provides supporting functions for API routes pertaining to following.
"""
from flask import Blueprint, jsonify, request
import related_type_enum
import constants
import sql_query
following_bp = Blueprint('following', __name__)
@following_bp.route("/api/user/follow", methods=['POST'])
def api_user_follow():
"""
Creates a following relationship between two users
Example:
POST /api/user/follow?follower=LastFmProfileName&followee=LastFmProfileName
Raises:
400 Bad Request: If the follower or followee is not provided or invalid.
400 Bad Request: If a following record already exists for this follower/followee
combination.
Returns:
201 Success: The database ID of the newly created following record.
"""
follower = request.args.get("follower", "")
followee = request.args.get("followee", "")
follower_id = sql_query.query_user_id(follower)
followee_id = sql_query.query_user_id(followee)
if follower_id == 0:
return jsonify({"error": "Missing or invalid follower"}), 400
if followee_id == 0:
return jsonify({"error": "Missing or invalid followee"}), 400
following_id = sql_query.query_following_id(follower_id, followee_id)
if following_id != 0:
return jsonify({"error": f"This following already exists: {following_id}"}), 400
connection = sql_query.get_db_connection_isolation_none()
cursor = connection.cursor()
cursor.execute(
"INSERT INTO Following(FollowerID, FolloweeID, FollowingSince) " \
"VALUES (?, ?, CURRENT_TIMESTAMP)",
(follower_id, followee_id))
cursor.close()
connection.close()
sql_query.store_broadcast(0,
constants.SYSTEM_ACCOUNT_ID,
"New Following",
f"{follower} is now following {followee}!",
related_type_enum.RelatedType.FOLLOWING.value,
cursor.lastrowid)
return jsonify({"success": cursor.lastrowid}), 201
@following_bp.route("/api/user/unfollow", methods=['POST'])
def api_user_unfollow():
"""
Removes a following relationship between two users
Example:
POST /api/user/unfollow?follower=LastFmProfileName&followee=LastFmProfileName
Raises:
400 Bad Request: If the follower or followee is not provided or invalid.
400 Bad Request: If a following record does not exist for this follower/followee
combination.
Returns:
200 Success: Indicates the following record was successfully removed.
"""
follower = request.args.get("follower", "")
followee = request.args.get("followee", "")
follower_id = sql_query.query_user_id(follower)
followee_id = sql_query.query_user_id(followee)
if follower_id == 0:
return jsonify({"error": "Missing or invalid follower"}), 400
if followee_id == 0:
return jsonify({"error": "Missing or invalid followee"}), 400
following_id = sql_query.query_following_id(follower_id, followee_id)
if following_id == 0:
return jsonify({"error": "Could not locate a following between the specified users."}), 400
connection = sql_query.get_db_connection_isolation_none()
cursor = connection.cursor()
cursor.execute(
"DELETE FROM Following " \
"WHERE FollowingID = ?",
(following_id,))
cursor.close()
connection.close()
return jsonify({"success": f"Following {following_id} successfully removed."}), 200
@following_bp.route("/api/user/followers")
def api_user_followers():
"""
Retrieves records for who is following a user.
Example:
GET /api/user/followers?user=LastFmProfileName&limit=n
Returns JSON:
{
"followers": [
{ "follower": str, "followingsince": str },
…
]
}
"""
user = request.args.get("user", "")
limit = int(request.args.get("limit", -1))
user_id = sql_query.query_user_id(user)
if user_id == 0:
return jsonify({"error": "Missing or invalid user"}), 400
sql = """
SELECT Follower.LastFmProfileName as follower, Following.FollowingSince AS followingsince
FROM Following
INNER JOIN User AS Follower ON Following.FollowerID = Follower.UserID
WHERE Following.FolloweeID = ?
LIMIT ?
"""
conn = sql_query.get_db_connection()
rows = conn.execute(sql, (user_id, limit)).fetchall()
conn.close()
followers = [
{
"follower": row["follower"],
"followingsince": row["followingsince"]
}
for row in rows
]
return jsonify({ "followers": followers })
@following_bp.route("/api/user/following")
def api_user_following():
"""
Retrieves records for who a user is following.
Example:
GET /api/user/following?user=LastFmProfileName&limit=n
Returns JSON:
{
"following": [
{ "following": str, "followingsince": str },
…
]
}
"""
user = request.args.get("user", "")
limit = int(request.args.get("limit", -1))
user_id = sql_query.query_user_id(user)
if user_id == 0:
return jsonify({"error": "Missing or invalid user"}), 400
sql = """
SELECT Followee.LastFmProfileName as followee, Following.FollowingSince AS followingsince
FROM Following
INNER JOIN User AS Followee ON Following.FolloweeID = Followee.UserID
WHERE Following.FollowerID = ?
LIMIT ?
"""
conn = sql_query.get_db_connection()
rows = conn.execute(sql, (user_id, limit)).fetchall()
conn.close()
following = [
{
"following": row["followee"],
"followingsince": row["followingsince"]
}
for row in rows
]
return jsonify({ "following": following })