aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorlonkaars <l.leblansch@gmail.com>2021-03-12 20:36:33 +0100
committerlonkaars <l.leblansch@gmail.com>2021-03-12 20:36:33 +0100
commite6e74321b7dd62364172155e59b9c08454307f90 (patch)
tree113e68dd6757c12bf02a5603d1b67615a34dbb31
parentdecb742136440a43ad35ac20908c72b44108f3fa (diff)
friend requests are in the notification area now :tada:
-rw-r--r--api/social/friend_accept.py28
-rw-r--r--api/social/request_list.py26
-rw-r--r--components/navbar.tsx43
3 files changed, 86 insertions, 11 deletions
diff --git a/api/social/friend_accept.py b/api/social/friend_accept.py
new file mode 100644
index 0000000..8d09862
--- /dev/null
+++ b/api/social/friend_accept.py
@@ -0,0 +1,28 @@
+from flask import Blueprint, request
+from db import cursor, connection
+from auth.login_token import token_login
+import time
+
+accept = Blueprint('accept', __name__)
+
+@accept.route("/accept", methods = ['POST'])
+def route():
+ data = request.get_json()
+
+ user_1_id = data.get("id") or ""
+ token = request.cookies.get("token") or ""
+
+ if not token: return "", 401
+ user_2_id = token_login(token) or ""
+
+ if not user_1_id or \
+ not user_2_id:
+ return "", 403
+
+ cursor.execute("update social set type = \"friendship\" where user_1_id = ? and user_2_id = ?",
+ [user_1_id, user_2_id])
+
+ return "", 200
+
+dynamic_route = ["/social", accept]
+
diff --git a/api/social/request_list.py b/api/social/request_list.py
new file mode 100644
index 0000000..56426dc
--- /dev/null
+++ b/api/social/request_list.py
@@ -0,0 +1,26 @@
+from flask import Blueprint, request
+from db import cursor, connection
+from auth.login_token import token_login
+from user.info import format_user
+import time
+
+requests = Blueprint('requests', __name__)
+
+@requests.route("/requests")
+def route():
+ token = request.cookies.get("token") or ""
+ if not token: return "", 401
+ user_2_id = token_login(token) or ""
+ if not user_2_id: return "", 403
+
+ request_list = cursor.execute("select user_1_id from social where user_2_id = ? and type = \"outgoing\"",
+ [user_2_id]).fetchall()
+
+ 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
+
+dynamic_route = ["/social/list", requests]
+
diff --git a/components/navbar.tsx b/components/navbar.tsx
index c2c1299..635be86 100644
--- a/components/navbar.tsx
+++ b/components/navbar.tsx
@@ -1,4 +1,5 @@
import { CSSProperties, useEffect, useState, ReactNode } from "react";
+import axios from "axios";
import { LogoDark } from "../components/logo";
import { AccountAvatar } from "./account";
@@ -17,6 +18,7 @@ import CloseIcon from '@material-ui/icons/Close';
function NotificationsArea(props: {
visible?: boolean;
+ friendRequests?: Array<userInfo>;
}) {
return props.visible && <Bubble style={{
left: 48 + 12,
@@ -37,13 +39,8 @@ function NotificationsArea(props: {
height: 450 - 24 * 4,
borderRadius: 6
}}>
- <FriendRequest user={ {"id": "1398093580938", "username": "test", "status": "Lorum ipsum"} }/>
- <FriendRequest user={ {"id": "1398093580938", "username": "test", "status": "Lorum ipsum"} }/>
- <GameInvite game={ {"id": "23489023908", "opponent": { "username": "gert" }} }/>
- <FriendRequest user={ {"id": "1398093580938", "username": "test", "status": "Lorum ipsum"} }/>
- <FriendRequest user={ {"id": "1398093580938", "username": "test", "status": "Lorum ipsum"} }/>
- <FriendRequest user={ {"id": "1398093580938", "username": "test", "status": "Lorum ipsum"} }/>
- <FriendRequest user={ {"id": "1398093580938", "username": "test", "status": "Lorum ipsum"} }/>
+ { /* here should be the game invites */ }
+ { props.friendRequests?.map(user => <FriendRequest user={user}/>) }
</div>
</Bubble>
}
@@ -134,9 +131,31 @@ var NavBarItemStyle: CSSProperties = {
export function NavBar() {
var [ loggedIn, setLoggedIn ] = useState(false);
- useEffect(() => setLoggedIn(document.cookie.includes("token")), []);
+ var [ gotData, setGotData ] = useState(false);
+
+ var [ friendRequests, setFriendRequests ] = useState<Array<userInfo>>(null);
var [ notificationsAreaVisible, setNotificationsAreaVisible ] = useState(false);
+ var [ gotNotifications, setGotNotifications ] = useState(false);
+
+ useEffect(() => {(async () => {
+ if (gotData) return;
+ if (typeof window === "undefined") return;
+
+ var loggedIn = document.cookie.includes("token");
+ setLoggedIn(loggedIn);
+
+ if (loggedIn) {
+ var friendRequestsReq = await axios.request<{ requests: Array<userInfo> }>({
+ method: "get",
+ url: `/api/social/list/requests`
+ });
+ setFriendRequests(friendRequestsReq.data.requests);
+ setGotNotifications(true);
+ }
+
+ setGotData(true);
+ })()});
return <div className="navbar" style={{
width: 48,
@@ -174,15 +193,17 @@ export function NavBar() {
}}>
<div style={{ cursor: "pointer" }} onClick={() => setNotificationsAreaVisible(!notificationsAreaVisible)}>
<NotificationsIcon/>
- <div style={{
+ { gotNotifications && <div style={{
backgroundColor: "var(--disk-a)",
width: 8, height: 8,
borderRadius: 4,
position: "absolute",
top: 2, right: 2
- }}/>
+ }}/> }
</div>
- <NotificationsArea visible={notificationsAreaVisible}/>
+ <NotificationsArea
+ visible={notificationsAreaVisible}
+ friendRequests={friendRequests}/>
</a> }
<a href={loggedIn ? "/user" : "/login"} style={NavBarItemStyle}>
{