summary refs log tree commit diff
path: root/util/src/models/Channel.ts
blob: 1dd05896fcf345125e5264edf08489cb1b5b410e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import { Schema, model, Types, Document } from "mongoose";
import db from "../util/Database";
import toBigInt from "../util/toBigInt";
import { PublicUserProjection, UserModel } from "./User";

// @ts-ignore
export interface AnyChannel extends Channel, DMChannel, TextChannel, VoiceChannel {
	recipient_ids: null | string[];
}

export interface ChannelDocument extends Document, AnyChannel {
	id: string;
}

export const ChannelSchema = new Schema({
	id: String,
	created_at: { type: Schema.Types.Date, required: true },
	name: String, // can't be required for dm channels
	type: { type: Number, required: true },
	guild_id: String,
	owner_id: String,
	parent_id: String,
	recipient_ids: [String],
	position: Number,
	last_message_id: String,
	last_pin_timestamp: Date,
	nsfw: Boolean,
	rate_limit_per_user: Number,
	topic: String,
	permission_overwrites: [
		{
			allow: { type: String, get: toBigInt },
			deny: { type: String, get: toBigInt },
			id: String,
			type: { type: Number },
		},
	],
});

ChannelSchema.virtual("recipients", {
	ref: UserModel,
	localField: "recipient_ids",
	foreignField: "id",
	justOne: false,
	autopopulate: { select: PublicUserProjection },
});

ChannelSchema.set("removeResponse", ["recipient_ids"]);

// @ts-ignore
export const ChannelModel = db.model<ChannelDocument>("Channel", ChannelSchema, "channels");

export interface Channel {
	id: string;
	created_at: Date;
	name: string;
	type: number;
}

export interface TextBasedChannel {
	last_message_id?: string;
	last_pin_timestamp?: number;
}

export interface GuildChannel extends Channel {
	guild_id: string;
	position: number;
	parent_id?: string;
	permission_overwrites: ChannelPermissionOverwrite[];
}

export interface ChannelPermissionOverwrite {
	allow: bigint; // for bitfields we use bigints
	deny: bigint; // for bitfields we use bigints
	id: string;
	type: ChannelPermissionOverwriteType;
}

export enum ChannelPermissionOverwriteType {
	role = 0,
	member = 1,
}

export interface VoiceChannel extends GuildChannel {
	video_quality_mode?: number;
	bitrate?: number;
	user_limit?: number;
}

export interface TextChannel extends GuildChannel, TextBasedChannel {
	nsfw: boolean;
	rate_limit_per_user: number;
	topic?: string;
}
// @ts-ignore
export interface DMChannel extends Channel, TextBasedChannel {
	owner_id: string;
	recipient_ids: string[];
}

export enum ChannelType {
	GUILD_TEXT = 0, // a text channel within a server
	DM = 1, // a direct message between users
	GUILD_VOICE = 2, // a voice channel within a server
	GROUP_DM = 3, // a direct message between multiple users
	GUILD_CATEGORY = 4, // an organizational category that contains up to 50 channels
	GUILD_NEWS = 5, // a channel that users can follow and crosspost into their own server
	GUILD_STORE = 6, // a channel in which game developers can sell their game on Discord
}