]>
Commit | Line | Data |
---|---|---|
1 | import { AllowNull, Column, CreatedAt, Default, HasMany, Is, Model, Table, UpdatedAt } from 'sequelize-typescript' | |
2 | import { isHostValid } from '../../helpers/custom-validators/servers' | |
3 | import { ActorModel } from '../activitypub/actor' | |
4 | import { throwIfNotValid } from '../utils' | |
5 | import { ServerBlocklistModel } from './server-blocklist' | |
6 | import * as Bluebird from 'bluebird' | |
7 | import { MServer, MServerFormattable } from '@server/typings/models/server' | |
8 | ||
9 | @Table({ | |
10 | tableName: 'server', | |
11 | indexes: [ | |
12 | { | |
13 | fields: [ 'host' ], | |
14 | unique: true | |
15 | } | |
16 | ] | |
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 | ||
25 | @AllowNull(false) | |
26 | @Default(false) | |
27 | @Column | |
28 | redundancyAllowed: boolean | |
29 | ||
30 | @CreatedAt | |
31 | createdAt: Date | |
32 | ||
33 | @UpdatedAt | |
34 | updatedAt: Date | |
35 | ||
36 | @HasMany(() => ActorModel, { | |
37 | foreignKey: { | |
38 | name: 'serverId', | |
39 | allowNull: true | |
40 | }, | |
41 | onDelete: 'CASCADE', | |
42 | hooks: true | |
43 | }) | |
44 | Actors: ActorModel[] | |
45 | ||
46 | @HasMany(() => ServerBlocklistModel, { | |
47 | foreignKey: { | |
48 | allowNull: false | |
49 | }, | |
50 | onDelete: 'CASCADE' | |
51 | }) | |
52 | BlockedByAccounts: ServerBlocklistModel[] | |
53 | ||
54 | static load (id: number): Bluebird<MServer> { | |
55 | const query = { | |
56 | where: { | |
57 | id | |
58 | } | |
59 | } | |
60 | ||
61 | return ServerModel.findOne(query) | |
62 | } | |
63 | ||
64 | static loadByHost (host: string): Bluebird<MServer> { | |
65 | const query = { | |
66 | where: { | |
67 | host | |
68 | } | |
69 | } | |
70 | ||
71 | return ServerModel.findOne(query) | |
72 | } | |
73 | ||
74 | isBlocked () { | |
75 | return this.BlockedByAccounts && this.BlockedByAccounts.length !== 0 | |
76 | } | |
77 | ||
78 | toFormattedJSON (this: MServerFormattable) { | |
79 | return { | |
80 | host: this.host | |
81 | } | |
82 | } | |
83 | } |