Repository navigation
Expand file tree
/
Copy pathrole.ts
More file actions
82 lines (70 loc) · 1.89 KB
/
Copy pathrole.ts
File metadata and controls
82 lines (70 loc) · 1.89 KB
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
import { Column, Entity, Index, JoinTable, ManyToMany, ManyToOne, Unique } from "typeorm";
import { z } from "zod";
import { ActorMention } from "../util/activitypub/constants.js";
import { DefaultPermissions, PERMISSION } from "../util/permission.js";
import { BaseModel } from "./basemodel.js";
import type { Guild } from "./guild.js";
import type { Member } from "./member.js";
@Entity("roles")
@Unique("role_ordering", ["position", "guild"], {
deferrable: "INITIALLY DEFERRED",
})
export class Role extends BaseModel {
/**
* Roles are federated objects, and to prevent ID collision,
* we need to generate local IDs and store the remote ID to track foreign roles
*/
@Column({ type: String, nullable: true })
@Index({ unique: true, where: "remote_id IS NOT NULL" })
remote_id: string | null;
@Column()
name: string;
@ManyToOne("guilds", { onDelete: "CASCADE" })
guild: Guild;
@Column()
position: number;
@Column({
type: "enum",
enum: PERMISSION,
array: true,
default: DefaultPermissions,
})
allow: PERMISSION[];
@Column({
type: "enum",
enum: PERMISSION,
array: true,
default: [],
})
deny: PERMISSION[];
@ManyToMany("guild_members", "roles")
@JoinTable()
members: Member[];
public toPublic(): PublicRole {
return {
id: this.remote_id ?? this.id,
name: this.name,
guild: this.guild?.mention,
allow: this.allow,
deny: this.deny,
position: this.position,
};
}
public toPrivate(): PublicRole {
return this.toPublic();
}
}
export type PublicRole = Pick<Role, "id" | "name" | "allow" | "deny" | "position"> & {
guild?: ActorMention;
};
export const ZodPermission = z.enum(PERMISSION).openapi("Permission");
export const PublicRole: z.ZodType<Omit<PublicRole, "guild">> = z
.object({
id: z.uuid(),
name: z.string(),
allow: ZodPermission.array(),
deny: ZodPermission.array(),
guild: ActorMention,
position: z.number(),
})
.openapi("PublicRole");