diff options
Diffstat (limited to 'server/models/account')
-rw-r--r-- | server/models/account/account-blocklist.ts | 111 |
1 files changed, 111 insertions, 0 deletions
diff --git a/server/models/account/account-blocklist.ts b/server/models/account/account-blocklist.ts new file mode 100644 index 000000000..bacd122e8 --- /dev/null +++ b/server/models/account/account-blocklist.ts | |||
@@ -0,0 +1,111 @@ | |||
1 | import { BelongsTo, Column, CreatedAt, ForeignKey, Model, Scopes, Table, UpdatedAt } from 'sequelize-typescript' | ||
2 | import { AccountModel } from './account' | ||
3 | import { getSort } from '../utils' | ||
4 | import { AccountBlock } from '../../../shared/models/blocklist' | ||
5 | |||
6 | enum ScopeNames { | ||
7 | WITH_ACCOUNTS = 'WITH_ACCOUNTS' | ||
8 | } | ||
9 | |||
10 | @Scopes({ | ||
11 | [ScopeNames.WITH_ACCOUNTS]: { | ||
12 | include: [ | ||
13 | { | ||
14 | model: () => AccountModel, | ||
15 | required: true, | ||
16 | as: 'ByAccount' | ||
17 | }, | ||
18 | { | ||
19 | model: () => AccountModel, | ||
20 | required: true, | ||
21 | as: 'AccountBlocked' | ||
22 | } | ||
23 | ] | ||
24 | } | ||
25 | }) | ||
26 | |||
27 | @Table({ | ||
28 | tableName: 'accountBlocklist', | ||
29 | indexes: [ | ||
30 | { | ||
31 | fields: [ 'accountId', 'targetAccountId' ], | ||
32 | unique: true | ||
33 | }, | ||
34 | { | ||
35 | fields: [ 'targetAccountId' ] | ||
36 | } | ||
37 | ] | ||
38 | }) | ||
39 | export class AccountBlocklistModel extends Model<AccountBlocklistModel> { | ||
40 | |||
41 | @CreatedAt | ||
42 | createdAt: Date | ||
43 | |||
44 | @UpdatedAt | ||
45 | updatedAt: Date | ||
46 | |||
47 | @ForeignKey(() => AccountModel) | ||
48 | @Column | ||
49 | accountId: number | ||
50 | |||
51 | @BelongsTo(() => AccountModel, { | ||
52 | foreignKey: { | ||
53 | name: 'accountId', | ||
54 | allowNull: false | ||
55 | }, | ||
56 | as: 'ByAccount', | ||
57 | onDelete: 'CASCADE' | ||
58 | }) | ||
59 | ByAccount: AccountModel | ||
60 | |||
61 | @ForeignKey(() => AccountModel) | ||
62 | @Column | ||
63 | targetAccountId: number | ||
64 | |||
65 | @BelongsTo(() => AccountModel, { | ||
66 | foreignKey: { | ||
67 | name: 'targetAccountId', | ||
68 | allowNull: false | ||
69 | }, | ||
70 | as: 'AccountBlocked', | ||
71 | onDelete: 'CASCADE' | ||
72 | }) | ||
73 | AccountBlocked: AccountModel | ||
74 | |||
75 | static loadByAccountAndTarget (accountId: number, targetAccountId: number) { | ||
76 | const query = { | ||
77 | where: { | ||
78 | accountId, | ||
79 | targetAccountId | ||
80 | } | ||
81 | } | ||
82 | |||
83 | return AccountBlocklistModel.findOne(query) | ||
84 | } | ||
85 | |||
86 | static listForApi (accountId: number, start: number, count: number, sort: string) { | ||
87 | const query = { | ||
88 | offset: start, | ||
89 | limit: count, | ||
90 | order: getSort(sort), | ||
91 | where: { | ||
92 | accountId | ||
93 | } | ||
94 | } | ||
95 | |||
96 | return AccountBlocklistModel | ||
97 | .scope([ ScopeNames.WITH_ACCOUNTS ]) | ||
98 | .findAndCountAll(query) | ||
99 | .then(({ rows, count }) => { | ||
100 | return { total: count, data: rows } | ||
101 | }) | ||
102 | } | ||
103 | |||
104 | toFormattedJSON (): AccountBlock { | ||
105 | return { | ||
106 | byAccount: this.ByAccount.toFormattedJSON(), | ||
107 | accountBlocked: this.AccountBlocked.toFormattedJSON(), | ||
108 | createdAt: this.createdAt | ||
109 | } | ||
110 | } | ||
111 | } | ||