mirror of
https://github.com/spacebarchat/server.git
synced 2024-11-13 14:12:41 +01:00
POST /users/@me/channels
This commit is contained in:
parent
ca02ca690b
commit
75461b9f6e
@ -1,20 +1,79 @@
|
|||||||
import { Router, Request, Response } from "express";
|
import {
|
||||||
import { ChannelModel, MemberModel, UserModel, GuildDeleteEvent, GuildMemberRemoveEvent, toObject } from "@fosscord/server-util";
|
Router,
|
||||||
import { HTTPError } from "lambert-server";
|
Request,
|
||||||
import { emitEvent } from "../../../util/Event";
|
Response
|
||||||
import { getPublicUser } from "../../../util/User";
|
} from "express";
|
||||||
|
import {
|
||||||
|
ChannelModel,
|
||||||
|
ChannelCreateEvent,
|
||||||
|
DMChannel,
|
||||||
|
UserModel,
|
||||||
|
toObject,
|
||||||
|
ChannelType,
|
||||||
|
Snowflake
|
||||||
|
} 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();
|
const router: Router = Router();
|
||||||
|
|
||||||
router.get("/", async (req: Request, res: Response) => {
|
router.get("/", async (req: Request, res: Response) => {
|
||||||
const user = await UserModel.findOne({ id: req.user_id }, { guilds: true }).exec();
|
const user = await UserModel.findOne({
|
||||||
|
id: req.user_id
|
||||||
|
}, {
|
||||||
|
guilds: true
|
||||||
|
}).exec();
|
||||||
if (!user) throw new HTTPError("User not found", 404);
|
if (!user) throw new HTTPError("User not found", 404);
|
||||||
|
|
||||||
var testID = "829044530203328513"; //FOR TEST
|
var testID = "829044530203328513"; //FOR TEST
|
||||||
|
|
||||||
var channels = await ChannelModel.find({ recipients: req.user_id }).exec();
|
var channels = await ChannelModel.find({
|
||||||
|
recipients: req.user_id,
|
||||||
|
type: 1
|
||||||
|
}).exec();
|
||||||
|
|
||||||
res.json(toObject(channels));
|
res.json(toObject(channels));
|
||||||
});
|
});
|
||||||
|
|
||||||
|
router.post("/", check(DmChannelCreateSchema), async (req, res) => {
|
||||||
|
const body = req.body as DmChannelCreateSchema;
|
||||||
|
switch (body.type) {
|
||||||
|
case ChannelType.GUILD_CATEGORY:
|
||||||
|
case ChannelType.GUILD_TEXT:
|
||||||
|
case ChannelType.GUILD_VOICE:
|
||||||
|
throw new HTTPError("You can't create a dm channel in a guild");
|
||||||
|
// TODO:
|
||||||
|
case ChannelType.GUILD_STORE:
|
||||||
|
throw new HTTPError("Not yet supported");
|
||||||
|
case ChannelType.GUILD_NEWS:
|
||||||
|
// TODO: check if guild is community server
|
||||||
|
}
|
||||||
|
|
||||||
|
const channel = {
|
||||||
|
...body,
|
||||||
|
owner_id: req.user_id,
|
||||||
|
id: Snowflake.generate(),
|
||||||
|
created_at: new Date(),
|
||||||
|
};
|
||||||
|
await new ChannelModel(channel).save();
|
||||||
|
|
||||||
|
/*Event({ event: "CHANNEL_CREATE", data: channel } as ChannelCreateEvent);*/
|
||||||
|
|
||||||
|
|
||||||
|
res.json(channel);
|
||||||
|
});
|
||||||
|
|
||||||
export default router;
|
export default router;
|
@ -1,3 +1,4 @@
|
|||||||
|
import { ChannelType } from "@fosscord/server-util";
|
||||||
import { Length } from "../util/instanceOf";
|
import { Length } from "../util/instanceOf";
|
||||||
|
|
||||||
export const ChannelModifySchema = {
|
export const ChannelModifySchema = {
|
||||||
@ -20,6 +21,24 @@ export const ChannelModifySchema = {
|
|||||||
$nsfw: Boolean,
|
$nsfw: Boolean,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const DmChannelCreateSchema = {
|
||||||
|
owner_id: String,
|
||||||
|
$id: String,
|
||||||
|
$created_at: Date,
|
||||||
|
name: String,
|
||||||
|
type: Number,
|
||||||
|
recipients: [String]
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface DmChannelCreateSchema {
|
||||||
|
owner_id: String;
|
||||||
|
id?: String;
|
||||||
|
created_at?: Date;
|
||||||
|
name: String;
|
||||||
|
type: Number;
|
||||||
|
recipients: String[];
|
||||||
|
}
|
||||||
|
|
||||||
export interface ChannelModifySchema {
|
export interface ChannelModifySchema {
|
||||||
name: string;
|
name: string;
|
||||||
type: number;
|
type: number;
|
||||||
|
Loading…
Reference in New Issue
Block a user