From abba7e0f02891276291c0643fbfc82510598f82a Mon Sep 17 00:00:00 2001 From: wentokay <101902546+wentokay@users.noreply.github.com> Date: Wed, 5 Apr 2023 18:15:45 +0100 Subject: [PATCH] friends updates (#3617) --- .../backpack-api/src/routes/v1/friends.ts | 75 ++++++------------- .../backpack-api/src/routes/v1/inbox.ts | 25 ++----- 2 files changed, 31 insertions(+), 69 deletions(-) diff --git a/backend/native/backpack-api/src/routes/v1/friends.ts b/backend/native/backpack-api/src/routes/v1/friends.ts index 1d0e0448d..84bd5eb4e 100644 --- a/backend/native/backpack-api/src/routes/v1/friends.ts +++ b/backend/native/backpack-api/src/routes/v1/friends.ts @@ -1,10 +1,6 @@ import { insertNotification } from "@coral-xyz/backend-common"; import type { RemoteUserData } from "@coral-xyz/common"; -import { - AVATAR_BASE_URL, - EXECUTE_BARTER, - NOTIFICATION_ADD, -} from "@coral-xyz/common"; +import { AVATAR_BASE_URL, NOTIFICATION_ADD } from "@coral-xyz/common"; import express from "express"; import { extractUserId } from "../../auth/middleware"; @@ -26,11 +22,8 @@ import { enrichFriendships } from "./inbox"; const router = express.Router(); router.post("/spam", extractUserId, async (req, res) => { - //@ts-ignore - const uuid: string = req.id; // TODO from from - // @ts-ignore - const to: string = req.body.to; - // @ts-ignore + const uuid = req.id!; // TODO from from + const to: string = req.body.to!; if (uuid === to) { res.status(411).json({ @@ -44,11 +37,8 @@ router.post("/spam", extractUserId, async (req, res) => { }); router.post("/block", extractUserId, async (req, res) => { - //@ts-ignore - const uuid: string = req.id; // TODO from from - // @ts-ignore - const to: string = req.body.to; - // @ts-ignore + const uuid = req.id!; // TODO from from + const to: string = req.body.to!; if (uuid === to) { res.status(411).json({ @@ -62,12 +52,8 @@ router.post("/block", extractUserId, async (req, res) => { }); router.post("/unfriend", extractUserId, async (req, res) => { - //@ts-ignore - const uuid: string = req.id; // TODO from from - // @ts-ignore - const to: string = req.body.to; - // @ts-ignore - + const uuid = req.id!; // TODO from from + const to: string = req.body.to!; if (uuid === to) { res.status(411).json({ msg: "To and from cant be the same", @@ -79,13 +65,11 @@ router.post("/unfriend", extractUserId, async (req, res) => { }); router.get("/sent", extractUserId, async (req, res) => { - // @ts-ignore - const uuid: string = req.id; - + const uuid = req.id!; const requestedUserIds = await getSentRequests({ uuid }); const users = await getUsers(requestedUserIds); - const requestedWithMetadata: RemoteUserData[] = requestedUserIds.map( - (userId) => ({ + const requestedWithMetadata: Omit[] = + requestedUserIds.map((userId) => ({ id: userId, username: users.find((x) => x.id === userId)?.username as string, image: `${AVATAR_BASE_URL}/${ @@ -94,19 +78,16 @@ router.get("/sent", extractUserId, async (req, res) => { areFriends: false, remoteRequested: false, requested: true, - }) - ); + })); res.json({ requests: requestedWithMetadata }); }); router.get("/requests", extractUserId, async (req, res) => { - //@ts-ignore - const uuid: string = req.id; // TODO from from - + const uuid = req.id!; // TODO from from const requestUserIds = await getReceivedRequests({ uuid }); const users = await getUsers(requestUserIds); - const requestsWithMetadata: RemoteUserData[] = requestUserIds.map( - (requestUserId) => ({ + const requestsWithMetadata: Omit[] = + requestUserIds.map((requestUserId) => ({ id: requestUserId, username: users.find((x) => x.id === requestUserId)?.username as string, image: `${AVATAR_BASE_URL}/${ @@ -115,19 +96,13 @@ router.get("/requests", extractUserId, async (req, res) => { areFriends: false, remoteRequested: true, requested: false, - }) - ); - res.json({ - requests: requestsWithMetadata, - }); + })); + res.json({ requests: requestsWithMetadata }); }); router.post("/request", extractUserId, async (req, res) => { - //@ts-ignore - const uuid: string = req.id; // TODO from from - // @ts-ignore - const to: string = req.body.to; - // @ts-ignore + const uuid = req.id!; // TODO from from + const to: string = req.body.to!; if (uuid === to) { res.status(411).json({ @@ -136,8 +111,8 @@ router.post("/request", extractUserId, async (req, res) => { return; } const sendRequest: boolean = req.body.sendRequest; - const areFriends = await setFriendship({ from: uuid, to, sendRequest }); + if (sendRequest) { if (areFriends) { // entry in DB @@ -199,8 +174,7 @@ router.post("/request", extractUserId, async (req, res) => { }); router.get("/all", extractUserId, async (req, res) => { - //@ts-ignore - const uuid: string = req.id; // TODO from from + const uuid = req.id!; // TODO from from try { const friends = await getAllFriends({ @@ -215,11 +189,8 @@ router.get("/all", extractUserId, async (req, res) => { }); router.get("/", extractUserId, async (req, res) => { - //@ts-ignore - const uuid: string = req.id; // TODO from from - // @ts-ignore - const userId: string = req.query.userId; - // @ts-ignore + const uuid = req.id!; // TODO from from + const userId = req.query.userId as string; if (userId === uuid) { res.json({ @@ -233,7 +204,7 @@ router.get("/", extractUserId, async (req, res) => { from: uuid, to: userId, }); - const user = await getUser(userId); + const user = await getUser(userId, true); res.json({ user, are_friends, diff --git a/backend/native/backpack-api/src/routes/v1/inbox.ts b/backend/native/backpack-api/src/routes/v1/inbox.ts index e1ef623b7..b6c0bd236 100644 --- a/backend/native/backpack-api/src/routes/v1/inbox.ts +++ b/backend/native/backpack-api/src/routes/v1/inbox.ts @@ -13,9 +13,7 @@ import { getUsers } from "../../db/users"; const router = express.Router(); router.post("/", extractUserId, async (req, res) => { - // @ts-ignore - const from: string = req.id; - // @ts-ignore + const from = req.id!; const to: string = req.body.to; if (!from || !to) { return res.status(411).json({ msg: "incorrect input" }); @@ -34,12 +32,9 @@ router.post("/", extractUserId, async (req, res) => { }); router.get("/", extractUserId, async (req, res) => { - //@ts-ignore - const uuid: string = req.id; - //@ts-ignore - const limit: number = req.query.limit || 50; - //@ts-ignore - const offset: number = req.query.limit || 0; + const uuid = req.id!; + const limit = Number(req.query.limit || 50); + const offset = Number(req.query.limit || 0); const areConnected: boolean = req.query.areConnected === "true" ? true : false; const { friendships, requestCount } = await getFriendships({ @@ -53,14 +48,10 @@ router.get("/", extractUserId, async (req, res) => { }); router.get("/all", extractUserId, async (req, res) => { - //@ts-ignore - const uuid: string = req.id; - //@ts-ignore - const limit: number = req.query.limit || 50; - //@ts-ignore - const offset: number = req.query.limit || 0; - //@ts-ignore - const userSpecifiedId: string = req.query.uuid; + const uuid = req.id!; + const limit = Number(req.query.limit || 50); + const offset = Number(req.query.limit || 0); + const userSpecifiedId = req.query.uuid as string; if (uuid !== userSpecifiedId) { return res.json({ chats: [] });