Skip to content

ActivityPub-federated video streaming platform using P2P directly in your web browser

Federate video in a job

Chocobozzze55325c62ed1
diff --git a/packages/models/src/server/job.model.ts b/packages/models/src/server/job.model.ts
index 10894e3957..985ab4c3f9 100644
--- a/packages/models/src/server/job.model.ts
+++ b/packages/models/src/server/job.model.ts
@@ -308,6 +308,9 @@ export type NotifyPayload = {
export interface FederateVideoPayload {
videoUUID: string
+
+ // Actor that overrides the video channel account actor to send the update activity
+ overriddenByActorId?: number
}
// ---------------------------------------------------------------------------
diff --git a/server/core/controllers/activitypub/client.ts b/server/core/controllers/activitypub/client.ts
index c6fe595f0f..2830534ead 100644
--- a/server/core/controllers/activitypub/client.ts
+++ b/server/core/controllers/activitypub/client.ts
@@ -141,14 +141,14 @@ activityPubClientRouter.get(
executeIfActivityPub,
activityPubRateLimiter,
cacheRoute(ROUTE_CACHE_LIFETIME.ACTIVITY_PUB.VIDEOS),
- asyncMiddleware(videoGetValidatorFactory('full')),
+ asyncMiddleware(videoGetValidatorFactory('ap')),
asyncMiddleware(videoController)
)
activityPubClientRouter.get(
'/videos/watch/:id/activity',
executeIfActivityPub,
activityPubRateLimiter,
- asyncMiddleware(videoGetValidatorFactory('full')),
+ asyncMiddleware(videoGetValidatorFactory('ap')),
asyncMiddleware(videoController)
)
activityPubClientRouter.get(
@@ -332,18 +332,15 @@ function getAccountVideoRateFactory (rateType: VideoRateType) {
}
async function videoController (req: express.Request, res: express.Response) {
- const video = res.locals.videoFull
+ const videoAP = res.locals.videoAP
- if (redirectIfNotOwned(video.url, res)) return
-
- // We need captions to render AP object
- const videoAP = await video.lightAPToFullAP(undefined)
+ if (redirectIfNotOwned(videoAP.url, res)) return
const audience = getVideoAudience({ account: videoAP.VideoChannel.Account, channel: videoAP.VideoChannel, privacy: videoAP.privacy })
const videoObject = audiencify(await videoAP.toActivityPubObject(), audience)
if (req.path.endsWith('/activity')) {
- const data = buildCreateActivity(videoAP.url, video.VideoChannel.Account.Actor, videoObject, audience)
+ const data = buildCreateActivity(videoAP.url, videoAP.VideoChannel.Account.Actor, videoObject, audience)
return activityPubResponse(activityPubContextify(data, 'Video', getContextFilter()), res)
}
diff --git a/server/core/controllers/api/videos/captions.ts b/server/core/controllers/api/videos/captions.ts
index aa9446a9a4..51ef75c839 100644
--- a/server/core/controllers/api/videos/captions.ts
+++ b/server/core/controllers/api/videos/captions.ts
@@ -10,7 +10,7 @@ import { logger, loggerTagsFactory } from '../../../helpers/logger.js'
import { getFormattedObjects } from '../../../helpers/utils.js'
import { MIMETYPES } from '../../../initializers/constants.js'
import { sequelizeTypescript } from '../../../initializers/database.js'
-import { federateVideoIfNeeded } from '../../../lib/activitypub/videos/index.js'
+import { scheduleVideoFederation } from '../../../lib/activitypub/videos/index.js'
import { asyncMiddleware, asyncRetryTransactionMiddleware, authenticate } from '../../../middlewares/index.js'
import {
addVideoCaptionValidator,
@@ -105,7 +105,7 @@ async function createVideoCaption (req: express.Request, res: express.Response)
transaction: t
})
- return federateVideoIfNeeded(video, t)
+ scheduleVideoFederation({ video, transaction: t })
})
})
@@ -137,7 +137,7 @@ async function deleteVideoCaption (req: express.Request, res: express.Response)
transaction: t
})
- return federateVideoIfNeeded(video, t)
+ scheduleVideoFederation({ video, transaction: t })
})
})
diff --git a/server/core/controllers/api/videos/chapters.ts b/server/core/controllers/api/videos/chapters.ts
index 48759da76b..bb4d6f3305 100644
--- a/server/core/controllers/api/videos/chapters.ts
+++ b/server/core/controllers/api/videos/chapters.ts
@@ -1,7 +1,7 @@
import { HttpStatusCode, VideoChannelActivityAction, VideoChapterUpdate } from '@peertube/peertube-models'
import { retryTransactionWrapper } from '@server/helpers/database-utils.js'
import { sequelizeTypescript } from '@server/initializers/database.js'
-import { federateVideoIfNeeded } from '@server/lib/activitypub/videos/federate.js'
+import { scheduleVideoFederation } from '@server/lib/activitypub/videos/federate.js'
import { replaceChapters } from '@server/lib/video-chapters.js'
import { VideoChannelActivityModel } from '@server/models/video/video-channel-activity.js'
import { VideoChapterModel } from '@server/models/video/video-chapter.js'
@@ -54,7 +54,7 @@ async function replaceVideoChapters (req: express.Request, res: express.Response
transaction: t
})
- await federateVideoIfNeeded(video, t)
+ scheduleVideoFederation({ video, transaction: t })
})
})
diff --git a/server/core/controllers/api/videos/embed-privacy.ts b/server/core/controllers/api/videos/embed-privacy.ts
index bcacc70f5c..c0650b1501 100644
--- a/server/core/controllers/api/videos/embed-privacy.ts
+++ b/server/core/controllers/api/videos/embed-privacy.ts
@@ -10,7 +10,7 @@ import {
import { getAuthUser } from '@server/helpers/express-utils.js'
import { logger, loggerTagsFactory } from '@server/helpers/logger.js'
import { VIDEO_EMBED_PRIVACY_POLICIES, WEBSERVER } from '@server/initializers/constants.js'
-import { federateVideoIfNeeded } from '@server/lib/activitypub/videos/index.js'
+import { scheduleVideoFederation } from '@server/lib/activitypub/videos/index.js'
import { checkCanManageVideo } from '@server/middlewares/validators/shared/videos.js'
import { VideoChannelActivityModel } from '@server/models/video/video-channel-activity.js'
import { VideoEmbedPrivacyDomainModel } from '@server/models/video/video-embed-privacy-domain.js'
@@ -127,7 +127,7 @@ async function updateVideoEmbedPrivacy (req: express.Request, res: express.Respo
transaction: t
})
- await federateVideoIfNeeded(video, t)
+ scheduleVideoFederation({ video, transaction: t })
})
logger.info(`Video embed policy for video with name ${video.name} and uuid ${video.uuid} have been updated`, lTags(video.uuid))
diff --git a/server/core/controllers/api/videos/files.ts b/server/core/controllers/api/videos/files.ts
index d15e47bc04..4c9911bccf 100644
--- a/server/core/controllers/api/videos/files.ts
+++ b/server/core/controllers/api/videos/files.ts
@@ -1,6 +1,6 @@
import { HttpStatusCode, UserRight } from '@peertube/peertube-models'
import { logger, loggerTagsFactory } from '@server/helpers/logger.js'
-import { federateVideoIfNeeded } from '@server/lib/activitypub/videos/index.js'
+import { scheduleVideoFederation } from '@server/lib/activitypub/videos/index.js'
import { updateM3U8AndShaPlaylist } from '@server/lib/hls.js'
import { removeAllWebVideoFiles, removeHLSFile, removeHLSPlaylist, removeWebVideoFile } from '@server/lib/video-file.js'
import { VideoFileModel } from '@server/models/video/video-file.js'
@@ -80,7 +80,7 @@ async function removeHLSPlaylistController (req: express.Request, res: express.R
logger.info('Deleting HLS playlist of %s.', video.url, lTags(video.uuid))
await removeHLSPlaylist(video)
- await federateVideoIfNeeded(video)
+ scheduleVideoFederation({ video })
return res.sendStatus(HttpStatusCode.NO_CONTENT_204)
}
@@ -94,7 +94,7 @@ async function removeHLSFileController (req: express.Request, res: express.Respo
const playlist = await removeHLSFile(video, videoFileId)
if (playlist) await updateM3U8AndShaPlaylist(video, playlist)
- await federateVideoIfNeeded(video)
+ scheduleVideoFederation({ video })
return res.sendStatus(HttpStatusCode.NO_CONTENT_204)
}
@@ -107,7 +107,7 @@ async function removeAllWebVideoFilesController (req: express.Request, res: expr
logger.info('Deleting Web Video files of %s.', video.url, lTags(video.uuid))
await removeAllWebVideoFiles(video)
- await federateVideoIfNeeded(video)
+ scheduleVideoFederation({ video })
return res.sendStatus(HttpStatusCode.NO_CONTENT_204)
}
@@ -119,7 +119,7 @@ async function removeWebVideoFileController (req: express.Request, res: express.
logger.info('Deleting Web Video file %d of %s.', videoFileId, video.url, lTags(video.uuid))
await removeWebVideoFile(video, videoFileId)
- await federateVideoIfNeeded(video)
+ scheduleVideoFederation({ video })
return res.sendStatus(HttpStatusCode.NO_CONTENT_204)
}
diff --git a/server/core/controllers/api/videos/live.ts b/server/core/controllers/api/videos/live.ts
index fea8de4478..557785dc69 100644
--- a/server/core/controllers/api/videos/live.ts
+++ b/server/core/controllers/api/videos/live.ts
@@ -15,7 +15,7 @@ import { getFormattedObjects } from '@server/helpers/utils.js'
import { getVideoThumbnailFile } from '@server/helpers/video.js'
import { ASSETS_PATH, MIMETYPES } from '@server/initializers/constants.js'
import { sequelizeTypescript } from '@server/initializers/database.js'
-import { federateVideoIfNeeded } from '@server/lib/activitypub/videos/index.js'
+import { scheduleVideoFederation } from '@server/lib/activitypub/videos/index.js'
import { LocalVideoCreator } from '@server/lib/local-video-creator.js'
import { Hooks } from '@server/lib/plugins/hooks.js'
import { checkCanManageVideo } from '@server/middlewares/validators/shared/videos.js'
@@ -169,7 +169,7 @@ async function updateLiveVideo (req: express.Request, res: express.Response) {
})
})
- await federateVideoIfNeeded(video)
+ scheduleVideoFederation({ video })
return res.status(HttpStatusCode.NO_CONTENT_204).end()
}
diff --git a/server/core/controllers/api/videos/ownership.ts b/server/core/controllers/api/videos/ownership.ts
index 55c2da3e18..336e497e43 100644
--- a/server/core/controllers/api/videos/ownership.ts
+++ b/server/core/controllers/api/videos/ownership.ts
@@ -1,12 +1,11 @@
import { ChangeOwnershipState, ChangeOwnershipStateType, HttpStatusCode, VideoChannelActivityAction } from '@peertube/peertube-models'
-import { canVideoBeFederated } from '@server/lib/activitypub/videos/federate.js'
+import { canVideoBeFederated, scheduleVideoFederation } from '@server/lib/activitypub/videos/federate.js'
import { VideoChannelActivityModel } from '@server/models/video/video-channel-activity.js'
-import { MVideoFull } from '@server/types/models/index.js'
+import { MVideoWithRights } from '@server/types/models/index.js'
import express from 'express'
import { logger } from '../../../helpers/logger.js'
import { getFormattedObjects } from '../../../helpers/utils.js'
import { sequelizeTypescript } from '../../../initializers/database.js'
-import { sendUpdateVideo } from '../../../lib/activitypub/send/index.js'
import { changeVideoChannelShare } from '../../../lib/activitypub/share.js'
import { Notifier } from '../../../lib/notifier/notifier.js'
import {
@@ -150,19 +149,21 @@ async function acceptOwnershipChange (req: express.Request, res: express.Respons
const changeOwnership = res.locals.changeOwnership
const channel = res.locals.videoChannel
- // We need more attributes for federation
- const targetVideo = await VideoModel.loadFull(changeOwnership.Video.id, t)
+ // We need the blacklist and the channel to know if we can federate the video
+ const targetVideo = await VideoModel.loadWithRights(changeOwnership.Video.id, t)
const oldVideoChannel = await VideoChannelModel.loadAndPopulateAccount(targetVideo.channelId, t)
targetVideo.channelId = channel.id
- const targetVideoUpdated = await targetVideo.save({ transaction: t }) as MVideoFull
+ const targetVideoUpdated = await targetVideo.save({ transaction: t }) as MVideoWithRights
targetVideoUpdated.VideoChannel = channel
if (canVideoBeFederated(targetVideoUpdated)) {
await changeVideoChannelShare(targetVideoUpdated, oldVideoChannel, t)
- await sendUpdateVideo(targetVideoUpdated, t, oldVideoChannel.Account.Actor)
+
+ // Send the update on behalf of the previous owner so its followers are also notified
+ scheduleVideoFederation({ video: targetVideoUpdated, overriddenBy: oldVideoChannel.Account.Actor, transaction: t })
}
changeOwnership.state = ChangeOwnershipState.ACCEPTED
diff --git a/server/core/controllers/api/videos/source.ts b/server/core/controllers/api/videos/source.ts
index 57784385d5..a2105d54fa 100644
--- a/server/core/controllers/api/videos/source.ts
+++ b/server/core/controllers/api/videos/source.ts
@@ -1,6 +1,7 @@
import { buildAspectRatio } from '@peertube/peertube-core-utils'
import { HttpStatusCode, VideoChannelActivityAction, VideoState } from '@peertube/peertube-models'
import { sequelizeTypescript } from '@server/initializers/database.js'
+import { buildNonDuplicatedFederateVideoJob } from '@server/lib/activitypub/videos/federate.js'
import { CreateJobOptions, CreateJobTypeAndPayload, JobQueue } from '@server/lib/job-queue/index.js'
import { Hooks } from '@server/lib/plugins/hooks.js'
import { regenerateLocalVideoThumbnailsFromVideoIfNeeded } from '@server/lib/thumbnail.js'
@@ -196,10 +197,7 @@ async function addVideoJobsAfterUpload (video: MVideoFull, videoFile: MVideoFile
await buildLocalStoryboardJobIfNeeded({ video, federate: false }),
- {
- type: 'federate-video' as const,
- payload: { videoUUID: video.uuid }
- }
+ buildNonDuplicatedFederateVideoJob({ video })
]
if (video.state === VideoState.TO_MOVE_TO_EXTERNAL_STORAGE) {
diff --git a/server/core/helpers/video.ts b/server/core/helpers/video.ts
index 9dbea3c3da..e16e6e53a6 100644
--- a/server/core/helpers/video.ts
+++ b/server/core/helpers/video.ts
@@ -11,7 +11,11 @@ export function getVideoThumbnailFile (files: express.UploadFiles): Express.Mult
}
export function getVideoWithAttributes (res: express.Response) {
- return res.locals.videoAPI || res.locals.videoFull || res.locals.videoWithBlacklist || res.locals.videoWithRights
+ return res.locals.videoAPI ||
+ res.locals.videoAP ||
+ res.locals.videoFull ||
+ res.locals.videoWithBlacklist ||
+ res.locals.videoWithRights
}
export function extractVideo (videoOrPlaylist: MVideo | MStreamingPlaylistVideo) {
diff --git a/server/core/lib/activitypub/process/process-dislike.ts b/server/core/lib/activitypub/process/process-dislike.ts
index 8666e73d27..e356c7431e 100644
--- a/server/core/lib/activitypub/process/process-dislike.ts
+++ b/server/core/lib/activitypub/process/process-dislike.ts
@@ -6,7 +6,7 @@ import { sequelizeTypescript } from '../../../initializers/database.js'
import { AccountVideoRateModel } from '../../../models/account/account-video-rate.js'
import { APProcessorOptions } from '../../../types/activitypub-processor.model.js'
import { MActorSignature } from '../../../types/models/index.js'
-import { canVideoBeFederated, federateVideoIfNeeded, maybeGetOrCreateAPVideo } from '../videos/index.js'
+import { canVideoBeFederated, maybeGetOrCreateAPVideo, scheduleVideoFederation } from '../videos/index.js'
async function processDislikeActivity (options: APProcessorOptions<ActivityDislike>) {
const { activity, byActor } = options
@@ -37,7 +37,7 @@ async function processDislike (activity: ActivityDislike, byActor: MActorSignatu
}
return sequelizeTypescript.transaction(async t => {
- const video = await VideoModel.loadFull(onlyVideo.id, t)
+ const video = await VideoModel.load(onlyVideo.id, t)
const existingRate = await AccountVideoRateModel.loadByAccountAndVideoOrUrl(byAccount.id, video.id, activity.id, t)
if (existingRate?.type === 'dislike') return
@@ -58,6 +58,6 @@ async function processDislike (activity: ActivityDislike, byActor: MActorSignatu
await rate.save({ transaction: t })
- await federateVideoIfNeeded(video, t)
+ scheduleVideoFederation({ video, transaction: t })
})
}
diff --git a/server/core/lib/activitypub/process/process-like.ts b/server/core/lib/activitypub/process/process-like.ts
index 996909f284..400119bcb0 100644
--- a/server/core/lib/activitypub/process/process-like.ts
+++ b/server/core/lib/activitypub/process/process-like.ts
@@ -7,7 +7,7 @@ import { getAPId } from '../../../lib/activitypub/activity.js'
import { AccountVideoRateModel } from '../../../models/account/account-video-rate.js'
import { APProcessorOptions } from '../../../types/activitypub-processor.model.js'
import { MActorSignature } from '../../../types/models/index.js'
-import { canVideoBeFederated, federateVideoIfNeeded, maybeGetOrCreateAPVideo } from '../videos/index.js'
+import { canVideoBeFederated, maybeGetOrCreateAPVideo, scheduleVideoFederation } from '../videos/index.js'
async function processLikeActivity (options: APProcessorOptions<ActivityLike>) {
const { activity, byActor } = options
@@ -38,7 +38,7 @@ async function processLikeVideo (byActor: MActorSignature, activity: ActivityLik
}
return sequelizeTypescript.transaction(async t => {
- const video = await VideoModel.loadFull(onlyVideo.id, t)
+ const video = await VideoModel.load(onlyVideo.id, t)
const existingRate = await AccountVideoRateModel.loadByAccountAndVideoOrUrl(byAccount.id, video.id, activity.id, t)
if (existingRate?.type === 'like') return
@@ -59,6 +59,6 @@ async function processLikeVideo (byActor: MActorSignature, activity: ActivityLik
await rate.save({ transaction: t })
- await federateVideoIfNeeded(video, t)
+ scheduleVideoFederation({ video, transaction: t })
})
}
diff --git a/server/core/lib/activitypub/process/process-undo.ts b/server/core/lib/activitypub/process/process-undo.ts
index f67dc0e210..836250028e 100644
--- a/server/core/lib/activitypub/process/process-undo.ts
+++ b/server/core/lib/activitypub/process/process-undo.ts
@@ -21,7 +21,7 @@ import { APProcessorOptions } from '../../../types/activitypub-processor.model.j
import { MActorSignature } from '../../../types/models/index.js'
import { fetchAPObjectIfNeeded } from '../activity.js'
import { forwardVideoRelatedActivity } from '../send/shared/send-utils.js'
-import { federateVideoIfNeeded, getOrCreateAPVideo, maybeGetOrCreateAPVideo } from '../videos/index.js'
+import { getOrCreateAPVideo, maybeGetOrCreateAPVideo, scheduleVideoFederation } from '../videos/index.js'
async function processUndoActivity (options: APProcessorOptions<ActivityUndo<ActivityUndoObject>>) {
const { activity, byActor } = options
@@ -75,7 +75,7 @@ async function processUndoLike (byActor: MActorSignature, activity: ActivityUndo
return sequelizeTypescript.transaction(async t => {
if (!byActor.Account) throw new Error('Unknown account ' + byActor.url)
- const video = await VideoModel.loadFull(onlyVideo.id, t)
+ const video = await VideoModel.load(onlyVideo.id, t)
const rate = await AccountVideoRateModel.loadByAccountAndVideoOrUrl(byActor.Account.id, video.id, likeActivity.id, t)
if (rate?.type !== 'like') {
logger.warn('Unknown like by account %d for video %d.', byActor.Account.id, video.id)
@@ -86,7 +86,7 @@ async function processUndoLike (byActor: MActorSignature, activity: ActivityUndo
await video.decrement('likes', { transaction: t })
video.likes--
- await federateVideoIfNeeded(video, t)
+ scheduleVideoFederation({ video, transaction: t })
})
}
@@ -99,7 +99,7 @@ async function processUndoDislike (byActor: MActorSignature, activity: ActivityU
return sequelizeTypescript.transaction(async t => {
if (!byActor.Account) throw new Error('Unknown account ' + byActor.url)
- const video = await VideoModel.loadFull(onlyVideo.id, t)
+ const video = await VideoModel.load(onlyVideo.id, t)
const rate = await AccountVideoRateModel.loadByAccountAndVideoOrUrl(byActor.Account.id, video.id, dislikeActivity.id, t)
if (rate?.type !== 'dislike') {
logger.warn(`Unknown dislike by account %d for video %d.`, byActor.Account.id, video.id)
@@ -110,7 +110,7 @@ async function processUndoDislike (byActor: MActorSignature, activity: ActivityU
await video.decrement('dislikes', { transaction: t })
video.dislikes--
- await federateVideoIfNeeded(video, t)
+ scheduleVideoFederation({ video, transaction: t })
})
}
diff --git a/server/core/lib/activitypub/send/send-update.ts b/server/core/lib/activitypub/send/send-update.ts
index 931434a0c7..73a7bae8ed 100644
--- a/server/core/lib/activitypub/send/send-update.ts
+++ b/server/core/lib/activitypub/send/send-update.ts
@@ -12,7 +12,7 @@ import {
MActor,
MActorLight,
MChannelDefault,
- MVideoAPLight,
+ MVideoAP,
MVideoFull,
MVideoPlaylistFull,
MVideoRedundancyVideo
@@ -22,10 +22,8 @@ import { getLocalChannelPlayerSettingsActivityPubUrl, getLocalVideoPlayerSetting
import { canVideoBeFederated } from '../videos/federate.js'
import { broadcastToFollowers, getActorsInvolvedInVideo, sendVideoRelatedActivity } from './shared/send-utils.js'
-export async function sendUpdateVideo (videoArg: MVideoAPLight, transaction: Transaction, overriddenByActor?: MActor) {
- if (!canVideoBeFederated(videoArg)) return undefined
-
- const video = await videoArg.lightAPToFullAP(transaction)
+export async function sendUpdateVideo (video: MVideoAP, transaction: Transaction, overriddenByActor?: MActor) {
+ if (!canVideoBeFederated(video)) return undefined
logger.info('Creating job to update video %s.', video.url)
diff --git a/server/core/lib/activitypub/video-rates.ts b/server/core/lib/activitypub/video-rates.ts
index 44ab5e3e15..a2105feebe 100644
--- a/server/core/lib/activitypub/video-rates.ts
+++ b/server/core/lib/activitypub/video-rates.ts
@@ -1,19 +1,19 @@
import { VideoRateType } from '@peertube/peertube-models'
import { Transaction } from 'sequelize'
-import { MAccountActor, MActorUrl, MVideoAccountLight, MVideoFull, MVideoId } from '../../types/models/index.js'
+import { MAccountActor, MActorUrl, MVideoAccountLight, MVideoId } from '../../types/models/index.js'
import { sendLike, sendUndoDislike, sendUndoLike } from './send/index.js'
import { sendDislike } from './send/send-dislike.js'
import { getVideoDislikeActivityPubUrlByLocalActor, getVideoLikeActivityPubUrlByLocalActor } from './url.js'
-import { federateVideoIfNeeded } from './videos/index.js'
+import { scheduleVideoFederation } from './videos/index.js'
async function sendVideoRateChange (
account: MAccountActor,
- video: MVideoFull,
+ video: MVideoAccountLight,
likes: number,
dislikes: number,
t: Transaction
) {
- if (video.isLocal()) return federateVideoIfNeeded(video, t)
+ if (video.isLocal()) return scheduleVideoFederation({ video, transaction: t })
return sendVideoRateChangeToOrigin(account, video, likes, dislikes, t)
}
diff --git a/server/core/lib/activitypub/videos/federate.ts b/server/core/lib/activitypub/videos/federate.ts
index 5b4e09f949..c92d49d86f 100644
--- a/server/core/lib/activitypub/videos/federate.ts
+++ b/server/core/lib/activitypub/videos/federate.ts
@@ -1,17 +1,71 @@
import { forceNumber } from '@peertube/peertube-core-utils'
import { VideoPrivacy, VideoPrivacyType, VideoState, VideoStateType } from '@peertube/peertube-models'
+import { afterCommitIfTransaction } from '@server/helpers/database-utils.js'
import { CONFIG } from '@server/initializers/config.js'
-import { MVideoAPLight, MVideoWithBlacklistRights } from '@server/types/models/index.js'
+import { CreateJobOptions, CreateJobTypeAndPayload, JobQueue } from '@server/lib/job-queue/job-queue.js'
+import { MActor, MActorId, MVideo, MVideoAP, MVideoUUID, MVideoWithBlacklistRights } from '@server/types/models/index.js'
import { Transaction } from 'sequelize'
import { sendCreateVideo, sendUpdateVideo } from '../send/index.js'
import { isSharedByServer, shareByServerIfNeeded, shareByVideoChannelIfNeeded } from '../share.js'
-export async function federateVideoIfNeeded (videoArg: MVideoAPLight, transaction?: Transaction) {
- if (!canVideoBeFederated(videoArg)) return
+// Enough to know if it's worth creating a federation job for this video
+export type MVideoToFederate = MVideoUUID & Pick<MVideo, 'privacy' | 'state'>
- const alreadyShared = await isSharedByServer({ video: videoArg, transaction })
+export type FederateVideoJobOptions = {
+ video: MVideoToFederate
- const video = await videoArg.lightAPToFullAP(transaction)
+ // Actor that overrides the video channel account actor to send the update activity
+ overriddenBy?: MActorId
+}
+
+// bullmq drops a deduplicated job before storing it, so a flow job would vanish while its children still reference it as their parent
+export function buildNonDuplicatedFederateVideoJob (options: FederateVideoJobOptions): CreateJobTypeAndPayload & CreateJobOptions {
+ const { video, overriddenBy } = options
+
+ return {
+ type: 'federate-video',
+ payload: {
+ videoUUID: video.uuid,
+ overriddenByActorId: overriddenBy?.id
+ }
+ }
+}
+
+// Deduplicate the job by video, so jobs scheduled here don't federate the same video in parallel
+// `keepLastIfActive` also guarantees a change that occurred while the video was being federated is federated afterwards
+export function scheduleVideoFederation (options: FederateVideoJobOptions & { transaction?: Transaction }) {
+ const { video, overriddenBy, transaction } = options
+
+ // Check federation possibility early
+ // Delay blacklist check in job execution
+ if (!isPrivacyForFederation(video.privacy) || !isStateForFederation(video.state)) return
+
+ // Keep a dedicated key per overriding actor: a job deduplicated against a *waiting* one is dropped, and we would
+ // then lose the update sent on behalf of that actor (its followers would never be notified)
+ const deduplicationId = overriddenBy
+ ? `federate-video-${video.uuid}-by-${overriddenBy.id}`
+ : `federate-video-${video.uuid}`
+
+ const job = {
+ ...buildNonDuplicatedFederateVideoJob(options),
+
+ deduplicationId,
+ deduplicationKeepLastIfActive: true
+ }
+
+ afterCommitIfTransaction(transaction, () => JobQueue.Instance.createJobAsync(job))
+}
+
+export async function federateVideoIfNeeded (options: {
+ video: MVideoAP
+ overriddenByActor?: MActor
+ transaction?: Transaction
+}) {
+ const { video, overriddenByActor, transaction } = options
+
+ if (!canVideoBeFederated(video)) return
+
+ const alreadyShared = await isSharedByServer({ video, transaction })
if (!alreadyShared) {
await sendCreateVideo(video, transaction)
@@ -27,7 +81,7 @@ export async function federateVideoIfNeeded (videoArg: MVideoAPLight, transactio
shareByVideoChannelIfNeeded({ video, skipFederation: true, transaction })
])
- await sendUpdateVideo(video, transaction)
+ await sendUpdateVideo(video, transaction, overriddenByActor)
}
}
diff --git a/server/core/lib/activitypub/videos/refresh.ts b/server/core/lib/activitypub/videos/refresh.ts
index 7c5fa92446..67d485b5c1 100644
--- a/server/core/lib/activitypub/videos/refresh.ts
+++ b/server/core/lib/activitypub/videos/refresh.ts
@@ -4,7 +4,7 @@ import { PeerTubeRequestError } from '@server/helpers/requests.js'
import { JobQueue } from '@server/lib/job-queue/job-queue.js'
import { VideoLoadByUrlType } from '@server/lib/model-loaders/index.js'
import { VideoModel } from '@server/models/video/video.js'
-import { MVideo, MVideoFull, MVideoThumbnails } from '@server/types/models/index.js'
+import { MVideo, MVideoAccountLightBlacklistAllFiles, MVideoThumbnails } from '@server/types/models/index.js'
import { ActorFollowHealthCache } from '../../actor-follow-health-cache.js'
import { fetchRemoteVideo, SyncParam, syncVideoExternalAttributes } from './shared/index.js'
import { APVideoUpdater } from './updater.js'
@@ -28,7 +28,7 @@ export async function refreshVideoIfNeeded (options: {
// We need more attributes if the argument video was fetched with not enough joints
const video = options.fetchedType === 'full'
- ? options.video as MVideoFull
+ ? options.video as MVideoAccountLightBlacklistAllFiles
: await VideoModel.loadByUrlAndPopulateAccountAndFiles(options.video.url)
const lTags = loggerTagsFactory('ap', 'video', 'refresh', video.uuid, video.url)
diff --git a/server/core/lib/job-queue/handlers/federate-video.ts b/server/core/lib/job-queue/handlers/federate-video.ts
index 612f2eaed8..298f1de5b7 100644
--- a/server/core/lib/job-queue/handlers/federate-video.ts
+++ b/server/core/lib/job-queue/handlers/federate-video.ts
@@ -2,6 +2,7 @@ import { FederateVideoPayload } from '@peertube/peertube-models'
import { retryTransactionWrapper } from '@server/helpers/database-utils.js'
import { sequelizeTypescript } from '@server/initializers/database.js'
import { federateVideoIfNeeded } from '@server/lib/activitypub/videos/index.js'
+import { ActorModel } from '@server/models/actor/actor.js'
import { VideoModel } from '@server/models/video/video.js'
import { Job } from 'bullmq'
import { logger } from '../../../helpers/logger.js'
@@ -13,10 +14,14 @@ export function processFederateVideo (job: Job) {
return retryTransactionWrapper(() => {
return sequelizeTypescript.transaction(async t => {
- const video = await VideoModel.loadFull(payload.videoUUID, t)
+ const video = await VideoModel.loadAP(payload.videoUUID, t)
if (!video) return
- return federateVideoIfNeeded(video, t)
+ const overriddenByActor = payload.overriddenByActorId
+ ? await ActorModel.loadFull(payload.overriddenByActorId, t)
+ : undefined
+
+ return federateVideoIfNeeded({ video, overriddenByActor, transaction: t })
})
})
}
diff --git a/server/core/lib/job-queue/handlers/manage-video-torrent.ts b/server/core/lib/job-queue/handlers/manage-video-torrent.ts
index 465e4f8411..a54ff967db 100644
--- a/server/core/lib/job-queue/handlers/manage-video-torrent.ts
+++ b/server/core/lib/job-queue/handlers/manage-video-torrent.ts
@@ -1,5 +1,7 @@
import { ManageVideoTorrentPayload } from '@peertube/peertube-models'
+import { retryTransactionWrapper } from '@server/helpers/database-utils.js'
import { extractVideo } from '@server/helpers/video.js'
+import { sequelizeTypescript } from '@server/initializers/database.js'
import { VideoPathManager } from '@server/lib/video-path-manager.js'
import { createTorrentForFile, updateTorrentForFileAndSave } from '@server/lib/webtorrent.js'
import { VideoFileModel } from '@server/models/video/video-file.js'
@@ -41,15 +43,24 @@ async function doCreateAction (payload: ManageVideoTorrentPayload & { action: 'c
const { infoHash, torrentFilename } = await createTorrentForFile(video, file)
- // Refresh videoFile because the createTorrentAndSetInfoHash could be long
- const refreshedFile = await VideoFileModel.loadWithVideo(file.id)
- // File does not exist anymore, remove the generated torrent
- if (!refreshedFile) return file.removeTorrent()
+ const saved = await retryTransactionWrapper(() => {
+ return sequelizeTypescript.transaction(async transaction => {
+ // Refresh videoFile because the createTorrentAndSetInfoHash could be long
+ // Also reload on every attempt: after a rollback the previous instance has no changed attribute left to save
+ const refreshedFile = await VideoFileModel.loadWithVideo(file.id, transaction)
+ if (!refreshedFile) return false
+
+ refreshedFile.torrentFilename = torrentFilename
+ await refreshedFile.save({ transaction })
- refreshedFile.torrentFilename = torrentFilename
- await refreshedFile.save()
+ await VideoInfohashModel.replaceFileInfohash(refreshedFile.id, infoHash, transaction)
- await VideoInfohashModel.replaceFileInfohash(refreshedFile.id, infoHash)
+ return true
+ })
+ })
+
+ // File does not exist anymore, remove the generated torrent
+ if (!saved) await file.removeTorrent()
} finally {
fileMutexReleaser()
}
diff --git a/server/core/lib/job-queue/handlers/video-file-import.ts b/server/core/lib/job-queue/handlers/video-file-import.ts
index b2506170ae..d51110f8e8 100644
--- a/server/core/lib/job-queue/handlers/video-file-import.ts
+++ b/server/core/lib/job-queue/handlers/video-file-import.ts
@@ -1,7 +1,7 @@
import { getVideoStreamDimensionsInfo } from '@peertube/peertube-ffmpeg'
import { VideoFileImportPayload } from '@peertube/peertube-models'
import { CONFIG } from '@server/initializers/config.js'
-import { federateVideoIfNeeded } from '@server/lib/activitypub/videos/index.js'
+import { scheduleVideoFederation } from '@server/lib/activitypub/videos/index.js'
import { buildNewFile } from '@server/lib/video-file.js'
import { buildMoveVideoJob } from '@server/lib/video-jobs.js'
import { VideoPathManager } from '@server/lib/video-path-manager.js'
@@ -38,7 +38,7 @@ async function processVideoFileImport (job: Job) {
})
)
} else {
- await federateVideoIfNeeded(video)
+ scheduleVideoFederation({ video })
}
return video
@@ -73,10 +73,9 @@ async function updateVideoFile (video: MVideoFull, inputFilePath: string) {
const { infoHash, torrentFilename } = await createTorrentForFile(video, newVideoFile)
newVideoFile.torrentFilename = torrentFilename
+ await newVideoFile.save()
const infohashModel = await VideoInfohashModel.replaceFileInfohash(newVideoFile.id, infoHash)
- await newVideoFile.save()
-
video.VideoFiles.push(Object.assign(newVideoFile, { InfoHash: infohashModel }))
}
diff --git a/server/core/lib/job-queue/handlers/video-import.ts b/server/core/lib/job-queue/handlers/video-import.ts
index b416f974a5..86cc4666e1 100644
--- a/server/core/lib/job-queue/handlers/video-import.ts
+++ b/server/core/lib/job-queue/handlers/video-import.ts
@@ -40,7 +40,7 @@ import { sequelizeTypescript } from '../../../initializers/database.js'
import { VideoFileModel } from '../../../models/video/video-file.js'
import { VideoImportModel } from '../../../models/video/video-import.js'
import { VideoModel } from '../../../models/video/video.js'
-import { federateVideoIfNeeded } from '../../activitypub/videos/index.js'
+import { scheduleVideoFederation } from '../../activitypub/videos/index.js'
import { Notifier } from '../../notifier/index.js'
import { createLocalVideoThumbnailsFromVideo } from '../../thumbnail.js'
import { JobQueue } from '../job-queue.js'
@@ -245,9 +245,7 @@ async function processFile (options: {
await replaceChaptersIfNotExist({ video, chapters: containerChapters, transaction: t })
- // Now we can federate the video (reload from database, we need more attributes)
- const videoForFederation = await VideoModel.loadFull(video.uuid, t)
- await federateVideoIfNeeded(videoForFederation, t)
+ scheduleVideoFederation({ video, transaction: t })
// Update video import object
videoImportWithFiles.state = VideoImportState.SUCCESS
@@ -255,7 +253,8 @@ async function processFile (options: {
logger.info('Video %s imported.', video.uuid)
- return { videoImportUpdated, video: videoForFederation }
+ // Reload the video from the database, notifications need more attributes
+ return { videoImportUpdated, video: await VideoModel.loadFull(video.uuid, t) }
})
})
diff --git a/server/core/lib/job-queue/handlers/video-live-ending.ts b/server/core/lib/job-queue/handlers/video-live-ending.ts
index cc9d152af8..6f05a9b769 100644
--- a/server/core/lib/job-queue/handlers/video-live-ending.ts
+++ b/server/core/lib/job-queue/handlers/video-live-ending.ts
@@ -3,7 +3,7 @@ import { VideoFileStream, VideoLiveEndingPayload, VideoState } from '@peertube/p
import { peertubeTruncate } from '@server/helpers/core-utils.js'
import { CONSTRAINTS_FIELDS } from '@server/initializers/constants.js'
import { getLocalVideoActivityPubUrl } from '@server/lib/activitypub/url.js'
-import { federateVideoIfNeeded } from '@server/lib/activitypub/videos/index.js'
+import { scheduleVideoFederation } from '@server/lib/activitypub/videos/index.js'
import { cleanupAndDestroyPermanentLive, cleanupTMPLiveFiles, cleanupUnsavedNormalLive } from '@server/lib/live/index.js'
import {
generateHLSMasterPlaylistFilename,
@@ -395,8 +395,11 @@ async function cleanupLiveAndFederate (options: {
}
try {
- const fullVideo = await VideoModel.loadFull(video.id)
- return federateVideoIfNeeded(fullVideo)
+ // Reload the video: the cleanup may have changed its state
+ const refreshedVideo = await VideoModel.load(video.id)
+ if (!refreshedVideo) return
+
+ scheduleVideoFederation({ video: refreshedVideo })
} catch (err) {
logger.warn('Cannot federate live after cleanup', { videoId: video.id, err })
}
diff --git a/server/core/lib/job-queue/job-queue.ts b/server/core/lib/job-queue/job-queue.ts
index b316d5f9a5..d469f3edb2 100644
--- a/server/core/lib/job-queue/job-queue.ts
+++ b/server/core/lib/job-queue/job-queue.ts
@@ -114,6 +114,11 @@ export type CreateJobOptions = {
priority?: number
failParentOnFailure?: boolean
deduplicationId?: string
+
+ // Instead of dropping the job added while another one with the same deduplication id is active,
+ // store it and automatically create it when the active job has finished
+ // It guarantees we don't miss a change that occurred during the processing of the active job
+ deduplicationKeepLastIfActive?: boolean
}
const handlers: { [id in JobType]: (job: Job, signal?: AbortSignal) => Promise<any> } = {
@@ -383,7 +388,10 @@ class JobQueue {
return
}
- const jobOptions = this.buildJobOptions(options.type as JobType, pick(options, [ 'priority', 'delay', 'deduplicationId' ]))
+ const jobOptions = this.buildJobOptions(
+ options.type as JobType,
+ pick(options, [ 'priority', 'delay', 'deduplicationId', 'deduplicationKeepLastIfActive' ])
+ )
return queue.add('job', options.payload, jobOptions)
}
@@ -424,7 +432,10 @@ class JobQueue {
opts: {
failParentOnFailure: true,
- ...this.buildJobOptions(job.type as JobType, pick(job, [ 'priority', 'delay', 'failParentOnFailure', 'deduplicationId' ]))
+ ...this.buildJobOptions(
+ job.type as JobType,
+ pick(job, [ 'priority', 'delay', 'failParentOnFailure', 'deduplicationId', 'deduplicationKeepLastIfActive' ])
+ )
}
}
}
@@ -438,7 +449,8 @@ class JobQueue {
deduplication: options.deduplicationId
? {
- id: options.deduplicationId
+ id: options.deduplicationId,
+ keepLastIfActive: options.deduplicationKeepLastIfActive
}
: undefined,
diff --git a/server/core/lib/live/live-manager.ts b/server/core/lib/live/live-manager.ts
index dcbcd2296f..94a1a0f79e 100644
--- a/server/core/lib/live/live-manager.ts
+++ b/server/core/lib/live/live-manager.ts
@@ -29,7 +29,7 @@ import context from 'node-media-server/src/node_core_ctx.js'
import nodeMediaServerLogger from 'node-media-server/src/node_core_logger.js'
import NodeRtmpSession from 'node-media-server/src/node_rtmp_session.js'
import { Server as ServerTLS, createServer as createServerTLS } from 'tls'
-import { federateVideoIfNeeded } from '../activitypub/videos/index.js'
+import { scheduleVideoFederation } from '../activitypub/videos/index.js'
import { JobQueue } from '../job-queue/index.js'
import { Notifier } from '../notifier/notifier.js'
import { getLiveReplayBaseDirectory } from '../paths.js'
@@ -562,11 +562,7 @@ class LiveManager {
await wait(getLiveSegmentTime(live.latencyMode) * 1000 * VIDEO_LIVE.EDGE_LIVE_DELAY_SEGMENTS_NOTIFICATION)
- try {
- await federateVideoIfNeeded(video)
- } catch (err) {
- logger.error('Cannot federate live video %s.', video.url, { err, ...localLTags })
- }
+ scheduleVideoFederation({ video })
Notifier.Instance.notifyOnNewVideoOrLiveIfNeeded(video)
PeerTubeSocket.Instance.sendVideoLiveNewState(video)
@@ -641,7 +637,7 @@ class LiveManager {
PeerTubeSocket.Instance.sendVideoLiveNewState(fullVideo)
- await federateVideoIfNeeded(fullVideo)
+ scheduleVideoFederation({ video: fullVideo })
Hooks.runAction('action:live.video.state.updated', { video: fullVideo })
} catch (err) {
diff --git a/server/core/lib/local-video-creator.ts b/server/core/lib/local-video-creator.ts
index bb5611676c..3a4f629956 100644
--- a/server/core/lib/local-video-creator.ts
+++ b/server/core/lib/local-video-creator.ts
@@ -30,7 +30,7 @@ import { FilteredModelAttributes } from '@server/types/sequelize.js'
import { FfprobeData } from 'fluent-ffmpeg'
import { move } from 'fs-extra/esm'
import { getLocalVideoActivityPubUrl } from './activitypub/url.js'
-import { federateVideoIfNeeded } from './activitypub/videos/federate.js'
+import { scheduleVideoFederation } from './activitypub/videos/federate.js'
import { AutomaticTagger } from './automatic-tags/automatic-tagger.js'
import { setAndSaveVideoAutomaticTags } from './automatic-tags/automatic-tags.js'
import { Hooks } from './plugins/hooks.js'
@@ -256,7 +256,7 @@ export class LocalVideoCreator {
}).catch(err => logger.error('Cannot build new video jobs of %s.', this.video.uuid, { err, ...this.lTags(this.video.uuid) }))
})
} else {
- await federateVideoIfNeeded(this.video, transaction)
+ scheduleVideoFederation({ video: this.video, transaction })
}
}).catch(err => {
// Reset elements to reinsert them in the database
diff --git a/server/core/lib/local-video-updater.ts b/server/core/lib/local-video-updater.ts
index 281de57a00..c783bb7c77 100644
--- a/server/core/lib/local-video-updater.ts
+++ b/server/core/lib/local-video-updater.ts
@@ -20,7 +20,7 @@ import { FilteredModelAttributes } from '@server/types/sequelize.js'
import { Transaction } from 'sequelize'
import { sendDeleteVideo } from './activitypub/send/send-delete.js'
import { changeVideoChannelShare } from './activitypub/share.js'
-import { isPrivacyForFederation } from './activitypub/videos/index.js'
+import { buildNonDuplicatedFederateVideoJob, isPrivacyForFederation } from './activitypub/videos/index.js'
import { AutomaticTagger } from './automatic-tags/automatic-tagger.js'
import { setAndSaveVideoAutomaticTags } from './automatic-tags/automatic-tags.js'
import { CreateJobTypeAndPayload, JobQueue } from './job-queue/job-queue.js'
@@ -367,16 +367,11 @@ export class LocalVideoUpdater {
}
private buildFederationJob (options: {
- video: MVideoUUID
+ video: MVideoFull
}): CreateJobTypeAndPayload[] {
const { video } = options
- return [
- {
- type: 'federate-video',
- payload: { videoUUID: video.uuid }
- }
- ]
+ return [ buildNonDuplicatedFederateVideoJob({ video }) ]
}
private buildNotifyJobIfNeeded (options: {
diff --git a/server/core/lib/model-loaders/video.ts b/server/core/lib/model-loaders/video.ts
index 1b0df1e680..aca6d8f22a 100644
--- a/server/core/lib/model-loaders/video.ts
+++ b/server/core/lib/model-loaders/video.ts
@@ -1,6 +1,7 @@
import { CONFIG } from '@server/initializers/config.js'
import { VideoModel } from '@server/models/video/video.js'
import {
+ MVideoAP,
MVideoAccountLightBlacklistAllFiles,
MVideoFormattableDetails,
MVideoFull,
@@ -14,6 +15,7 @@ import { getOrCreateAPVideo } from '../activitypub/videos/get.js'
type VideoLoadType =
| 'for-api'
+ | 'ap'
| 'full'
| 'with-blacklist'
| 'with-thumbnails'
@@ -23,6 +25,7 @@ type VideoLoadType =
| 'unsafe-immutable-only'
function loadVideo (id: number | string, fetchType: 'for-api', userId?: number): Promise<MVideoFormattableDetails>
+function loadVideo (id: number | string, fetchType: 'ap'): Promise<MVideoAP>
function loadVideo (id: number | string, fetchType: 'full', userId?: number): Promise<MVideoFull>
function loadVideo (id: number | string, fetchType: 'with-blacklist', userId?: number): Promise<MVideoWithBlacklist>
function loadVideo (id: number | string, fetchType: 'with-thumbnails', userId?: number): Promise<MVideoThumbnails>
@@ -33,15 +36,17 @@ function loadVideo (
id: number | string,
fetchType: VideoLoadType,
userId?: number
-): Promise<MVideoFull | MVideoWithBlacklist | MVideoId | MVideoImmutable | MVideoThumbnails | MVideoWithRights>
+): Promise<MVideoAP | MVideoFull | MVideoWithBlacklist | MVideoId | MVideoImmutable | MVideoThumbnails | MVideoWithRights>
function loadVideo (
id: number | string,
fetchType: VideoLoadType,
userId?: number
-): Promise<MVideoFull | MVideoWithBlacklist | MVideoId | MVideoImmutable | MVideoThumbnails> {
+): Promise<MVideoAP | MVideoFull | MVideoWithBlacklist | MVideoId | MVideoImmutable | MVideoThumbnails> {
if (fetchType === 'for-api') return VideoModel.loadForGetAPI({ id, userId })
- if (fetchType === 'full') return VideoModel.loadFull(id, undefined, userId)
+ if (fetchType === 'ap') return VideoModel.loadAP(id)
+
+ if (fetchType === 'full') return VideoModel.loadFull(id)
if (fetchType === 'unsafe-immutable-only') return VideoModel.loadImmutableAttributes(id)
diff --git a/server/core/lib/moderation.ts b/server/core/lib/moderation.ts
index 861aebfa04..e533030ed5 100644
--- a/server/core/lib/moderation.ts
+++ b/server/core/lib/moderation.ts
@@ -19,7 +19,7 @@ import {
MUser,
MUserDefault,
MVideoAbuseVideoFull,
- MVideoAccountLightBlacklistAllFiles
+ MVideoAccountLightBlacklist
} from '@server/types/models/index.js'
import { LiveVideoCreate, VideoCommentCreate, VideoCreate, VideoImportCreate } from '@peertube/peertube-models'
import { UserModel } from '../models/user/user.js'
@@ -109,7 +109,7 @@ function isPostImportVideoAccepted (object: {
async function createVideoAbuse (options: {
baseAbuse: FilteredModelAttributes<AbuseModel>
- videoInstance: MVideoAccountLightBlacklistAllFiles
+ videoInstance: MVideoAccountLightBlacklist
startAt: number
endAt: number
transaction: Transaction
diff --git a/server/core/lib/move-storage/move-to-object-storage.ts b/server/core/lib/move-storage/move-to-object-storage.ts
index 62ca4b71bc..3ef61eb3cf 100644
--- a/server/core/lib/move-storage/move-to-object-storage.ts
+++ b/server/core/lib/move-storage/move-to-object-storage.ts
@@ -21,7 +21,7 @@ import { MVideoSource } from '@server/types/models/video/video-source.js'
import { remove } from 'fs-extra/esm'
import { rmdir } from 'fs/promises'
import { join } from 'path'
-import { federateVideoIfNeeded } from '../activitypub/videos/federate.js'
+import { scheduleVideoFederation } from '../activitypub/videos/federate.js'
import { moveCaptionToStorage } from './shared/move-caption.js'
import { moveVideoToStorage, onMoveVideoToStorageFailure } from './shared/move-video.js'
@@ -53,8 +53,8 @@ export async function moveVideoToObjectStorage (options: {
if (options.moveVideoState) {
await moveToNextState({ video: { uuid: videoUUID }, ...moveVideoState })
} else {
- const videoFull = await VideoModel.loadFull(videoUUID)
- if (videoFull) await federateVideoIfNeeded(videoFull)
+ const video = await VideoModel.load(videoUUID)
+ if (video) scheduleVideoFederation({ video })
}
}
diff --git a/server/core/lib/move-storage/shared/move-caption.ts b/server/core/lib/move-storage/shared/move-caption.ts
index a632114e15..d78a288804 100644
--- a/server/core/lib/move-storage/shared/move-caption.ts
+++ b/server/core/lib/move-storage/shared/move-caption.ts
@@ -1,11 +1,8 @@
-import { retryTransactionWrapper } from '@server/helpers/database-utils.js'
import { logger, LoggerTags, loggerTagsFactory } from '@server/helpers/logger.js'
-import { sequelizeTypescript } from '@server/initializers/database.js'
-import { federateVideoIfNeeded } from '@server/lib/activitypub/videos/federate.js'
+import { scheduleVideoFederation } from '@server/lib/activitypub/videos/federate.js'
import { VideoPathManager } from '@server/lib/video-path-manager.js'
import { VideoCaptionModel } from '@server/models/video/video-caption.js'
import { VideoStreamingPlaylistModel } from '@server/models/video/video-streaming-playlist.js'
-import { VideoModel } from '@server/models/video/video.js'
import { MStreamingPlaylistVideoUUID, MVideoCaption } from '@server/types/models/index.js'
export async function moveCaptionToStorage (options: {
@@ -36,13 +33,7 @@ export async function moveCaptionToStorage (options: {
try {
await moveCaptionFiles([ caption ], hls)
- await retryTransactionWrapper(() => {
- return sequelizeTypescript.transaction(async t => {
- const videoFull = await VideoModel.loadFull(caption.Video.id, t)
-
- await federateVideoIfNeeded(videoFull, t)
- })
- })
+ scheduleVideoFederation({ video: caption.Video })
} finally {
fileMutexReleaser()
}
diff --git a/server/core/lib/schedulers/video-stats-buffer-scheduler.ts b/server/core/lib/schedulers/video-stats-buffer-scheduler.ts
index e374feca63..0f27a8cd10 100644
--- a/server/core/lib/schedulers/video-stats-buffer-scheduler.ts
+++ b/server/core/lib/schedulers/video-stats-buffer-scheduler.ts
@@ -1,7 +1,7 @@
import { logger, loggerTagsFactory } from '@server/helpers/logger.js'
import { VideoModel } from '@server/models/video/video.js'
import { SCHEDULER_INTERVALS_MS } from '../../initializers/constants.js'
-import { federateVideoIfNeeded } from '../activitypub/videos/index.js'
+import { scheduleVideoFederation } from '../activitypub/videos/index.js'
import { Redis } from '../redis.js'
import { AbstractScheduler } from './abstract-scheduler.js'
@@ -35,7 +35,7 @@ export class VideoStatsBufferScheduler extends AbstractScheduler {
if (!views && !downloads) continue
- const video = await VideoModel.loadFull(videoId)
+ const video = await VideoModel.load(videoId)
if (!video) {
logger.debug(`Video ${videoId} does not exist anymore, skipping videos stats addition.`, lTags())
continue
@@ -55,7 +55,7 @@ export class VideoStatsBufferScheduler extends AbstractScheduler {
}
// Send video update
- await federateVideoIfNeeded(video)
+ scheduleVideoFederation({ video })
} catch (err) {
logger.error(`Cannot process local video stats buffer of video ${videoId}.`, { err, ...lTags() })
}
diff --git a/server/core/lib/storyboard.ts b/server/core/lib/storyboard.ts
index a40d207ea4..86b746244b 100644
--- a/server/core/lib/storyboard.ts
+++ b/server/core/lib/storyboard.ts
@@ -6,7 +6,7 @@ import { sequelizeTypescript } from '@server/initializers/database.js'
import { StoryboardModel } from '@server/models/video/storyboard.js'
import { VideoModel } from '@server/models/video/video.js'
import { MVideo } from '@server/types/models/index.js'
-import { federateVideoIfNeeded } from './activitypub/videos/federate.js'
+import { scheduleVideoFederation } from './activitypub/videos/federate.js'
import { deleteFileAndCatch } from '@server/helpers/fs.js'
export async function buildSpriteSize (videoPath: string) {
@@ -89,7 +89,7 @@ export async function insertStoryboardInDatabase (options: {
}, { transaction })
if (federate) {
- await federateVideoIfNeeded(video, transaction)
+ scheduleVideoFederation({ video, transaction })
}
})
})
diff --git a/server/core/lib/transcoding/hls-transcoding.ts b/server/core/lib/transcoding/hls-transcoding.ts
index b0738b1977..746712e4d4 100644
--- a/server/core/lib/transcoding/hls-transcoding.ts
+++ b/server/core/lib/transcoding/hls-transcoding.ts
@@ -131,12 +131,14 @@ export async function onHLSVideoFileTranscoding (options: {
await oldFile.destroy()
}
- const savedVideoFile = await sequelizeTypescript.transaction(async t => {
- const savedVideoFile = await VideoFileModel.customUpsert(newVideoFile, 'streaming-playlist', t) as MVideoFile
+ const savedVideoFile = await retryTransactionWrapper(() => {
+ return sequelizeTypescript.transaction(async t => {
+ const savedVideoFile = await VideoFileModel.customUpsert(newVideoFile, 'streaming-playlist', t) as MVideoFile
- await VideoInfohashModel.replaceFileInfohash(savedVideoFile.id, infoHash, t)
+ await VideoInfohashModel.replaceFileInfohash(savedVideoFile.id, infoHash, t)
- return savedVideoFile
+ return savedVideoFile
+ })
})
if (playlistGenerated) {
diff --git a/server/core/lib/transcoding/web-transcoding.ts b/server/core/lib/transcoding/web-transcoding.ts
index 485237ca43..fb53884dbf 100644
--- a/server/core/lib/transcoding/web-transcoding.ts
+++ b/server/core/lib/transcoding/web-transcoding.ts
@@ -6,6 +6,7 @@ import {
getVideoStreamDuration
} from '@peertube/peertube-ffmpeg'
import { VideoFileStream } from '@peertube/peertube-models'
+import { retryTransactionWrapper } from '@server/helpers/database-utils.js'
import { computeOutputFPS } from '@server/helpers/ffmpeg/index.js'
import { deleteFileAndCatch } from '@server/helpers/fs.js'
import { sequelizeTypescript } from '@server/initializers/database.js'
@@ -209,7 +210,7 @@ export async function onWebVideoFileTranscoding (options: {
const mutexReleaser = await VideoPathManager.Instance.lockFiles(video.uuid)
- const videoFile = await buildNewFile({ mode: 'web-video', path: videoOutputPath })
+ let videoFile = await buildNewFile({ mode: 'web-video', path: videoOutputPath })
videoFile.videoId = video.id
try {
@@ -240,9 +241,11 @@ export async function onWebVideoFileTranscoding (options: {
const existingFile = await VideoFileModel.loadWebVideoFile({ videoId: video.id, fps: videoFile.fps, resolution: videoFile.resolution })
if (existingFile) await video.removeWebVideoFile(existingFile)
- await sequelizeTypescript.transaction(async t => {
- await VideoFileModel.customUpsert(videoFile, 'video', t)
- await VideoInfohashModel.replaceFileInfohash(videoFile.id, infoHash, t)
+ await retryTransactionWrapper(() => {
+ return sequelizeTypescript.transaction(async t => {
+ videoFile = await VideoFileModel.customUpsert(videoFile, 'video', t)
+ await VideoInfohashModel.replaceFileInfohash(videoFile.id, infoHash, t)
+ })
})
video.VideoFiles = await video.$get('VideoFiles')
diff --git a/server/core/lib/user-import-export/exporters/videos-exporter.ts b/server/core/lib/user-import-export/exporters/videos-exporter.ts
index 0d0748077c..6b2e09c0fc 100644
--- a/server/core/lib/user-import-export/exporters/videos-exporter.ts
+++ b/server/core/lib/user-import-export/exporters/videos-exporter.ts
@@ -29,7 +29,6 @@ import {
MVideoCaptionLanguageUrl,
MVideoChapter,
MVideoFile,
- MVideoFull,
MVideoLiveWithSettingSchedules,
MVideoPassword
} from '@server/types/models/index.js'
@@ -87,7 +86,7 @@ export class VideosExporter extends AbstractUserExporter<VideoExportJSON> {
private async exportVideo (videoId: number) {
const [ video, captions, source, chapters, playerSettings, embedPrivacyDomains ] = await Promise.all([
- VideoModel.loadFull(videoId),
+ VideoModel.loadAP(videoId),
VideoCaptionModel.listVideoCaptions(videoId),
VideoSourceModel.loadLatest(videoId),
VideoChapterModel.listChaptersOfVideo(videoId),
@@ -101,10 +100,7 @@ export class VideosExporter extends AbstractUserExporter<VideoExportJSON> {
const live = video.isLive
? await VideoLiveModel.loadByVideoIdFull(videoId)
- : undefined // We already have captions, so we can set it to the video object
- ;(video as any).VideoCaptions = captions
- // Then fetch more attributes for AP serialization
- const videoAP = await video.lightAPToFullAP(undefined)
+ : undefined
const { relativePathsFromJSON, staticFiles, exportedVideoFileOrSource } = await this.exportVideoFiles({ video, captions })
@@ -122,14 +118,14 @@ export class VideosExporter extends AbstractUserExporter<VideoExportJSON> {
}),
staticFiles,
relativePathsFromJSON,
- activityPubOutbox: await this.exportVideoAP(videoAP, chapters, exportedVideoFileOrSource)
+ activityPubOutbox: await this.exportVideoAP(video, chapters, exportedVideoFileOrSource)
}
}
// ---------------------------------------------------------------------------
private exportVideoJSON (options: {
- video: MVideoFull
+ video: MVideoAP
captions: MVideoCaption[]
live: MVideoLiveWithSettingSchedules
passwords: MVideoPassword[]
@@ -374,7 +370,7 @@ export class VideosExporter extends AbstractUserExporter<VideoExportJSON> {
// ---------------------------------------------------------------------------
private async exportVideoFiles (options: {
- video: MVideoFull
+ video: MVideoAP
captions: MVideoCaption[]
}) {
const { video, captions } = options
@@ -448,7 +444,7 @@ export class VideosExporter extends AbstractUserExporter<VideoExportJSON> {
private async generateVideoFileReadStream (options: {
videoFile: MVideoFile
separatedAudioFile: MVideoFile
- video: MVideoFull
+ video: MVideoAP
}): Promise<Readable> {
const { video, videoFile, separatedAudioFile } = options
@@ -485,7 +481,7 @@ export class VideosExporter extends AbstractUserExporter<VideoExportJSON> {
// ---------------------------------------------------------------------------
- private async getArchiveVideo (video: MVideoFull) {
+ private async getArchiveVideo (video: MVideoAP) {
const source = await VideoSourceModel.loadLatest(video.id)
const { videoFile, separatedAudioFile } = video.getMaxQualityAudioAndVideoFiles()
diff --git a/server/core/lib/video-blacklist.ts b/server/core/lib/video-blacklist.ts
index efc4ee8104..a61f9e5f8e 100644
--- a/server/core/lib/video-blacklist.ts
+++ b/server/core/lib/video-blacklist.ts
@@ -12,7 +12,6 @@ import { englishLanguage, t } from '@server/helpers/i18n.js'
import { sequelizeTypescript } from '@server/initializers/database.js'
import { getServerAccount } from '@server/models/application/application.js'
import { AccountAutomaticTagPolicyModel } from '@server/models/automatic-tag/account-automatic-tag-policy.js'
-import { VideoModel } from '@server/models/video/video.js'
import {
MUser,
MVideoAccountLight,
@@ -27,7 +26,7 @@ import { logger, loggerTagsFactory } from '../helpers/logger.js'
import { CONFIG } from '../initializers/config.js'
import { VideoBlacklistModel } from '../models/video/video-blacklist.js'
import { sendDeleteVideo } from './activitypub/send/index.js'
-import { federateVideoIfNeeded, isPrivacyForFederation } from './activitypub/videos/index.js'
+import { isPrivacyForFederation, scheduleVideoFederation } from './activitypub/videos/index.js'
import { LiveManager } from './live/live-manager.js'
import { Notifier } from './notifier/index.js'
import { Hooks } from './plugins/hooks.js'
@@ -178,7 +177,7 @@ export async function unblacklistVideo (videoBlacklist: MVideoBlacklist, video:
// Re federate the video
if (unfederated === true) {
- await federateVideoIfNeeded(await VideoModel.loadFull(video.id, t), t)
+ scheduleVideoFederation({ video, transaction: t })
}
return videoBlacklistType
diff --git a/server/core/lib/video-captions.ts b/server/core/lib/video-captions.ts
index a00f8aa466..926c303a53 100644
--- a/server/core/lib/video-captions.ts
+++ b/server/core/lib/video-captions.ts
@@ -17,7 +17,7 @@ import { MutexInterface } from 'async-mutex'
import { ensureDir, remove } from 'fs-extra/esm'
import { writeFile } from 'fs/promises'
import { dirname, join } from 'path'
-import { federateVideoIfNeeded } from './activitypub/videos/federate.js'
+import { scheduleVideoFederation } from './activitypub/videos/federate.js'
import { buildCaptionM3U8Content, updateM3U8AndShaPlaylist } from './hls.js'
import { JobQueue } from './job-queue/job-queue.js'
import { Notifier } from './notifier/notifier.js'
@@ -280,9 +280,7 @@ export async function onTranscriptionEnded (options: {
videoFileMutexReleaser()
}
- await sequelizeTypescript.transaction(async t => {
- await federateVideoIfNeeded(video, t)
- })
+ scheduleVideoFederation({ video })
logger.info(`Transcription ended for ${video.uuid}`, lTags(video.uuid, ...customLTags))
}
diff --git a/server/core/lib/video-channel.ts b/server/core/lib/video-channel.ts
index 05dff8c9f5..c3ff876d83 100644
--- a/server/core/lib/video-channel.ts
+++ b/server/core/lib/video-channel.ts
@@ -4,7 +4,7 @@ import { VideoChannelModel } from '../models/video/video-channel.js'
import { VideoModel } from '../models/video/video.js'
import { MAccountId, MChannelId } from '../types/models/index.js'
import { getLocalVideoChannelActivityPubUrl } from './activitypub/url.js'
-import { federateVideoIfNeeded } from './activitypub/videos/index.js'
+import { scheduleVideoFederation } from './activitypub/videos/index.js'
import { buildActorInstance } from './local-actor.js'
export async function createLocalVideoChannelWithoutKeys (body: VideoChannelCreate, account: MAccountId, t: Sequelize.Transaction) {
@@ -26,11 +26,9 @@ export async function createLocalVideoChannelWithoutKeys (body: VideoChannelCrea
}
export async function federateAllVideosOfChannel (videoChannel: MChannelId) {
- const videoIds = await VideoModel.getAllIdsFromChannel({ videoChannel, count: 1000 })
+ const videos = await VideoModel.listForFederationFromChannel({ videoChannel, count: 1000 })
- for (const videoId of videoIds) {
- const video = await VideoModel.loadFull(videoId)
-
- await federateVideoIfNeeded(video)
+ for (const video of videos) {
+ scheduleVideoFederation({ video })
}
}
diff --git a/server/core/lib/video-jobs.ts b/server/core/lib/video-jobs.ts
index 9bb3670518..497b0b3e0e 100644
--- a/server/core/lib/video-jobs.ts
+++ b/server/core/lib/video-jobs.ts
@@ -3,6 +3,7 @@ import { CONFIG } from '@server/initializers/config.js'
import { VideoJobInfoModel } from '@server/models/video/video-job-info.js'
import { VideoModel } from '@server/models/video/video.js'
import { MVideo, MVideoFile, MVideoUUID } from '@server/types/models/index.js'
+import { buildNonDuplicatedFederateVideoJob } from './activitypub/videos/federate.js'
import { CreateJobOptions, CreateJobTypeAndPayload, JobQueue } from './job-queue/job-queue.js'
import { VideoStoryboardJobHandler } from './runners/index.js'
import { createTranscriptionTaskIfNeeded } from './video-captions.js'
@@ -51,10 +52,7 @@ export async function buildLocalStoryboardJobIfNeeded (options: {
}
if (federate === true) {
- return {
- type: 'federate-video' as 'federate-video',
- payload: { videoUUID: video.uuid }
- }
+ return buildNonDuplicatedFederateVideoJob({ video })
}
return undefined
@@ -114,10 +112,7 @@ export async function addVideoJobsAfterCreation (options: {
}
},
- {
- type: 'federate-video' as 'federate-video',
- payload: { videoUUID: video.uuid }
- }
+ buildNonDuplicatedFederateVideoJob({ video })
]
// No transcoding, move the file directly on object storage
diff --git a/server/core/lib/video-state.ts b/server/core/lib/video-state.ts
index a91dea3e2d..b6d7d2ac7c 100644
--- a/server/core/lib/video-state.ts
+++ b/server/core/lib/video-state.ts
@@ -6,7 +6,7 @@ import { sequelizeTypescript } from '@server/initializers/database.js'
import { VideoModel } from '@server/models/video/video.js'
import { MVideo, MVideoFull, MVideoUUID } from '@server/types/models/index.js'
import { Transaction } from 'sequelize'
-import { federateVideoIfNeeded } from './activitypub/videos/index.js'
+import { scheduleVideoFederation } from './activitypub/videos/index.js'
import { JobQueue } from './job-queue/index.js'
import { Notifier } from './notifier/index.js'
import { buildMoveVideoJob } from './video-jobs.js'
@@ -54,7 +54,7 @@ export function moveToNextState (options: {
// Already in its final state
if (videoDatabase.state === VideoState.PUBLISHED) {
- await federateVideoIfNeeded(videoDatabase, t)
+ scheduleVideoFederation({ video: videoDatabase, transaction: t })
logger.debug(`Video ${videoDatabase.uuid} is already published, no state change.`, lTags(videoDatabase.uuid))
@@ -190,7 +190,7 @@ async function moveToPublishedState (options: {
await video.setNewStateAndPublishedAt({ newState: VideoState.PUBLISHED, transaction })
- await federateVideoIfNeeded(video, transaction)
+ scheduleVideoFederation({ video, transaction })
if (previousState === VideoState.TO_EDIT) {
Notifier.Instance.notifyOfFinishedVideoStudioEdition(video)
diff --git a/server/core/lib/video-studio.ts b/server/core/lib/video-studio.ts
index d6c6a7c88f..80287ed352 100644
--- a/server/core/lib/video-studio.ts
+++ b/server/core/lib/video-studio.ts
@@ -4,6 +4,7 @@ import { VideoStudioEditionPayload, VideoStudioTask, VideoStudioTaskPayload } fr
import { logger, loggerTagsFactory } from '@server/helpers/logger.js'
import { CONFIG } from '@server/initializers/config.js'
import { sequelizeTypescript } from '@server/initializers/database.js'
+import { buildNonDuplicatedFederateVideoJob } from '@server/lib/activitypub/videos/federate.js'
import { createTorrentForFileFromPath } from '@server/lib/webtorrent.js'
import { VideoInfohashModel } from '@server/models/video/video-infohash.js'
import { VideoModel } from '@server/models/video/video.js'
@@ -125,10 +126,7 @@ export async function onVideoStudioEnded (options: {
await JobQueue.Instance.createSequentialJobFlow(
await buildLocalStoryboardJobIfNeeded({ video, federate: false }),
- {
- type: 'federate-video' as 'federate-video',
- payload: { videoUUID: video.uuid }
- },
+ buildNonDuplicatedFederateVideoJob({ video }),
{
type: 'transcoding-job-builder' as 'transcoding-job-builder',
payload: {
diff --git a/server/core/middlewares/validators/shared/videos.ts b/server/core/middlewares/validators/shared/videos.ts
index 5ca439584a..5782362f17 100644
--- a/server/core/middlewares/validators/shared/videos.ts
+++ b/server/core/middlewares/validators/shared/videos.ts
@@ -12,6 +12,7 @@ import {
MUserAccountUrl,
MUserId,
MVideo,
+ MVideoAP,
MVideoAccountLight,
MVideoFormattableDetails,
MVideoFull,
@@ -44,6 +45,10 @@ export async function doesVideoExist (id: number | string, res: Response, fetchT
res.locals.videoAPI = video as MVideoFormattableDetails
break
+ case 'ap':
+ res.locals.videoAP = video as MVideoAP
+ break
+
case 'full':
res.locals.videoFull = video as MVideoFull
break
diff --git a/server/core/middlewares/validators/videos/videos.ts b/server/core/middlewares/validators/videos/videos.ts
index 45e242f8d6..192ed014e2 100644
--- a/server/core/middlewares/validators/videos/videos.ts
+++ b/server/core/middlewares/validators/videos/videos.ts
@@ -308,7 +308,7 @@ export async function checkVideoFollowConstraints (req: express.Request, res: ex
})
}
-type FetchType = Extract<VideoLoadType, 'for-api' | 'full' | 'with-blacklist' | 'unsafe-immutable-only'>
+type FetchType = Extract<VideoLoadType, 'for-api' | 'ap' | 'full' | 'with-blacklist' | 'unsafe-immutable-only'>
export const videoGetValidatorFactory = (fetchType: FetchType) => {
return [
isValidVideoIdParam('id'),
diff --git a/server/core/models/video/formatter/video-activity-pub-format.ts b/server/core/models/video/formatter/video-activity-pub-format.ts
index 7375383381..39706e4aad 100644
--- a/server/core/models/video/formatter/video-activity-pub-format.ts
+++ b/server/core/models/video/formatter/video-activity-pub-format.ts
@@ -26,7 +26,7 @@ import {
getLocalVideoPlayerSettingsActivityPubUrl,
getLocalVideoSharesActivityPubUrl
} from '../../../lib/activitypub/url.js'
-import { MStreamingPlaylistFormattable, MUserId, MVideo, MVideoAP, MVideoFileInfoHash } from '../../../types/models/index.js'
+import { MStreamingPlaylistAP, MUserId, MVideo, MVideoAP, MVideoFileInfoHash } from '../../../types/models/index.js'
import { sortByResolutionDesc } from './shared/index.js'
import { getCategoryLabel, getLanguageLabel, getLicenceLabel } from './video-api-format.js'
@@ -231,7 +231,7 @@ function buildVideoFileUrls (options: {
fps: file.fps
})
- if (file.hasTorrent()) {
+ if (file.canBuildMagnetUri()) {
urls.push({
type: 'Link',
mediaType: 'application/x-bittorrent' as 'application/x-bittorrent',
@@ -269,7 +269,7 @@ function buildStreamingPlaylistUrls (video: MVideoAP): ActivityPlaylistUrlObject
}))
}
-function buildStreamingPlaylistTags (video: MVideoAP, playlist: MStreamingPlaylistFormattable) {
+function buildStreamingPlaylistTags (video: MVideoAP, playlist: MStreamingPlaylistAP) {
return [
...(playlist.InfoHashes ?? []).map(i => ({ type: 'Infohash' as 'Infohash', name: i.toP2PMediaLoaderInfohash() })),
diff --git a/server/core/models/video/formatter/video-api-format.ts b/server/core/models/video/formatter/video-api-format.ts
index dd08126681..d2e3081a81 100644
--- a/server/core/models/video/formatter/video-api-format.ts
+++ b/server/core/models/video/formatter/video-api-format.ts
@@ -31,7 +31,7 @@ import {
MVideoFormattableAdditionalAttributes,
MVideoFormattableDetails
} from '../../../types/models/index.js'
-import { MVideoFileInfoHash } from '../../../types/models/video/video-file.js'
+import { MVideoFile } from '../../../types/models/video/video-file.js'
import { sortByResolutionDesc } from './shared/index.js'
export type VideoFormattingJSONOptions = {
@@ -238,7 +238,7 @@ export function streamingPlaylistsModelToFormattedJSON (
export function videoFilesModelToFormattedJSON (
video: MVideoFormattable,
- videoFiles: MVideoFileInfoHash[],
+ videoFiles: MVideoFile[],
options?: {
includePlaylistUrl?: true
includeMagnet?: boolean
@@ -247,7 +247,7 @@ export function videoFilesModelToFormattedJSON (
export function videoFilesModelToFormattedJSON (
video: MVideoFormattable,
- videoFiles: MVideoFileInfoHash[],
+ videoFiles: MVideoFile[],
options: {
includePlaylistUrl?: boolean // default false
includeMagnet?: boolean // default true
@@ -283,7 +283,7 @@ export function videoFilesModelToFormattedJSON (
width: videoFile.width,
height: videoFile.height,
- magnetUri: includeMagnet && videoFile.hasTorrent()
+ magnetUri: includeMagnet && videoFile.canBuildMagnetUri()
? generateMagnetUri(video, videoFile, trackerUrls)
: undefined,
diff --git a/server/core/models/video/sql/video/shared/abstract-video-query-builder.ts b/server/core/models/video/sql/video/shared/abstract-video-query-builder.ts
index 10dc1fe160..e566d2a1a8 100644
--- a/server/core/models/video/sql/video/shared/abstract-video-query-builder.ts
+++ b/server/core/models/video/sql/video/shared/abstract-video-query-builder.ts
@@ -116,7 +116,7 @@ export class AbstractVideoQueryBuilder extends AbstractRunQuery {
}
}
- protected includeWebVideoFiles () {
+ protected includeWebVideoFiles (includeInfohashes: boolean) {
this.addJoin('LEFT JOIN "videoFile" AS "VideoFiles" ON "VideoFiles"."videoId" = "video"."id"')
this.attributes = {
@@ -125,10 +125,10 @@ export class AbstractVideoQueryBuilder extends AbstractRunQuery {
...this.buildAttributesObject('VideoFiles', this.tables.getFileAttributes())
}
- this.includeFileInfohashJSONJoin('VideoFiles')
+ if (includeInfohashes) this.includeFileInfohashJSONJoin('VideoFiles')
}
- protected includeStreamingPlaylistFiles () {
+ protected includeStreamingPlaylistFiles (includeInfohashes: boolean) {
this.addJoin(
'LEFT JOIN "videoStreamingPlaylist" AS "VideoStreamingPlaylists" ON "VideoStreamingPlaylists"."videoId" = "video"."id"'
)
@@ -145,8 +145,10 @@ export class AbstractVideoQueryBuilder extends AbstractRunQuery {
...this.buildAttributesObject('VideoStreamingPlaylists->VideoFiles', this.tables.getFileAttributes())
}
- this.includeFileInfohashJSONJoin('VideoStreamingPlaylists->VideoFiles')
- this.includePlaylistInfohashesJSON()
+ if (includeInfohashes) {
+ this.includeFileInfohashJSONJoin('VideoStreamingPlaylists->VideoFiles')
+ this.includePlaylistInfohashesJSON()
+ }
}
private includePlaylistInfohashesJSON () {
@@ -374,6 +376,18 @@ export class AbstractVideoQueryBuilder extends AbstractRunQuery {
}
}
+ protected includeStoryboard () {
+ this.addJoin(
+ 'LEFT OUTER JOIN "storyboard" AS "Storyboard" ON "video"."id" = "Storyboard"."videoId"'
+ )
+
+ this.attributes = {
+ ...this.attributes,
+
+ ...this.buildAttributesObject('Storyboard', this.tables.getStoryboardAttributes())
+ }
+ }
+
protected buildActorInclude (prefixKey: string) {
return this.buildAttributesObject(prefixKey, this.tables.getActorAttributes())
}
diff --git a/server/core/models/video/sql/video/shared/video-file-query-builder.ts b/server/core/models/video/sql/video/shared/video-file-query-builder.ts
index 09dbf46587..a010d1c500 100644
--- a/server/core/models/video/sql/video/shared/video-file-query-builder.ts
+++ b/server/core/models/video/sql/video/shared/video-file-query-builder.ts
@@ -7,6 +7,7 @@ export type FileQueryOptions = {
url?: string
includeRedundancy: boolean
+ includeInfohashes: boolean
transaction?: Transaction
@@ -41,7 +42,7 @@ export class VideoFileQueryBuilder extends AbstractVideoQueryBuilder {
'"video"."id"': ''
}
- this.includeWebVideoFiles()
+ this.includeWebVideoFiles(options.includeInfohashes)
this.whereId(options)
@@ -53,7 +54,7 @@ export class VideoFileQueryBuilder extends AbstractVideoQueryBuilder {
'"video"."id"': ''
}
- this.includeStreamingPlaylistFiles()
+ this.includeStreamingPlaylistFiles(options.includeInfohashes)
if (options.includeRedundancy) {
this.includeStreamingPlaylistRedundancies(options.tableAttributes)
diff --git a/server/core/models/video/sql/video/shared/video-model-builder.ts b/server/core/models/video/sql/video/shared/video-model-builder.ts
index 1042f40baa..235ee0fd48 100644
--- a/server/core/models/video/sql/video/shared/video-model-builder.ts
+++ b/server/core/models/video/sql/video/shared/video-model-builder.ts
@@ -14,6 +14,7 @@ import { VideoCaptionModel } from '@server/models/video/video-caption.js'
import { VideoLiveScheduleModel } from '@server/models/video/video-live-schedule.js'
import { VideoSourceModel } from '@server/models/video/video-source.js'
import { ScheduleVideoUpdateModel } from '../../../schedule-video-update.js'
+import { StoryboardModel } from '../../../storyboard.js'
import { TagModel } from '../../../tag.js'
import { ThumbnailModel } from '../../../thumbnail.js'
import { VideoBlacklistModel } from '../../../video-blacklist.js'
@@ -48,6 +49,7 @@ export class VideoModelBuilder {
private liveScheduleDone: Set<any>
private redundancyDone: Set<any>
private scheduleVideoUpdateDone: Set<any>
+ private storyboardDone: Set<any>
private trackersDone: Set<string>
private tagsDone: Set<string>
@@ -116,6 +118,7 @@ export class VideoModelBuilder {
this.addCaption(row, videoModel)
this.setBlacklisted(row, videoModel)
this.setScheduleVideoUpdate(row, videoModel)
+ this.setStoryboard(row, videoModel)
} else {
if (include & VideoInclude.BLACKLISTED) {
this.setBlacklisted(row, videoModel)
@@ -164,6 +167,7 @@ export class VideoModelBuilder {
this.redundancyDone = new Set()
this.scheduleVideoUpdateDone = new Set()
this.liveScheduleDone = new Set()
+ this.storyboardDone = new Set()
this.accountBlocklistDone = new Set()
this.serverBlocklistDone = new Set()
@@ -366,13 +370,19 @@ export class VideoModelBuilder {
}
private addPlaylistInfohashes (row: SQLRow, streamingPlaylist: VideoStreamingPlaylistModel) {
- const hashes = row['VideoStreamingPlaylists.InfohashesJSON'] as any as string[] || []
+ const key = 'VideoStreamingPlaylists.InfohashesJSON'
+ if (!(key in row)) return
+
+ const hashes = row[key] as any as string[] || []
streamingPlaylist.InfoHashes = hashes.map(h => new VideoInfohashModel({ infohash: Buffer.from(h, 'hex') }, this.buildOpts))
}
private addFileInfohash (row: SQLRow, prefixKey: string, videoFile: VideoFileModel) {
- const hashes = row[`${prefixKey}.InfohashJSON`] as any as string[] || []
+ const key = `${prefixKey}.InfohashJSON`
+ if (!(key in row)) return
+
+ const hashes = row[key] as any as string[] || []
videoFile.InfoHash = hashes.length !== 0
? new VideoInfohashModel({ infohash: Buffer.from(hashes[0], 'hex') }, this.buildOpts)
@@ -525,6 +535,16 @@ export class VideoModelBuilder {
this.liveScheduleDone.add(id)
}
+ private setStoryboard (row: SQLRow, videoModel: VideoModel) {
+ const id = row['Storyboard.id']
+ if (!id || this.storyboardDone.has(id)) return
+
+ const attributes = this.grab(row, this.tables.getStoryboardAttributes(), 'Storyboard')
+ videoModel.Storyboard = new StoryboardModel(attributes, this.buildOpts)
+
+ this.storyboardDone.add(id)
+ }
+
private setSource (row: SQLRow, videoModel: VideoModel) {
const id = row['VideoSource.id']
if (!id || this.sourceDone.has(id)) return
diff --git a/server/core/models/video/sql/video/shared/video-table-attributes.ts b/server/core/models/video/sql/video/shared/video-table-attributes.ts
index 6fc2d95087..bd201f9df9 100644
--- a/server/core/models/video/sql/video/shared/video-table-attributes.ts
+++ b/server/core/models/video/sql/video/shared/video-table-attributes.ts
@@ -224,6 +224,23 @@ export class VideoTableAttributes {
return [ 'id', 'language', 'fileUrl', 'storage', 'filename', 'automaticallyGenerated', 'm3u8Filename', 'm3u8Url' ]
}
+ getStoryboardAttributes () {
+ return [
+ 'id',
+ 'filename',
+ 'totalHeight',
+ 'totalWidth',
+ 'spriteHeight',
+ 'spriteWidth',
+ 'spriteDuration',
+ 'fileUrl',
+ 'cached',
+ 'videoId',
+ 'createdAt',
+ 'updatedAt'
+ ]
+ }
+
getActorAttributes () {
let attributeKeys = [
'id',
diff --git a/server/core/models/video/sql/video/video-model-get-query-builder.ts b/server/core/models/video/sql/video/video-model-get-query-builder.ts
index 7d5ec7d5a0..8676aee3bc 100644
--- a/server/core/models/video/sql/video/video-model-get-query-builder.ts
+++ b/server/core/models/video/sql/video/video-model-get-query-builder.ts
@@ -12,6 +12,7 @@ import { TableAttributeOptions } from './shared/table-attributes-options.model.j
export type GetType =
| 'api'
+ | 'ap'
| 'full'
| 'account-blacklist'
| 'account-blacklist-files'
@@ -23,18 +24,23 @@ export type GetType =
| 'video'
| 'seo'
-const videoFilesInclude = new Set<GetType>([ 'api', 'full', 'account-blacklist-files', 'all-files' ])
-const captionsInclude = new Set<GetType>([ 'seo' ])
+const videoFilesInclude = new Set<GetType>([ 'api', 'ap', 'full', 'account-blacklist-files', 'all-files' ])
+// Infohashes are only needed to build magnet URIs (API) and P2P media loader tags (AP)
+const infohashesInclude = new Set<GetType>([ 'api', 'ap' ])
+const captionsInclude = new Set<GetType>([ 'ap', 'seo' ])
+const storyboardInclude = new Set<GetType>([ 'ap' ])
const trackersInclude = new Set<GetType>([ 'api' ])
-const liveInclude = new Set<GetType>([ 'api', 'full' ])
-const scheduleUpdateInclude = new Set<GetType>([ 'api', 'full' ])
-const tagsInclude = new Set<GetType>([ 'api', 'full', 'seo' ])
-const userHistoryInclude = new Set<GetType>([ 'api', 'full' ])
-const accountInclude = new Set<GetType>([ 'api', 'full', 'account', 'account-blacklist', 'account-blacklist-files', 'seo' ])
+const liveInclude = new Set<GetType>([ 'api', 'ap', 'full' ])
+// `ap` also needs it: the video destroy hook saves a formatted version of the video in its abuses
+const scheduleUpdateInclude = new Set<GetType>([ 'api', 'ap', 'full' ])
+const tagsInclude = new Set<GetType>([ 'api', 'ap', 'full', 'seo' ])
+const userHistoryInclude = new Set<GetType>([ 'api' ])
+const accountInclude = new Set<GetType>([ 'api', 'ap', 'full', 'account', 'account-blacklist', 'account-blacklist-files', 'seo' ])
const blacklistedInclude = new Set<GetType>([
'api',
+ 'ap',
'full',
'account-blacklist',
'account-blacklist-files',
@@ -44,6 +50,7 @@ const blacklistedInclude = new Set<GetType>([
const thumbnailsInclude = new Set<GetType>([
'api',
+ 'ap',
'full',
'account-blacklist-files',
'all-files',
@@ -84,7 +91,8 @@ export class VideoModelGetQueryBuilder {
const fileQueryOptions = {
...pick(options, [ 'id', 'url', 'transaction', 'logging', 'tableAttributes' ]),
- includeRedundancy: this.shouldIncludeRedundancies(options)
+ includeRedundancy: this.shouldIncludeRedundancies(options),
+ includeInfohashes: infohashesInclude.has(options.type)
}
const [ videoRows, webVideoFilesRows, streamingPlaylistFilesRows ] = await Promise.all([
@@ -177,6 +185,10 @@ export class VideosModelGetQuerySubBuilder extends AbstractVideoQueryBuilder {
this.includeCaptions()
}
+ if (storyboardInclude.has(options.type)) {
+ this.includeStoryboard()
+ }
+
this.whereId(options)
this.query = this.buildQuery(options)
diff --git a/server/core/models/video/sql/video/videos-model-list-query-builder.ts b/server/core/models/video/sql/video/videos-model-list-query-builder.ts
index 259c6e6c84..b73f2348d3 100644
--- a/server/core/models/video/sql/video/videos-model-list-query-builder.ts
+++ b/server/core/models/video/sql/video/videos-model-list-query-builder.ts
@@ -50,7 +50,9 @@ export class VideosModelListQueryBuilder extends AbstractVideoQueryBuilder {
...pick(options, [ 'transaction', 'logging', 'tableAttributes' ]),
ids: videoIds,
- includeRedundancy: options.includeRedundancy === true
+ includeRedundancy: options.includeRedundancy === true,
+ // The API can format the files of listed videos, which requires infohashes to build magnet URIs
+ includeInfohashes: true
}
const [ rowsWebVideoFiles, rowsStreamingPlaylist ] = await Promise.all([
diff --git a/server/core/models/video/video-file.ts b/server/core/models/video/video-file.ts
index bd5a479d77..a01bdfdd62 100644
--- a/server/core/models/video/video-file.ts
+++ b/server/core/models/video/video-file.ts
@@ -49,7 +49,7 @@ import {
isVideoFileSizeValid
} from '../../helpers/custom-validators/videos.js'
import { DOWNLOAD_PATHS, LAZY_STATIC_PATHS, STATIC_PATHS, WEBSERVER } from '../../initializers/constants.js'
-import { MVideoFile, MVideoFileStreamingPlaylistVideo, MVideoFileVideo } from '../../types/models/video/video-file.js'
+import { MVideoFile, MVideoFileInfoHash, MVideoFileStreamingPlaylistVideo, MVideoFileVideo } from '../../types/models/video/video-file.js'
import { SequelizeModel, doesExist, parseAggregateResult, throwIfNotValid } from '../shared/index.js'
import { VideoInfohashModel } from './video-infohash.js'
import { VideoStreamingPlaylistModel } from './video-streaming-playlist.js'
@@ -406,7 +406,11 @@ export class VideoFileModel extends SequelizeModel<VideoFileModel> {
? await VideoFileModel.loadHLSFile({ ...baseFind, playlistId: videoFile.videoStreamingPlaylistId })
: await VideoFileModel.loadWebVideoFile({ ...baseFind, videoId: videoFile.videoId })
- if (!element) return videoFile.save({ transaction })
+ if (!element) {
+ videoFile.isNewRecord = true
+
+ return videoFile.save({ transaction })
+ }
for (const k of Object.keys(videoFile.toJSON())) {
element.set(k, videoFile[k])
@@ -457,6 +461,11 @@ export class VideoFileModel extends SequelizeModel<VideoFileModel> {
return !!this.torrentFilename
}
+ // A magnet URI also needs the infohash, which is not fetched by every video query
+ canBuildMagnetUri (this: MVideoFile): this is MVideoFileInfoHash {
+ return !!this.torrentFilename && !!(this as MVideoFileInfoHash).InfoHash
+ }
+
getVideoOrStreamingPlaylist (this: MVideoFileVideo | MVideoFileStreamingPlaylistVideo): MVideo | MStreamingPlaylistVideo {
if (this.videoId || (this as MVideoFileVideo).Video) return (this as MVideoFileVideo).Video
diff --git a/server/core/models/video/video-infohash.ts b/server/core/models/video/video-infohash.ts
index d9d12f0557..5c0ae91fbc 100644
--- a/server/core/models/video/video-infohash.ts
+++ b/server/core/models/video/video-infohash.ts
@@ -76,6 +76,8 @@ export class VideoInfohashModel extends SequelizeModel<VideoInfohashModel> {
// p2p-media-loader infohashes are 20 ASCII characters (base64), announced as those 20 bytes
static replacePlaylistInfohashes (playlistId: number, hashes: string[], transaction?: Transaction) {
+ if (!playlistId) throw new Error('playlistId is required')
+
logger.debug('Replacing infohash for streaming playlist %d', playlistId, { hashes })
return this.replaceInfohashes({
@@ -87,6 +89,8 @@ export class VideoInfohashModel extends SequelizeModel<VideoInfohashModel> {
// Classic BitTorrent infohash stored as its 40 char hex on the video file
static async replaceFileInfohash (fileId: number, infohashHex: string | null, transaction?: Transaction) {
+ if (!fileId) throw new Error('fileId is required')
+
logger.debug('Replacing infohash for file %d', fileId, { infohashHex })
const results = await this.replaceInfohashes({
diff --git a/server/core/models/video/video-streaming-playlist.ts b/server/core/models/video/video-streaming-playlist.ts
index 4d6e47a1aa..2c80d13a9d 100644
--- a/server/core/models/video/video-streaming-playlist.ts
+++ b/server/core/models/video/video-streaming-playlist.ts
@@ -286,10 +286,11 @@ export class VideoStreamingPlaylistModel extends SequelizeModel<VideoStreamingPl
const replace = (t: Transaction) => VideoInfohashModel.replacePlaylistInfohashes(this.id, hashes, t)
// Keep the delete + insert atomic even when the caller has no transaction
- if (transaction) await replace(transaction)
- else await sequelizeTypescript.transaction(replace)
+ const infoHashes = transaction
+ ? await replace(transaction)
+ : await sequelizeTypescript.transaction(replace)
- this.InfoHashes = hashes.map(h => new VideoInfohashModel({ infohash: Buffer.from(h, 'binary') }))
+ this.InfoHashes = infoHashes
}
// ---------------------------------------------------------------------------
diff --git a/server/core/models/video/video.ts b/server/core/models/video/video.ts
index 16334db952..3e50db4ff8 100644
--- a/server/core/models/video/video.ts
+++ b/server/core/models/video/video.ts
@@ -25,7 +25,7 @@ import {
import { uuidToShort } from '@peertube/peertube-node-utils'
import { AttributesOnly } from '@peertube/peertube-typescript-utils'
import { getPrivaciesForFederation } from '@server/helpers/video.js'
-import { isPrivacyForFederation } from '@server/lib/activitypub/videos/federate.js'
+import { isPrivacyForFederation, MVideoToFederate } from '@server/lib/activitypub/videos/federate.js'
import { InternalEventEmitter } from '@server/lib/internal-event-emitter.js'
import { LiveManager } from '@server/lib/live/live-manager.js'
import {
@@ -70,7 +70,7 @@ import {
} from 'sequelize-typescript'
import { peertubeTruncate } from '../../helpers/core-utils.js'
import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc.js'
-import { isArray, isBooleanValid, isUUIDValid } from '../../helpers/custom-validators/misc.js'
+import { isBooleanValid, isUUIDValid } from '../../helpers/custom-validators/misc.js'
import {
isNSFWFlagsValid,
isNSFWSummaryValid,
@@ -89,16 +89,13 @@ import type {
MAccountId,
MChannel,
MChannelId,
- MStoryboard,
MStreamingPlaylist,
MStreamingPlaylistFilesVideo,
MUserAccountId,
MVideo,
MVideoAP,
- MVideoAPLight,
MVideoAccountLight,
MVideoAccountLightBlacklistAllFiles,
- MVideoCaptionLanguageUrl,
MVideoDetails,
MVideoFileVideo,
MVideoFormattable,
@@ -852,14 +849,15 @@ export class VideoModel extends SequelizeModel<VideoModel> {
@BeforeDestroy
static async beforeDestroyHook (instance: VideoModel, options: { transaction: Transaction }) {
- const videoFull = await this.loadFull(instance.id, options.transaction)
+ // We need infohashes to save the magnet URIs of the video in its abuses
+ const video = await this.loadAP(instance.id, options.transaction)
- this.stopLiveIfNeeded(videoFull)
- this.invalidateCache(videoFull)
+ this.stopLiveIfNeeded(video)
+ this.invalidateCache(video)
- await this.sendDelete(videoFull, options.transaction)
- await this.saveEssentialDataToAbuses(videoFull, options.transaction)
- await this.removeFiles(videoFull, options.transaction)
+ await this.sendDelete(video, options.transaction)
+ await this.saveEssentialDataToAbuses(video, options.transaction)
+ await this.removeFiles(video, options.transaction)
}
static stopLiveIfNeeded (instance: MVideo) {
@@ -875,7 +873,7 @@ export class VideoModel extends SequelizeModel<VideoModel> {
ModelCache.Instance.invalidateCache('video', instance.id)
}
- static async sendDelete (instance: MVideoFull, transaction: Transaction) {
+ static async sendDelete (instance: MVideoAccountLight, transaction: Transaction) {
if (!instance.isLocal()) return undefined
if (!isPrivacyForFederation(instance.privacy)) return undefined
@@ -922,7 +920,7 @@ export class VideoModel extends SequelizeModel<VideoModel> {
return undefined
}
- static async saveEssentialDataToAbuses (instance: MVideoFull & MVideoFormattableDetails, transaction: Transaction) {
+ static async saveEssentialDataToAbuses (instance: MVideoFormattableDetails, transaction: Transaction) {
const tasks: Promise<any>[] = []
const videoAbuses = await instance.$get('VideoAbuses', { transaction })
@@ -1496,10 +1494,17 @@ export class VideoModel extends SequelizeModel<VideoModel> {
return queryBuilder.queryVideo({ url, transaction, type: 'account-blacklist-files' })
}
- static loadFull (id: number | string, t?: Transaction, userId?: number): Promise<MVideoFull> {
+ static loadFull (id: number | string, t?: Transaction): Promise<MVideoFull> {
const queryBuilder = new VideoModelGetQueryBuilder(VideoModel.sequelize)
- return queryBuilder.queryVideo({ id, transaction: t, type: 'full', userId })
+ return queryBuilder.queryVideo({ id, transaction: t, type: 'full' })
+ }
+
+ // Contains everything needed to build the ActivityPub object of the video
+ static loadAP (id: number | string, t?: Transaction): Promise<MVideoAP> {
+ const queryBuilder = new VideoModelGetQueryBuilder(VideoModel.sequelize)
+
+ return queryBuilder.queryVideo({ id, transaction: t, type: 'ap' })
}
static loadWithRights (id: number | string, t?: Transaction, userId?: number): Promise<MVideoWithRights> {
@@ -1666,21 +1671,20 @@ export class VideoModel extends SequelizeModel<VideoModel> {
return VideoModel.update({ support: ofChannel.support }, options)
}
- static async getAllIdsFromChannel (options: {
+ // Only the attributes needed to know if these videos have to be federated
+ static listForFederationFromChannel (options: {
videoChannel: MChannelId
count: number
- }): Promise<number[]> {
+ }): Promise<MVideoToFederate[]> {
const { videoChannel, count } = options
- const videos = await VideoModel.findAll({
- attributes: [ 'id' ],
+ return VideoModel.findAll({
+ attributes: [ 'uuid', 'privacy', 'state' ],
where: {
channelId: videoChannel.id
},
limit: count
})
-
- return videos.map(v => v.id)
}
static async getAllIdsByAccount (options: {
@@ -2118,32 +2122,6 @@ export class VideoModel extends SequelizeModel<VideoModel> {
)
}
- async lightAPToFullAP (this: MVideoAPLight, transaction: Transaction): Promise<MVideoAP> {
- const videoAP = this as MVideoAP
-
- const getCaptions = () => {
- if (isArray(videoAP.VideoCaptions)) return videoAP.VideoCaptions
-
- return this.$get('VideoCaptions', {
- attributes: [ 'filename', 'language', 'fileUrl', 'storage', 'automaticallyGenerated', 'm3u8Filename', 'm3u8Url' ],
- transaction
- }) as Promise<MVideoCaptionLanguageUrl[]>
- }
-
- const getStoryboard = () => {
- if (videoAP.Storyboard) return videoAP.Storyboard
-
- return this.$get('Storyboard', { transaction }) as Promise<MStoryboard>
- }
-
- const [ captions, storyboard ] = await Promise.all([ getCaptions(), getStoryboard() ])
-
- return Object.assign(this, {
- VideoCaptions: captions,
- Storyboard: storyboard
- })
- }
-
getTruncatedDescription () {
if (!this.description) return null
diff --git a/server/core/types/express.d.ts b/server/core/types/express.d.ts
index f292e7bd19..2a8c69ac1a 100644
--- a/server/core/types/express.d.ts
+++ b/server/core/types/express.d.ts
@@ -59,6 +59,7 @@ import {
MComment,
MCommentOwnerVideoReply,
MUserDefault,
+ MVideoAP,
MVideoBlacklist,
MVideoCaptionVideo,
MVideoFull,
@@ -155,6 +156,7 @@ declare module 'express' {
ffprobe?: FfprobeData
videoAPI?: MVideoFormattableDetails
+ videoAP?: MVideoAP
videoFull?: MVideoFull
videoImmutable?: MVideoImmutable
videoWithBlacklist?: MVideoWithBlacklist
diff --git a/server/core/types/models/abuse/abuse.ts b/server/core/types/models/abuse/abuse.ts
index fd57fd05fc..22393f3954 100644
--- a/server/core/types/models/abuse/abuse.ts
+++ b/server/core/types/models/abuse/abuse.ts
@@ -5,7 +5,7 @@ import { PickWith } from '@peertube/peertube-typescript-utils'
import { AbuseModel } from '../../../models/abuse/abuse.js'
import { MAccountDefault, MAccountFormattable, MAccountLight, MAccountUrl } from '../account/index.js'
import { MComment, MCommentOwner, MCommentUrl, MCommentVideo, MVideoUrl } from '../video/index.js'
-import { MVideo, MVideoAccountLightBlacklistAllFiles } from '../video/video.js'
+import { MVideo, MVideoAccountLightBlacklist } from '../video/video.js'
type Use<K extends keyof AbuseModel, M> = PickWith<AbuseModel, K, M>
type UseVideoAbuse<K extends keyof VideoAbuseModel, M> = PickWith<VideoAbuseModel, K, M>
@@ -35,14 +35,14 @@ export type MVideoAbuseVideoUrl =
export type MVideoAbuseVideoFull =
& MVideoAbuse
- & UseVideoAbuse<'Video', Omit<MVideoAccountLightBlacklistAllFiles, 'VideoFiles' | 'VideoStreamingPlaylists'>>
+ & UseVideoAbuse<'Video', MVideoAccountLightBlacklist>
export type MVideoAbuseFormattable =
& MVideoAbuse
& UseVideoAbuse<
'Video',
Pick<
- MVideoAccountLightBlacklistAllFiles,
+ MVideoAccountLightBlacklist,
| 'id'
| 'uuid'
| 'name'
diff --git a/server/core/types/models/video/video-streaming-playlist.ts b/server/core/types/models/video/video-streaming-playlist.ts
index 2b9a830fc8..dbf711437f 100644
--- a/server/core/types/models/video/video-streaming-playlist.ts
+++ b/server/core/types/models/video/video-streaming-playlist.ts
@@ -43,9 +43,14 @@ export type MStreamingPlaylistRedundancies =
& Use<'RedundancyVideos', MVideoRedundancyFileUrl[]>
export type MStreamingPlaylistFormattable =
+ & MStreamingPlaylist
+ & Use<'VideoFiles', MVideoFile[]>
+ & PickWithOpt<VideoStreamingPlaylistModel, 'RedundancyVideos', MVideoRedundancyFileUrl[]>
+
+// Infohashes are needed to build the P2P media loader tags of the AP object
+export type MStreamingPlaylistAP =
& MStreamingPlaylistInfoHash
& Use<'VideoFiles', MVideoFileInfoHash[]>
- & PickWithOpt<VideoStreamingPlaylistModel, 'RedundancyVideos', MVideoRedundancyFileUrl[]>
export function isStreamingPlaylist (value: MVideo | MStreamingPlaylistVideo): value is MStreamingPlaylistVideo {
return !!(value as MStreamingPlaylist).videoId
diff --git a/server/core/types/models/video/video.ts b/server/core/types/models/video/video.ts
index 45ebf4d106..5b3644fd1d 100644
--- a/server/core/types/models/video/video.ts
+++ b/server/core/types/models/video/video.ts
@@ -22,7 +22,12 @@ import {
import { MVideoFile, MVideoFileInfoHash } from './video-file.js'
import { MVideoLiveWithSchedules } from './video-live.js'
import { MVideoSource } from './video-source.js'
-import { MStreamingPlaylistFiles, MStreamingPlaylistFormattable, MStreamingPlaylistRedundanciesAll } from './video-streaming-playlist.js'
+import {
+ MStreamingPlaylistAP,
+ MStreamingPlaylistFiles,
+ MStreamingPlaylistFormattable,
+ MStreamingPlaylistRedundanciesAll
+} from './video-streaming-playlist.js'
type Use<K extends keyof VideoModel, M> = PickWith<VideoModel, K, M>
@@ -146,14 +151,18 @@ export type MVideoWithAllFiles =
& Use<'Thumbnails', MThumbnail[]>
& Use<'VideoStreamingPlaylists', MStreamingPlaylistFiles[]>
-export type MVideoAccountLightBlacklistAllFiles =
+export type MVideoAccountLightBlacklist =
& MVideo
- & Use<'VideoFiles', MVideoFileInfoHash[]>
& Use<'Thumbnails', MThumbnail[]>
- & Use<'VideoStreamingPlaylists', MStreamingPlaylistFiles[]>
& Use<'VideoChannel', MChannelAccountLight>
& Use<'VideoBlacklist', MVideoBlacklistLight>
+// Does not contain file infohashes: none of its consumers build magnet URIs or AP objects from it
+export type MVideoAccountLightBlacklistAllFiles =
+ & MVideoAccountLightBlacklist
+ & Use<'VideoFiles', MVideoFile[]>
+ & Use<'VideoStreamingPlaylists', MStreamingPlaylistFiles[]>
+
// ############################################################################
// With account
@@ -187,16 +196,16 @@ export type MVideoWithHost =
& MVideo
& Use<'VideoChannel', MChannelHostOnly>
+// Does not contain file/playlist infohashes: use MVideoAP if you need to build magnet URIs or the AP object
export type MVideoFull =
& MVideo
& Use<'Thumbnails', MThumbnail[]>
& Use<'VideoBlacklist', MVideoBlacklistLight>
& Use<'Tags', MTag[]>
& Use<'VideoChannel', MChannelAccountLight>
- & Use<'UserVideoHistories', MUserVideoHistoryTime[]>
- & Use<'VideoFiles', MVideoFileInfoHash[]>
+ & Use<'VideoFiles', MVideoFile[]>
& Use<'ScheduleVideoUpdate', MScheduleVideoUpdate>
- & Use<'VideoStreamingPlaylists', MStreamingPlaylistFormattable[]>
+ & Use<'VideoStreamingPlaylists', MStreamingPlaylistFiles[]>
& Use<'VideoLive', MVideoLiveWithSchedules>
// ############################################################################
@@ -207,16 +216,15 @@ export type MVideoAP =
& MVideo
& Use<'Tags', MTag[]>
& Use<'VideoChannel', MChannelAccountLight>
- & Use<'VideoStreamingPlaylists', MStreamingPlaylistFormattable[]>
+ & Use<'VideoStreamingPlaylists', MStreamingPlaylistAP[]>
& Use<'VideoCaptions', MVideoCaptionLanguageUrl[]>
& Use<'VideoBlacklist', MVideoBlacklistUnfederated>
& Use<'VideoFiles', MVideoFileInfoHash[]>
& Use<'Thumbnails', MThumbnail[]>
+ & Use<'ScheduleVideoUpdate', MScheduleVideoUpdate>
& Use<'VideoLive', MVideoLiveWithSchedules>
& Use<'Storyboard', MStoryboard>
-export type MVideoAPLight = Omit<MVideoAP, 'VideoCaptions' | 'Storyboard'>
-
export type MVideoDetails =
& MVideo
& Use<'VideoBlacklist', MVideoBlacklistLight>
@@ -226,7 +234,7 @@ export type MVideoDetails =
& Use<'Thumbnails', MThumbnail[]>
& Use<'UserVideoHistories', MUserVideoHistoryTime[]>
& Use<'VideoStreamingPlaylists', MStreamingPlaylistFormattable[]>
- & Use<'VideoFiles', MVideoFileInfoHash[]>
+ & Use<'VideoFiles', MVideoFile[]>
& Use<'Trackers', MTrackerUrl[]>
export type MVideoForUser =
@@ -253,7 +261,7 @@ export type MVideoFormattableAdditionalAttributes =
& PickWithOpt<VideoModel, 'ScheduleVideoUpdate', Pick<MScheduleVideoUpdate, 'updateAt' | 'privacy'>>
& PickWithOpt<VideoModel, 'VideoBlacklist', Pick<MVideoBlacklist, 'reason'>>
& PickWithOpt<VideoModel, 'VideoStreamingPlaylists', MStreamingPlaylistFormattable[]>
- & PickWithOpt<VideoModel, 'VideoFiles', MVideoFileInfoHash[]>
+ & PickWithOpt<VideoModel, 'VideoFiles', MVideoFile[]>
& PickWithOpt<VideoModel, 'VideoLive', MVideoLiveWithSchedules>
& PickWithOpt<VideoModel, 'VideoAutomaticTags', MVideoAutomaticTagWithTag[]>
& PickWithOpt<VideoModel, 'Tags', MTag[]>
diff --git a/server/scripts/migrations/peertube-4.0.ts b/server/scripts/migrations/peertube-4.0.ts
index 74f0af4a5c..0b4c04adfa 100644
--- a/server/scripts/migrations/peertube-4.0.ts
+++ b/server/scripts/migrations/peertube-4.0.ts
@@ -102,8 +102,8 @@ async function processVideo (videoId: number) {
// Everything worked, we can save the playlist now
await playlist.save()
- const allVideo = await VideoModel.loadFull(video.id)
- await federateVideoIfNeeded(allVideo)
+ const videoAP = await VideoModel.loadAP(video.id)
+ await federateVideoIfNeeded({ video: videoAP })
console.log(`Successfully moved HLS files of ${video.name}.`)
}
diff --git a/server/scripts/regenerate-thumbnails.ts b/server/scripts/regenerate-thumbnails.ts
index 05eb81c13c..64fc536005 100644
--- a/server/scripts/regenerate-thumbnails.ts
+++ b/server/scripts/regenerate-thumbnails.ts
@@ -7,7 +7,7 @@ import { JobQueue } from '@server/lib/job-queue/job-queue.js'
import { ThumbnailModel } from '@server/models/video/thumbnail.js'
import { VideoPlaylistModel } from '@server/models/video/video-playlist.js'
import { VideoModel } from '@server/models/video/video.js'
-import { MVideoFull, MVideoPlaylistFull } from '@server/types/models/index.js'
+import { MVideoAP, MVideoPlaylistFull } from '@server/types/models/index.js'
import Bluebird from 'bluebird'
import { program } from 'commander'
import { pathExists } from 'fs-extra/esm'
@@ -41,13 +41,13 @@ async function run () {
}
async function processVideo (id: number) {
- const video = await VideoModel.loadFull(id)
+ const video = await VideoModel.loadAP(id)
console.log('Processing video %s.', video.name)
await processThumbnails({ entity: video, generateFilename: () => generateImageFilename() })
- await federateVideoIfNeeded(video)
+ await federateVideoIfNeeded({ video })
}
async function processPlaylist (id: number) {
@@ -64,7 +64,7 @@ async function processPlaylist (id: number) {
}
async function processThumbnails (options: {
- entity: MVideoFull | MVideoPlaylistFull
+ entity: MVideoAP | MVideoPlaylistFull
generateFilename: () => string
}) {
const { entity, generateFilename } = options