Newer
Older
import { Ban, Instance as InstanceDB } from "@prisma/client";
import { prisma } from "../lib/prisma";
export interface IInstanceMeta {
logo_uri?: string;
banner_uri?: string;
name?: string;
}
export class Instance {
private instance: InstanceDB;
private constructor(data: InstanceDB) {
this.instance = data;
}
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
/**
* Update Instance instance
*
* @throws InstanceNotFound Instance no longer exists (was deleted?)
*/
async update() {
const instance = await prisma.instance.findFirst({
where: {
id: this.instance.id,
},
});
if (!instance) throw new InstanceNotFound("Instance no longer exists");
this.instance = instance;
}
/**
* Get effective ban
*
* Filters through any subdomain bans
*/
async getEffectiveBan(): Promise<(Ban & { hostname: string }) | undefined> {
let applicable: Ban | undefined | null;
let hostname: string = this.instance.hostname;
const check = async (domain: string): Promise<any> => {
const instance = await Instance.fromDomain(domain);
hostname = domain;
applicable = await instance.getBan();
if (!applicable) {
const newDomain = domain.split(".").slice(1).join(".");
if (newDomain) {
return check(newDomain);
}
}
};
await check(this.instance.hostname);
return applicable
? {
...applicable,
hostname,
}
: undefined;
}
/**
* Get ban for this hostname
*
* @see Instance#getBans use this instead
*/
async getBan() {
const ban = await prisma.ban.findFirst({
where: {
instanceId: this.instance.id,
},
});
return ban;
}
/**
* Bans an instance (create / update)
*
* This bans all subdomains
*
* @note does not create audit log
* @note does not retroactively ban users, only blocks new users
*/
async ban(
expires: Date,
publicNote: string | null | undefined,
privateNote: string | null | undefined
) {
this.instance.hostname
);
const ban = await prisma.ban.upsert({
where: {
instanceId: this.instance.id,
},
create: {
instanceId: this.instance.id,
expiresAt: expires,
publicNote,
privateNote,
},
update: {
instanceId: this.instance.id,
expiresAt: expires,
publicNote,
privateNote,
},
});
}
/**
* Unbans an instance
*
* @note does not create audit log
* @note does not unban a subdomain that was banned because of inheritance
* @throws InstanceNotBanned
*/
async unban() {
const existing = await this.getBan();
if (!existing) throw new InstanceNotBanned();
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
}
static async fromDomain(hostname: string): Promise<Instance> {
const instance = await prisma.instance.upsert({
where: {
hostname,
},
update: {},
create: {
hostname,
},
});
return new this(instance);
}
/**
* Get instance from hostname & update with new instance meta
* @param hostname
* @param instanceMeta
* @returns
*/
static async fromAuth(
hostname: string,
instanceMeta: IInstanceMeta
): Promise<Instance> {
if (!this.isHostnameValid(hostname)) {
throw new InstanceInvalid();
}
const instance = await prisma.instance.upsert({
where: {
hostname,
},
update: {
hostname,
name: instanceMeta.name,
logo_url: instanceMeta.logo_uri,
banner_url: instanceMeta.banner_uri,
},
create: {
hostname,
name: instanceMeta.name,
logo_url: instanceMeta.logo_uri,
banner_url: instanceMeta.banner_uri,
},
});
return new this(instance);
}
/**
* Get all registered subdomains from a domain
* @param hostname
*/
static async getRegisteredSubdomains(hostname: string): Promise<Instance[]> {
return [];
}
/**
* Determine if a hostname is valid to be an instance
*
* Currently restricts the amount of domain parts
*
* @param hostname
* @returns
*/
static isHostnameValid(hostname: string): boolean {
return (hostname.match(/\./g) || []).length <= 5;
}
}
export class InstanceInvalid extends Error {
constructor() {
super();
this.name = "InstanceInvalid";
}
}
export class InstanceNotFound extends Error {
constructor(message?: string) {
super(message);
this.name = "InstanceNotFound";
}
}
export class InstanceNotBanned extends Error {
constructor() {
super();
this.name = "InstanceNotBanned";
}
}