]>
Commit | Line | Data |
---|---|---|
7ad9b984 C |
1 | import { BelongsTo, Column, CreatedAt, ForeignKey, Model, Scopes, Table, UpdatedAt } from 'sequelize-typescript' |
2 | import { AccountModel } from '../account/account' | |
3 | import { ServerModel } from './server' | |
4 | import { ServerBlock } from '../../../shared/models/blocklist' | |
5 | import { getSort } from '../utils' | |
6 | ||
7 | enum ScopeNames { | |
8 | WITH_ACCOUNT = 'WITH_ACCOUNT', | |
9 | WITH_SERVER = 'WITH_SERVER' | |
10 | } | |
11 | ||
3acc5084 | 12 | @Scopes(() => ({ |
7ad9b984 C |
13 | [ScopeNames.WITH_ACCOUNT]: { |
14 | include: [ | |
15 | { | |
3acc5084 | 16 | model: AccountModel, |
7ad9b984 C |
17 | required: true |
18 | } | |
19 | ] | |
20 | }, | |
21 | [ScopeNames.WITH_SERVER]: { | |
22 | include: [ | |
23 | { | |
3acc5084 | 24 | model: ServerModel, |
7ad9b984 C |
25 | required: true |
26 | } | |
27 | ] | |
28 | } | |
3acc5084 | 29 | })) |
7ad9b984 C |
30 | |
31 | @Table({ | |
32 | tableName: 'serverBlocklist', | |
33 | indexes: [ | |
34 | { | |
35 | fields: [ 'accountId', 'targetServerId' ], | |
36 | unique: true | |
37 | }, | |
38 | { | |
39 | fields: [ 'targetServerId' ] | |
40 | } | |
41 | ] | |
42 | }) | |
43 | export class ServerBlocklistModel extends Model<ServerBlocklistModel> { | |
44 | ||
45 | @CreatedAt | |
46 | createdAt: Date | |
47 | ||
48 | @UpdatedAt | |
49 | updatedAt: Date | |
50 | ||
51 | @ForeignKey(() => AccountModel) | |
52 | @Column | |
53 | accountId: number | |
54 | ||
55 | @BelongsTo(() => AccountModel, { | |
56 | foreignKey: { | |
57 | name: 'accountId', | |
58 | allowNull: false | |
59 | }, | |
60 | onDelete: 'CASCADE' | |
61 | }) | |
62 | ByAccount: AccountModel | |
63 | ||
64 | @ForeignKey(() => ServerModel) | |
65 | @Column | |
66 | targetServerId: number | |
67 | ||
68 | @BelongsTo(() => ServerModel, { | |
69 | foreignKey: { | |
70 | name: 'targetServerId', | |
71 | allowNull: false | |
72 | }, | |
73 | onDelete: 'CASCADE' | |
74 | }) | |
af5767ff | 75 | BlockedServer: ServerModel |
7ad9b984 C |
76 | |
77 | static loadByAccountAndHost (accountId: number, host: string) { | |
78 | const query = { | |
79 | where: { | |
80 | accountId | |
81 | }, | |
82 | include: [ | |
83 | { | |
84 | model: ServerModel, | |
85 | where: { | |
86 | host | |
87 | }, | |
88 | required: true | |
89 | } | |
90 | ] | |
91 | } | |
92 | ||
93 | return ServerBlocklistModel.findOne(query) | |
94 | } | |
95 | ||
96 | static listForApi (accountId: number, start: number, count: number, sort: string) { | |
97 | const query = { | |
98 | offset: start, | |
99 | limit: count, | |
100 | order: getSort(sort), | |
101 | where: { | |
102 | accountId | |
103 | } | |
104 | } | |
105 | ||
106 | return ServerBlocklistModel | |
107 | .scope([ ScopeNames.WITH_ACCOUNT, ScopeNames.WITH_SERVER ]) | |
108 | .findAndCountAll(query) | |
109 | .then(({ rows, count }) => { | |
110 | return { total: count, data: rows } | |
111 | }) | |
112 | } | |
113 | ||
114 | toFormattedJSON (): ServerBlock { | |
115 | return { | |
116 | byAccount: this.ByAccount.toFormattedJSON(), | |
af5767ff | 117 | blockedServer: this.BlockedServer.toFormattedJSON(), |
7ad9b984 C |
118 | createdAt: this.createdAt |
119 | } | |
120 | } | |
121 | } |