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