diff options
author | Chocobozzz <me@florianbigard.com> | 2021-06-02 11:54:29 +0200 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2021-06-02 16:57:53 +0200 |
commit | c56faf0d9453490737f283b29a203bb1ca632b95 (patch) | |
tree | 6f6f92ae762921112e49cf5a440381053bb5a492 /server/lib/activitypub/videos/shared | |
parent | 08a47c75f992e7138dca5121f227909a8347d365 (diff) | |
download | PeerTube-c56faf0d9453490737f283b29a203bb1ca632b95.tar.gz PeerTube-c56faf0d9453490737f283b29a203bb1ca632b95.tar.zst PeerTube-c56faf0d9453490737f283b29a203bb1ca632b95.zip |
Move AP video channel creation
Diffstat (limited to 'server/lib/activitypub/videos/shared')
-rw-r--r-- | server/lib/activitypub/videos/shared/abstract-builder.ts | 13 | ||||
-rw-r--r-- | server/lib/activitypub/videos/shared/creator.ts | 21 |
2 files changed, 21 insertions, 13 deletions
diff --git a/server/lib/activitypub/videos/shared/abstract-builder.ts b/server/lib/activitypub/videos/shared/abstract-builder.ts index 9d5f37e5f..c7631cd45 100644 --- a/server/lib/activitypub/videos/shared/abstract-builder.ts +++ b/server/lib/activitypub/videos/shared/abstract-builder.ts | |||
@@ -1,4 +1,5 @@ | |||
1 | import { Transaction } from 'sequelize/types' | 1 | import { Transaction } from 'sequelize/types' |
2 | import { checkUrlsSameHost } from '@server/helpers/activitypub' | ||
2 | import { deleteNonExistingModels } from '@server/helpers/database-utils' | 3 | import { deleteNonExistingModels } from '@server/helpers/database-utils' |
3 | import { logger } from '@server/helpers/logger' | 4 | import { logger } from '@server/helpers/logger' |
4 | import { createPlaceholderThumbnail, createVideoMiniatureFromUrl } from '@server/lib/thumbnail' | 5 | import { createPlaceholderThumbnail, createVideoMiniatureFromUrl } from '@server/lib/thumbnail' |
@@ -9,6 +10,7 @@ import { VideoLiveModel } from '@server/models/video/video-live' | |||
9 | import { VideoStreamingPlaylistModel } from '@server/models/video/video-streaming-playlist' | 10 | import { VideoStreamingPlaylistModel } from '@server/models/video/video-streaming-playlist' |
10 | import { MStreamingPlaylistFilesVideo, MThumbnail, MVideoCaption, MVideoFile, MVideoFullLight, MVideoThumbnail } from '@server/types/models' | 11 | import { MStreamingPlaylistFilesVideo, MThumbnail, MVideoCaption, MVideoFile, MVideoFullLight, MVideoThumbnail } from '@server/types/models' |
11 | import { ActivityTagObject, ThumbnailType, VideoObject, VideoStreamingPlaylistType } from '@shared/models' | 12 | import { ActivityTagObject, ThumbnailType, VideoObject, VideoStreamingPlaylistType } from '@shared/models' |
13 | import { getOrCreateActorAndServerAndModel } from '../../actor' | ||
12 | import { | 14 | import { |
13 | getCaptionAttributesFromObject, | 15 | getCaptionAttributesFromObject, |
14 | getFileAttributesFromUrl, | 16 | getFileAttributesFromUrl, |
@@ -23,6 +25,17 @@ import { getTrackerUrls, setVideoTrackers } from './trackers' | |||
23 | export abstract class APVideoAbstractBuilder { | 25 | export abstract class APVideoAbstractBuilder { |
24 | protected abstract videoObject: VideoObject | 26 | protected abstract videoObject: VideoObject |
25 | 27 | ||
28 | protected async getOrCreateVideoChannelFromVideoObject () { | ||
29 | const channel = this.videoObject.attributedTo.find(a => a.type === 'Group') | ||
30 | if (!channel) throw new Error('Cannot find associated video channel to video ' + this.videoObject.url) | ||
31 | |||
32 | if (checkUrlsSameHost(channel.id, this.videoObject.id) !== true) { | ||
33 | throw new Error(`Video channel url ${channel.id} does not have the same host than video object id ${this.videoObject.id}`) | ||
34 | } | ||
35 | |||
36 | return getOrCreateActorAndServerAndModel(channel.id, 'all') | ||
37 | } | ||
38 | |||
26 | protected tryToGenerateThumbnail (video: MVideoThumbnail): Promise<MThumbnail> { | 39 | protected tryToGenerateThumbnail (video: MVideoThumbnail): Promise<MThumbnail> { |
27 | return createVideoMiniatureFromUrl({ | 40 | return createVideoMiniatureFromUrl({ |
28 | downloadUrl: getThumbnailFromIcons(this.videoObject).url, | 41 | downloadUrl: getThumbnailFromIcons(this.videoObject).url, |
diff --git a/server/lib/activitypub/videos/shared/creator.ts b/server/lib/activitypub/videos/shared/creator.ts index 4f2d79374..dd9bfb508 100644 --- a/server/lib/activitypub/videos/shared/creator.ts +++ b/server/lib/activitypub/videos/shared/creator.ts | |||
@@ -3,29 +3,24 @@ import { logger } from '@server/helpers/logger' | |||
3 | import { sequelizeTypescript } from '@server/initializers/database' | 3 | import { sequelizeTypescript } from '@server/initializers/database' |
4 | import { autoBlacklistVideoIfNeeded } from '@server/lib/video-blacklist' | 4 | import { autoBlacklistVideoIfNeeded } from '@server/lib/video-blacklist' |
5 | import { VideoModel } from '@server/models/video/video' | 5 | import { VideoModel } from '@server/models/video/video' |
6 | import { MChannelAccountLight, MThumbnail, MVideoFullLight, MVideoThumbnail } from '@server/types/models' | 6 | import { MThumbnail, MVideoFullLight, MVideoThumbnail } from '@server/types/models' |
7 | import { VideoObject } from '@shared/models' | 7 | import { VideoObject } from '@shared/models' |
8 | import { APVideoAbstractBuilder } from './abstract-builder' | 8 | import { APVideoAbstractBuilder } from './abstract-builder' |
9 | import { getVideoAttributesFromObject } from './object-to-model-attributes' | 9 | import { getVideoAttributesFromObject } from './object-to-model-attributes' |
10 | 10 | ||
11 | export class APVideoCreator extends APVideoAbstractBuilder { | 11 | export class APVideoCreator extends APVideoAbstractBuilder { |
12 | protected readonly videoObject: VideoObject | ||
13 | private readonly channel: MChannelAccountLight | ||
14 | 12 | ||
15 | constructor (options: { | 13 | constructor (protected readonly videoObject: VideoObject) { |
16 | videoObject: VideoObject | ||
17 | channel: MChannelAccountLight | ||
18 | }) { | ||
19 | super() | 14 | super() |
20 | |||
21 | this.videoObject = options.videoObject | ||
22 | this.channel = options.channel | ||
23 | } | 15 | } |
24 | 16 | ||
25 | async create (waitThumbnail = false) { | 17 | async create (waitThumbnail = false) { |
26 | logger.debug('Adding remote video %s.', this.videoObject.id) | 18 | logger.debug('Adding remote video %s.', this.videoObject.id) |
27 | 19 | ||
28 | const videoData = await getVideoAttributesFromObject(this.channel, this.videoObject, this.videoObject.to) | 20 | const channelActor = await this.getOrCreateVideoChannelFromVideoObject() |
21 | const channel = channelActor.VideoChannel | ||
22 | |||
23 | const videoData = await getVideoAttributesFromObject(channel, this.videoObject, this.videoObject.to) | ||
29 | const video = VideoModel.build(videoData) as MVideoThumbnail | 24 | const video = VideoModel.build(videoData) as MVideoThumbnail |
30 | 25 | ||
31 | const promiseThumbnail = this.tryToGenerateThumbnail(video) | 26 | const promiseThumbnail = this.tryToGenerateThumbnail(video) |
@@ -38,7 +33,7 @@ export class APVideoCreator extends APVideoAbstractBuilder { | |||
38 | const { autoBlacklisted, videoCreated } = await sequelizeTypescript.transaction(async t => { | 33 | const { autoBlacklisted, videoCreated } = await sequelizeTypescript.transaction(async t => { |
39 | try { | 34 | try { |
40 | const videoCreated = await video.save({ transaction: t }) as MVideoFullLight | 35 | const videoCreated = await video.save({ transaction: t }) as MVideoFullLight |
41 | videoCreated.VideoChannel = this.channel | 36 | videoCreated.VideoChannel = channel |
42 | 37 | ||
43 | if (thumbnailModel) await videoCreated.addAndSaveThumbnail(thumbnailModel, t) | 38 | if (thumbnailModel) await videoCreated.addAndSaveThumbnail(thumbnailModel, t) |
44 | 39 | ||
@@ -51,7 +46,7 @@ export class APVideoCreator extends APVideoAbstractBuilder { | |||
51 | await this.insertOrReplaceLive(videoCreated, t) | 46 | await this.insertOrReplaceLive(videoCreated, t) |
52 | 47 | ||
53 | // We added a video in this channel, set it as updated | 48 | // We added a video in this channel, set it as updated |
54 | await this.channel.setAsUpdated(t) | 49 | await channel.setAsUpdated(t) |
55 | 50 | ||
56 | const autoBlacklisted = await autoBlacklistVideoIfNeeded({ | 51 | const autoBlacklisted = await autoBlacklistVideoIfNeeded({ |
57 | video: videoCreated, | 52 | video: videoCreated, |