Skip to content

Commit

Permalink
friends updates (#3617)
Browse files Browse the repository at this point in the history
  • Loading branch information
wentokay authored Apr 5, 2023
1 parent 40f5bd6 commit abba7e0
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 69 deletions.
75 changes: 23 additions & 52 deletions backend/native/backpack-api/src/routes/v1/friends.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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({
Expand All @@ -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({
Expand All @@ -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",
Expand All @@ -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<RemoteUserData, "public_keys">[] =
requestedUserIds.map((userId) => ({
id: userId,
username: users.find((x) => x.id === userId)?.username as string,
image: `${AVATAR_BASE_URL}/${
Expand All @@ -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<RemoteUserData, "public_keys">[] =
requestUserIds.map((requestUserId) => ({
id: requestUserId,
username: users.find((x) => x.id === requestUserId)?.username as string,
image: `${AVATAR_BASE_URL}/${
Expand All @@ -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({
Expand All @@ -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
Expand Down Expand Up @@ -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({
Expand All @@ -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({
Expand All @@ -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,
Expand Down
25 changes: 8 additions & 17 deletions backend/native/backpack-api/src/routes/v1/inbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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" });
Expand All @@ -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({
Expand All @@ -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: [] });
Expand Down

1 comment on commit abba7e0

@vercel
Copy link

@vercel vercel bot commented on abba7e0 Apr 5, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.