Spaces:
Runtime error
Runtime error
Commit
·
11213cb
1
Parent(s):
299424a
done
Browse files- app/dependencies.py +21 -16
- app/graphdb/main.py +11 -10
- app/routers/friend_request.py +30 -19
- app/routers/image.py +1 -1
app/dependencies.py
CHANGED
@@ -1,31 +1,36 @@
|
|
1 |
from fastapi import Depends, HTTPException, status
|
2 |
from fastapi.security import HTTPAuthorizationCredentials, HTTPBearer
|
3 |
-
from jwt import InvalidTokenError
|
4 |
-
from app import logger
|
5 |
from firebase_admin import auth
|
|
|
6 |
|
7 |
security = HTTPBearer()
|
8 |
|
9 |
|
10 |
-
|
11 |
credentials: HTTPAuthorizationCredentials = Depends(security),
|
12 |
):
|
13 |
-
credentials_exception = HTTPException(
|
14 |
-
status_code=status.HTTP_401_UNAUTHORIZED,
|
15 |
-
detail="Could not validate credentials",
|
16 |
-
headers={"WWW-Authenticate": "Bearer "},
|
17 |
-
)
|
18 |
try:
|
19 |
payload = auth.verify_id_token(credentials.credentials)
|
20 |
-
except
|
21 |
-
|
22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
except ValueError as e:
|
24 |
-
|
25 |
-
|
|
|
|
|
|
|
|
|
26 |
except Exception as e:
|
27 |
-
|
28 |
-
print(e)
|
29 |
-
raise credentials_exception
|
30 |
|
31 |
return payload
|
|
|
1 |
from fastapi import Depends, HTTPException, status
|
2 |
from fastapi.security import HTTPAuthorizationCredentials, HTTPBearer
|
|
|
|
|
3 |
from firebase_admin import auth
|
4 |
+
from firebase_admin.auth import ExpiredIdTokenError, InvalidIdTokenError
|
5 |
|
6 |
security = HTTPBearer()
|
7 |
|
8 |
|
9 |
+
def get_current_user(
|
10 |
credentials: HTTPAuthorizationCredentials = Depends(security),
|
11 |
):
|
|
|
|
|
|
|
|
|
|
|
12 |
try:
|
13 |
payload = auth.verify_id_token(credentials.credentials)
|
14 |
+
except ExpiredIdTokenError as e:
|
15 |
+
raise HTTPException(
|
16 |
+
status_code=status.HTTP_401_UNAUTHORIZED,
|
17 |
+
detail="Token expired",
|
18 |
+
headers={"WWW-Authenticate": "Bearer"},
|
19 |
+
)
|
20 |
+
except InvalidIdTokenError as e:
|
21 |
+
raise HTTPException(
|
22 |
+
status_code=status.HTTP_401_UNAUTHORIZED,
|
23 |
+
detail="Invalid token",
|
24 |
+
headers={"WWW-Authenticate": "Bearer"},
|
25 |
+
)
|
26 |
except ValueError as e:
|
27 |
+
raise HTTPException(
|
28 |
+
status_code=status.HTTP_401_UNAUTHORIZED,
|
29 |
+
detail="Invalid token",
|
30 |
+
headers={"WWW-Authenticate": "Bearer"},
|
31 |
+
)
|
32 |
+
|
33 |
except Exception as e:
|
34 |
+
raise HTTPException(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR)
|
|
|
|
|
35 |
|
36 |
return payload
|
app/graphdb/main.py
CHANGED
@@ -1,28 +1,29 @@
|
|
1 |
-
from app import driver
|
2 |
|
3 |
|
4 |
def match_person_nodes(tx, uid1: str, uid2: str):
|
5 |
-
tx.run(
|
6 |
-
tx.run(
|
7 |
tx.run(
|
8 |
-
|
9 |
+ uid1
|
10 |
-
+ "}) MATCH (p2:Person {uid: "
|
11 |
+ uid2
|
12 |
-
+ "}) MERGE (p1)-[:FRIEND]-(p2)
|
13 |
)
|
14 |
|
15 |
|
16 |
async def insert2PersonAndSetFriend(uid1: str, uid2: str):
|
|
|
17 |
with driver.session() as session:
|
18 |
session.write_transaction(match_person_nodes, uid1, uid2)
|
19 |
|
20 |
|
21 |
async def deleteFriend(uid1: str, uid2: str):
|
22 |
-
driver.execute_query(
|
23 |
-
|
24 |
+ uid1
|
25 |
-
+ "})-[r:FRIEND]-(p2:Person {uid: "
|
26 |
+ uid2
|
27 |
-
+ "}) DELETE r
|
28 |
)
|
|
|
1 |
+
from app import driver, logger
|
2 |
|
3 |
|
4 |
def match_person_nodes(tx, uid1: str, uid2: str):
|
5 |
+
tx.run('MERGE (p1:Person {uid: "' + uid1 + '"})')
|
6 |
+
tx.run('MERGE (p2:Person {uid: "' + uid2 + '"})')
|
7 |
tx.run(
|
8 |
+
'MATCH (p1:Person {uid: "'
|
9 |
+ uid1
|
10 |
+
+ '"}) MATCH (p2:Person {uid: "'
|
11 |
+ uid2
|
12 |
+
+ '"}) MERGE (p1)-[:FRIEND]-(p2)'
|
13 |
)
|
14 |
|
15 |
|
16 |
async def insert2PersonAndSetFriend(uid1: str, uid2: str):
|
17 |
+
logger.info(uid1 + " " + uid2)
|
18 |
with driver.session() as session:
|
19 |
session.write_transaction(match_person_nodes, uid1, uid2)
|
20 |
|
21 |
|
22 |
async def deleteFriend(uid1: str, uid2: str):
|
23 |
+
await driver.execute_query(
|
24 |
+
'MATCH (p1:Person {uid: "'
|
25 |
+ uid1
|
26 |
+
+ '"})-[r:FRIEND]-(p2:Person {uid: "'
|
27 |
+ uid2
|
28 |
+
+ '"}) DELETE r'
|
29 |
)
|
app/routers/friend_request.py
CHANGED
@@ -1,8 +1,8 @@
|
|
1 |
import datetime
|
2 |
import io
|
3 |
-
import cv2
|
4 |
-
from fastapi.responses import StreamingResponse
|
5 |
import qrcode
|
|
|
|
|
6 |
from fastapi import APIRouter, Depends, HTTPException, Response
|
7 |
from app.dependencies import get_current_user
|
8 |
from app import db, logger
|
@@ -24,13 +24,20 @@ EXPIRE_MINUTES = 15
|
|
24 |
|
25 |
|
26 |
@router.get("")
|
27 |
-
def getFriendRequest(
|
28 |
-
if
|
29 |
-
|
30 |
friend_requests = (
|
31 |
-
db.collection(COLLECTION_NAME)
|
|
|
|
|
32 |
)
|
33 |
-
|
|
|
|
|
|
|
|
|
|
|
34 |
|
35 |
|
36 |
@router.post("")
|
@@ -39,9 +46,9 @@ def createRequest(user=Depends(get_current_user)):
|
|
39 |
raise HTTPException(status_code=400, detail="User not found")
|
40 |
_, fr_ref = db.collection(COLLECTION_NAME).add(
|
41 |
{
|
42 |
-
"
|
43 |
"status": RequestStatus.WAITING_INVITEE.value,
|
44 |
-
"expire_at": datetime.datetime.now()
|
45 |
+ datetime.timedelta(minutes=EXPIRE_MINUTES),
|
46 |
}
|
47 |
)
|
@@ -59,7 +66,7 @@ def createRequest(user=Depends(get_current_user)):
|
|
59 |
|
60 |
|
61 |
@router.patch("/{RequestId}")
|
62 |
-
def acceptRequest(RequestId: str, user=Depends(get_current_user)):
|
63 |
if user["sub"] is None:
|
64 |
raise HTTPException(status_code=400, detail="User not found")
|
65 |
|
@@ -69,6 +76,8 @@ def acceptRequest(RequestId: str, user=Depends(get_current_user)):
|
|
69 |
if not fr.exists:
|
70 |
raise HTTPException(status_code=404, detail="Friend request not found")
|
71 |
|
|
|
|
|
72 |
if isRequestExpired(fr):
|
73 |
raise HTTPException(status_code=400, detail="Friend request expired")
|
74 |
|
@@ -76,19 +85,21 @@ def acceptRequest(RequestId: str, user=Depends(get_current_user)):
|
|
76 |
raise HTTPException(status_code=400, detail="Friend request already done")
|
77 |
|
78 |
if isInviter(user, fr):
|
|
|
|
|
79 |
fr_ref.update({"status": RequestStatus.COMPLETE.value})
|
80 |
-
makeFriend(fr
|
81 |
return {"status": "OK"}
|
82 |
|
83 |
if isInviteeEmpty(fr) and not isInviter(user, fr):
|
84 |
fr_ref.update(
|
85 |
{"invitee": user["sub"], "status": RequestStatus.WAITING_INVITER.value}
|
86 |
)
|
87 |
-
sendNotificationToInviter(fr
|
88 |
return {"status": "OK"}
|
89 |
|
90 |
|
91 |
-
|
92 |
return HTTPException(status_code=501, detail="Not implemented yet")
|
93 |
|
94 |
|
@@ -98,11 +109,11 @@ async def makeFriend(inviteeId: str, inviterId: str):
|
|
98 |
|
99 |
@router.delete("/{RequestId}")
|
100 |
def deleteRequest(RequestId: str, user=Depends(get_current_user)):
|
101 |
-
if user
|
102 |
raise HTTPException(status_code=400, detail="User not found")
|
103 |
|
104 |
Request_ref = db.collection(COLLECTION_NAME).document(RequestId)
|
105 |
-
Request = Request_ref.get()
|
106 |
|
107 |
if not Request.exists:
|
108 |
raise HTTPException(status_code=404, detail="Friend request not found")
|
@@ -115,16 +126,16 @@ def deleteRequest(RequestId: str, user=Depends(get_current_user)):
|
|
115 |
|
116 |
|
117 |
def isRequestExpired(request):
|
118 |
-
return request
|
119 |
|
120 |
|
121 |
def isInviter(user, Request):
|
122 |
-
return Request
|
123 |
|
124 |
|
125 |
def isRequestDone(Request):
|
126 |
-
return Request
|
127 |
|
128 |
|
129 |
def isInviteeEmpty(Request):
|
130 |
-
return Request.invitee is None
|
|
|
1 |
import datetime
|
2 |
import io
|
|
|
|
|
3 |
import qrcode
|
4 |
+
|
5 |
+
from fastapi.responses import StreamingResponse
|
6 |
from fastapi import APIRouter, Depends, HTTPException, Response
|
7 |
from app.dependencies import get_current_user
|
8 |
from app import db, logger
|
|
|
24 |
|
25 |
|
26 |
@router.get("")
|
27 |
+
def getFriendRequest(current_user=Depends(get_current_user)):
|
28 |
+
if current_user["sub"] is None:
|
29 |
+
return HTTPException(status_code=400, detail="User not found")
|
30 |
friend_requests = (
|
31 |
+
db.collection(COLLECTION_NAME)
|
32 |
+
.where("inviter", "==", current_user["sub"])
|
33 |
+
.stream()
|
34 |
)
|
35 |
+
|
36 |
+
return {
|
37 |
+
"friend_requests": [
|
38 |
+
{**Request.to_dict(), "id": Request.id} for Request in friend_requests
|
39 |
+
]
|
40 |
+
}
|
41 |
|
42 |
|
43 |
@router.post("")
|
|
|
46 |
raise HTTPException(status_code=400, detail="User not found")
|
47 |
_, fr_ref = db.collection(COLLECTION_NAME).add(
|
48 |
{
|
49 |
+
"inviter": user["sub"],
|
50 |
"status": RequestStatus.WAITING_INVITEE.value,
|
51 |
+
"expire_at": datetime.datetime.now(tz=datetime.timezone.utc)
|
52 |
+ datetime.timedelta(minutes=EXPIRE_MINUTES),
|
53 |
}
|
54 |
)
|
|
|
66 |
|
67 |
|
68 |
@router.patch("/{RequestId}")
|
69 |
+
async def acceptRequest(RequestId: str, user=Depends(get_current_user)):
|
70 |
if user["sub"] is None:
|
71 |
raise HTTPException(status_code=400, detail="User not found")
|
72 |
|
|
|
76 |
if not fr.exists:
|
77 |
raise HTTPException(status_code=404, detail="Friend request not found")
|
78 |
|
79 |
+
fr = fr.to_dict()
|
80 |
+
|
81 |
if isRequestExpired(fr):
|
82 |
raise HTTPException(status_code=400, detail="Friend request expired")
|
83 |
|
|
|
85 |
raise HTTPException(status_code=400, detail="Friend request already done")
|
86 |
|
87 |
if isInviter(user, fr):
|
88 |
+
if isInviteeEmpty(fr):
|
89 |
+
raise HTTPException(status_code=400, detail="Invitee is empty")
|
90 |
fr_ref.update({"status": RequestStatus.COMPLETE.value})
|
91 |
+
await makeFriend(fr["invitee"], fr["inviter"])
|
92 |
return {"status": "OK"}
|
93 |
|
94 |
if isInviteeEmpty(fr) and not isInviter(user, fr):
|
95 |
fr_ref.update(
|
96 |
{"invitee": user["sub"], "status": RequestStatus.WAITING_INVITER.value}
|
97 |
)
|
98 |
+
sendNotificationToInviter(fr["inviter"], user)
|
99 |
return {"status": "OK"}
|
100 |
|
101 |
|
102 |
+
def sendNotificationToInviter(inviterId: str, invitee):
|
103 |
return HTTPException(status_code=501, detail="Not implemented yet")
|
104 |
|
105 |
|
|
|
109 |
|
110 |
@router.delete("/{RequestId}")
|
111 |
def deleteRequest(RequestId: str, user=Depends(get_current_user)):
|
112 |
+
if user.sub is None:
|
113 |
raise HTTPException(status_code=400, detail="User not found")
|
114 |
|
115 |
Request_ref = db.collection(COLLECTION_NAME).document(RequestId)
|
116 |
+
Request = Request_ref.get().to_dict()
|
117 |
|
118 |
if not Request.exists:
|
119 |
raise HTTPException(status_code=404, detail="Friend request not found")
|
|
|
126 |
|
127 |
|
128 |
def isRequestExpired(request):
|
129 |
+
return request["expire_at"] < datetime.datetime.now(tz=datetime.timezone.utc)
|
130 |
|
131 |
|
132 |
def isInviter(user, Request):
|
133 |
+
return Request["inviter"] == user["sub"]
|
134 |
|
135 |
|
136 |
def isRequestDone(Request):
|
137 |
+
return Request["status"] == RequestStatus.COMPLETE.value
|
138 |
|
139 |
|
140 |
def isInviteeEmpty(Request):
|
141 |
+
return True if Request.get("invitee", None) is None else False
|
app/routers/image.py
CHANGED
@@ -19,7 +19,7 @@ async def handleImageRequest(
|
|
19 |
try:
|
20 |
img = imfrombytes(file, cv2.IMREAD_COLOR)
|
21 |
except:
|
22 |
-
|
23 |
|
24 |
if raw:
|
25 |
bboxes, labels = inferenceImage(img, threshold, raw)
|
|
|
19 |
try:
|
20 |
img = imfrombytes(file, cv2.IMREAD_COLOR)
|
21 |
except:
|
22 |
+
raise Response(content="Failed to read image", status_code=400)
|
23 |
|
24 |
if raw:
|
25 |
bboxes, labels = inferenceImage(img, threshold, raw)
|