import { toMongoDate, toOid } from "../../helpers/inventoryHelpers.ts"; import { getJSONfromString } from "../../helpers/stringHelpers.ts"; import { Friendship } from "../../models/friendModel.ts"; import { Account } from "../../models/loginModel.ts"; import { addInventoryDataToFriendInfo, areFriendsOfFriends } from "../../services/friendService.ts"; import { getInventory } from "../../services/inventoryService.ts"; import { getAccountIdForRequest } from "../../services/loginService.ts"; import type { IFriendInfo } from "../../types/friendTypes.ts"; import type { RequestHandler } from "express"; export const addPendingFriendController: RequestHandler = async (req, res) => { const payload = getJSONfromString(String(req.body)); const account = await Account.findOne({ DisplayName: payload.friend }); if (!account) { res.status(400).end(); return; } const accountId = await getAccountIdForRequest(req); const inventory = await getInventory(account._id.toString(), "Settings"); if ( inventory.Settings?.FriendInvRestriction == "GIFT_MODE_NONE" || (inventory.Settings?.FriendInvRestriction == "GIFT_MODE_FRIENDS" && !(await areFriendsOfFriends(account._id, accountId))) ) { res.status(400).send("Friend Invite Restriction"); return; } await Friendship.insertOne({ owner: accountId, friend: account._id, Note: payload.message }); const friendInfo: IFriendInfo = { _id: toOid(account._id), DisplayName: account.DisplayName, LastLogin: toMongoDate(account.LastLogin), Note: payload.message }; await addInventoryDataToFriendInfo(friendInfo); res.json({ Friend: friendInfo }); }; interface IAddPendingFriendRequest { friend: string; message: string; }