]>
Commit | Line | Data |
---|---|---|
3fd3ab2d | 1 | import { |
06a05d5f C |
2 | AllowNull, |
3 | BeforeDestroy, | |
4 | BelongsTo, | |
5 | Column, | |
6 | CreatedAt, | |
7 | DataType, | |
8 | Default, | |
9 | DefaultScope, | |
10 | ForeignKey, | |
11 | HasMany, | |
12 | Is, | |
13 | Model, | |
14 | Scopes, | |
f37dc0dd | 15 | Sequelize, |
06a05d5f C |
16 | Table, |
17 | UpdatedAt | |
3fd3ab2d | 18 | } from 'sequelize-typescript' |
50d6de9c | 19 | import { ActivityPubActor } from '../../../shared/models/activitypub' |
2422c46b C |
20 | import { VideoChannel } from '../../../shared/models/videos' |
21 | import { | |
06a05d5f C |
22 | isVideoChannelDescriptionValid, |
23 | isVideoChannelNameValid, | |
2422c46b C |
24 | isVideoChannelSupportValid |
25 | } from '../../helpers/custom-validators/video-channels' | |
c5a893d5 | 26 | import { sendDeleteActor } from '../../lib/activitypub/send' |
3fd3ab2d | 27 | import { AccountModel } from '../account/account' |
f37dc0dd C |
28 | import { ActorModel, unusedActorAttributesForAPI } from '../activitypub/actor' |
29 | import { buildTrigramSearchIndex, createSimilarityAttribute, getSort, throwIfNotValid } from '../utils' | |
3fd3ab2d | 30 | import { VideoModel } from './video' |
a10fc78b | 31 | import { CONSTRAINTS_FIELDS } from '../../initializers' |
8a19bee1 | 32 | import { ServerModel } from '../server/server' |
f37dc0dd C |
33 | import { DefineIndexesOptions } from 'sequelize' |
34 | ||
35 | // FIXME: Define indexes here because there is an issue with TS and Sequelize.literal when called directly in the annotation | |
36 | const indexes: DefineIndexesOptions[] = [ | |
37 | buildTrigramSearchIndex('video_channel_name_trigram', 'name'), | |
38 | ||
39 | { | |
40 | fields: [ 'accountId' ] | |
41 | }, | |
42 | { | |
43 | fields: [ 'actorId' ] | |
44 | } | |
45 | ] | |
3fd3ab2d | 46 | |
d48ff09d | 47 | enum ScopeNames { |
f37dc0dd | 48 | AVAILABLE_FOR_LIST = 'AVAILABLE_FOR_LIST', |
d48ff09d | 49 | WITH_ACCOUNT = 'WITH_ACCOUNT', |
50d6de9c | 50 | WITH_ACTOR = 'WITH_ACTOR', |
d48ff09d C |
51 | WITH_VIDEOS = 'WITH_VIDEOS' |
52 | } | |
53 | ||
f37dc0dd C |
54 | type AvailableForListOptions = { |
55 | actorId: number | |
56 | } | |
57 | ||
50d6de9c C |
58 | @DefaultScope({ |
59 | include: [ | |
60 | { | |
61 | model: () => ActorModel, | |
62 | required: true | |
63 | } | |
64 | ] | |
65 | }) | |
d48ff09d | 66 | @Scopes({ |
f37dc0dd C |
67 | [ScopeNames.AVAILABLE_FOR_LIST]: (options: AvailableForListOptions) => { |
68 | const actorIdNumber = parseInt(options.actorId + '', 10) | |
69 | ||
70 | // Only list local channels OR channels that are on an instance followed by actorId | |
71 | const inQueryInstanceFollow = '(' + | |
c3c2ab1c C |
72 | 'SELECT "actor"."serverId" FROM "actorFollow" ' + |
73 | 'INNER JOIN "actor" ON actor.id= "actorFollow"."targetActorId" ' + | |
8b604880 | 74 | 'WHERE "actorFollow"."actorId" = ' + actorIdNumber + |
f37dc0dd C |
75 | ')' |
76 | ||
77 | return { | |
78 | include: [ | |
79 | { | |
80 | attributes: { | |
81 | exclude: unusedActorAttributesForAPI | |
82 | }, | |
83 | model: ActorModel, | |
84 | where: { | |
85 | [Sequelize.Op.or]: [ | |
c305467c | 86 | { |
f37dc0dd C |
87 | serverId: null |
88 | }, | |
89 | { | |
90 | serverId: { | |
91 | [ Sequelize.Op.in ]: Sequelize.literal(inQueryInstanceFollow) | |
92 | } | |
c305467c C |
93 | } |
94 | ] | |
50d6de9c | 95 | } |
f37dc0dd C |
96 | }, |
97 | { | |
98 | model: AccountModel, | |
99 | required: true, | |
100 | include: [ | |
101 | { | |
102 | attributes: { | |
103 | exclude: unusedActorAttributesForAPI | |
104 | }, | |
105 | model: ActorModel, // Default scope includes avatar and server | |
106 | required: true | |
107 | } | |
108 | ] | |
109 | } | |
110 | ] | |
111 | } | |
112 | }, | |
113 | [ScopeNames.WITH_ACCOUNT]: { | |
114 | include: [ | |
115 | { | |
116 | model: () => AccountModel, | |
117 | required: true | |
d48ff09d C |
118 | } |
119 | ] | |
120 | }, | |
121 | [ScopeNames.WITH_VIDEOS]: { | |
122 | include: [ | |
123 | () => VideoModel | |
124 | ] | |
50d6de9c C |
125 | }, |
126 | [ScopeNames.WITH_ACTOR]: { | |
127 | include: [ | |
128 | () => ActorModel | |
129 | ] | |
d48ff09d C |
130 | } |
131 | }) | |
3fd3ab2d C |
132 | @Table({ |
133 | tableName: 'videoChannel', | |
f37dc0dd | 134 | indexes |
3fd3ab2d C |
135 | }) |
136 | export class VideoChannelModel extends Model<VideoChannelModel> { | |
72c7248b | 137 | |
3fd3ab2d C |
138 | @AllowNull(false) |
139 | @Is('VideoChannelName', value => throwIfNotValid(value, isVideoChannelNameValid, 'name')) | |
140 | @Column | |
141 | name: string | |
72c7248b | 142 | |
3fd3ab2d | 143 | @AllowNull(true) |
2422c46b | 144 | @Default(null) |
3fd3ab2d | 145 | @Is('VideoChannelDescription', value => throwIfNotValid(value, isVideoChannelDescriptionValid, 'description')) |
a10fc78b | 146 | @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_CHANNELS.DESCRIPTION.max)) |
3fd3ab2d | 147 | description: string |
72c7248b | 148 | |
2422c46b C |
149 | @AllowNull(true) |
150 | @Default(null) | |
151 | @Is('VideoChannelSupport', value => throwIfNotValid(value, isVideoChannelSupportValid, 'support')) | |
a10fc78b | 152 | @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_CHANNELS.SUPPORT.max)) |
2422c46b C |
153 | support: string |
154 | ||
3fd3ab2d C |
155 | @CreatedAt |
156 | createdAt: Date | |
72c7248b | 157 | |
3fd3ab2d C |
158 | @UpdatedAt |
159 | updatedAt: Date | |
4e50b6a1 | 160 | |
fadf619a C |
161 | @ForeignKey(() => ActorModel) |
162 | @Column | |
163 | actorId: number | |
164 | ||
165 | @BelongsTo(() => ActorModel, { | |
166 | foreignKey: { | |
167 | allowNull: false | |
168 | }, | |
169 | onDelete: 'cascade' | |
170 | }) | |
171 | Actor: ActorModel | |
172 | ||
3fd3ab2d C |
173 | @ForeignKey(() => AccountModel) |
174 | @Column | |
175 | accountId: number | |
4e50b6a1 | 176 | |
3fd3ab2d C |
177 | @BelongsTo(() => AccountModel, { |
178 | foreignKey: { | |
179 | allowNull: false | |
180 | }, | |
6b738c7a | 181 | hooks: true |
3fd3ab2d C |
182 | }) |
183 | Account: AccountModel | |
72c7248b | 184 | |
3fd3ab2d | 185 | @HasMany(() => VideoModel, { |
72c7248b | 186 | foreignKey: { |
3fd3ab2d | 187 | name: 'channelId', |
72c7248b C |
188 | allowNull: false |
189 | }, | |
f05a1c30 C |
190 | onDelete: 'CASCADE', |
191 | hooks: true | |
72c7248b | 192 | }) |
3fd3ab2d | 193 | Videos: VideoModel[] |
72c7248b | 194 | |
f05a1c30 C |
195 | @BeforeDestroy |
196 | static async sendDeleteIfOwned (instance: VideoChannelModel, options) { | |
197 | if (!instance.Actor) { | |
198 | instance.Actor = await instance.$get('Actor', { transaction: options.transaction }) as ActorModel | |
199 | } | |
200 | ||
c5a893d5 | 201 | if (instance.Actor.isOwned()) { |
c5a893d5 C |
202 | return sendDeleteActor(instance.Actor, options.transaction) |
203 | } | |
204 | ||
205 | return undefined | |
3fd3ab2d | 206 | } |
72c7248b | 207 | |
3fd3ab2d C |
208 | static countByAccount (accountId: number) { |
209 | const query = { | |
210 | where: { | |
211 | accountId | |
212 | } | |
72c7248b | 213 | } |
3fd3ab2d C |
214 | |
215 | return VideoChannelModel.count(query) | |
72c7248b C |
216 | } |
217 | ||
f37dc0dd | 218 | static listForApi (actorId: number, start: number, count: number, sort: string) { |
3fd3ab2d C |
219 | const query = { |
220 | offset: start, | |
221 | limit: count, | |
3bb6c526 | 222 | order: getSort(sort) |
3fd3ab2d | 223 | } |
72c7248b | 224 | |
f37dc0dd C |
225 | const scopes = { |
226 | method: [ ScopeNames.AVAILABLE_FOR_LIST, { actorId } as AvailableForListOptions ] | |
227 | } | |
50d6de9c | 228 | return VideoChannelModel |
f37dc0dd C |
229 | .scope(scopes) |
230 | .findAndCountAll(query) | |
231 | .then(({ rows, count }) => { | |
232 | return { total: count, data: rows } | |
233 | }) | |
234 | } | |
235 | ||
2feebf3e C |
236 | static listLocalsForSitemap (sort: string) { |
237 | const query = { | |
238 | attributes: [ ], | |
239 | offset: 0, | |
240 | order: getSort(sort), | |
241 | include: [ | |
242 | { | |
243 | attributes: [ 'preferredUsername', 'serverId' ], | |
244 | model: ActorModel.unscoped(), | |
245 | where: { | |
246 | serverId: null | |
247 | } | |
248 | } | |
249 | ] | |
250 | } | |
251 | ||
252 | return VideoChannelModel | |
253 | .unscoped() | |
254 | .findAll(query) | |
255 | } | |
256 | ||
f37dc0dd C |
257 | static searchForApi (options: { |
258 | actorId: number | |
259 | search: string | |
260 | start: number | |
261 | count: number | |
262 | sort: string | |
263 | }) { | |
264 | const attributesInclude = [] | |
265 | const escapedSearch = VideoModel.sequelize.escape(options.search) | |
266 | const escapedLikeSearch = VideoModel.sequelize.escape('%' + options.search + '%') | |
267 | attributesInclude.push(createSimilarityAttribute('VideoChannelModel.name', options.search)) | |
268 | ||
269 | const query = { | |
270 | attributes: { | |
271 | include: attributesInclude | |
272 | }, | |
273 | offset: options.start, | |
274 | limit: options.count, | |
275 | order: getSort(options.sort), | |
276 | where: { | |
c3c2ab1c C |
277 | [Sequelize.Op.or]: [ |
278 | Sequelize.literal( | |
279 | 'lower(immutable_unaccent("VideoChannelModel"."name")) % lower(immutable_unaccent(' + escapedSearch + '))' | |
280 | ), | |
281 | Sequelize.literal( | |
282 | 'lower(immutable_unaccent("VideoChannelModel"."name")) LIKE lower(immutable_unaccent(' + escapedLikeSearch + '))' | |
f37dc0dd | 283 | ) |
c3c2ab1c | 284 | ] |
f37dc0dd C |
285 | } |
286 | } | |
287 | ||
288 | const scopes = { | |
289 | method: [ ScopeNames.AVAILABLE_FOR_LIST, { actorId: options.actorId } as AvailableForListOptions ] | |
290 | } | |
291 | return VideoChannelModel | |
292 | .scope(scopes) | |
50d6de9c | 293 | .findAndCountAll(query) |
3fd3ab2d C |
294 | .then(({ rows, count }) => { |
295 | return { total: count, data: rows } | |
296 | }) | |
72c7248b C |
297 | } |
298 | ||
3fd3ab2d C |
299 | static listByAccount (accountId: number) { |
300 | const query = { | |
3bb6c526 | 301 | order: getSort('createdAt'), |
3fd3ab2d C |
302 | include: [ |
303 | { | |
304 | model: AccountModel, | |
305 | where: { | |
306 | id: accountId | |
307 | }, | |
50d6de9c | 308 | required: true |
3fd3ab2d C |
309 | } |
310 | ] | |
311 | } | |
72c7248b | 312 | |
50d6de9c C |
313 | return VideoChannelModel |
314 | .findAndCountAll(query) | |
3fd3ab2d C |
315 | .then(({ rows, count }) => { |
316 | return { total: count, data: rows } | |
317 | }) | |
72c7248b C |
318 | } |
319 | ||
5cf84858 C |
320 | static loadByIdAndPopulateAccount (id: number) { |
321 | return VideoChannelModel.unscoped() | |
322 | .scope([ ScopeNames.WITH_ACTOR, ScopeNames.WITH_ACCOUNT ]) | |
323 | .findById(id) | |
324 | } | |
325 | ||
3fd3ab2d | 326 | static loadByIdAndAccount (id: number, accountId: number) { |
8a19bee1 | 327 | const query = { |
3fd3ab2d C |
328 | where: { |
329 | id, | |
330 | accountId | |
d48ff09d | 331 | } |
571389d4 | 332 | } |
3fd3ab2d | 333 | |
5cf84858 | 334 | return VideoChannelModel.unscoped() |
50d6de9c | 335 | .scope([ ScopeNames.WITH_ACTOR, ScopeNames.WITH_ACCOUNT ]) |
8a19bee1 | 336 | .findOne(query) |
0d0e8dd0 C |
337 | } |
338 | ||
3fd3ab2d | 339 | static loadAndPopulateAccount (id: number) { |
5cf84858 | 340 | return VideoChannelModel.unscoped() |
50d6de9c C |
341 | .scope([ ScopeNames.WITH_ACTOR, ScopeNames.WITH_ACCOUNT ]) |
342 | .findById(id) | |
3fd3ab2d | 343 | } |
0d0e8dd0 | 344 | |
3fd3ab2d | 345 | static loadByUUIDAndPopulateAccount (uuid: string) { |
8a19bee1 | 346 | const query = { |
50d6de9c C |
347 | include: [ |
348 | { | |
349 | model: ActorModel, | |
350 | required: true, | |
351 | where: { | |
352 | uuid | |
353 | } | |
354 | } | |
355 | ] | |
3fd3ab2d C |
356 | } |
357 | ||
50d6de9c | 358 | return VideoChannelModel |
f37dc0dd C |
359 | .scope([ ScopeNames.WITH_ACCOUNT ]) |
360 | .findOne(query) | |
361 | } | |
362 | ||
363 | static loadByUrlAndPopulateAccount (url: string) { | |
364 | const query = { | |
365 | include: [ | |
366 | { | |
367 | model: ActorModel, | |
368 | required: true, | |
369 | where: { | |
370 | url | |
371 | } | |
372 | } | |
373 | ] | |
374 | } | |
375 | ||
376 | return VideoChannelModel | |
377 | .scope([ ScopeNames.WITH_ACCOUNT ]) | |
8a19bee1 | 378 | .findOne(query) |
72c7248b C |
379 | } |
380 | ||
8a19bee1 C |
381 | static loadLocalByNameAndPopulateAccount (name: string) { |
382 | const query = { | |
3fd3ab2d | 383 | include: [ |
8a19bee1 C |
384 | { |
385 | model: ActorModel, | |
386 | required: true, | |
387 | where: { | |
388 | preferredUsername: name, | |
389 | serverId: null | |
390 | } | |
391 | } | |
3fd3ab2d C |
392 | ] |
393 | } | |
72c7248b | 394 | |
5cf84858 | 395 | return VideoChannelModel.unscoped() |
8a19bee1 C |
396 | .scope([ ScopeNames.WITH_ACTOR, ScopeNames.WITH_ACCOUNT ]) |
397 | .findOne(query) | |
72c7248b C |
398 | } |
399 | ||
8a19bee1 | 400 | static loadByNameAndHostAndPopulateAccount (name: string, host: string) { |
06a05d5f C |
401 | const query = { |
402 | include: [ | |
403 | { | |
404 | model: ActorModel, | |
405 | required: true, | |
406 | where: { | |
8a19bee1 C |
407 | preferredUsername: name |
408 | }, | |
409 | include: [ | |
410 | { | |
411 | model: ServerModel, | |
412 | required: true, | |
413 | where: { host } | |
414 | } | |
415 | ] | |
06a05d5f C |
416 | } |
417 | ] | |
418 | } | |
419 | ||
5cf84858 | 420 | return VideoChannelModel.unscoped() |
8a19bee1 C |
421 | .scope([ ScopeNames.WITH_ACTOR, ScopeNames.WITH_ACCOUNT ]) |
422 | .findOne(query) | |
423 | } | |
424 | ||
425 | static loadAndPopulateAccountAndVideos (id: number) { | |
426 | const options = { | |
427 | include: [ | |
428 | VideoModel | |
429 | ] | |
430 | } | |
431 | ||
5cf84858 | 432 | return VideoChannelModel.unscoped() |
8a19bee1 C |
433 | .scope([ ScopeNames.WITH_ACTOR, ScopeNames.WITH_ACCOUNT, ScopeNames.WITH_VIDEOS ]) |
434 | .findById(id, options) | |
06a05d5f C |
435 | } |
436 | ||
2422c46b | 437 | toFormattedJSON (): VideoChannel { |
50d6de9c | 438 | const actor = this.Actor.toFormattedJSON() |
6b738c7a | 439 | const videoChannel = { |
3fd3ab2d | 440 | id: this.id, |
749c7247 | 441 | displayName: this.getDisplayName(), |
3fd3ab2d | 442 | description: this.description, |
2422c46b | 443 | support: this.support, |
50d6de9c | 444 | isLocal: this.Actor.isOwned(), |
3fd3ab2d | 445 | createdAt: this.createdAt, |
6b738c7a | 446 | updatedAt: this.updatedAt, |
06a05d5f | 447 | ownerAccount: undefined |
6b738c7a C |
448 | } |
449 | ||
a4f99a76 | 450 | if (this.Account) videoChannel.ownerAccount = this.Account.toFormattedJSON() |
72c7248b | 451 | |
6b738c7a | 452 | return Object.assign(actor, videoChannel) |
72c7248b C |
453 | } |
454 | ||
50d6de9c C |
455 | toActivityPubObject (): ActivityPubActor { |
456 | const obj = this.Actor.toActivityPubObject(this.name, 'VideoChannel') | |
457 | ||
458 | return Object.assign(obj, { | |
459 | summary: this.description, | |
2422c46b | 460 | support: this.support, |
50d6de9c C |
461 | attributedTo: [ |
462 | { | |
463 | type: 'Person' as 'Person', | |
464 | id: this.Account.Actor.url | |
465 | } | |
466 | ] | |
467 | }) | |
72c7248b | 468 | } |
749c7247 C |
469 | |
470 | getDisplayName () { | |
471 | return this.name | |
472 | } | |
744d0eca C |
473 | |
474 | isOutdated () { | |
475 | return this.Actor.isOutdated() | |
476 | } | |
72c7248b | 477 | } |