Implement fetching mutual friends
This commit is contained in:
parent
d647f882b1
commit
6a3155adba
@ -20,26 +20,28 @@ import { route } from "@spacebar/api";
|
||||
import {
|
||||
Badge,
|
||||
Config,
|
||||
emitEvent,
|
||||
FieldErrors,
|
||||
handleFile,
|
||||
Member,
|
||||
PrivateUserProjection,
|
||||
PublicUser,
|
||||
PublicUserProjection,
|
||||
Relationship,
|
||||
RelationshipType,
|
||||
User,
|
||||
UserProfileModifySchema,
|
||||
UserUpdateEvent,
|
||||
emitEvent,
|
||||
handleFile,
|
||||
} from "@spacebar/util";
|
||||
import { Request, Response, Router } from "express";
|
||||
import { In } from "typeorm";
|
||||
|
||||
const router: Router = Router({ mergeParams: true });
|
||||
|
||||
router.get(
|
||||
"/",
|
||||
route({ responses: { 200: { body: "UserProfileResponse" } } }),
|
||||
async (req: Request, res: Response) => {
|
||||
router.get("/", route({ responses: { 200: { body: "UserProfileResponse" } } }), async (req: Request, res: Response) => {
|
||||
if (req.params.id === "@me") req.params.id = req.user_id;
|
||||
|
||||
const { guild_id, with_mutual_guilds } = req.query;
|
||||
const { guild_id, with_mutual_guilds, with_mutual_friends, with_mutual_friends_count } = req.query;
|
||||
|
||||
const user = await User.getPublicUser(req.params.id, {
|
||||
relations: ["connected_accounts"],
|
||||
@ -104,13 +106,27 @@ router.get(
|
||||
|
||||
const badges = await Badge.find();
|
||||
|
||||
let mutual_friends: PublicUser[] = [];
|
||||
let mutual_friends_count = 0;
|
||||
|
||||
if (with_mutual_friends == "true" || with_mutual_friends_count == "true") {
|
||||
const relationshipsSelf = await Relationship.find({ where: { from_id: req.user_id, type: RelationshipType.friends } });
|
||||
const relationshipsUser = await Relationship.find({ where: { from_id: req.params.id, type: RelationshipType.friends } });
|
||||
const relationshipsIntersection = relationshipsSelf.filter((r1) => relationshipsUser.some((r2) => r2.to_id === r1.to_id));
|
||||
if (with_mutual_friends_count) mutual_friends_count = relationshipsIntersection.length;
|
||||
if (with_mutual_friends) {
|
||||
const users = await User.find({ where: { id: In(relationshipsIntersection.map((r) => r.to_id)) }, select: PublicUserProjection });
|
||||
mutual_friends = users.map((u) => u.toPublicUser());
|
||||
}
|
||||
}
|
||||
|
||||
res.json({
|
||||
connected_accounts: user.connected_accounts.filter(
|
||||
(x) => x.visibility != 0,
|
||||
),
|
||||
connected_accounts: user.connected_accounts.filter((x) => x.visibility != 0),
|
||||
premium_guild_since: premium_guild_since, // TODO
|
||||
premium_since: user.premium_since, // TODO
|
||||
mutual_guilds: mutual_guilds, // TODO {id: "", nick: null} when ?with_mutual_guilds=true
|
||||
mutual_guilds: with_mutual_guilds ? mutual_guilds : undefined, // TODO {id: "", nick: null} when ?with_mutual_guilds=true
|
||||
mutual_friends: with_mutual_friends ? mutual_friends : undefined,
|
||||
mutual_friends_count: with_mutual_friends_count ? mutual_friends_count : undefined,
|
||||
user: user.toPublicUser(),
|
||||
premium_type: user.premium_type,
|
||||
profile_themes_experiment_bucket: 4, // TODO: This doesn't make it available, for some reason?
|
||||
@ -119,20 +135,12 @@ router.get(
|
||||
guild_member_profile: guild_id && guildMemberProfile,
|
||||
badges: badges.filter((x) => user.badge_ids?.includes(x.id)),
|
||||
});
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
router.patch(
|
||||
"/",
|
||||
route({ requestBody: "UserProfileModifySchema" }),
|
||||
async (req: Request, res: Response) => {
|
||||
router.patch("/", route({ requestBody: "UserProfileModifySchema" }), async (req: Request, res: Response) => {
|
||||
const body = req.body as UserProfileModifySchema;
|
||||
|
||||
if (body.banner)
|
||||
body.banner = await handleFile(
|
||||
`/banners/${req.user_id}`,
|
||||
body.banner as string,
|
||||
);
|
||||
if (body.banner) body.banner = await handleFile(`/banners/${req.user_id}`, body.banner as string);
|
||||
const user = await User.findOneOrFail({
|
||||
where: { id: req.user_id },
|
||||
select: [...PrivateUserProjection, "data"],
|
||||
@ -171,7 +179,6 @@ router.patch(
|
||||
theme_colors: user.theme_colors,
|
||||
pronouns: user.pronouns,
|
||||
});
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
export default router;
|
||||
|
||||
Reference in New Issue
Block a user