From 4f87fd742f25a25782afb20131200bcff81b50f7 Mon Sep 17 00:00:00 2001 From: AlTech98 Date: Thu, 16 Sep 2021 21:33:36 +0200 Subject: Implemented DMs and group DMs --- api/src/routes/users/@me/channels.ts | 43 ++++++++++--------------------- api/src/routes/users/@me/relationships.ts | 19 +++++++++++--- 2 files changed, 29 insertions(+), 33 deletions(-) (limited to 'api/src/routes/users/@me') diff --git a/api/src/routes/users/@me/channels.ts b/api/src/routes/users/@me/channels.ts index da33f204..bd7af18c 100644 --- a/api/src/routes/users/@me/channels.ts +++ b/api/src/routes/users/@me/channels.ts @@ -1,15 +1,21 @@ -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 { PublicUserProjection, Recipient, User, ChannelService } 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"] }); + const recipients = await Recipient.find({ where: { user_id: req.user_id }, relations: ["channel", "user"] }); - res.json(recipients.map((x) => x.channel)); + //TODO check if this is right + const aa = await Promise.all(recipients.map(async (x) => { + return { + ...(x.channel), + recipients: await User.findOneOrFail({ where: { id: x.user_id }, select: PublicUserProjection }), + } + })) + + res.json(aa); }); export interface DmChannelCreateSchema { @@ -19,30 +25,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 ChannelService.createDMChannel(body.recipients, req.user_id, body.name)); }); export default router; diff --git a/api/src/routes/users/@me/relationships.ts b/api/src/routes/users/@me/relationships.ts index 58d2e481..1d72f11a 100644 --- a/api/src/routes/users/@me/relationships.ts +++ b/api/src/routes/users/@me/relationships.ts @@ -18,9 +18,19 @@ 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"] }); + + //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(user.relationships); + return res.json(related_users); }); export interface RelationshipPutSchema { @@ -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 ); -- cgit 1.4.1 From 7cbb45f979b8e4f01b1028dede05046a3a6a9858 Mon Sep 17 00:00:00 2001 From: AlTech98 Date: Fri, 17 Sep 2021 13:59:21 +0200 Subject: Fix GET /users/@me/channels --- api/src/routes/users/@me/channels.ts | 15 +++------------ 1 file changed, 3 insertions(+), 12 deletions(-) (limited to 'api/src/routes/users/@me') diff --git a/api/src/routes/users/@me/channels.ts b/api/src/routes/users/@me/channels.ts index bd7af18c..873ff245 100644 --- a/api/src/routes/users/@me/channels.ts +++ b/api/src/routes/users/@me/channels.ts @@ -1,21 +1,12 @@ import { Request, Response, Router } from "express"; -import { PublicUserProjection, Recipient, User, ChannelService } from "@fosscord/util"; +import { Recipient, ChannelService, DmChannelDTO } from "@fosscord/util"; import { route } from "@fosscord/api"; 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", "user"] }); - - //TODO check if this is right - const aa = await Promise.all(recipients.map(async (x) => { - return { - ...(x.channel), - recipients: await User.findOneOrFail({ where: { id: x.user_id }, select: PublicUserProjection }), - } - })) - - res.json(aa); + 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 { -- cgit 1.4.1 From a00c5030b3d9e72c2b696d4395516a2c3c03acd7 Mon Sep 17 00:00:00 2001 From: The Arcane Brony Date: Fri, 17 Sep 2021 15:35:35 +0200 Subject: Add /users/@me/connections --- api/src/routes/users/@me/connections.ts | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 api/src/routes/users/@me/connections.ts (limited to 'api/src/routes/users/@me') diff --git a/api/src/routes/users/@me/connections.ts b/api/src/routes/users/@me/connections.ts new file mode 100644 index 00000000..e4fbe1e4 --- /dev/null +++ b/api/src/routes/users/@me/connections.ts @@ -0,0 +1,10 @@ +import { Request, Response, Router } from "express"; + +const router: Router = Router(); + +router.get("/", async (req: Request, res: Response) => { + //TODO + res.json([]).status(200); +}); + +export default router; \ No newline at end of file -- cgit 1.4.1 From d36970e1e7b1690edb5fd9a9b82f474621108f64 Mon Sep 17 00:00:00 2001 From: Flam3rboy <34555296+Flam3rboy@users.noreply.github.com> Date: Sat, 18 Sep 2021 16:24:42 +0200 Subject: Update connections.ts --- api/src/routes/users/@me/connections.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'api/src/routes/users/@me') diff --git a/api/src/routes/users/@me/connections.ts b/api/src/routes/users/@me/connections.ts index e4fbe1e4..411e95bf 100644 --- a/api/src/routes/users/@me/connections.ts +++ b/api/src/routes/users/@me/connections.ts @@ -1,10 +1,11 @@ import { Request, Response, Router } from "express"; +import { route } from "@fosscord/api"; const router: Router = Router(); -router.get("/", async (req: Request, res: Response) => { +router.get("/", route({}), async (req: Request, res: Response) => { //TODO res.json([]).status(200); }); -export default router; \ No newline at end of file +export default router; -- cgit 1.4.1 From ceb770fb5b6648c18d66d6997c8916ed60b8c192 Mon Sep 17 00:00:00 2001 From: AlTech98 Date: Sat, 18 Sep 2021 18:36:29 +0200 Subject: Removed ChannelService, more fixes --- api/src/routes/channels/#channel_id/index.ts | 4 +- .../routes/channels/#channel_id/messages/index.ts | 6 +- api/src/routes/channels/#channel_id/recipients.ts | 6 +- api/src/routes/users/@me/channels.ts | 4 +- gateway/src/listener/listener.ts | 2 +- util/src/entities/Channel.ts | 124 +++++++++++++++++++-- util/src/index.ts | 1 - util/src/services/ChannelService.ts | 118 -------------------- util/src/services/index.ts | 1 - util/src/util/Array.ts | 3 + util/src/util/index.ts | 1 + 11 files changed, 131 insertions(+), 139 deletions(-) delete mode 100644 util/src/services/ChannelService.ts delete mode 100644 util/src/services/index.ts create mode 100644 util/src/util/Array.ts (limited to 'api/src/routes/users/@me') diff --git a/api/src/routes/channels/#channel_id/index.ts b/api/src/routes/channels/#channel_id/index.ts index 70dd3994..3f434f5e 100644 --- a/api/src/routes/channels/#channel_id/index.ts +++ b/api/src/routes/channels/#channel_id/index.ts @@ -1,4 +1,4 @@ -import { Channel, ChannelDeleteEvent, ChannelPermissionOverwriteType, ChannelService, ChannelType, ChannelUpdateEvent, emitEvent, Recipient } from "@fosscord/util"; +import { Channel, ChannelDeleteEvent, ChannelPermissionOverwriteType, ChannelType, ChannelUpdateEvent, emitEvent, Recipient } from "@fosscord/util"; import { Request, Response, Router } from "express"; import { handleFile, route } from "@fosscord/api"; @@ -28,7 +28,7 @@ router.delete("/", route({ permission: "MANAGE_CHANNELS" }), async (req: Request ]); } else if (channel.type === ChannelType.GROUP_DM) { - await ChannelService.removeRecipientFromChannel(channel, req.user_id) + await Channel.removeRecipientFromChannel(channel, req.user_id) } else { //TODO messages in this channel should be deleted before deleting the channel await Promise.all([ diff --git a/api/src/routes/channels/#channel_id/messages/index.ts b/api/src/routes/channels/#channel_id/messages/index.ts index bb610a6a..cde14164 100644 --- a/api/src/routes/channels/#channel_id/messages/index.ts +++ b/api/src/routes/channels/#channel_id/messages/index.ts @@ -1,5 +1,5 @@ import { Router, Response, Request } from "express"; -import { Attachment, Channel, ChannelType, DmChannelDTO, Embed, emitEvent, getPermission, Message, MessageCreateEvent } from "@fosscord/util"; +import { Attachment, Channel, ChannelType, DmChannelDTO, Embed, emitEvent, getPermission, Message, MessageCreateEvent, Recipient } from "@fosscord/util"; import { HTTPError } from "lambert-server"; import { handleMessage, postHandleMessage, route } from "@fosscord/api"; import multer from "multer"; @@ -150,7 +150,6 @@ router.post( return res.status(400).json(error); } } - //TODO querying the DB at every message post should be avoided, caching maybe? const channel = await Channel.findOneOrFail({ where: { id: channel_id }, relations: ["recipients", "recipients.user"] }) const embeds = []; @@ -184,7 +183,8 @@ router.post( } } - await Promise.all(channel.recipients!.map(async r => { + //Only one recipients should be closed here, since in group DMs the recipient is deleted not closed + await Promise.all(channel.recipients!.filter(r => r.closed).map(async r => { r.closed = false; return await r.save() })); diff --git a/api/src/routes/channels/#channel_id/recipients.ts b/api/src/routes/channels/#channel_id/recipients.ts index d88b38f3..c7beeee8 100644 --- a/api/src/routes/channels/#channel_id/recipients.ts +++ b/api/src/routes/channels/#channel_id/recipients.ts @@ -1,5 +1,5 @@ import { Request, Response, Router } from "express"; -import { Channel, ChannelRecipientAddEvent, ChannelService, ChannelType, DiscordApiErrors, DmChannelDTO, emitEvent, PublicUserProjection, Recipient, User } from "@fosscord/util"; +import { Channel, ChannelRecipientAddEvent, ChannelType, DiscordApiErrors, DmChannelDTO, emitEvent, PublicUserProjection, Recipient, User } from "@fosscord/util"; const router: Router = Router(); @@ -13,7 +13,7 @@ router.put("/:user_id", async (req: Request, res: Response) => { user_id ].unique() - const new_channel = await ChannelService.createDMChannel(recipients, req.user_id) + const new_channel = await Channel.createDMChannel(recipients, req.user_id) return res.status(201).json(new_channel); } else { if (channel.recipients!.map(r => r.user_id).includes(user_id)) { @@ -49,7 +49,7 @@ router.delete("/:user_id", async (req: Request, res: Response) => { throw DiscordApiErrors.INVALID_RECIPIENT //TODO is this the right error? } - await ChannelService.removeRecipientFromChannel(channel, user_id) + await Channel.removeRecipientFromChannel(channel, user_id) return res.sendStatus(204); }); diff --git a/api/src/routes/users/@me/channels.ts b/api/src/routes/users/@me/channels.ts index 873ff245..b5782eca 100644 --- a/api/src/routes/users/@me/channels.ts +++ b/api/src/routes/users/@me/channels.ts @@ -1,5 +1,5 @@ import { Request, Response, Router } from "express"; -import { Recipient, ChannelService, DmChannelDTO } from "@fosscord/util"; +import { Recipient, DmChannelDTO, Channel } from "@fosscord/util"; import { route } from "@fosscord/api"; const router: Router = Router(); @@ -16,7 +16,7 @@ export interface DmChannelCreateSchema { router.post("/", route({ body: "DmChannelCreateSchema" }), async (req: Request, res: Response) => { const body = req.body as DmChannelCreateSchema; - res.json(await ChannelService.createDMChannel(body.recipients, req.user_id, body.name)); + res.json(await Channel.createDMChannel(body.recipients, req.user_id, body.name)); }); export default router; diff --git a/gateway/src/listener/listener.ts b/gateway/src/listener/listener.ts index 35841312..ae13cca7 100644 --- a/gateway/src/listener/listener.ts +++ b/gateway/src/listener/listener.ts @@ -32,7 +32,7 @@ export async function setupListener(this: WebSocket) { }); const guilds = members.map((x) => x.guild); const recipients = await Recipient.find({ - where: { user_id: this.user_id }, + where: { user_id: this.user_id, closed: false }, relations: ["channel"], }); const dm_channels = recipients.map((x) => x.channel); diff --git a/util/src/entities/Channel.ts b/util/src/entities/Channel.ts index aa2bfab3..ea632778 100644 --- a/util/src/entities/Channel.ts +++ b/util/src/entities/Channel.ts @@ -1,11 +1,13 @@ import { Column, Entity, JoinColumn, ManyToOne, OneToMany, RelationId } from "typeorm"; import { BaseClass } from "./BaseClass"; import { Guild } from "./Guild"; -import { User } from "./User"; +import { PublicUserProjection, User } from "./User"; import { HTTPError } from "lambert-server"; -import { emitEvent, getPermission, Snowflake } from "../util"; -import { ChannelCreateEvent } from "../interfaces"; +import { containsAll, emitEvent, getPermission, Snowflake, trimSpecial } from "../util"; +import { ChannelCreateEvent, ChannelRecipientRemoveEvent } from "../interfaces"; import { Recipient } from "./Recipient"; +import { DmChannelDTO } from "../dtos"; +import { Message } from "./Message"; export enum ChannelType { GUILD_TEXT = 0, // a text channel within a server @@ -97,7 +99,6 @@ export class Channel extends BaseClass { @Column({ nullable: true }) topic?: string; - // TODO: DM channel static async createChannel( channel: Partial, user_id: string = "0", @@ -150,16 +151,123 @@ export class Channel extends BaseClass { new Channel(channel).save(), !opts?.skipEventEmit ? emitEvent({ - event: "CHANNEL_CREATE", - data: channel, - guild_id: channel.guild_id, - } as ChannelCreateEvent) + event: "CHANNEL_CREATE", + data: channel, + guild_id: channel.guild_id, + } as ChannelCreateEvent) : Promise.resolve(), ]); return channel; } + static async createDMChannel(recipients: string[], creator_user_id: string, name?: string) { + recipients = recipients.unique().filter((x) => x !== creator_user_id); + const otherRecipientsUsers = await User.find({ where: recipients.map((x) => ({ id: x })) }); + + // TODO: check config for max number of recipients + if (otherRecipientsUsers.length !== recipients.length) { + throw new HTTPError("Recipient/s not found"); + } + + const type = recipients.length === 1 ? ChannelType.DM : ChannelType.GROUP_DM; + + let channel = null; + + const channelRecipients = [...recipients, creator_user_id] + + const userRecipients = await Recipient.find({ where: { user_id: creator_user_id }, relations: ["channel", "channel.recipients"] }) + + for (let ur of userRecipients) { + let re = ur.channel.recipients!.map(r => r.user_id) + if (re.length === channelRecipients.length) { + if (containsAll(re, channelRecipients)) { + if (channel == null) { + channel = ur.channel + await ur.assign({ closed: false }).save() + } + } + } + } + + if (channel == null) { + name = trimSpecial(name); + + channel = await new Channel({ + name, + type, + owner_id: (type === ChannelType.DM ? undefined : creator_user_id), + created_at: new Date(), + last_message_id: null, + recipients: channelRecipients.map((x) => new Recipient({ user_id: x, closed: !(type === ChannelType.GROUP_DM || x === creator_user_id) })), + }).save(); + } + + + const channel_dto = await DmChannelDTO.from(channel) + + if (type === ChannelType.GROUP_DM) { + + for (let recipient of channel.recipients!) { + await emitEvent({ + event: "CHANNEL_CREATE", + data: channel_dto.excludedRecipients([recipient.user_id]), + user_id: recipient.user_id + }) + } + } else { + await emitEvent({ event: "CHANNEL_CREATE", data: channel_dto, user_id: creator_user_id }); + } + + return channel_dto.excludedRecipients([creator_user_id]) + } + + static async removeRecipientFromChannel(channel: Channel, user_id: string) { + await Recipient.delete({ channel_id: channel.id, user_id: user_id }) + channel.recipients = channel.recipients?.filter(r => r.user_id !== user_id) + + if (channel.recipients?.length === 0) { + await Channel.deleteChannel(channel); + await emitEvent({ + event: "CHANNEL_DELETE", + data: await DmChannelDTO.from(channel, [user_id]), + user_id: user_id + }); + return + } + + await emitEvent({ + event: "CHANNEL_DELETE", + data: await DmChannelDTO.from(channel, [user_id]), + user_id: user_id + }); + + //If the owner leave we make the first recipient in the list the new owner + if (channel.owner_id === user_id) { + channel.owner_id = channel.recipients!.find(r => r.user_id !== user_id)!.user_id //Is there a criteria to choose the new owner? + await emitEvent({ + event: "CHANNEL_UPDATE", + data: await DmChannelDTO.from(channel, [user_id]), + channel_id: channel.id + }); + } + + await channel.save() + + await emitEvent({ + event: "CHANNEL_RECIPIENT_REMOVE", data: { + channel_id: channel.id, + user: await User.findOneOrFail({ where: { id: user_id }, select: PublicUserProjection }) + }, channel_id: channel.id + } as ChannelRecipientRemoveEvent); + } + + static async deleteChannel(channel: Channel) { + await Message.delete({ channel_id: channel.id }) //TODO we should also delete the attachments from the cdn but to do that we need to move cdn.ts in util + //TODO before deleting the channel we should check and delete other relations + await Channel.delete({ id: channel.id }) + } + isDm() { return this.type === ChannelType.DM || this.type === ChannelType.GROUP_DM } diff --git a/util/src/index.ts b/util/src/index.ts index 538bfdd1..fc00d46b 100644 --- a/util/src/index.ts +++ b/util/src/index.ts @@ -4,7 +4,6 @@ import "reflect-metadata"; export * from "./util/index"; export * from "./interfaces/index"; export * from "./entities/index"; -export * from "./services/index"; export * from "./dtos/index"; // import Config from "../util/Config"; diff --git a/util/src/services/ChannelService.ts b/util/src/services/ChannelService.ts deleted file mode 100644 index aa021a4a..00000000 --- a/util/src/services/ChannelService.ts +++ /dev/null @@ -1,118 +0,0 @@ -import { Channel, ChannelType, Message, PublicUserProjection, Recipient, User } from "../entities"; -import { HTTPError } from "lambert-server"; -import { emitEvent, trimSpecial } from "../util"; -import { DmChannelDTO } from "../dtos"; -import { ChannelRecipientRemoveEvent } from "../interfaces"; - -export function checker(arr: any[], target: any[]) { - return target.every(v => arr.includes(v)); -} - -export class ChannelService { - public static async createDMChannel(recipients: string[], creator_user_id: string, name?: string) { - recipients = recipients.unique().filter((x) => x !== creator_user_id); - const otherRecipientsUsers = await User.find({ where: recipients.map((x) => ({ id: x })) }); - - // TODO: check config for max number of recipients - if (otherRecipientsUsers.length !== recipients.length) { - throw new HTTPError("Recipient/s not found"); - } - - const type = recipients.length === 1 ? ChannelType.DM : ChannelType.GROUP_DM; - - let channel = null; - - const channelRecipients = [...recipients, creator_user_id] - - const userRecipients = await Recipient.find({ where: { user_id: creator_user_id }, relations: ["channel", "channel.recipients"] }) - - for (let ur of userRecipients) { - let re = ur.channel.recipients!.map(r => r.user_id) - if (re.length === channelRecipients.length) { - if (checker(re, channelRecipients)) { - if (channel == null) { - channel = ur.channel - await ur.assign({ closed: false }).save() - } - } - } - } - - if (channel == null) { - name = trimSpecial(name); - - channel = await new Channel({ - name, - type, - owner_id: (type === ChannelType.DM ? undefined : creator_user_id), - created_at: new Date(), - last_message_id: null, - recipients: channelRecipients.map((x) => new Recipient({ user_id: x, closed: !(type === ChannelType.GROUP_DM || x === creator_user_id) })), - }).save(); - } - - - const channel_dto = await DmChannelDTO.from(channel) - - if (type === ChannelType.GROUP_DM) { - - for (let recipient of channel.recipients!) { - await emitEvent({ - event: "CHANNEL_CREATE", - data: channel_dto.excludedRecipients([recipient.user_id]), - user_id: recipient.user_id - }) - } - } else { - await emitEvent({ event: "CHANNEL_CREATE", data: channel_dto, user_id: creator_user_id }); - } - - return channel_dto.excludedRecipients([creator_user_id]) - } - - public static async removeRecipientFromChannel(channel: Channel, user_id: string) { - await Recipient.delete({ channel_id: channel.id, user_id: user_id }) - channel.recipients = channel.recipients?.filter(r => r.user_id !== user_id) - - if (channel.recipients?.length === 0) { - await ChannelService.deleteChannel(channel); - await emitEvent({ - event: "CHANNEL_DELETE", - data: await DmChannelDTO.from(channel, [user_id]), - user_id: user_id - }); - return - } - - await emitEvent({ - event: "CHANNEL_DELETE", - data: await DmChannelDTO.from(channel, [user_id]), - user_id: user_id - }); - - //If the owner leave we make the first recipient in the list the new owner - if (channel.owner_id === user_id) { - channel.owner_id = channel.recipients!.find(r => r.user_id !== user_id)!.user_id //Is there a criteria to choose the new owner? - await emitEvent({ - event: "CHANNEL_UPDATE", - data: await DmChannelDTO.from(channel, [user_id]), - channel_id: channel.id - }); - } - - await channel.save() - - await emitEvent({ - event: "CHANNEL_RECIPIENT_REMOVE", data: { - channel_id: channel.id, - user: await User.findOneOrFail({ where: { id: user_id }, select: PublicUserProjection }) - }, channel_id: channel.id - } as ChannelRecipientRemoveEvent); - } - - public static async deleteChannel(channel: Channel) { - await Message.delete({ channel_id: channel.id }) //TODO we should also delete the attachments from the cdn but to do that we need to move cdn.ts in util - //TODO before deleting the channel we should check and delete other relations - await Channel.delete({ id: channel.id }) - } -} diff --git a/util/src/services/index.ts b/util/src/services/index.ts deleted file mode 100644 index c012a208..00000000 --- a/util/src/services/index.ts +++ /dev/null @@ -1 +0,0 @@ -export * from "./ChannelService"; diff --git a/util/src/util/Array.ts b/util/src/util/Array.ts new file mode 100644 index 00000000..27f7c961 --- /dev/null +++ b/util/src/util/Array.ts @@ -0,0 +1,3 @@ +export function containsAll(arr: any[], target: any[]) { + return target.every(v => arr.includes(v)); +} \ No newline at end of file diff --git a/util/src/util/index.ts b/util/src/util/index.ts index 4e92f017..2de26fc7 100644 --- a/util/src/util/index.ts +++ b/util/src/util/index.ts @@ -12,3 +12,4 @@ export * from "./RabbitMQ"; export * from "./Regex"; export * from "./Snowflake"; export * from "./String"; +export * from "./Array"; -- cgit 1.4.1 From f8b1d93e3f549cdb3d074a6b4cea45e33faa08ef Mon Sep 17 00:00:00 2001 From: Flam3rboy <34555296+Flam3rboy@users.noreply.github.com> Date: Sun, 19 Sep 2021 18:44:49 +0200 Subject: :truck: move handleFile to cdn --- api/src/routes/channels/#channel_id/messages/index.ts | 3 +-- api/src/routes/guilds/#guild_id/index.ts | 3 +-- api/src/routes/guilds/index.ts | 5 ++--- api/src/routes/users/@me/index.ts | 3 +-- 4 files changed, 5 insertions(+), 9 deletions(-) (limited to 'api/src/routes/users/@me') diff --git a/api/src/routes/channels/#channel_id/messages/index.ts b/api/src/routes/channels/#channel_id/messages/index.ts index ec93649e..07bfc22d 100644 --- a/api/src/routes/channels/#channel_id/messages/index.ts +++ b/api/src/routes/channels/#channel_id/messages/index.ts @@ -1,10 +1,9 @@ import { Router, Response, Request } from "express"; -import { Attachment, Channel, ChannelType, Embed, getPermission, Message } from "@fosscord/util"; +import { Attachment, Channel, ChannelType, Embed, getPermission, Message, uploadFile } from "@fosscord/util"; import { HTTPError } from "lambert-server"; import { route } from "@fosscord/api"; import multer from "multer"; import { sendMessage } from "@fosscord/api"; -import { uploadFile } from "@fosscord/api"; import { FindManyOptions, LessThan, MoreThan } from "typeorm"; const router: Router = Router(); diff --git a/api/src/routes/guilds/#guild_id/index.ts b/api/src/routes/guilds/#guild_id/index.ts index 63000b84..d8ee86ff 100644 --- a/api/src/routes/guilds/#guild_id/index.ts +++ b/api/src/routes/guilds/#guild_id/index.ts @@ -1,8 +1,7 @@ import { Request, Response, Router } from "express"; -import { emitEvent, getPermission, Guild, GuildUpdateEvent, Member } from "@fosscord/util"; +import { emitEvent, getPermission, Guild, GuildUpdateEvent, handleFile, Member } from "@fosscord/util"; import { HTTPError } from "lambert-server"; import { route } from "@fosscord/api"; -import { handleFile } from "@fosscord/api"; import "missing-native-js-functions"; import { GuildCreateSchema } from "../index"; diff --git a/api/src/routes/guilds/index.ts b/api/src/routes/guilds/index.ts index 3612ca82..abde147d 100644 --- a/api/src/routes/guilds/index.ts +++ b/api/src/routes/guilds/index.ts @@ -1,7 +1,6 @@ import { Router, Request, Response } from "express"; -import { Role, Guild, Snowflake, Config, User, Member, Channel } from "@fosscord/util"; -import { handleFile, route } from "@fosscord/api"; -import { DiscordApiErrors } from "@fosscord/util"; +import { Role, Guild, Snowflake, Config, Member, Channel, DiscordApiErrors, handleFile } from "@fosscord/util"; +import { route } from "@fosscord/api"; import { ChannelModifySchema } from "../channels/#channel_id"; const router: Router = Router(); diff --git a/api/src/routes/users/@me/index.ts b/api/src/routes/users/@me/index.ts index da2f3348..835aab4d 100644 --- a/api/src/routes/users/@me/index.ts +++ b/api/src/routes/users/@me/index.ts @@ -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(); -- cgit 1.4.1 From cb83f11ca43e5baa16a38eafc54b0616cd770d82 Mon Sep 17 00:00:00 2001 From: Chris Chrome Date: Mon, 20 Sep 2021 11:29:37 -0400 Subject: Fix one thing, another problem pops up Co-authored-by: TheArcaneBrony --- api/src/routes/channels/#channel_id/index.ts | 5 ++++- api/src/routes/users/@me/relationships.ts | 2 +- api/src/util/route.ts | 1 + 3 files changed, 6 insertions(+), 2 deletions(-) (limited to 'api/src/routes/users/@me') diff --git a/api/src/routes/channels/#channel_id/index.ts b/api/src/routes/channels/#channel_id/index.ts index ca8cc87c..9a3cb4c2 100644 --- a/api/src/routes/channels/#channel_id/index.ts +++ b/api/src/routes/channels/#channel_id/index.ts @@ -1,4 +1,4 @@ -import { Channel, Message, ChannelDeleteEvent, ChannelPermissionOverwriteType, ChannelType, ChannelUpdateEvent, emitEvent, Recipient } from "@fosscord/util"; +import { Channel, Message, Invite, ReadState, ChannelDeleteEvent, ChannelPermissionOverwriteType, ChannelType, ChannelUpdateEvent, emitEvent, Recipient } from "@fosscord/util"; import { Request, Response, Router } from "express"; import { handleFile, route } from "@fosscord/api"; @@ -32,6 +32,9 @@ router.delete("/", route({ permission: "MANAGE_CHANNELS" }), async (req: Request } else { await Promise.all([ Message.delete({ channel_id: channel_id }), + Invite.delete({ channel_id: channel_id }), + Recipient.delete({ channel_id: channel_id }), + ReadState.delete({ channel_id: channel_id }), Channel.delete({ id: channel_id }), emitEvent({ event: "CHANNEL_DELETE", data: channel, channel_id } as ChannelDeleteEvent) ]); diff --git a/api/src/routes/users/@me/relationships.ts b/api/src/routes/users/@me/relationships.ts index 1d72f11a..6ad873a6 100644 --- a/api/src/routes/users/@me/relationships.ts +++ b/api/src/routes/users/@me/relationships.ts @@ -59,7 +59,7 @@ router.post("/", route({ body: "RelationshipPostSchema" }), async (req: Request, relations: ["relationships", "relationships.to"], select: userProjection, where: { - discriminator: String(req.body.discriminator,).padStart(4, '0'), //Discord send the discriminator as integer, we need to add leading zeroes + discriminator: String(req.body.discriminator).padStart(4, '0'), //Discord send the discriminator as integer, we need to add leading zeroes username: req.body.username } }), diff --git a/api/src/util/route.ts b/api/src/util/route.ts index 678ca64c..b7e6296b 100644 --- a/api/src/util/route.ts +++ b/api/src/util/route.ts @@ -14,6 +14,7 @@ export const ajv = new Ajv({ parseDate: true, allowDate: true, schemas, + coerceTypes: true, messages: true, strict: true, strictRequired: true -- cgit 1.4.1 From 28abad49cd3ff5b9b08fcbb3821d2a6de60179f4 Mon Sep 17 00:00:00 2001 From: Flam3rboy <34555296+Flam3rboy@users.noreply.github.com> Date: Mon, 20 Sep 2021 21:34:33 +0200 Subject: :bug: fix relationships --- api/assets/schemas.json | 254 +----------------------------- api/src/routes/users/@me/relationships.ts | 14 +- 2 files changed, 14 insertions(+), 254 deletions(-) (limited to 'api/src/routes/users/@me') diff --git a/api/assets/schemas.json b/api/assets/schemas.json index da193b28..cc45ebb3 100644 --- a/api/assets/schemas.json +++ b/api/assets/schemas.json @@ -385,15 +385,6 @@ } }, "additionalProperties": false - }, - "RelationshipType": { - "enum": [ - 1, - 2, - 3, - 4 - ], - "type": "number" } }, "$schema": "http://json-schema.org/draft-07/schema#" @@ -707,15 +698,6 @@ } }, "additionalProperties": false - }, - "RelationshipType": { - "enum": [ - 1, - 2, - 3, - 4 - ], - "type": "number" } }, "$schema": "http://json-schema.org/draft-07/schema#" @@ -978,15 +960,6 @@ } }, "additionalProperties": false - }, - "RelationshipType": { - "enum": [ - 1, - 2, - 3, - 4 - ], - "type": "number" } }, "$schema": "http://json-schema.org/draft-07/schema#" @@ -1228,15 +1201,6 @@ } }, "additionalProperties": false - }, - "RelationshipType": { - "enum": [ - 1, - 2, - 3, - 4 - ], - "type": "number" } }, "$schema": "http://json-schema.org/draft-07/schema#" @@ -1481,15 +1445,6 @@ } }, "additionalProperties": false - }, - "RelationshipType": { - "enum": [ - 1, - 2, - 3, - 4 - ], - "type": "number" } }, "$schema": "http://json-schema.org/draft-07/schema#" @@ -1743,15 +1698,6 @@ } }, "additionalProperties": false - }, - "RelationshipType": { - "enum": [ - 1, - 2, - 3, - 4 - ], - "type": "number" } }, "$schema": "http://json-schema.org/draft-07/schema#" @@ -1998,15 +1944,6 @@ } }, "additionalProperties": false - }, - "RelationshipType": { - "enum": [ - 1, - 2, - 3, - 4 - ], - "type": "number" } }, "$schema": "http://json-schema.org/draft-07/schema#" @@ -2248,15 +2185,6 @@ } }, "additionalProperties": false - }, - "RelationshipType": { - "enum": [ - 1, - 2, - 3, - 4 - ], - "type": "number" } }, "$schema": "http://json-schema.org/draft-07/schema#" @@ -2510,15 +2438,6 @@ } }, "additionalProperties": false - }, - "RelationshipType": { - "enum": [ - 1, - 2, - 3, - 4 - ], - "type": "number" } }, "$schema": "http://json-schema.org/draft-07/schema#" @@ -2785,15 +2704,6 @@ } }, "additionalProperties": false - }, - "RelationshipType": { - "enum": [ - 1, - 2, - 3, - 4 - ], - "type": "number" } }, "$schema": "http://json-schema.org/draft-07/schema#" @@ -3099,15 +3009,6 @@ } }, "additionalProperties": false - }, - "RelationshipType": { - "enum": [ - 1, - 2, - 3, - 4 - ], - "type": "number" } }, "$schema": "http://json-schema.org/draft-07/schema#" @@ -3349,15 +3250,6 @@ } }, "additionalProperties": false - }, - "RelationshipType": { - "enum": [ - 1, - 2, - 3, - 4 - ], - "type": "number" } }, "$schema": "http://json-schema.org/draft-07/schema#" @@ -3599,15 +3491,6 @@ } }, "additionalProperties": false - }, - "RelationshipType": { - "enum": [ - 1, - 2, - 3, - 4 - ], - "type": "number" } }, "$schema": "http://json-schema.org/draft-07/schema#" @@ -3861,15 +3744,6 @@ } }, "additionalProperties": false - }, - "RelationshipType": { - "enum": [ - 1, - 2, - 3, - 4 - ], - "type": "number" } }, "$schema": "http://json-schema.org/draft-07/schema#" @@ -4118,15 +3992,6 @@ } }, "additionalProperties": false - }, - "RelationshipType": { - "enum": [ - 1, - 2, - 3, - 4 - ], - "type": "number" } }, "$schema": "http://json-schema.org/draft-07/schema#" @@ -4371,15 +4236,6 @@ } }, "additionalProperties": false - }, - "RelationshipType": { - "enum": [ - 1, - 2, - 3, - 4 - ], - "type": "number" } }, "$schema": "http://json-schema.org/draft-07/schema#" @@ -4624,15 +4480,6 @@ } }, "additionalProperties": false - }, - "RelationshipType": { - "enum": [ - 1, - 2, - 3, - 4 - ], - "type": "number" } }, "$schema": "http://json-schema.org/draft-07/schema#" @@ -4873,15 +4720,6 @@ } }, "additionalProperties": false - }, - "RelationshipType": { - "enum": [ - 1, - 2, - 3, - 4 - ], - "type": "number" } }, "$schema": "http://json-schema.org/draft-07/schema#" @@ -5142,15 +4980,6 @@ } }, "additionalProperties": false - }, - "RelationshipType": { - "enum": [ - 1, - 2, - 3, - 4 - ], - "type": "number" } }, "$schema": "http://json-schema.org/draft-07/schema#" @@ -5418,15 +5247,6 @@ } }, "additionalProperties": false - }, - "RelationshipType": { - "enum": [ - 1, - 2, - 3, - 4 - ], - "type": "number" } }, "$schema": "http://json-schema.org/draft-07/schema#" @@ -5672,15 +5492,6 @@ } }, "additionalProperties": false - }, - "RelationshipType": { - "enum": [ - 1, - 2, - 3, - 4 - ], - "type": "number" } }, "$schema": "http://json-schema.org/draft-07/schema#" @@ -5928,15 +5739,6 @@ } }, "additionalProperties": false - }, - "RelationshipType": { - "enum": [ - 1, - 2, - 3, - 4 - ], - "type": "number" } }, "$schema": "http://json-schema.org/draft-07/schema#" @@ -6184,15 +5986,6 @@ } }, "additionalProperties": false - }, - "RelationshipType": { - "enum": [ - 1, - 2, - 3, - 4 - ], - "type": "number" } }, "$schema": "http://json-schema.org/draft-07/schema#" @@ -6461,15 +6254,6 @@ } }, "additionalProperties": false - }, - "RelationshipType": { - "enum": [ - 1, - 2, - 3, - 4 - ], - "type": "number" } }, "$schema": "http://json-schema.org/draft-07/schema#" @@ -6478,13 +6262,16 @@ "type": "object", "properties": { "type": { - "$ref": "#/definitions/RelationshipType" + "enum": [ + 1, + 2, + 3, + 4 + ], + "type": "number" } }, "additionalProperties": false, - "required": [ - "type" - ], "definitions": { "ChannelPermissionOverwriteType": { "enum": [ @@ -6711,15 +6498,6 @@ } }, "additionalProperties": false - }, - "RelationshipType": { - "enum": [ - 1, - 2, - 3, - 4 - ], - "type": "number" } }, "$schema": "http://json-schema.org/draft-07/schema#" @@ -6965,15 +6743,6 @@ } }, "additionalProperties": false - }, - "RelationshipType": { - "enum": [ - 1, - 2, - 3, - 4 - ], - "type": "number" } }, "$schema": "http://json-schema.org/draft-07/schema#" @@ -7402,15 +7171,6 @@ } }, "additionalProperties": false - }, - "RelationshipType": { - "enum": [ - 1, - 2, - 3, - 4 - ], - "type": "number" } }, "$schema": "http://json-schema.org/draft-07/schema#" diff --git a/api/src/routes/users/@me/relationships.ts b/api/src/routes/users/@me/relationships.ts index 6ad873a6..567c734e 100644 --- a/api/src/routes/users/@me/relationships.ts +++ b/api/src/routes/users/@me/relationships.ts @@ -21,20 +21,20 @@ router.get("/", route({}), async (req: Request, res: Response) => { const user = await User.findOneOrFail({ where: { id: req.user_id }, relations: ["relationships", "relationships.to"] }); //TODO DTO - const related_users = user.relationships.map(r => { + const related_users = user.relationships.map((r) => { return { id: r.to.id, type: r.type, nickname: null, - user: r.to.toPublicUser(), - } - }) + 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) => { @@ -42,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 ); }); @@ -59,7 +59,7 @@ router.post("/", route({ body: "RelationshipPostSchema" }), async (req: Request, relations: ["relationships", "relationships.to"], select: userProjection, where: { - discriminator: String(req.body.discriminator).padStart(4, '0'), //Discord send the discriminator as integer, we need to add leading zeroes + discriminator: String(req.body.discriminator).padStart(4, "0"), //Discord send the discriminator as integer, we need to add leading zeroes username: req.body.username } }), -- cgit 1.4.1