summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--api/src/routes/channels/#channel_id/permissions.ts4
-rw-r--r--api/src/routes/users/@me/channels.ts16
-rw-r--r--api/tests/automatic.test.js2
-rw-r--r--gateway/src/listener/listener.ts2
-rw-r--r--gateway/src/opcodes/Identify.ts43
-rw-r--r--util/src/entities/Channel.ts12
-rw-r--r--util/src/util/Permissions.ts2
7 files changed, 50 insertions, 31 deletions
diff --git a/api/src/routes/channels/#channel_id/permissions.ts b/api/src/routes/channels/#channel_id/permissions.ts

index 827e46f2..959ab8e0 100644 --- a/api/src/routes/channels/#channel_id/permissions.ts +++ b/api/src/routes/channels/#channel_id/permissions.ts
@@ -35,7 +35,7 @@ router.put( allow: body.allow, deny: body.deny }; - channel.permission_overwrites.push(overwrite); + channel.permission_overwrites!.push(overwrite); } overwrite.allow = body.allow; overwrite.deny = body.deny; @@ -60,7 +60,7 @@ router.delete("/:overwrite_id", route({ permission: "MANAGE_ROLES" }), async (re const channel = await Channel.findOneOrFail({ id: channel_id }); if (!channel.guild_id) throw new HTTPError("Channel not found", 404); - channel.permission_overwrites = channel.permission_overwrites.filter((x) => x.id === overwrite_id); + channel.permission_overwrites = channel.permission_overwrites!.filter((x) => x.id === overwrite_id); await Promise.all([ channel.save(), diff --git a/api/src/routes/users/@me/channels.ts b/api/src/routes/users/@me/channels.ts
index 5515a217..da33f204 100644 --- a/api/src/routes/users/@me/channels.ts +++ b/api/src/routes/users/@me/channels.ts
@@ -4,11 +4,6 @@ import { HTTPError } from "lambert-server"; import { route } from "@fosscord/api"; import { In } from "typeorm"; -export interface DmChannelCreateSchema { - name?: string; - recipients: string[]; -} - const router: Router = Router(); router.get("/", route({}), async (req: Request, res: Response) => { @@ -17,12 +12,17 @@ router.get("/", route({}), async (req: Request, res: Response) => { res.json(recipients.map((x) => x.channel)); }); +export interface DmChannelCreateSchema { + name?: string; + recipients: string[]; +} + 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({ id: In(body.recipients) }); + 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"); @@ -34,10 +34,10 @@ router.post("/", route({ body: "DmChannelCreateSchema" }), async (req: Request, const channel = await new Channel({ name, type, - owner_id: req.user_id, + // owner_id only for group dm channels created_at: new Date(), last_message_id: null, - recipients: [...body.recipients.map((x) => new Recipient({ id: x })), new Recipient({ id: req.user_id })] + 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); diff --git a/api/tests/automatic.test.js b/api/tests/automatic.test.js
index e69de29b..2d0a9fcb 100644 --- a/api/tests/automatic.test.js +++ b/api/tests/automatic.test.js
@@ -0,0 +1,2 @@ +// TODO: check every route based on route() paramters: https://github.com/fosscord/fosscord-server/issues/308 +// TODO: check every route with different database engine diff --git a/gateway/src/listener/listener.ts b/gateway/src/listener/listener.ts
index 7152c74b..ef3dd890 100644 --- a/gateway/src/listener/listener.ts +++ b/gateway/src/listener/listener.ts
@@ -70,7 +70,7 @@ export async function setupListener(this: WebSocket) { for (const channel of guild.channels) { if ( x - .overwriteChannel(channel.permission_overwrites) + .overwriteChannel(channel.permission_overwrites!) .has("VIEW_CHANNEL") ) { this.events[channel.id] = await listenEvent( diff --git a/gateway/src/opcodes/Identify.ts b/gateway/src/opcodes/Identify.ts
index 04a6c84c..88c9b942 100644 --- a/gateway/src/opcodes/Identify.ts +++ b/gateway/src/opcodes/Identify.ts
@@ -14,6 +14,8 @@ import { dbConnection, PublicMemberProjection, PublicMember, + ChannelType, + PublicUser, } from "@fosscord/util"; import { setupListener } from "../listener/listener"; import { IdentifySchema } from "../schema/Identify"; @@ -57,6 +59,7 @@ export async function onIdentify(this: WebSocket, data: Payload) { return this.close(CLOSECODES.Invalid_shard); } } + var users: PublicUser[] = []; const members = await Member.find({ where: { id: this.user_id }, @@ -85,12 +88,36 @@ export async function onIdentify(this: WebSocket, data: Payload) { const recipients = await Recipient.find({ where: { user_id: this.user_id }, - relations: ["channel", "channel.recipients"], + relations: ["channel", "channel.recipients", "channel.recipients.user"], + // TODO: public user selection + }); + const channels = recipients.map((x) => { + // @ts-ignore + x.channel.recipients = x.channel.recipients?.map((x) => x.user); + // @ts-ignore + users = users.concat(x.channel.recipients); + if (x.channel.type === ChannelType.DM) { + x.channel.recipients = [ + // @ts-ignore + x.channel.recipients.find((x) => x.id !== this.user_id), + ]; + } + return x.channel; }); - const channels = recipients.map((x) => x.channel); const user = await User.findOneOrFail({ id: this.user_id }); if (!user) return this.close(CLOSECODES.Authentication_failed); + const public_user = { + username: user.username, + discriminator: user.discriminator, + id: user.id, + public_flags: user.public_flags, + avatar: user.avatar, + bot: user.bot, + bio: user.bio, + }; + users.push(public_user); + const session_id = genSessionId(); this.session_id = session_id; //Set the session of the WebSocket object const session = new Session({ @@ -108,16 +135,6 @@ export async function onIdentify(this: WebSocket, data: Payload) { //We save the session and we delete it when the websocket is closed await session.save(); - const public_user = { - username: user.username, - discriminator: user.discriminator, - id: user.id, - public_flags: user.public_flags, - avatar: user.avatar, - bot: user.bot, - bio: user.bio, - }; - const privateUser = { avatar: user.avatar, mobile: user.mobile, @@ -180,7 +197,7 @@ export async function onIdentify(this: WebSocket, data: Payload) { // @ts-ignore experiments: experiments, // TODO guild_join_requests: [], // TODO what is this? - users: [public_user].unique(), // TODO + users: users.unique(), // TODO merged_members: merged_members, // shard // TODO: only for bots sharding // application // TODO for applications diff --git a/util/src/entities/Channel.ts b/util/src/entities/Channel.ts
index 592b0b83..fc954f63 100644 --- a/util/src/entities/Channel.ts +++ b/util/src/entities/Channel.ts
@@ -28,8 +28,8 @@ export class Channel extends BaseClass { @Column() created_at: Date; - @Column() - name: string; + @Column({ nullable: true }) + name?: string; @Column({ type: "simple-enum", enum: ChannelType }) type: ChannelType; @@ -76,11 +76,11 @@ export class Channel extends BaseClass { @Column({ nullable: true }) default_auto_archive_duration?: number; - @Column() - position: number; + @Column({ nullable: true }) + position?: number; - @Column({ type: "simple-json" }) - permission_overwrites: ChannelPermissionOverwrite[]; + @Column({ type: "simple-json", nullable: true }) + permission_overwrites?: ChannelPermissionOverwrite[]; @Column({ nullable: true }) video_quality_mode?: number; diff --git a/util/src/util/Permissions.ts b/util/src/util/Permissions.ts
index 628a495d..9d87253a 100644 --- a/util/src/util/Permissions.ts +++ b/util/src/util/Permissions.ts
@@ -242,7 +242,7 @@ export async function getPermission( }); } - let recipient_ids: any = channel?.recipients?.map((x) => x.id); + let recipient_ids: any = channel?.recipients?.map((x) => x.user_id); if (!recipient_ids?.length) recipient_ids = null; // TODO: remove guild.roles and convert recipient_ids to recipients