🚧 dm channels

This commit is contained in:
Flam3rboy 2021-05-31 20:32:23 +02:00
parent 711dc1c4d4
commit 536900d255
6 changed files with 31 additions and 26 deletions

BIN
package-lock.json generated

Binary file not shown.

View File

@ -29,7 +29,7 @@
},
"homepage": "https://github.com/fosscord/fosscord-api#readme",
"dependencies": {
"@fosscord/server-util": "^1.3.14",
"@fosscord/server-util": "^1.3.15",
"@types/jest": "^26.0.22",
"@types/json-schema": "^7.0.7",
"ajv": "^8.4.0",
@ -49,7 +49,7 @@
"i18next-http-middleware": "^3.1.3",
"i18next-node-fs-backend": "^2.1.3",
"jsonwebtoken": "^8.5.1",
"lambert-server": "^1.2.3",
"lambert-server": "^1.2.4",
"missing-native-js-functions": "^1.2.6",
"mongoose": "^5.12.3",
"mongoose-autopopulate": "^0.12.3",

View File

@ -2,50 +2,52 @@ import { Router, Request, Response } from "express";
import {
ChannelModel,
ChannelCreateEvent,
DMChannel,
UserModel,
toObject,
ChannelType,
Snowflake,
trimSpecial,
Channel,
DMChannel,
UserModel
} from "@fosscord/server-util";
import { HTTPError } from "lambert-server";
import { emitEvent } from "../../../util/Event";
import { getPublicUser } from "../../../util/User";
import { DmChannelCreateSchema } from "../../../schema/Channel";
import { check } from "../../../util/instanceOf";
const router: Router = Router();
router.get("/", async (req: Request, res: Response) => {
var channels = await ChannelModel.find({
$or: [
{ recipients: req.user_id, type: ChannelType.DM },
{ recipients: req.user_id, type: ChannelType.GROUP_DM },
],
}).exec();
var channels = await ChannelModel.find({ recipient_ids: req.user_id }).exec();
res.json(toObject(channels));
});
router.post("/", check(DmChannelCreateSchema), async (req, res) => {
const body = req.body as DmChannelCreateSchema;
if (body.recipients.length === 0) throw new HTTPError("You need to specify at least one recipient");
body.recipients = body.recipients.filter((x) => x !== req.user_id).unique();
if (!(await Promise.all(body.recipients.map((x) => UserModel.exists({ id: x })))).every((x) => x)) {
throw new HTTPError("Recipient not found");
}
const type = body.recipients.length === 1 ? ChannelType.DM : ChannelType.GROUP_DM;
const name = trimSpecial(body.name);
const channel = {
const channel = await new ChannelModel({
name,
type,
owner_id: req.user_id,
id: Snowflake.generate(),
created_at: new Date(),
};
await new ChannelModel(channel).save();
last_message_id: null,
recipient_ids: [...body.recipients, req.user_id]
}).save();
/*Event({ event: "CHANNEL_CREATE", data: channel } as ChannelCreateEvent);*/
await emitEvent({ event: "CHANNEL_CREATE", data: toObject(channel), user_id: req.user_id } as ChannelCreateEvent);
res.json(channel);
res.json(toObject(channel));
});
export default router;

View File

@ -1,5 +1,5 @@
import { Router, Request, Response } from "express";
import { UserModel } from "@fosscord/server-util";
import { GuildModel, MemberModel, UserModel } from "@fosscord/server-util";
import bcrypt from "bcrypt";
const router = Router();
@ -8,7 +8,10 @@ router.post("/", async (req: Request, res: Response) => {
let correctpass = await bcrypt.compare(req.body.password, user!.user_data.hash); //Not sure if user typed right password :/
if (correctpass) {
await UserModel.deleteOne({ id: req.user_id }).exec(); //Yeetus user deletus
await Promise.all([
UserModel.deleteOne({ id: req.user_id }).exec(), //Yeetus user deletus
MemberModel.deleteMany({ id: req.user_id }).exec()
]);
res.sendStatus(204);
} else {

View File

@ -14,16 +14,16 @@ export const ChannelModifySchema = {
id: String,
type: new Length(Number, 0, 1), // either 0 (role) or 1 (member)
allow: BigInt,
deny: BigInt,
},
deny: BigInt
}
],
$parent_id: String,
$nsfw: Boolean,
$nsfw: Boolean
};
export const DmChannelCreateSchema = {
$name: String,
recipients: [String],
recipients: new Length([String], 1, 10)
};
export interface DmChannelCreateSchema {
@ -52,8 +52,8 @@ export interface ChannelModifySchema {
export const ChannelGuildPositionUpdateSchema = [
{
id: String,
$position: Number,
},
$position: Number
}
];
export type ChannelGuildPositionUpdateSchema = {

View File

@ -45,7 +45,7 @@ export async function createChannel(channel: Partial<TextChannel | VoiceChannel>
id: Snowflake.generate(),
created_at: new Date(),
// @ts-ignore
recipients: null
recipient_ids: null
}).save();
await emitEvent({ event: "CHANNEL_CREATE", data: channel, guild_id: channel.guild_id } as ChannelCreateEvent);