diff options
Diffstat (limited to 'server/models/account/account.ts')
-rw-r--r-- | server/models/account/account.ts | 71 |
1 files changed, 63 insertions, 8 deletions
diff --git a/server/models/account/account.ts b/server/models/account/account.ts index 09cada096..28014946f 100644 --- a/server/models/account/account.ts +++ b/server/models/account/account.ts | |||
@@ -27,12 +27,19 @@ import { UserModel } from './user' | |||
27 | import { AvatarModel } from '../avatar/avatar' | 27 | import { AvatarModel } from '../avatar/avatar' |
28 | import { VideoPlaylistModel } from '../video/video-playlist' | 28 | import { VideoPlaylistModel } from '../video/video-playlist' |
29 | import { CONSTRAINTS_FIELDS, WEBSERVER } from '../../initializers/constants' | 29 | import { CONSTRAINTS_FIELDS, WEBSERVER } from '../../initializers/constants' |
30 | import { Op, Transaction, WhereOptions } from 'sequelize' | 30 | import { FindOptions, IncludeOptions, Op, Transaction, WhereOptions } from 'sequelize' |
31 | import { AccountBlocklistModel } from './account-blocklist' | ||
32 | import { ServerBlocklistModel } from '../server/server-blocklist' | ||
31 | 33 | ||
32 | export enum ScopeNames { | 34 | export enum ScopeNames { |
33 | SUMMARY = 'SUMMARY' | 35 | SUMMARY = 'SUMMARY' |
34 | } | 36 | } |
35 | 37 | ||
38 | export type SummaryOptions = { | ||
39 | whereActor?: WhereOptions | ||
40 | withAccountBlockerIds?: number[] | ||
41 | } | ||
42 | |||
36 | @DefaultScope(() => ({ | 43 | @DefaultScope(() => ({ |
37 | include: [ | 44 | include: [ |
38 | { | 45 | { |
@@ -42,8 +49,16 @@ export enum ScopeNames { | |||
42 | ] | 49 | ] |
43 | })) | 50 | })) |
44 | @Scopes(() => ({ | 51 | @Scopes(() => ({ |
45 | [ ScopeNames.SUMMARY ]: (whereActor?: WhereOptions) => { | 52 | [ ScopeNames.SUMMARY ]: (options: SummaryOptions = {}) => { |
46 | return { | 53 | const whereActor = options.whereActor || undefined |
54 | |||
55 | const serverInclude: IncludeOptions = { | ||
56 | attributes: [ 'host' ], | ||
57 | model: ServerModel.unscoped(), | ||
58 | required: false | ||
59 | } | ||
60 | |||
61 | const query: FindOptions = { | ||
47 | attributes: [ 'id', 'name' ], | 62 | attributes: [ 'id', 'name' ], |
48 | include: [ | 63 | include: [ |
49 | { | 64 | { |
@@ -52,11 +67,8 @@ export enum ScopeNames { | |||
52 | required: true, | 67 | required: true, |
53 | where: whereActor, | 68 | where: whereActor, |
54 | include: [ | 69 | include: [ |
55 | { | 70 | serverInclude, |
56 | attributes: [ 'host' ], | 71 | |
57 | model: ServerModel.unscoped(), | ||
58 | required: false | ||
59 | }, | ||
60 | { | 72 | { |
61 | model: AvatarModel.unscoped(), | 73 | model: AvatarModel.unscoped(), |
62 | required: false | 74 | required: false |
@@ -65,6 +77,35 @@ export enum ScopeNames { | |||
65 | } | 77 | } |
66 | ] | 78 | ] |
67 | } | 79 | } |
80 | |||
81 | if (options.withAccountBlockerIds) { | ||
82 | query.include.push({ | ||
83 | attributes: [ 'id' ], | ||
84 | model: AccountBlocklistModel.unscoped(), | ||
85 | as: 'BlockedAccounts', | ||
86 | required: false, | ||
87 | where: { | ||
88 | accountId: { | ||
89 | [Op.in]: options.withAccountBlockerIds | ||
90 | } | ||
91 | } | ||
92 | }) | ||
93 | |||
94 | serverInclude.include = [ | ||
95 | { | ||
96 | attributes: [ 'id' ], | ||
97 | model: ServerBlocklistModel.unscoped(), | ||
98 | required: false, | ||
99 | where: { | ||
100 | accountId: { | ||
101 | [Op.in]: options.withAccountBlockerIds | ||
102 | } | ||
103 | } | ||
104 | } | ||
105 | ] | ||
106 | } | ||
107 | |||
108 | return query | ||
68 | } | 109 | } |
69 | })) | 110 | })) |
70 | @Table({ | 111 | @Table({ |
@@ -163,6 +204,16 @@ export class AccountModel extends Model<AccountModel> { | |||
163 | }) | 204 | }) |
164 | VideoComments: VideoCommentModel[] | 205 | VideoComments: VideoCommentModel[] |
165 | 206 | ||
207 | @HasMany(() => AccountBlocklistModel, { | ||
208 | foreignKey: { | ||
209 | name: 'targetAccountId', | ||
210 | allowNull: false | ||
211 | }, | ||
212 | as: 'BlockedAccounts', | ||
213 | onDelete: 'CASCADE' | ||
214 | }) | ||
215 | BlockedAccounts: AccountBlocklistModel[] | ||
216 | |||
166 | @BeforeDestroy | 217 | @BeforeDestroy |
167 | static async sendDeleteIfOwned (instance: AccountModel, options) { | 218 | static async sendDeleteIfOwned (instance: AccountModel, options) { |
168 | if (!instance.Actor) { | 219 | if (!instance.Actor) { |
@@ -343,4 +394,8 @@ export class AccountModel extends Model<AccountModel> { | |||
343 | getDisplayName () { | 394 | getDisplayName () { |
344 | return this.name | 395 | return this.name |
345 | } | 396 | } |
397 | |||
398 | isBlocked () { | ||
399 | return this.BlockedAccounts && this.BlockedAccounts.length !== 0 | ||
400 | } | ||
346 | } | 401 | } |