mirror of
https://github.com/spacebarchat/server.git
synced 2026-10-27 23:13:08 +00:00
Merge branch 'master' into unittests
This commit is contained in:
commit
16a3d1b334
70 files changed
+1446
-1066
No files matched your search
@@ -19,6 +19,7 @@ router.get("/", route({ test: { response: { body: "UserProfileResponse" } } }),
|
||||
connected_accounts: user.connected_accounts,
|
||||
premium_guild_since: null, // TODO
|
||||
premium_since: null, // TODO
|
||||
mutual_guilds: [], // TODO {id: "", nick: null} when ?with_mutual_guilds=true
|
||||
user: {
|
||||
username: user.username,
|
||||
discriminator: user.discriminator,
|
||||
|
||||
@@ -1,15 +1,12 @@
|
||||
import { Router, Request, Response } from "express";
|
||||
import { Channel, ChannelCreateEvent, ChannelType, Snowflake, trimSpecial, User, emitEvent, Recipient } from "@fosscord/util";
|
||||
import { HTTPError } from "lambert-server";
|
||||
import { Request, Response, Router } from "express";
|
||||
import { Recipient, DmChannelDTO, Channel } from "@fosscord/util";
|
||||
import { route } from "@fosscord/api";
|
||||
import { In } from "typeorm";
|
||||
|
||||
const router: Router = Router();
|
||||
|
||||
router.get("/", route({}), async (req: Request, res: Response) => {
|
||||
const recipients = await Recipient.find({ where: { user_id: req.user_id }, relations: ["channel"] });
|
||||
|
||||
res.json(recipients.map((x) => x.channel));
|
||||
const recipients = await Recipient.find({ where: { user_id: req.user_id, closed: false }, relations: ["channel", "channel.recipients"] });
|
||||
res.json(await Promise.all(recipients.map(r => DmChannelDTO.from(r.channel, [req.user_id]))));
|
||||
});
|
||||
|
||||
export interface DmChannelCreateSchema {
|
||||
@@ -19,30 +16,7 @@ export interface DmChannelCreateSchema {
|
||||
|
||||
router.post("/", route({ body: "DmChannelCreateSchema" }), async (req: Request, res: Response) => {
|
||||
const body = req.body as DmChannelCreateSchema;
|
||||
|
||||
body.recipients = body.recipients.filter((x) => x !== req.user_id).unique();
|
||||
|
||||
const recipients = await User.find({ where: body.recipients.map((x) => ({ id: x })) });
|
||||
|
||||
if (recipients.length !== body.recipients.length) {
|
||||
throw new HTTPError("Recipient/s not found");
|
||||
}
|
||||
|
||||
const type = body.recipients.length === 1 ? ChannelType.DM : ChannelType.GROUP_DM;
|
||||
const name = trimSpecial(body.name);
|
||||
|
||||
const channel = await new Channel({
|
||||
name,
|
||||
type,
|
||||
// owner_id only for group dm channels
|
||||
created_at: new Date(),
|
||||
last_message_id: null,
|
||||
recipients: [...body.recipients.map((x) => new Recipient({ user_id: x })), new Recipient({ user_id: req.user_id })]
|
||||
}).save();
|
||||
|
||||
await emitEvent({ event: "CHANNEL_CREATE", data: channel, user_id: req.user_id } as ChannelCreateEvent);
|
||||
|
||||
res.json(channel);
|
||||
res.json(await Channel.createDMChannel(body.recipients, req.user_id, body.name));
|
||||
});
|
||||
|
||||
export default router;
|
||||
@@ -0,0 +1,11 @@
|
||||
import { Request, Response, Router } from "express";
|
||||
import { route } from "@fosscord/api";
|
||||
|
||||
const router: Router = Router();
|
||||
|
||||
router.get("/", route({}), async (req: Request, res: Response) => {
|
||||
//TODO
|
||||
res.json([]).status(200);
|
||||
});
|
||||
|
||||
export default router;
|
||||
@@ -1,7 +1,6 @@
|
||||
import { Router, Request, Response } from "express";
|
||||
import { User, PrivateUserProjection, emitEvent, UserUpdateEvent } from "@fosscord/util";
|
||||
import { User, PrivateUserProjection, emitEvent, UserUpdateEvent, handleFile } from "@fosscord/util";
|
||||
import { route } from "@fosscord/api";
|
||||
import { handleFile } from "@fosscord/api";
|
||||
|
||||
const router: Router = Router();
|
||||
|
||||
|
||||
@@ -18,13 +18,23 @@ const router = Router();
|
||||
const userProjection: (keyof User)[] = ["relationships", ...PublicUserProjection];
|
||||
|
||||
router.get("/", route({}), async (req: Request, res: Response) => {
|
||||
const user = await User.findOneOrFail({ where: { id: req.user_id }, relations: ["relationships"] });
|
||||
const user = await User.findOneOrFail({ where: { id: req.user_id }, relations: ["relationships", "relationships.to"] });
|
||||
|
||||
return res.json(user.relationships);
|
||||
//TODO DTO
|
||||
const related_users = user.relationships.map((r) => {
|
||||
return {
|
||||
id: r.to.id,
|
||||
type: r.type,
|
||||
nickname: null,
|
||||
user: r.to.toPublicUser()
|
||||
};
|
||||
});
|
||||
|
||||
return res.json(related_users);
|
||||
});
|
||||
|
||||
export interface RelationshipPutSchema {
|
||||
type: RelationshipType;
|
||||
type?: RelationshipType;
|
||||
}
|
||||
|
||||
router.put("/:id", route({ body: "RelationshipPutSchema" }), async (req: Request, res: Response) => {
|
||||
@@ -32,7 +42,7 @@ router.put("/:id", route({ body: "RelationshipPutSchema" }), async (req: Request
|
||||
req,
|
||||
res,
|
||||
await User.findOneOrFail({ id: req.params.id }, { relations: ["relationships", "relationships.to"], select: userProjection }),
|
||||
req.body.type
|
||||
req.body.type ?? RelationshipType.friends
|
||||
);
|
||||
});
|
||||
|
||||
@@ -48,7 +58,10 @@ router.post("/", route({ body: "RelationshipPostSchema" }), async (req: Request,
|
||||
await User.findOneOrFail({
|
||||
relations: ["relationships", "relationships.to"],
|
||||
select: userProjection,
|
||||
where: req.body as { discriminator: string; username: string }
|
||||
where: {
|
||||
discriminator: String(req.body.discriminator).padStart(4, "0"), //Discord send the discriminator as integer, we need to add leading zeroes
|
||||
username: req.body.username
|
||||
}
|
||||
}),
|
||||
req.body.type
|
||||
);
|
||||
|
||||
Reference in new issue
Block a user