🐛 fix dm #321

This commit is contained in:
Flam3rboy 2021-09-13 01:11:03 +02:00
parent 6b21e107dd
commit 4f1b926d09
7 changed files with 50 additions and 31 deletions

View File

@ -35,7 +35,7 @@ router.put(
allow: body.allow, allow: body.allow,
deny: body.deny deny: body.deny
}; };
channel.permission_overwrites.push(overwrite); channel.permission_overwrites!.push(overwrite);
} }
overwrite.allow = body.allow; overwrite.allow = body.allow;
overwrite.deny = body.deny; 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 }); const channel = await Channel.findOneOrFail({ id: channel_id });
if (!channel.guild_id) throw new HTTPError("Channel not found", 404); 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([ await Promise.all([
channel.save(), channel.save(),

View File

@ -4,11 +4,6 @@ import { HTTPError } from "lambert-server";
import { route } from "@fosscord/api"; import { route } from "@fosscord/api";
import { In } from "typeorm"; import { In } from "typeorm";
export interface DmChannelCreateSchema {
name?: string;
recipients: string[];
}
const router: Router = Router(); const router: Router = Router();
router.get("/", route({}), async (req: Request, res: Response) => { 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)); res.json(recipients.map((x) => x.channel));
}); });
export interface DmChannelCreateSchema {
name?: string;
recipients: string[];
}
router.post("/", route({ body: "DmChannelCreateSchema" }), async (req: Request, res: Response) => { router.post("/", route({ body: "DmChannelCreateSchema" }), async (req: Request, res: Response) => {
const body = req.body as DmChannelCreateSchema; const body = req.body as DmChannelCreateSchema;
body.recipients = body.recipients.filter((x) => x !== req.user_id).unique(); 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) { if (recipients.length !== body.recipients.length) {
throw new HTTPError("Recipient/s not found"); throw new HTTPError("Recipient/s not found");
@ -34,10 +34,10 @@ router.post("/", route({ body: "DmChannelCreateSchema" }), async (req: Request,
const channel = await new Channel({ const channel = await new Channel({
name, name,
type, type,
owner_id: req.user_id, // owner_id only for group dm channels
created_at: new Date(), created_at: new Date(),
last_message_id: null, 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(); }).save();
await emitEvent({ event: "CHANNEL_CREATE", data: channel, user_id: req.user_id } as ChannelCreateEvent); await emitEvent({ event: "CHANNEL_CREATE", data: channel, user_id: req.user_id } as ChannelCreateEvent);

View File

@ -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

View File

@ -70,7 +70,7 @@ export async function setupListener(this: WebSocket) {
for (const channel of guild.channels) { for (const channel of guild.channels) {
if ( if (
x x
.overwriteChannel(channel.permission_overwrites) .overwriteChannel(channel.permission_overwrites!)
.has("VIEW_CHANNEL") .has("VIEW_CHANNEL")
) { ) {
this.events[channel.id] = await listenEvent( this.events[channel.id] = await listenEvent(

View File

@ -14,6 +14,8 @@ import {
dbConnection, dbConnection,
PublicMemberProjection, PublicMemberProjection,
PublicMember, PublicMember,
ChannelType,
PublicUser,
} from "@fosscord/util"; } from "@fosscord/util";
import { setupListener } from "../listener/listener"; import { setupListener } from "../listener/listener";
import { IdentifySchema } from "../schema/Identify"; import { IdentifySchema } from "../schema/Identify";
@ -57,6 +59,7 @@ export async function onIdentify(this: WebSocket, data: Payload) {
return this.close(CLOSECODES.Invalid_shard); return this.close(CLOSECODES.Invalid_shard);
} }
} }
var users: PublicUser[] = [];
const members = await Member.find({ const members = await Member.find({
where: { id: this.user_id }, where: { id: this.user_id },
@ -85,12 +88,36 @@ export async function onIdentify(this: WebSocket, data: Payload) {
const recipients = await Recipient.find({ const recipients = await Recipient.find({
where: { user_id: this.user_id }, 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 }); const user = await User.findOneOrFail({ id: this.user_id });
if (!user) return this.close(CLOSECODES.Authentication_failed); 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(); const session_id = genSessionId();
this.session_id = session_id; //Set the session of the WebSocket object this.session_id = session_id; //Set the session of the WebSocket object
const session = new Session({ 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 //We save the session and we delete it when the websocket is closed
await session.save(); 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 = { const privateUser = {
avatar: user.avatar, avatar: user.avatar,
mobile: user.mobile, mobile: user.mobile,
@ -180,7 +197,7 @@ export async function onIdentify(this: WebSocket, data: Payload) {
// @ts-ignore // @ts-ignore
experiments: experiments, // TODO experiments: experiments, // TODO
guild_join_requests: [], // TODO what is this? guild_join_requests: [], // TODO what is this?
users: [public_user].unique(), // TODO users: users.unique(), // TODO
merged_members: merged_members, merged_members: merged_members,
// shard // TODO: only for bots sharding // shard // TODO: only for bots sharding
// application // TODO for applications // application // TODO for applications

View File

@ -28,8 +28,8 @@ export class Channel extends BaseClass {
@Column() @Column()
created_at: Date; created_at: Date;
@Column() @Column({ nullable: true })
name: string; name?: string;
@Column({ type: "simple-enum", enum: ChannelType }) @Column({ type: "simple-enum", enum: ChannelType })
type: ChannelType; type: ChannelType;
@ -76,11 +76,11 @@ export class Channel extends BaseClass {
@Column({ nullable: true }) @Column({ nullable: true })
default_auto_archive_duration?: number; default_auto_archive_duration?: number;
@Column() @Column({ nullable: true })
position: number; position?: number;
@Column({ type: "simple-json" }) @Column({ type: "simple-json", nullable: true })
permission_overwrites: ChannelPermissionOverwrite[]; permission_overwrites?: ChannelPermissionOverwrite[];
@Column({ nullable: true }) @Column({ nullable: true })
video_quality_mode?: number; video_quality_mode?: number;

View File

@ -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; if (!recipient_ids?.length) recipient_ids = null;
// TODO: remove guild.roles and convert recipient_ids to recipients // TODO: remove guild.roles and convert recipient_ids to recipients