]>
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: { | |
7ad9b984 C |
70 | allowNull: false |
71 | }, | |
72 | onDelete: 'CASCADE' | |
73 | }) | |
af5767ff | 74 | BlockedServer: ServerModel |
7ad9b984 C |
75 | |
76 | static loadByAccountAndHost (accountId: number, host: string) { | |
77 | const query = { | |
78 | where: { | |
79 | accountId | |
80 | }, | |
81 | include: [ | |
82 | { | |
83 | model: ServerModel, | |
84 | where: { | |
85 | host | |
86 | }, | |
87 | required: true | |
88 | } | |
89 | ] | |
90 | } | |
91 | ||
92 | return ServerBlocklistModel.findOne(query) | |
93 | } | |
94 | ||
95 | static listForApi (accountId: number, start: number, count: number, sort: string) { | |
96 | const query = { | |
97 | offset: start, | |
98 | limit: count, | |
99 | order: getSort(sort), | |
100 | where: { | |
101 | accountId | |
102 | } | |
103 | } | |
104 | ||
105 | return ServerBlocklistModel | |
106 | .scope([ ScopeNames.WITH_ACCOUNT, ScopeNames.WITH_SERVER ]) | |
107 | .findAndCountAll(query) | |
108 | .then(({ rows, count }) => { | |
109 | return { total: count, data: rows } | |
110 | }) | |
111 | } | |
112 | ||
113 | toFormattedJSON (): ServerBlock { | |
114 | return { | |
115 | byAccount: this.ByAccount.toFormattedJSON(), | |
af5767ff | 116 | blockedServer: this.BlockedServer.toFormattedJSON(), |
7ad9b984 C |
117 | createdAt: this.createdAt |
118 | } | |
119 | } | |
120 | } |