mirror of
https://github.com/spacebarchat/server.git
synced 2024-11-10 12:42:44 +01:00
🐛 fix dm #321
This commit is contained in:
parent
13b7b84761
commit
07ba78d391
@ -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(),
|
||||
|
@ -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);
|
||||
|
@ -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
|
@ -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(
|
||||
|
@ -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
|
||||
|
@ -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;
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user