aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/models/account
diff options
context:
space:
mode:
Diffstat (limited to 'server/models/account')
-rw-r--r--server/models/account/account-video-rate.ts4
-rw-r--r--server/models/account/account.ts71
2 files changed, 65 insertions, 10 deletions
diff --git a/server/models/account/account-video-rate.ts b/server/models/account/account-video-rate.ts
index 59f586b54..85af9e378 100644
--- a/server/models/account/account-video-rate.ts
+++ b/server/models/account/account-video-rate.ts
@@ -9,7 +9,7 @@ import { ActorModel } from '../activitypub/actor'
9import { getSort, throwIfNotValid } from '../utils' 9import { getSort, throwIfNotValid } from '../utils'
10import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc' 10import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc'
11import { AccountVideoRate } from '../../../shared' 11import { AccountVideoRate } from '../../../shared'
12import { ScopeNames as VideoChannelScopeNames, VideoChannelModel } from '../video/video-channel' 12import { ScopeNames as VideoChannelScopeNames, SummaryOptions, VideoChannelModel } from '../video/video-channel'
13 13
14/* 14/*
15 Account rates per video. 15 Account rates per video.
@@ -109,7 +109,7 @@ export class AccountVideoRateModel extends Model<AccountVideoRateModel> {
109 required: true, 109 required: true,
110 include: [ 110 include: [
111 { 111 {
112 model: VideoChannelModel.scope({ method: [VideoChannelScopeNames.SUMMARY, true] }), 112 model: VideoChannelModel.scope({ method: [VideoChannelScopeNames.SUMMARY, { withAccount: true } as SummaryOptions ] }),
113 required: true 113 required: true
114 } 114 }
115 ] 115 ]
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'
27import { AvatarModel } from '../avatar/avatar' 27import { AvatarModel } from '../avatar/avatar'
28import { VideoPlaylistModel } from '../video/video-playlist' 28import { VideoPlaylistModel } from '../video/video-playlist'
29import { CONSTRAINTS_FIELDS, WEBSERVER } from '../../initializers/constants' 29import { CONSTRAINTS_FIELDS, WEBSERVER } from '../../initializers/constants'
30import { Op, Transaction, WhereOptions } from 'sequelize' 30import { FindOptions, IncludeOptions, Op, Transaction, WhereOptions } from 'sequelize'
31import { AccountBlocklistModel } from './account-blocklist'
32import { ServerBlocklistModel } from '../server/server-blocklist'
31 33
32export enum ScopeNames { 34export enum ScopeNames {
33 SUMMARY = 'SUMMARY' 35 SUMMARY = 'SUMMARY'
34} 36}
35 37
38export 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}