]>
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' |
6b5f72be | 4 | import { AttributesOnly } from '@shared/typescript-utils' |
3fd3ab2d | 5 | import { isHostValid } from '../../helpers/custom-validators/servers' |
7d9ba5c0 | 6 | import { ActorModel } from '../actor/actor' |
8c4bbd94 | 7 | import { buildSQLAttributes, throwIfNotValid } from '../shared' |
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 | }) | |
2760b454 | 53 | BlockedBy: ServerBlocklistModel[] |
bfbd9128 | 54 | |
eb66ee88 C |
55 | // --------------------------------------------------------------------------- |
56 | ||
57 | static getSQLAttributes (tableName: string, aliasPrefix = '') { | |
58 | return buildSQLAttributes({ | |
59 | model: this, | |
60 | tableName, | |
61 | aliasPrefix | |
62 | }) | |
63 | } | |
64 | ||
65 | // --------------------------------------------------------------------------- | |
66 | ||
eae0365b | 67 | static load (id: number, transaction?: Transaction): Promise<MServer> { |
8424c402 C |
68 | const query = { |
69 | where: { | |
70 | id | |
eae0365b C |
71 | }, |
72 | transaction | |
8424c402 C |
73 | } |
74 | ||
75 | return ServerModel.findOne(query) | |
76 | } | |
77 | ||
b49f22d8 | 78 | static loadByHost (host: string): Promise<MServer> { |
c48e82b5 C |
79 | const query = { |
80 | where: { | |
81 | host | |
82 | } | |
83 | } | |
84 | ||
85 | return ServerModel.findOne(query) | |
86 | } | |
7ad9b984 | 87 | |
80fdaf06 C |
88 | static async loadOrCreateByHost (host: string) { |
89 | let server = await ServerModel.loadByHost(host) | |
90 | if (!server) server = await ServerModel.create({ host }) | |
91 | ||
92 | return server | |
93 | } | |
94 | ||
bfbd9128 | 95 | isBlocked () { |
2760b454 | 96 | return this.BlockedBy && this.BlockedBy.length !== 0 |
bfbd9128 C |
97 | } |
98 | ||
1ca9f7c3 | 99 | toFormattedJSON (this: MServerFormattable) { |
7ad9b984 C |
100 | return { |
101 | host: this.host | |
102 | } | |
103 | } | |
39445ead | 104 | } |