XFE Git
XFE Studio Git
Git 首页 全局搜索
XFE 主站 文档 NuGet

XFESpaceNinjaServer

A simple server for a small space ninja game

公开
关注 0 Fork 0 Star 0
返回提交历史

XFEstudio/XFESpaceNinjaServer

chore: update removeFriend response for U40 (#3165)

Migrating from oids to names was certainly a choice Reviewed-on: https://onlyg.it/OpenWF/SpaceNinjaServer/pulls/3165 Co-authored-by: Sainan <63328889+Sainan@users.noreply.github.com> Co-committed-by: Sainan <63328889+Sainan@users.noreply.github.com>

d5ccfbc7
Sainan <63328889+Sainan@users.noreply.github.com>
提交于

代码差异

1 个文件 +37 -16
Modified src/controllers/api/removeFriendController.ts +37 -16
@@ -1,16 +1,18 @@
1 import { toOid } from "../../helpers/inventoryHelpers.ts";
1 import gameToBuildVersion from "../../constants/gameToBuildVersion.ts";
2 import { toOid, version_compare } from "../../helpers/inventoryHelpers.ts";
2 3 import { getJSONfromString } from "../../helpers/stringHelpers.ts";
3 4 import { Friendship } from "../../models/friendModel.ts";
4 5 import { Account } from "../../models/loginModel.ts";
5 6 import { getInventory } from "../../services/inventoryService.ts";
6 import { getAccountIdForRequest } from "../../services/loginService.ts";
7 import { getAccountForRequest } from "../../services/loginService.ts";
7 8 import type { IOid } from "../../types/commonTypes.ts";
8 9 import { parallelForeach } from "../../utils/async-utils.ts";
9 10 import type { RequestHandler } from "express";
10 11 import type { Types } from "mongoose";
11 12
12 13 export const removeFriendGetController: RequestHandler = async (req, res) => {
13 const accountId = await getAccountIdForRequest(req);
14 const account = await getAccountForRequest(req);
15 const accountId = account._id.toString();
14 16 if (req.query.all) {
15 17 const [internalFriendships, externalFriendships] = await Promise.all([
16 18 Friendship.find({ owner: accountId }, "friend"),
@@ -25,23 +27,20 @@ export const removeFriendGetController: RequestHandler = async (req, res) => {
25 27 }
26 28 }
27 29 await Promise.all(promises);
28 res.json({
29 Friends: friends
30 } satisfies IRemoveFriendsResponse);
30 res.json(await toRemoveFriendsResponse(account.BuildLabel, friends));
31 31 } else {
32 32 const friendId = req.query.friendId as string;
33 33 await Promise.all([
34 34 Friendship.deleteOne({ owner: accountId, friend: friendId }),
35 35 Friendship.deleteOne({ owner: friendId, friend: accountId })
36 36 ]);
37 res.json({
38 Friends: [{ $oid: friendId }]
39 } satisfies IRemoveFriendsResponse);
37 res.json(await toRemoveFriendsResponse(account.BuildLabel, [{ $oid: friendId }]));
40 38 }
41 39 };
42 40
43 41 export const removeFriendPostController: RequestHandler = async (req, res) => {
44 const accountId = await getAccountIdForRequest(req);
42 const account = await getAccountForRequest(req);
43 const accountId = account._id.toString();
45 44 const data = getJSONfromString<IBatchRemoveFriendsRequest>(String(req.body));
46 45 const friends = new Set((await Friendship.find({ owner: accountId }, "friend")).map(x => x.friend));
47 46 // TOVERIFY: Should pending friendships also be kept?
@@ -69,18 +68,16 @@ export const removeFriendPostController: RequestHandler = async (req, res) => {
69 68
70 69 // Remove all remaining friends that aren't in SkipFriendIds & give response.
71 70 const promises = [];
72 const response: IOid[] = [];
71 const removeFriendOids: IOid[] = [];
73 72 for (const friend of friends) {
74 73 if (!data.SkipFriendIds.find(skipFriendId => checkFriendId(skipFriendId, friend))) {
75 74 promises.push(Friendship.deleteOne({ owner: accountId, friend: friend }));
76 75 promises.push(Friendship.deleteOne({ owner: friend, friend: accountId }));
77 response.push(toOid(friend));
76 removeFriendOids.push(toOid(friend));
78 77 }
79 78 }
80 79 await Promise.all(promises);
81 res.json({
82 Friends: response
83 } satisfies IRemoveFriendsResponse);
80 res.json(await toRemoveFriendsResponse(account.BuildLabel, removeFriendOids));
84 81 };
85 82
86 83 // The friend ids format is a bit weird, e.g. when 6633b81e9dba0b714f28ff02 (A) is friends with 67cdac105ef1f4b49741c267 (B), A's friend id for B is 808000105ef1f40560ca079e and B's friend id for A is 8000b81e9dba0b06408a8075.
@@ -94,6 +91,30 @@ interface IBatchRemoveFriendsRequest {
94 91 SkipFriendIds: string[];
95 92 }
96 93
97 interface IRemoveFriendsResponse {
94 // >= U40
95 interface IRemoveFriendsResponseU40 {
96 FriendNames: string[];
97 }
98
99 // < U40
100 interface IRemoveFriendsResponseU39 {
98 101 Friends: IOid[];
99 102 }
103
104 const toRemoveFriendsResponse = async (
105 buildLabel: string | undefined,
106 friends: IOid[]
107 ): Promise<IRemoveFriendsResponseU39 | IRemoveFriendsResponseU40> => {
108 if (buildLabel && version_compare(buildLabel, gameToBuildVersion["40.0.0"]) < 0) {
109 return { Friends: friends } satisfies IRemoveFriendsResponseU39;
110 } else {
111 const response: IRemoveFriendsResponseU40 = { FriendNames: [] };
112 for (const friend of friends) {
113 const acct = await Account.findById(friend.$oid, "DisplayName");
114 if (acct) {
115 response.FriendNames.push(acct.DisplayName);
116 }
117 }
118 return response;
119 }
120 };