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