diff options
Diffstat (limited to 'server/models/abuse/abuse-message.ts')
-rw-r--r-- | server/models/abuse/abuse-message.ts | 103 |
1 files changed, 103 insertions, 0 deletions
diff --git a/server/models/abuse/abuse-message.ts b/server/models/abuse/abuse-message.ts new file mode 100644 index 000000000..f7721c87d --- /dev/null +++ b/server/models/abuse/abuse-message.ts | |||
@@ -0,0 +1,103 @@ | |||
1 | import { AllowNull, BelongsTo, Column, CreatedAt, DataType, ForeignKey, Is, Model, Table, UpdatedAt } from 'sequelize-typescript' | ||
2 | import { isAbuseMessageValid } from '@server/helpers/custom-validators/abuses' | ||
3 | import { AbuseMessage } from '@shared/models' | ||
4 | import { AccountModel, ScopeNames as AccountScopeNames } from '../account/account' | ||
5 | import { throwIfNotValid, getSort } from '../utils' | ||
6 | import { AbuseModel } from './abuse' | ||
7 | import { MAbuseMessageFormattable, MAbuseMessage } from '@server/types/models' | ||
8 | |||
9 | @Table({ | ||
10 | tableName: 'abuseMessage', | ||
11 | indexes: [ | ||
12 | { | ||
13 | fields: [ 'abuseId' ] | ||
14 | }, | ||
15 | { | ||
16 | fields: [ 'accountId' ] | ||
17 | } | ||
18 | ] | ||
19 | }) | ||
20 | export class AbuseMessageModel extends Model<AbuseMessageModel> { | ||
21 | |||
22 | @AllowNull(false) | ||
23 | @Is('AbuseMessage', value => throwIfNotValid(value, isAbuseMessageValid, 'message')) | ||
24 | @Column(DataType.TEXT) | ||
25 | message: string | ||
26 | |||
27 | @AllowNull(false) | ||
28 | @Column | ||
29 | byModerator: boolean | ||
30 | |||
31 | @CreatedAt | ||
32 | createdAt: Date | ||
33 | |||
34 | @UpdatedAt | ||
35 | updatedAt: Date | ||
36 | |||
37 | @ForeignKey(() => AccountModel) | ||
38 | @Column | ||
39 | accountId: number | ||
40 | |||
41 | @BelongsTo(() => AccountModel, { | ||
42 | foreignKey: { | ||
43 | name: 'accountId', | ||
44 | allowNull: true | ||
45 | }, | ||
46 | onDelete: 'set null' | ||
47 | }) | ||
48 | Account: AccountModel | ||
49 | |||
50 | @ForeignKey(() => AbuseModel) | ||
51 | @Column | ||
52 | abuseId: number | ||
53 | |||
54 | @BelongsTo(() => AbuseModel, { | ||
55 | foreignKey: { | ||
56 | name: 'abuseId', | ||
57 | allowNull: false | ||
58 | }, | ||
59 | onDelete: 'cascade' | ||
60 | }) | ||
61 | Abuse: AbuseModel | ||
62 | |||
63 | static listForApi (abuseId: number) { | ||
64 | const options = { | ||
65 | where: { abuseId }, | ||
66 | |||
67 | order: getSort('createdAt'), | ||
68 | |||
69 | include: [ | ||
70 | { | ||
71 | model: AccountModel.scope(AccountScopeNames.SUMMARY), | ||
72 | required: false | ||
73 | } | ||
74 | ] | ||
75 | } | ||
76 | |||
77 | return AbuseMessageModel.findAndCountAll(options) | ||
78 | .then(({ rows, count }) => ({ data: rows, total: count })) | ||
79 | } | ||
80 | |||
81 | static loadByIdAndAbuseId (messageId: number, abuseId: number): Promise<MAbuseMessage> { | ||
82 | return AbuseMessageModel.findOne({ | ||
83 | where: { | ||
84 | id: messageId, | ||
85 | abuseId | ||
86 | } | ||
87 | }) | ||
88 | } | ||
89 | |||
90 | toFormattedJSON (this: MAbuseMessageFormattable): AbuseMessage { | ||
91 | const account = this.Account | ||
92 | ? this.Account.toFormattedSummaryJSON() | ||
93 | : null | ||
94 | |||
95 | return { | ||
96 | id: this.id, | ||
97 | byModerator: this.byModerator, | ||
98 | message: this.message, | ||
99 | |||
100 | account | ||
101 | } | ||
102 | } | ||
103 | } | ||