返回提交历史
Modified
src/controllers/api/getFriendsController.ts
+18
-13
Modified
src/utils/ts-utils.ts
+6
-0
XFEstudio/SpaceNinjaServer
feat: provide ActiveAvatarImageType in getFriends for older versions (#3553)
Reviewed-on: https://onlyg.it/OpenWF/SpaceNinjaServer/pulls/3553 Co-authored-by: Sainan <63328889+Sainan@users.noreply.github.com> Co-committed-by: Sainan <63328889+Sainan@users.noreply.github.com>
9ecbb551
代码差异
2 个文件
+24
-13
@@ -1,22 +1,25 @@
1
1
import { toOid2 } from "../../helpers/inventoryHelpers.ts";
2
2
import { Friendship } from "../../models/friendModel.ts";
3
import { Inventory } from "../../models/inventoryModels/inventoryModel.ts";
3
4
import { addAccountDataToFriendInfo, addInventoryDataToFriendInfo } from "../../services/friendService.ts";
4
5
import { getAccountForRequest } from "../../services/loginService.ts";
5
6
import type { IFriendInfo } from "../../types/friendTypes.ts";
6
7
import type { Request, RequestHandler, Response } from "express";
8
import { getObjectValues } from "../../utils/ts-utils.ts";
7
9
8
10
// POST with {} instead of GET as of 38.5.0
9
11
export const getFriendsController: RequestHandler = async (req: Request, res: Response) => {
10
12
const account = await getAccountForRequest(req);
11
const accountId = account._id.toString();
12
13
const response: IGetFriendsResponse = {
14
ActiveAvatarImageType: (await Inventory.findOne({ accountOwnerId: account._id }, "ActiveAvatarImageType"))
15
?.ActiveAvatarImageType,
13
16
Current: [],
14
17
IncomingFriendRequests: [],
15
18
OutgoingFriendRequests: []
16
19
};
17
20
const [internalFriendships, externalFriendships] = await Promise.all([
18
Friendship.find({ owner: accountId }),
19
Friendship.find({ friend: accountId }, "owner Note NewRequest")
21
Friendship.find({ owner: account._id }),
22
Friendship.find({ friend: account._id }, "owner Note NewRequest")
20
23
]);
21
24
for (const externalFriendship of externalFriendships) {
22
25
if (!internalFriendships.find(x => x.friend.equals(externalFriendship.owner))) {
@@ -38,19 +41,21 @@ export const getFriendsController: RequestHandler = async (req: Request, res: Re
38
41
}
39
42
}
40
43
const promises: Promise<void>[] = [];
41
for (const arr of Object.values(response)) {
42
for (const friendInfo of arr) {
43
promises.push(addAccountDataToFriendInfo(friendInfo, account.BuildLabel));
44
promises.push(addInventoryDataToFriendInfo(friendInfo));
44
for (const arr of getObjectValues(response)) {
45
if (Array.isArray(arr)) {
46
for (const friendInfo of arr) {
47
promises.push(addAccountDataToFriendInfo(friendInfo, account.BuildLabel));
48
promises.push(addInventoryDataToFriendInfo(friendInfo));
49
}
45
50
}
46
51
}
47
52
await Promise.all(promises);
48
53
res.json(response);
49
54
};
50
55
51
// interface IGetFriendsResponse {
52
// Current: IFriendInfo[];
53
// IncomingFriendRequests: IFriendInfo[];
54
// OutgoingFriendRequests: IFriendInfo[];
55
// }
56
type IGetFriendsResponse = Record<"Current" | "IncomingFriendRequests" | "OutgoingFriendRequests", IFriendInfo[]>;
56
interface IGetFriendsResponse {
57
ActiveAvatarImageType?: string; // ~U18
58
Current: IFriendInfo[];
59
IncomingFriendRequests: IFriendInfo[];
60
OutgoingFriendRequests: IFriendInfo[];
61
}
@@ -4,4 +4,10 @@ export function getEntriesUnsafe<T extends object>(object: T): Entries<T> {
4
4
return Object.entries(object) as Entries<T>;
5
5
}
6
6
7
type Values<T> = T[keyof T];
8
9
export const getObjectValues = <T extends object>(obj: T): Values<T>[] => {
10
return Object.values(obj) as Values<T>[];
11
};
12
7
13
export const exhaustive = (_: never): void => {};