aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/models/account/account.ts
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2019-07-31 15:57:32 +0200
committerChocobozzz <chocobozzz@cpy.re>2019-08-01 09:11:04 +0200
commitbfbd912886eba17b4aa9a40dcef2fddc685d85bf (patch)
tree85e0f22980210a8ccd0888eb5e1790b152074677 /server/models/account/account.ts
parent85394ba22a07bde1dfccebf3f591a5d6dbe9df56 (diff)
downloadPeerTube-bfbd912886eba17b4aa9a40dcef2fddc685d85bf.tar.gz
PeerTube-bfbd912886eba17b4aa9a40dcef2fddc685d85bf.tar.zst
PeerTube-bfbd912886eba17b4aa9a40dcef2fddc685d85bf.zip
Fix broken playlist api
Diffstat (limited to 'server/models/account/account.ts')
-rw-r--r--server/models/account/account.ts71
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'
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}