🚧 dm channels
This commit is contained in:
parent
711dc1c4d4
commit
536900d255
BIN
package-lock.json
generated
BIN
package-lock.json
generated
Binary file not shown.
@ -29,7 +29,7 @@
|
|||||||
},
|
},
|
||||||
"homepage": "https://github.com/fosscord/fosscord-api#readme",
|
"homepage": "https://github.com/fosscord/fosscord-api#readme",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@fosscord/server-util": "^1.3.14",
|
"@fosscord/server-util": "^1.3.15",
|
||||||
"@types/jest": "^26.0.22",
|
"@types/jest": "^26.0.22",
|
||||||
"@types/json-schema": "^7.0.7",
|
"@types/json-schema": "^7.0.7",
|
||||||
"ajv": "^8.4.0",
|
"ajv": "^8.4.0",
|
||||||
@ -49,7 +49,7 @@
|
|||||||
"i18next-http-middleware": "^3.1.3",
|
"i18next-http-middleware": "^3.1.3",
|
||||||
"i18next-node-fs-backend": "^2.1.3",
|
"i18next-node-fs-backend": "^2.1.3",
|
||||||
"jsonwebtoken": "^8.5.1",
|
"jsonwebtoken": "^8.5.1",
|
||||||
"lambert-server": "^1.2.3",
|
"lambert-server": "^1.2.4",
|
||||||
"missing-native-js-functions": "^1.2.6",
|
"missing-native-js-functions": "^1.2.6",
|
||||||
"mongoose": "^5.12.3",
|
"mongoose": "^5.12.3",
|
||||||
"mongoose-autopopulate": "^0.12.3",
|
"mongoose-autopopulate": "^0.12.3",
|
||||||
|
|||||||
@ -2,50 +2,52 @@ import { Router, Request, Response } from "express";
|
|||||||
import {
|
import {
|
||||||
ChannelModel,
|
ChannelModel,
|
||||||
ChannelCreateEvent,
|
ChannelCreateEvent,
|
||||||
DMChannel,
|
|
||||||
UserModel,
|
|
||||||
toObject,
|
toObject,
|
||||||
ChannelType,
|
ChannelType,
|
||||||
Snowflake,
|
Snowflake,
|
||||||
trimSpecial,
|
trimSpecial,
|
||||||
|
Channel,
|
||||||
|
DMChannel,
|
||||||
|
UserModel
|
||||||
} from "@fosscord/server-util";
|
} from "@fosscord/server-util";
|
||||||
import { HTTPError } from "lambert-server";
|
import { HTTPError } from "lambert-server";
|
||||||
import { emitEvent } from "../../../util/Event";
|
import { emitEvent } from "../../../util/Event";
|
||||||
import { getPublicUser } from "../../../util/User";
|
|
||||||
import { DmChannelCreateSchema } from "../../../schema/Channel";
|
import { DmChannelCreateSchema } from "../../../schema/Channel";
|
||||||
import { check } from "../../../util/instanceOf";
|
import { check } from "../../../util/instanceOf";
|
||||||
|
|
||||||
const router: Router = Router();
|
const router: Router = Router();
|
||||||
|
|
||||||
router.get("/", async (req: Request, res: Response) => {
|
router.get("/", async (req: Request, res: Response) => {
|
||||||
var channels = await ChannelModel.find({
|
var channels = await ChannelModel.find({ recipient_ids: req.user_id }).exec();
|
||||||
$or: [
|
|
||||||
{ recipients: req.user_id, type: ChannelType.DM },
|
|
||||||
{ recipients: req.user_id, type: ChannelType.GROUP_DM },
|
|
||||||
],
|
|
||||||
}).exec();
|
|
||||||
|
|
||||||
res.json(toObject(channels));
|
res.json(toObject(channels));
|
||||||
});
|
});
|
||||||
|
|
||||||
router.post("/", check(DmChannelCreateSchema), async (req, res) => {
|
router.post("/", check(DmChannelCreateSchema), async (req, res) => {
|
||||||
const body = req.body as DmChannelCreateSchema;
|
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 type = body.recipients.length === 1 ? ChannelType.DM : ChannelType.GROUP_DM;
|
||||||
const name = trimSpecial(body.name);
|
const name = trimSpecial(body.name);
|
||||||
|
|
||||||
const channel = {
|
const channel = await new ChannelModel({
|
||||||
name,
|
name,
|
||||||
type,
|
type,
|
||||||
owner_id: req.user_id,
|
owner_id: req.user_id,
|
||||||
id: Snowflake.generate(),
|
id: Snowflake.generate(),
|
||||||
created_at: new Date(),
|
created_at: new Date(),
|
||||||
};
|
last_message_id: null,
|
||||||
await new ChannelModel(channel).save();
|
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;
|
export default router;
|
||||||
|
|||||||
@ -1,5 +1,5 @@
|
|||||||
import { Router, Request, Response } from "express";
|
import { Router, Request, Response } from "express";
|
||||||
import { UserModel } from "@fosscord/server-util";
|
import { GuildModel, MemberModel, UserModel } from "@fosscord/server-util";
|
||||||
import bcrypt from "bcrypt";
|
import bcrypt from "bcrypt";
|
||||||
const router = Router();
|
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 :/
|
let correctpass = await bcrypt.compare(req.body.password, user!.user_data.hash); //Not sure if user typed right password :/
|
||||||
if (correctpass) {
|
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);
|
res.sendStatus(204);
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@ -14,16 +14,16 @@ export const ChannelModifySchema = {
|
|||||||
id: String,
|
id: String,
|
||||||
type: new Length(Number, 0, 1), // either 0 (role) or 1 (member)
|
type: new Length(Number, 0, 1), // either 0 (role) or 1 (member)
|
||||||
allow: BigInt,
|
allow: BigInt,
|
||||||
deny: BigInt,
|
deny: BigInt
|
||||||
},
|
}
|
||||||
],
|
],
|
||||||
$parent_id: String,
|
$parent_id: String,
|
||||||
$nsfw: Boolean,
|
$nsfw: Boolean
|
||||||
};
|
};
|
||||||
|
|
||||||
export const DmChannelCreateSchema = {
|
export const DmChannelCreateSchema = {
|
||||||
$name: String,
|
$name: String,
|
||||||
recipients: [String],
|
recipients: new Length([String], 1, 10)
|
||||||
};
|
};
|
||||||
|
|
||||||
export interface DmChannelCreateSchema {
|
export interface DmChannelCreateSchema {
|
||||||
@ -52,8 +52,8 @@ export interface ChannelModifySchema {
|
|||||||
export const ChannelGuildPositionUpdateSchema = [
|
export const ChannelGuildPositionUpdateSchema = [
|
||||||
{
|
{
|
||||||
id: String,
|
id: String,
|
||||||
$position: Number,
|
$position: Number
|
||||||
},
|
}
|
||||||
];
|
];
|
||||||
|
|
||||||
export type ChannelGuildPositionUpdateSchema = {
|
export type ChannelGuildPositionUpdateSchema = {
|
||||||
|
|||||||
@ -45,7 +45,7 @@ export async function createChannel(channel: Partial<TextChannel | VoiceChannel>
|
|||||||
id: Snowflake.generate(),
|
id: Snowflake.generate(),
|
||||||
created_at: new Date(),
|
created_at: new Date(),
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
recipients: null
|
recipient_ids: null
|
||||||
}).save();
|
}).save();
|
||||||
|
|
||||||
await emitEvent({ event: "CHANNEL_CREATE", data: channel, guild_id: channel.guild_id } as ChannelCreateEvent);
|
await emitEvent({ event: "CHANNEL_CREATE", data: channel, guild_id: channel.guild_id } as ChannelCreateEvent);
|
||||||
|
|||||||
Reference in New Issue
Block a user