diff options
-rw-r--r-- | api/api.ts | 1 | ||||
-rw-r--r-- | api/user/info.py | 40 | ||||
-rw-r--r-- | components/recentGames.tsx | 2 | ||||
-rw-r--r-- | pages/user.tsx | 14 |
4 files changed, 39 insertions, 18 deletions
@@ -6,6 +6,7 @@ export interface userInfo { registered?: number, type?: string, username?: string, + relation?: "none"|"friends"|"incoming"|"outgoing"|"blocked", }; export type ruleset = { diff --git a/api/user/info.py b/api/user/info.py index 6dc48f1..e720e56 100644 --- a/api/user/info.py +++ b/api/user/info.py @@ -3,7 +3,22 @@ from db import cursor from auth.login_token import token_login import json -def format_user(user_id): +def valid_user_id(user_id): + query = cursor.execute("select user_id from users where user_id = ?", [user_id]).fetchone() + return bool(query) + +def get_relation_to(user_1_id, user_2_id): + relation = cursor.execute("select * from social where " + \ + "(user_1_id = ? and user_2_id = ?) or " + \ + "(user_1_id = ? and user_2_id = ?)", [user_1_id, user_2_id, user_2_id, user_1_id]).fetchone() + if not relation: return "none" + if relation[2] == "friendship": return "friends" + if relation[2] == "outgoing" and relation[0] == user_1_id: return "outgoing" + if relation[2] == "outgoing" and relation[1] == user_1_id: return "incoming" + if relation[2] == "block" and relation[0] == user_1_id: return "blocked" + return "none" + +def format_user(user_id, viewer = ''): user = cursor.execute("select " + ", ".join([ "username", "user_id", @@ -12,7 +27,7 @@ def format_user(user_id): "avatar", "status", ]) + " from users where user_id = ?", [user_id]).fetchone() - return { + formatted_user = { "username": user[0], "id": user[1], "country": user[2], @@ -20,6 +35,9 @@ def format_user(user_id): "avatar": user[4], "status": user[5], } + if viewer: + formatted_user["relation"] = get_relation_to(viewer, user_id) + return formatted_user info = Blueprint('info', __name__) @@ -31,24 +49,28 @@ def index(): username = data.get("username") or "" user_id = data.get("id") or "" token = request.cookies.get("token") or "" + viewer = "" if not username and \ not user_id and \ not token: return "", 400 - if token and not (username or user_id): - user_id = token_login(token) - - if username and not user_id: + if username: temp_user_id = cursor.execute("select user_id from users where username = ?", [username]).fetchone() if len(temp_user_id) > 0: user_id = temp_user_id - user = format_user(user_id) + if token: + self_id = token_login(token) + if not (username or user_id): + user_id = self_id + if user_id: + viewer = self_id - if not user: return "", 403 + if user_id and not valid_user_id(user_id): return "", 403 + user = format_user(user_id, viewer) #TODO: rating uitrekenen zodra er game functionaliteit is - return user + return user, 200 dynamic_route = ["/user", info] diff --git a/components/recentGames.tsx b/components/recentGames.tsx index 13bb87c..150520c 100644 --- a/components/recentGames.tsx +++ b/components/recentGames.tsx @@ -52,7 +52,7 @@ export default function RecentGames(props: { games?: Array<gameInfo> }) { <th style={{ width: "20%" }}>Datum</th> </tr> { - props.games?.map(game => <tr> + props.games?.map(game => <tr key={game.id}> <td style={LeftAlignedTableColumn}> <a href={"/user?id=" + game.opponent?.id} style={{ fontWeight: 500 diff --git a/pages/user.tsx b/pages/user.tsx index 2003696..e5b2083 100644 --- a/pages/user.tsx +++ b/pages/user.tsx @@ -78,8 +78,7 @@ export default function AccountPage() { var [editingStatus, setEditingStatus] = useState(false); - var [isFriends, setIsFriends] = useState(false); - var [hasBlocked, setHasBlocked] = useState(false); + var [relation, setRelation] = useState<userInfo["relation"]>("none"); var { toast } = useContext(ToastContext); @@ -112,6 +111,7 @@ export default function AccountPage() { data: { "id": id || self_id } }); setUser(userReq.data); + setRelation(userReq.data.relation || "none"); } if (!gameInfo) { @@ -179,7 +179,7 @@ export default function AccountPage() { } </div> : <div> - { hasBlocked ? + { relation == "blocked" ? <IconLabelButton icon={<Icon size={1} path={mdiAccountCancelOutline}/>} text="Deblokkeren" onclick={() => { /* axios.request({ */ /* method: "post", */ @@ -205,12 +205,11 @@ export default function AccountPage() { toast(`${user.username} geblokkeerd`, "confirmation", <Icon size={32 / 24} path={mdiAccountCancelOutline}/>); - setHasBlocked(true); - setIsFriends(false); + setRelation("blocked"); }); }}/> } - { isFriends ? + { relation == "friends" ? <IconLabelButton icon={<Icon size={1} path={mdiAccountMinusOutline}/>} text="Vriend verwijderen" onclick={() => { /* axios.request({ */ /* method: "post", */ @@ -236,8 +235,7 @@ export default function AccountPage() { toast("Vriendschapsverzoek gestuurd", "confirmation", <PersonAddOutlinedIcon style={{ fontSize: 32 }}/>); - setIsFriends(true); - setHasBlocked(false); + setRelation("outgoing"); }); }}/> } |