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