]>
Commit | Line | Data |
---|---|---|
3fd3ab2d | 1 | import { |
c5a893d5 C |
2 | AllowNull, BeforeDestroy, BelongsTo, Column, CreatedAt, DefaultScope, ForeignKey, HasMany, Is, Model, Scopes, Table, |
3 | UpdatedAt, Default, DataType | |
3fd3ab2d | 4 | } from 'sequelize-typescript' |
50d6de9c | 5 | import { ActivityPubActor } from '../../../shared/models/activitypub' |
2422c46b C |
6 | import { VideoChannel } from '../../../shared/models/videos' |
7 | import { | |
c5a893d5 | 8 | isVideoChannelDescriptionValid, isVideoChannelNameValid, |
2422c46b C |
9 | isVideoChannelSupportValid |
10 | } from '../../helpers/custom-validators/video-channels' | |
c5a893d5 C |
11 | import { logger } from '../../helpers/logger' |
12 | import { sendDeleteActor } from '../../lib/activitypub/send' | |
3fd3ab2d | 13 | import { AccountModel } from '../account/account' |
fadf619a | 14 | import { ActorModel } from '../activitypub/actor' |
3fd3ab2d C |
15 | import { getSort, throwIfNotValid } from '../utils' |
16 | import { VideoModel } from './video' | |
a10fc78b | 17 | import { CONSTRAINTS_FIELDS } from '../../initializers' |
c305467c | 18 | import { AvatarModel } from '../avatar/avatar' |
3fd3ab2d | 19 | |
d48ff09d C |
20 | enum ScopeNames { |
21 | WITH_ACCOUNT = 'WITH_ACCOUNT', | |
50d6de9c | 22 | WITH_ACTOR = 'WITH_ACTOR', |
d48ff09d C |
23 | WITH_VIDEOS = 'WITH_VIDEOS' |
24 | } | |
25 | ||
50d6de9c C |
26 | @DefaultScope({ |
27 | include: [ | |
28 | { | |
29 | model: () => ActorModel, | |
30 | required: true | |
31 | } | |
32 | ] | |
33 | }) | |
d48ff09d C |
34 | @Scopes({ |
35 | [ScopeNames.WITH_ACCOUNT]: { | |
36 | include: [ | |
37 | { | |
a4f99a76 | 38 | model: () => AccountModel.unscoped(), |
50d6de9c C |
39 | required: true, |
40 | include: [ | |
41 | { | |
a4f99a76 | 42 | model: () => ActorModel.unscoped(), |
c305467c C |
43 | required: true, |
44 | include: [ | |
45 | { | |
46 | model: () => AvatarModel.unscoped(), | |
47 | required: false | |
48 | } | |
49 | ] | |
50d6de9c C |
50 | } |
51 | ] | |
d48ff09d C |
52 | } |
53 | ] | |
54 | }, | |
55 | [ScopeNames.WITH_VIDEOS]: { | |
56 | include: [ | |
57 | () => VideoModel | |
58 | ] | |
50d6de9c C |
59 | }, |
60 | [ScopeNames.WITH_ACTOR]: { | |
61 | include: [ | |
62 | () => ActorModel | |
63 | ] | |
d48ff09d C |
64 | } |
65 | }) | |
3fd3ab2d C |
66 | @Table({ |
67 | tableName: 'videoChannel', | |
68 | indexes: [ | |
72c7248b | 69 | { |
3fd3ab2d | 70 | fields: [ 'accountId' ] |
8cd72bd3 C |
71 | }, |
72 | { | |
73 | fields: [ 'actorId' ] | |
72c7248b | 74 | } |
72c7248b | 75 | ] |
3fd3ab2d C |
76 | }) |
77 | export class VideoChannelModel extends Model<VideoChannelModel> { | |
72c7248b | 78 | |
3fd3ab2d C |
79 | @AllowNull(false) |
80 | @Is('VideoChannelName', value => throwIfNotValid(value, isVideoChannelNameValid, 'name')) | |
81 | @Column | |
82 | name: string | |
72c7248b | 83 | |
3fd3ab2d | 84 | @AllowNull(true) |
2422c46b | 85 | @Default(null) |
3fd3ab2d | 86 | @Is('VideoChannelDescription', value => throwIfNotValid(value, isVideoChannelDescriptionValid, 'description')) |
a10fc78b | 87 | @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_CHANNELS.DESCRIPTION.max)) |
3fd3ab2d | 88 | description: string |
72c7248b | 89 | |
2422c46b C |
90 | @AllowNull(true) |
91 | @Default(null) | |
92 | @Is('VideoChannelSupport', value => throwIfNotValid(value, isVideoChannelSupportValid, 'support')) | |
a10fc78b | 93 | @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_CHANNELS.SUPPORT.max)) |
2422c46b C |
94 | support: string |
95 | ||
3fd3ab2d C |
96 | @CreatedAt |
97 | createdAt: Date | |
72c7248b | 98 | |
3fd3ab2d C |
99 | @UpdatedAt |
100 | updatedAt: Date | |
4e50b6a1 | 101 | |
fadf619a C |
102 | @ForeignKey(() => ActorModel) |
103 | @Column | |
104 | actorId: number | |
105 | ||
106 | @BelongsTo(() => ActorModel, { | |
107 | foreignKey: { | |
108 | allowNull: false | |
109 | }, | |
110 | onDelete: 'cascade' | |
111 | }) | |
112 | Actor: ActorModel | |
113 | ||
3fd3ab2d C |
114 | @ForeignKey(() => AccountModel) |
115 | @Column | |
116 | accountId: number | |
4e50b6a1 | 117 | |
3fd3ab2d C |
118 | @BelongsTo(() => AccountModel, { |
119 | foreignKey: { | |
120 | allowNull: false | |
121 | }, | |
6b738c7a | 122 | hooks: true |
3fd3ab2d C |
123 | }) |
124 | Account: AccountModel | |
72c7248b | 125 | |
3fd3ab2d | 126 | @HasMany(() => VideoModel, { |
72c7248b | 127 | foreignKey: { |
3fd3ab2d | 128 | name: 'channelId', |
72c7248b C |
129 | allowNull: false |
130 | }, | |
f05a1c30 C |
131 | onDelete: 'CASCADE', |
132 | hooks: true | |
72c7248b | 133 | }) |
3fd3ab2d | 134 | Videos: VideoModel[] |
72c7248b | 135 | |
f05a1c30 C |
136 | @BeforeDestroy |
137 | static async sendDeleteIfOwned (instance: VideoChannelModel, options) { | |
138 | if (!instance.Actor) { | |
139 | instance.Actor = await instance.$get('Actor', { transaction: options.transaction }) as ActorModel | |
140 | } | |
141 | ||
c5a893d5 | 142 | if (instance.Actor.isOwned()) { |
c5a893d5 C |
143 | return sendDeleteActor(instance.Actor, options.transaction) |
144 | } | |
145 | ||
146 | return undefined | |
3fd3ab2d | 147 | } |
72c7248b | 148 | |
3fd3ab2d C |
149 | static countByAccount (accountId: number) { |
150 | const query = { | |
151 | where: { | |
152 | accountId | |
153 | } | |
72c7248b | 154 | } |
3fd3ab2d C |
155 | |
156 | return VideoChannelModel.count(query) | |
72c7248b C |
157 | } |
158 | ||
3fd3ab2d C |
159 | static listForApi (start: number, count: number, sort: string) { |
160 | const query = { | |
161 | offset: start, | |
162 | limit: count, | |
3bb6c526 | 163 | order: getSort(sort) |
3fd3ab2d | 164 | } |
72c7248b | 165 | |
50d6de9c C |
166 | return VideoChannelModel |
167 | .scope([ ScopeNames.WITH_ACTOR, ScopeNames.WITH_ACCOUNT ]) | |
168 | .findAndCountAll(query) | |
3fd3ab2d C |
169 | .then(({ rows, count }) => { |
170 | return { total: count, data: rows } | |
171 | }) | |
72c7248b C |
172 | } |
173 | ||
3fd3ab2d C |
174 | static listByAccount (accountId: number) { |
175 | const query = { | |
3bb6c526 | 176 | order: getSort('createdAt'), |
3fd3ab2d C |
177 | include: [ |
178 | { | |
179 | model: AccountModel, | |
180 | where: { | |
181 | id: accountId | |
182 | }, | |
50d6de9c | 183 | required: true |
3fd3ab2d C |
184 | } |
185 | ] | |
186 | } | |
72c7248b | 187 | |
50d6de9c C |
188 | return VideoChannelModel |
189 | .findAndCountAll(query) | |
3fd3ab2d C |
190 | .then(({ rows, count }) => { |
191 | return { total: count, data: rows } | |
192 | }) | |
72c7248b C |
193 | } |
194 | ||
3fd3ab2d C |
195 | static loadByIdAndAccount (id: number, accountId: number) { |
196 | const options = { | |
197 | where: { | |
198 | id, | |
199 | accountId | |
d48ff09d | 200 | } |
571389d4 | 201 | } |
3fd3ab2d | 202 | |
50d6de9c C |
203 | return VideoChannelModel |
204 | .scope([ ScopeNames.WITH_ACTOR, ScopeNames.WITH_ACCOUNT ]) | |
205 | .findOne(options) | |
0d0e8dd0 C |
206 | } |
207 | ||
3fd3ab2d | 208 | static loadAndPopulateAccount (id: number) { |
50d6de9c C |
209 | return VideoChannelModel |
210 | .scope([ ScopeNames.WITH_ACTOR, ScopeNames.WITH_ACCOUNT ]) | |
211 | .findById(id) | |
3fd3ab2d | 212 | } |
0d0e8dd0 | 213 | |
3fd3ab2d C |
214 | static loadByUUIDAndPopulateAccount (uuid: string) { |
215 | const options = { | |
50d6de9c C |
216 | include: [ |
217 | { | |
218 | model: ActorModel, | |
219 | required: true, | |
220 | where: { | |
221 | uuid | |
222 | } | |
223 | } | |
224 | ] | |
3fd3ab2d C |
225 | } |
226 | ||
50d6de9c C |
227 | return VideoChannelModel |
228 | .scope([ ScopeNames.WITH_ACTOR, ScopeNames.WITH_ACCOUNT ]) | |
229 | .findOne(options) | |
72c7248b C |
230 | } |
231 | ||
3fd3ab2d C |
232 | static loadAndPopulateAccountAndVideos (id: number) { |
233 | const options = { | |
234 | include: [ | |
3fd3ab2d C |
235 | VideoModel |
236 | ] | |
237 | } | |
72c7248b | 238 | |
50d6de9c C |
239 | return VideoChannelModel |
240 | .scope([ ScopeNames.WITH_ACTOR, ScopeNames.WITH_ACCOUNT, ScopeNames.WITH_VIDEOS ]) | |
241 | .findById(id, options) | |
72c7248b C |
242 | } |
243 | ||
2422c46b | 244 | toFormattedJSON (): VideoChannel { |
50d6de9c | 245 | const actor = this.Actor.toFormattedJSON() |
6b738c7a | 246 | const videoChannel = { |
3fd3ab2d | 247 | id: this.id, |
749c7247 | 248 | displayName: this.getDisplayName(), |
3fd3ab2d | 249 | description: this.description, |
2422c46b | 250 | support: this.support, |
50d6de9c | 251 | isLocal: this.Actor.isOwned(), |
3fd3ab2d | 252 | createdAt: this.createdAt, |
6b738c7a C |
253 | updatedAt: this.updatedAt, |
254 | ownerAccount: undefined, | |
255 | videos: undefined | |
256 | } | |
257 | ||
a4f99a76 | 258 | if (this.Account) videoChannel.ownerAccount = this.Account.toFormattedJSON() |
72c7248b | 259 | |
6b738c7a | 260 | return Object.assign(actor, videoChannel) |
72c7248b C |
261 | } |
262 | ||
50d6de9c C |
263 | toActivityPubObject (): ActivityPubActor { |
264 | const obj = this.Actor.toActivityPubObject(this.name, 'VideoChannel') | |
265 | ||
266 | return Object.assign(obj, { | |
267 | summary: this.description, | |
2422c46b | 268 | support: this.support, |
50d6de9c C |
269 | attributedTo: [ |
270 | { | |
271 | type: 'Person' as 'Person', | |
272 | id: this.Account.Actor.url | |
273 | } | |
274 | ] | |
275 | }) | |
72c7248b | 276 | } |
749c7247 C |
277 | |
278 | getDisplayName () { | |
279 | return this.name | |
280 | } | |
72c7248b | 281 | } |