ActivityPub-federated video streaming platform using P2P directly in your web browser
Refactor video updater
Chocobozzzf9b86777ab5f
diff --git a/server/core/controllers/api/videos/update.ts b/server/core/controllers/api/videos/update.tsindex 7c29e22d9d..6081a70716 100644--- a/server/core/controllers/api/videos/update.ts+++ b/server/core/controllers/api/videos/update.ts@@ -1,47 +1,17 @@-import { forceNumber } from '@peertube/peertube-core-utils'-import {- HttpStatusCode,- NSFWFlag,- VideoChannelActivityAction,- VideoPrivacy,- VideoPrivacyType,- VideoUpdate-} from '@peertube/peertube-models'-import { exists } from '@server/helpers/custom-validators/misc.js'+import { HttpStatusCode, VideoUpdate } from '@peertube/peertube-models'import { getVideoThumbnailFile } from '@server/helpers/video.js'-import { sendDeleteVideo } from '@server/lib/activitypub/send/send-delete.js'-import { changeVideoChannelShare } from '@server/lib/activitypub/share.js'-import { isPrivacyForFederation } from '@server/lib/activitypub/videos/federate.js'-import { AutomaticTagger } from '@server/lib/automatic-tags/automatic-tagger.js'-import { setAndSaveVideoAutomaticTags } from '@server/lib/automatic-tags/automatic-tags.js'+import { LocalVideoUpdater } from '@server/lib/local-video-updater.js'import { createLocalVideoThumbnailsFromImage } from '@server/lib/thumbnail.js'-import { replaceChaptersFromDescriptionIfNeeded } from '@server/lib/video-chapters.js'-import { onVideoLocalUpdate } from '@server/lib/video-jobs.js'-import { VideoPathManager } from '@server/lib/video-path-manager.js'-import { isNewVideoForSubscription, setVideoPrivacy } from '@server/lib/video-privacy.js'-import { setVideoTags } from '@server/lib/video.js'import { openapiOperationDoc } from '@server/middlewares/doc.js'-import { getServerAccount } from '@server/models/application/application.js'-import { VideoChannelActivityModel } from '@server/models/video/video-channel-activity.js'-import { VideoPasswordModel } from '@server/models/video/video-password.js'-import { FilteredModelAttributes } from '@server/types/index.js'-import { MVideoFull, MVideoThumbnails } from '@server/types/models/index.js'+import { MVideoThumbnails } from '@server/types/models/index.js'import express from 'express'-import { Transaction } from 'sequelize'-import { VideoAuditView, auditLoggerFactory, getAuditIdFromRes } from '../../../helpers/audit-logger.js'-import { resetSequelizeInstance } from '../../../helpers/database-utils.js'import { createReqFiles } from '../../../helpers/express-utils.js'-import { logger, loggerTagsFactory } from '../../../helpers/logger.js'+import { loggerTagsFactory } from '../../../helpers/logger.js'import { MIMETYPES } from '../../../initializers/constants.js'-import { sequelizeTypescript } from '../../../initializers/database.js'import { Hooks } from '../../../lib/plugins/hooks.js'-import { autoBlacklistVideoIfNeeded } from '../../../lib/video-blacklist.js'import { asyncMiddleware, asyncRetryTransactionMiddleware, authenticate, videosUpdateValidator } from '../../../middlewares/index.js'-import { ScheduleVideoUpdateModel } from '../../../models/video/schedule-video-update.js'-import { VideoModel } from '../../../models/video/video.js'const lTags = loggerTagsFactory('api', 'video')-const auditLogger = auditLoggerFactory('videos')const updateRouter = express.Router()const reqVideoFileUpdate = createReqFiles([ 'thumbnailfile', 'previewfile' ], MIMETYPES.IMAGE.MIMETYPE_EXT)@@ -65,248 +35,22 @@ export {async function updateVideo (req: express.Request, res: express.Response) {const videoFromReq = res.locals.videoFull- const oldVideoAuditView = new VideoAuditView(videoFromReq.toFormattedDetailsJSON())const body: VideoUpdate = req.bodyconst user = res.locals.oauth.token.User- const hadPrivacyForFederation = isPrivacyForFederation(videoFromReq.privacy)- const oldPrivacy = videoFromReq.privacy-const thumbnails = await buildVideoThumbnailsFromReq(videoFromReq, req)- const videoFileLockReleaser = await VideoPathManager.Instance.lockFiles(videoFromReq.uuid)-- try {- const { videoInstanceUpdated, newVideoForSubscription } = await sequelizeTypescript.transaction(async t => {- // Refresh video since thumbnails to prevent concurrent updates- const video = await VideoModel.loadFull(videoFromReq.id, t)-- const oldName = video.name- const oldDescription = video.description- const oldVideoChannel = video.VideoChannel-- const keysToUpdate: (keyof VideoUpdate & FilteredModelAttributes<VideoModel>)[] = [- 'name',- 'category',- 'licence',- 'language',- 'nsfw',- 'nsfwFlags',- 'nsfwSummary',- 'waitTranscoding',- 'support',- 'description',- 'downloadEnabled'- ]-- for (const key of keysToUpdate) {- if (body[key] !== undefined) video.set(key, body[key])- }-- if (!video.nsfw) {- video.nsfwFlags = NSFWFlag.NONE- video.nsfwSummary = null- }-- if (body.commentsPolicy !== undefined) {- video.commentsPolicy = body.commentsPolicy- }-- if (body.originallyPublishedAt !== undefined) {- video.originallyPublishedAt = body.originallyPublishedAt- ? new Date(body.originallyPublishedAt)- : null- }-- // Privacy update?- let newVideoForSubscription = false-- if (body.privacy !== undefined) {- newVideoForSubscription = isNewVideoForSubscription({- currentPrivacy: video.privacy,- newPrivacy: body.privacy,- firstPublishedAt: video.firstPublishedAt- })-- await updateVideoPrivacy({- videoInstance: video,- videoInfoToUpdate: body,- hadPrivacyForFederation,- transaction: t- })- }-- // Force updatedAt attribute change- if (!video.changed()) {- await video.setAsRefreshed(t)- }-- const videoInstanceUpdated = await video.save({ transaction: t }) as MVideoFull-- if (thumbnails.length !== 0) {- await videoInstanceUpdated.replaceAndSaveThumbnails(thumbnails, t)- }-- // Video tags update?- if (body.tags !== undefined) {- await setVideoTags({ video: videoInstanceUpdated, tags: body.tags, transaction: t })- }-- // Video channel update?- const newChannel = res.locals.videoChannel- if (newChannel && videoInstanceUpdated.channelId !== newChannel.id) {- const oldChannel = videoInstanceUpdated.VideoChannel-- await VideoChannelActivityModel.addVideoActivity({- action: VideoChannelActivityAction.REMOVE_CHANNEL_OWNERSHIP,- user,- channel: oldChannel,- video: videoInstanceUpdated,- transaction: t- })-- await VideoChannelActivityModel.addVideoActivity({- action: VideoChannelActivityAction.CREATE_CHANNEL_OWNERSHIP,- user,- channel: newChannel,- video: videoInstanceUpdated,- transaction: t- })-- await videoInstanceUpdated.$set('VideoChannel', newChannel, { transaction: t })- videoInstanceUpdated.VideoChannel = newChannel-- if (hadPrivacyForFederation === true) {- await changeVideoChannelShare(videoInstanceUpdated, oldVideoChannel, t)- }- } else {- await VideoChannelActivityModel.addVideoActivity({- action: VideoChannelActivityAction.UPDATE,- user: res.locals.oauth.token.User,- channel: videoInstanceUpdated.VideoChannel,- video: videoInstanceUpdated,- transaction: t- })- }- // Schedule an update in the future?- await updateSchedule(videoInstanceUpdated, body, t)+ const localVideoUpdater = new LocalVideoUpdater({ user, lTags, video: videoFromReq })- if (oldDescription !== video.description) {- await replaceChaptersFromDescriptionIfNeeded({- newDescription: videoInstanceUpdated.description,- transaction: t,- video,- oldDescription- })- }+ const video = await localVideoUpdater.update({ ...body, thumbnails })- let automaticTagsByAccount: Record<number, string[]>- if (oldName !== video.name || oldDescription !== video.description) {- automaticTagsByAccount = await new AutomaticTagger().buildVideoAutomaticTags({- serverAccount: await getServerAccount(),- video,- transaction: t- })- await setAndSaveVideoAutomaticTags({ video, automaticTagsByAccount, transaction: t })- }-- await autoBlacklistVideoIfNeeded({- video: videoInstanceUpdated,- user,- isRemote: false,- isNew: false,- isNewFile: false,- automaticTagsByAccount,- transaction: t- })-- auditLogger.update(- getAuditIdFromRes(res),- new VideoAuditView(videoInstanceUpdated.toFormattedDetailsJSON()),- oldVideoAuditView- )- logger.info('Video with name %s and uuid %s updated.', video.name, video.uuid, lTags(video.uuid))-- return { videoInstanceUpdated, newVideoForSubscription }- })-- Hooks.runAction('action:api.video.updated', { video: videoInstanceUpdated, body: req.body, req, res })-- await onVideoLocalUpdate({- video: videoInstanceUpdated,- nameChanged: !!body.name,- oldPrivacy,- isNewVideoForSubscription: newVideoForSubscription- })- } catch (err) {- // If the transaction is retried, sequelize will think the object has not changed- // So we need to restore the previous fields- await resetSequelizeInstance(videoFromReq)-- throw err- } finally {- videoFileLockReleaser()- }+ Hooks.runAction('action:api.video.updated', { video, body: req.body, req, res })return res.type('json').status(HttpStatusCode.NO_CONTENT_204).end()}-// Return a boolean indicating if the video is considered as "new" for remote instances in the federation-async function updateVideoPrivacy (options: {- videoInstance: MVideoFull- videoInfoToUpdate: VideoUpdate- hadPrivacyForFederation: boolean- transaction: Transaction-}) {- const { videoInstance, videoInfoToUpdate, hadPrivacyForFederation, transaction } = options-- const newPrivacy = forceNumber(videoInfoToUpdate.privacy) as VideoPrivacyType- setVideoPrivacy(videoInstance, newPrivacy)-- // Delete passwords if video is not anymore password protected- if (videoInstance.privacy === VideoPrivacy.PASSWORD_PROTECTED && newPrivacy !== VideoPrivacy.PASSWORD_PROTECTED) {- await VideoPasswordModel.deleteAllPasswords(videoInstance.id, transaction)- }-- if (newPrivacy === VideoPrivacy.PASSWORD_PROTECTED && exists(videoInfoToUpdate.videoPasswords)) {- await VideoPasswordModel.deleteAllPasswords(videoInstance.id, transaction)- await VideoPasswordModel.addPasswords(videoInfoToUpdate.videoPasswords, videoInstance.id, transaction)- }-- // Unfederate the video if the new privacy is not compatible with federation- if (hadPrivacyForFederation && !isPrivacyForFederation(videoInstance.privacy)) {- await sendDeleteVideo({ video: videoInstance, deleteForPrivacyChange: true, transaction })- }-}--async function updateSchedule (videoInstance: MVideoFull, videoInfoToUpdate: VideoUpdate, transaction: Transaction) {- if (videoInfoToUpdate.scheduleUpdate) {- const updateAt = new Date(videoInfoToUpdate.scheduleUpdate.updateAt)-- videoInstance.publishedAt = updateAt- await videoInstance.save({ transaction })-- await ScheduleVideoUpdateModel.upsert({- videoId: videoInstance.id,- updateAt,- privacy: videoInfoToUpdate.scheduleUpdate.privacy || null- }, { transaction })-- return- }-- if (videoInfoToUpdate.scheduleUpdate === null) {- const deleted = await ScheduleVideoUpdateModel.deleteByVideoId(videoInstance.id, transaction)-- if (deleted) {- videoInstance.publishedAt = new Date()- await videoInstance.save({ transaction })- }- }-}-async function buildVideoThumbnailsFromReq (video: MVideoThumbnails, req: express.Request) {const file = getVideoThumbnailFile(req.files)if (!file) return []diff --git a/server/core/helpers/audit-logger.ts b/server/core/helpers/audit-logger.tsindex f93e27f767..4d250be09e 100644--- a/server/core/helpers/audit-logger.ts+++ b/server/core/helpers/audit-logger.ts@@ -9,16 +9,22 @@ import {VideoImport} from '@peertube/peertube-models'import { AUDIT_LOG_FILENAME } from '@server/initializers/constants.js'+import { MUser } from '@server/types/models/index.js'import { diff } from 'deep-object-diff'import express from 'express'import { flatten } from 'flat'import { join } from 'path'import { addColors, config, createLogger, format, transports } from 'winston'import { CONFIG } from '../initializers/config.js'+import { getAuthUser } from './express-utils.js'import { jsonLoggerFormat, labelFormatter } from './logger.js'function getAuditIdFromRes (res: express.Response) {- return res.locals.oauth.token.User.username+ return getAuditIdFromUser(getAuthUser(res))+}++function getAuditIdFromUser (user: MUser) {+ return user.username}enum AUDIT_TYPE {@@ -285,14 +291,15 @@ class VideoChannelSyncAuditView extends EntityAuditView {}export {- getAuditIdFromRes,+ AbuseAuditView,auditLoggerFactory,- VideoImportAuditView,- VideoChannelAuditView,CommentAuditView,+ CustomConfigAuditView,+ getAuditIdFromRes,+ getAuditIdFromUser,UserAuditView,VideoAuditView,- AbuseAuditView,- CustomConfigAuditView,- VideoChannelSyncAuditView+ VideoChannelAuditView,+ VideoChannelSyncAuditView,+ VideoImportAuditView}diff --git a/server/core/lib/video-chapters.ts b/server/core/lib/video-chapters.tsindex de5f15a423..eac1c0e171 100644--- a/server/core/lib/video-chapters.ts+++ b/server/core/lib/video-chapters.ts@@ -1,12 +1,12 @@import { parseChapters, sortBy } from '@peertube/peertube-core-utils'import { VideoChapter } from '@peertube/peertube-models'+import { afterCommitIfTransaction } from '@server/helpers/database-utils.js'import { logger, loggerTagsFactory } from '@server/helpers/logger.js'+import { CONSTRAINTS_FIELDS } from '@server/initializers/constants.js'import { VideoChapterModel } from '@server/models/video/video-chapter.js'import { MVideoImmutable } from '@server/types/models/index.js'import { Transaction } from 'sequelize'import { InternalEventEmitter } from './internal-event-emitter.js'-import { CONSTRAINTS_FIELDS } from '@server/initializers/constants.js'-import { afterCommitIfTransaction } from '@server/helpers/database-utils.js'const lTags = loggerTagsFactory('video', 'chapters')