]>
Commit | Line | Data |
---|---|---|
eae0365b | 1 | import { Transaction } from 'sequelize' |
c48e82b5 | 2 | import { AllowNull, Column, CreatedAt, Default, HasMany, Is, Model, Table, UpdatedAt } from 'sequelize-typescript' |
b49f22d8 | 3 | import { MServer, MServerFormattable } from '@server/types/models/server' |
16c016e8 | 4 | import { AttributesOnly } from '@shared/core-utils' |
3fd3ab2d | 5 | import { isHostValid } from '../../helpers/custom-validators/servers' |
7d9ba5c0 | 6 | import { ActorModel } from '../actor/actor' |
3fd3ab2d | 7 | import { throwIfNotValid } from '../utils' |
bfbd9128 | 8 | import { ServerBlocklistModel } from './server-blocklist' |
60862425 | 9 | |
3fd3ab2d C |
10 | @Table({ |
11 | tableName: 'server', | |
12 | indexes: [ | |
60862425 | 13 | { |
3fd3ab2d C |
14 | fields: [ 'host' ], |
15 | unique: true | |
60862425 | 16 | } |
60862425 | 17 | ] |
3fd3ab2d | 18 | }) |
16c016e8 | 19 | export class ServerModel extends Model<Partial<AttributesOnly<ServerModel>>> { |
3fd3ab2d C |
20 | |
21 | @AllowNull(false) | |
22 | @Is('Host', value => throwIfNotValid(value, isHostValid, 'valid host')) | |
23 | @Column | |
24 | host: string | |
25 | ||
c48e82b5 C |
26 | @AllowNull(false) |
27 | @Default(false) | |
28 | @Column | |
29 | redundancyAllowed: boolean | |
30 | ||
3fd3ab2d C |
31 | @CreatedAt |
32 | createdAt: Date | |
33 | ||
34 | @UpdatedAt | |
35 | updatedAt: Date | |
f05a1c30 C |
36 | |
37 | @HasMany(() => ActorModel, { | |
38 | foreignKey: { | |
39 | name: 'serverId', | |
40 | allowNull: true | |
41 | }, | |
42 | onDelete: 'CASCADE', | |
43 | hooks: true | |
44 | }) | |
45 | Actors: ActorModel[] | |
c48e82b5 | 46 | |
bfbd9128 C |
47 | @HasMany(() => ServerBlocklistModel, { |
48 | foreignKey: { | |
49 | allowNull: false | |
50 | }, | |
51 | onDelete: 'CASCADE' | |
52 | }) | |
53 | BlockedByAccounts: ServerBlocklistModel[] | |
54 | ||
eae0365b | 55 | static load (id: number, transaction?: Transaction): Promise<MServer> { |
8424c402 C |
56 | const query = { |
57 | where: { | |
58 | id | |
eae0365b C |
59 | }, |
60 | transaction | |
8424c402 C |
61 | } |
62 | ||
63 | return ServerModel.findOne(query) | |
64 | } | |
65 | ||
b49f22d8 | 66 | static loadByHost (host: string): Promise<MServer> { |
c48e82b5 C |
67 | const query = { |
68 | where: { | |
69 | host | |
70 | } | |
71 | } | |
72 | ||
73 | return ServerModel.findOne(query) | |
74 | } | |
7ad9b984 | 75 | |
80fdaf06 C |
76 | static async loadOrCreateByHost (host: string) { |
77 | let server = await ServerModel.loadByHost(host) | |
78 | if (!server) server = await ServerModel.create({ host }) | |
79 | ||
80 | return server | |
81 | } | |
82 | ||
bfbd9128 C |
83 | isBlocked () { |
84 | return this.BlockedByAccounts && this.BlockedByAccounts.length !== 0 | |
85 | } | |
86 | ||
1ca9f7c3 | 87 | toFormattedJSON (this: MServerFormattable) { |
7ad9b984 C |
88 | return { |
89 | host: this.host | |
90 | } | |
91 | } | |
39445ead | 92 | } |