ActivityPub-federated video streaming platform using P2P directly in your web browser
Add ability to run auto tags async
Chocobozzzfba810b205c8
diff --git a/client/src/app/+admin/system/jobs/jobs.component.ts b/client/src/app/+admin/system/jobs/jobs.component.tsindex d817fdae8f..577a2d2468 100644--- a/client/src/app/+admin/system/jobs/jobs.component.ts+++ b/client/src/app/+admin/system/jobs/jobs.component.ts@@ -52,6 +52,7 @@ export class JobsComponent {'actor-keys','after-video-channel-import','build-automatic-tags',+ 'build-object-automatic-tags','create-user-export','email','federate-video',diff --git a/packages/models/src/server/job.model.ts b/packages/models/src/server/job.model.tsindex ae46aaf81d..1c8edefffe 100644--- a/packages/models/src/server/job.model.ts+++ b/packages/models/src/server/job.model.ts@@ -16,6 +16,7 @@ export type JobType =| 'actor-keys'| 'after-video-channel-import'| 'build-automatic-tags'+ | 'build-object-automatic-tags'| 'create-user-export'| 'email'| 'federate-video'@@ -369,3 +370,20 @@ export interface BuildAutomaticTagsPayload {ofComments: booleanofVideos: boolean}++// How the job has to handle the moderation policies bound to automatic tags:+// * `apply`: nothing is on hold, apply the policies using the tags built by the job+// * `release-hold`: the object has been put on hold (comment held for review, video auto blocked) while waiting for+// its tags, so the job has to confirm or release that hold+// * `none`: only rebuild the tags+export type AutomaticTagsModeration = 'apply' | 'release-hold' | 'none'++export interface BuildObjectAutomaticTagsPayload {+ objectType: 'video' | 'comment'+ objectId: number++ moderation: AutomaticTagsModeration++ // Only supported for comments+ notify: boolean | null+}diff --git a/packages/tests/fixtures/peertube-plugin-test-seven/main.js b/packages/tests/fixtures/peertube-plugin-test-seven/main.jsindex 43f49b57bf..2a257ed378 100644--- a/packages/tests/fixtures/peertube-plugin-test-seven/main.js+++ b/packages/tests/fixtures/peertube-plugin-test-seven/main.js@@ -5,31 +5,63 @@ async function register({getRouter}) {registerCommentAutoTagger({- autoTagName: 'plugin comment auto tag',+ autoTagNames: [ 'plugin comment auto tag' ],handler: async ({ comment }) => {if (comment && comment.text && comment.text.includes('plugin-tag-comment')) {- return { result: true }+ return { tags: [ 'plugin comment auto tag' ] }}- return { result: false }+ return { tags: [] }}})+ // Simulate an auto tagger that calls a slow external service: it must not block the comment creation+ registerCommentAutoTagger({+ autoTagNames: [ 'plugin slow comment auto tag' ],++ handler: async ({ comment }) => {+ if (!comment || !comment.text || !comment.text.includes('plugin-slow-tag-comment')) {+ return { tags: [] }+ }++ await new Promise(res => setTimeout(res, 3000))++ return { tags: [ 'plugin slow comment auto tag' ] }+ }+ })++ // Analyze the video file to decide of the tag: this runs in a job, outside of any transactionregisterVideoAutoTagger({- autoTagName: 'plugin video auto tag',+ autoTagNames: [ 'plugin video file auto tag' ],++ handler: async ({ video }) => {+ if (!video || !(video.name || '').includes('analyze-video-file')) return { tags: [] }++ const files = await peertubeHelpers.videos.getFiles(video.id)+ const path = (files?.webVideo?.videoFiles || []).map(f => f.path).find(p => !!p)+ if (!path) return { tags: [] }++ const probe = await peertubeHelpers.videos.ffprobe(path)+ if (!(probe.format.duration > 0)) return { tags: [] }++ return { tags: [ 'plugin video file auto tag' ] }+ }+ })++ registerVideoAutoTagger({+ autoTagNames: [ 'plugin video auto tag' ],handler: async ({ video }) => {if (video) {const text = (video.name || '') + ' ' + (video.description || '')if (text.includes('plugin-tag-video')) {- return { result: true }+ return { tags: [ 'plugin video auto tag' ] }}}-- return { result: false }+ return { tags: [] }}})diff --git a/packages/tests/src/api/moderation/automatic-tags.ts b/packages/tests/src/api/moderation/automatic-tags.tsindex 938833e3b6..d3909df359 100644--- a/packages/tests/src/api/moderation/automatic-tags.ts+++ b/packages/tests/src/api/moderation/automatic-tags.ts@@ -169,6 +169,7 @@ describe('Test automatic tags', function () {// No tags{await servers[0].comments.createThread({ videoId: videoUUID, text: 'my nautilus' })+ await waitJobs(servers)const { data } = await servers[0].comments.listCommentsOnMyVideos()expect(data.find(c => c.text === 'my nautilus').automaticTags).to.have.lengthOf(0)@@ -188,6 +189,8 @@ describe('Test automatic tags', function () {await servers[0].comments.createThread({ videoId: videoUUID, text: 'my nautilus 2' })await servers[0].comments.createThread({ videoId: videoUUID, text: 'word 1' })+ await waitJobs(servers)+const { data } = await servers[0].comments.listCommentsOnMyVideos()// Previous comment has been rebuilt using the new watched words listexpect(data.find(c => c.text === 'my nautilus').automaticTags).to.have.members([ 'list 3' ])@@ -204,6 +207,8 @@ describe('Test automatic tags', function () {await servers[0].comments.createThread({ videoId: videoUUID, text: 'my nautilus 3' })await servers[0].comments.createThread({ videoId: videoUUID, text: 'word 2' })+ await waitJobs(servers)+const { data } = await servers[0].comments.listCommentsOnMyVideos()expect(data.find(c => c.text === 'my nautilus 3').automaticTags).to.have.lengthOf(0)expect(data.find(c => c.text === 'word 2').automaticTags).to.have.members([ 'list 1' ])diff --git a/packages/tests/src/api/notifications/comments-notifications.ts b/packages/tests/src/api/notifications/comments-notifications.tsindex 85dcdb79b7..6af7e14d84 100644--- a/packages/tests/src/api/notifications/comments-notifications.ts+++ b/packages/tests/src/api/notifications/comments-notifications.ts@@ -197,6 +197,68 @@ describe('Test comments notifications', function () {}})+ it('Should notify with the final held status when the comment is held while its automatic tags are built', async function () {+ this.timeout(120000)++ await servers[0].watchedWordsLists.createList({+ token: userToken,+ accountName: 'user_1',+ listName: 'forbidden-list',+ words: [ 'forbidden' ]+ })++ await servers[0].autoTags.updateCommentPolicies({ token: userToken, accountName: 'user_1', review: [ 'forbidden-list' ] })++ const { uuid, shortUUID } = await servers[0].videos.upload({ token: userToken, attributes: { name: 'video with review policy' } })+ await waitJobs(servers)++ // A comment that does not match the policy is released by the job, so it must not be notified as requiring approval+ {+ const created = await servers[0].comments.createThread({ videoId: uuid, text: 'a regular comment', token: userToken2 })+ await waitJobs(servers)++ await checkNewCommentOnMyVideo({+ ...baseParams,+ shortUUID,+ threadId: created.id,+ commentId: created.id,+ checkType: 'presence',+ approval: false+ })++ const { data } = await servers[0].comments.listForAdmin()+ expect(data.find(c => c.text === 'a regular comment').heldForReview).to.be.false+ }++ // A comment that matches it stays held, so it is notified as requiring approval+ {+ const created = await servers[0].comments.createThread({ videoId: uuid, text: 'a forbidden comment', token: userToken2 })+ await waitJobs(servers)++ await checkNewCommentOnMyVideo({+ ...baseParams,+ shortUUID,+ threadId: created.id,+ commentId: created.id,+ checkType: 'presence',+ approval: true+ })++ const { data } = await servers[0].comments.listForAdmin()+ expect(data.find(c => c.text === 'a forbidden comment').heldForReview).to.be.true+ }++ // Exactly one notification per comment: the job must not re-notify a comment already notified at creation+ {+ const notifications = baseParams.socketNotifications+ .filter(n => n.type === UserNotificationType.NEW_COMMENT_ON_MY_VIDEO && n.comment?.video?.shortUUID === shortUUID)++ expect(notifications).to.have.lengthOf(2)+ }++ await servers[0].autoTags.updateCommentPolicies({ token: userToken, accountName: 'user_1', review: [] })+ })+it('Should convert markdown in comment to html', async function () {this.timeout(60000)diff --git a/packages/tests/src/api/notifications/moderation-notifications.ts b/packages/tests/src/api/notifications/moderation-notifications.tsindex d3d55c58f0..0ffffcf1b2 100644--- a/packages/tests/src/api/notifications/moderation-notifications.ts+++ b/packages/tests/src/api/notifications/moderation-notifications.ts@@ -1,7 +1,7 @@/* oxlint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */import { wait } from '@peertube/peertube-core-utils'-import { AbuseState, UserNotification, UserNotificationType, UserRole, VideoPrivacy } from '@peertube/peertube-models'+import { AbuseState, UserNotification, UserNotificationType, UserRole, VideoBlacklistType, VideoPrivacy } from '@peertube/peertube-models'import { buildUUID } from '@peertube/peertube-node-utils'import { cleanupTests, PeerTubeServer, waitJobs } from '@peertube/peertube-server-commands'import { MockCoreBlocklist } from '@tests/shared/mock-servers/mock-core-blocklist.js'@@ -21,6 +21,7 @@ import {import { checkMyVideoIsPublished, checkNewVideoFromSubscription } from '@tests/shared/notifications/check-video-notifications.js'import { prepareNotificationsTest, waitUntilNotification } from '@tests/shared/notifications/notifications-common.js'import { CheckerBaseParams } from '@tests/shared/notifications/shared/notification-checker.js'+import { expect } from 'chai'describe('Test moderation notifications', function () {let servers: PeerTubeServer[] = []@@ -584,6 +585,146 @@ describe('Test moderation notifications', function () {})})+ describe('Video-related notifications when an auto tag block policy is set', function () {+ let adminBaseParams: CheckerBaseParams+ let userBaseParams: CheckerBaseParams++ before(async function () {+ this.timeout(120000)++ adminBaseParams = {+ server: servers[0],+ emails,+ socketNotifications: adminNotifications,+ token: servers[0].accessToken+ }++ userBaseParams = {+ server: servers[0],+ emails,+ socketNotifications: userNotifications,+ token: userToken1+ }++ // Only the auto tag policy must block videos here+ await servers[0].config.disableAutoBlacklist()++ await servers[0].watchedWordsLists.createList({ listName: 'blocked-list', words: [ 'forbidden' ] })+ await servers[0].autoTags.updateServerVideoPolicies({ autoBlock: [ 'blocked-list' ] })++ await servers[0].subscriptions.add({ targetUri: 'user_1_channel@' + servers[0].host })+ })++ it('Should notify the moderators and not the subscribers when the automatic tags block the video', async function () {+ this.timeout(120000)++ const videoName = 'forbidden video ' + buildUUID()+ const { shortUUID } = await servers[0].videos.upload({ token: userToken1, attributes: { name: videoName } })++ await waitJobs(servers)++ const { data } = await servers[0].blacklist.list({ type: VideoBlacklistType.AUTO_BY_AUTO_TAG_POLICY })+ expect(data.find(b => b.video.shortUUID === shortUUID)).to.exist++ await checkVideoAutoBlacklistForModerators({ ...adminBaseParams, shortUUID, videoName, checkType: 'presence' })+ await checkNewVideoFromSubscription({ ...adminBaseParams, videoName, shortUUID, checkType: 'absence' })+ await checkMyVideoIsPublished({ ...userBaseParams, videoName, shortUUID, checkType: 'absence' })+ })++ it('Should notify the subscribers and not the moderators when the automatic tags do not block the video', async function () {+ this.timeout(120000)++ const videoName = 'allowed video ' + buildUUID()+ const { shortUUID } = await servers[0].videos.upload({ token: userToken1, attributes: { name: videoName } })++ await waitJobs(servers)++ const { data } = await servers[0].blacklist.list()+ expect(data.find(b => b.video.shortUUID === shortUUID)).to.not.exist++ // The video produces a subscription notification, so check the absence of the moderation one directly+ const blockNotifications = adminNotifications.filter(n => {+ return n.type === UserNotificationType.VIDEO_AUTO_BLACKLIST_FOR_MODERATORS &&+ n.videoBlacklist?.video?.shortUUID === shortUUID+ })+ expect(blockNotifications).to.have.lengthOf(0)++ await checkNewVideoFromSubscription({ ...adminBaseParams, videoName, shortUUID, checkType: 'presence' })+ })++ it('Should not re-announce an already published video when its automatic tags are rebuilt', async function () {+ this.timeout(120000)++ const videoName = 'allowed video to rename ' + buildUUID()+ const { id, shortUUID } = await servers[0].videos.upload({ token: userToken1, attributes: { name: videoName } })++ await waitJobs(servers)+ await checkNewVideoFromSubscription({ ...adminBaseParams, videoName, shortUUID, checkType: 'presence' })++ const notificationsBefore = countNewVideoNotifications(shortUUID)+ const emailsBefore = countNewVideoEmails(shortUUID)+ expect(notificationsBefore).to.equal(1)++ // Renaming rebuilds the automatic tags: the video must not be blocked while they are built, and so must not be+ // announced again to the subscribers when it is released+ await servers[0].videos.update({ id, token: userToken1, attributes: { name: 'allowed video renamed ' + buildUUID() } })++ await waitJobs(servers)+ await wait(3000)++ const { data } = await servers[0].blacklist.list()+ expect(data.find(b => b.video.shortUUID === shortUUID)).to.not.exist++ expect(countNewVideoNotifications(shortUUID)).to.equal(notificationsBefore)+ expect(countNewVideoEmails(shortUUID)).to.equal(emailsBefore)+ })++ it('Should block an already published video when its rebuilt automatic tags match the policy', async function () {+ this.timeout(120000)++ const videoName = 'allowed video to forbid ' + buildUUID()+ const { id, shortUUID } = await servers[0].videos.upload({ token: userToken1, attributes: { name: videoName } })++ await waitJobs(servers)+ await checkNewVideoFromSubscription({ ...adminBaseParams, videoName, shortUUID, checkType: 'presence' })++ const notificationsBefore = countNewVideoNotifications(shortUUID)+ expect(notificationsBefore).to.equal(1)++ const updatedName = 'forbidden video renamed ' + buildUUID()+ await servers[0].videos.update({ id, token: userToken1, attributes: { name: updatedName } })++ await waitJobs(servers)+ await wait(3000)++ const { data } = await servers[0].blacklist.list({ type: VideoBlacklistType.AUTO_BY_AUTO_TAG_POLICY })+ expect(data.find(b => b.video.shortUUID === shortUUID)).to.exist++ await checkVideoAutoBlacklistForModerators({ ...adminBaseParams, shortUUID, videoName: updatedName, checkType: 'presence' })++ expect(countNewVideoNotifications(shortUUID)).to.equal(notificationsBefore)+ })++ function countNewVideoNotifications (shortUUID: string) {+ return adminNotifications.filter(n => {+ return n.type === UserNotificationType.NEW_VIDEO_FROM_SUBSCRIPTION && n.video?.shortUUID === shortUUID+ }).length+ }++ function countNewVideoEmails (shortUUID: string) {+ return emails.filter(e => {+ const text = e['text'] as string++ return text.includes(shortUUID) && text.includes('Your subscription')+ }).length+ }++ after(async function () {+ await servers[0].autoTags.updateServerVideoPolicies({ autoBlock: [] })+ await servers[0].subscriptions.remove({ uri: 'user_1_channel@' + servers[0].host })+ })+ })+describe('Subscription auto blocklist notifications', function () {let mockServer: MockCoreBlocklistlet mockPort: numberdiff --git a/packages/tests/src/plugins/plugin-auto-taggers.ts b/packages/tests/src/plugins/plugin-auto-taggers.tsindex 767d40c175..8d7d8cca94 100644--- a/packages/tests/src/plugins/plugin-auto-taggers.ts+++ b/packages/tests/src/plugins/plugin-auto-taggers.ts@@ -40,7 +40,32 @@ describe('Test plugin auto taggers', function () {it('Should list available auto tag names for comments', async function () {const { available } = await server.autoTags.getAccountAvailable({ accountName: 'root' })- expect(available.map(t => t.name)).to.have.members([ 'external-link', 'plugin comment auto tag' ])+ expect(available.map(t => t.name)).to.have.members([+ 'external-link',+ 'plugin comment auto tag',+ 'plugin slow comment auto tag'+ ])+ })++ it('Should not block the comment creation while a slow auto tagger is running', async function () {+ this.timeout(30000)++ const before = new Date().getTime()+ await server.comments.createThread({ videoId: videoUUID, text: 'a comment with plugin-slow-tag-comment' })+ const duration = new Date().getTime() - before++ expect(duration).to.be.below(2000)++ await waitJobs([ server ])++ {+ const { data } = await server.comments.listForAdmin()+ const comment = data.find(c => c.text === 'a comment with plugin-slow-tag-comment')++ expect(comment.automaticTags).to.have.members([ 'plugin slow comment auto tag' ])+ }++ await server.comments.deleteAllComments({ videoUUID })})it('Should not assign plugin tag if the plugin does not trigger', async function () {@@ -49,6 +74,8 @@ describe('Test plugin auto taggers', function () {text: 'just a normal comment with no trigger'})+ await waitJobs([ server ])+{const { data } = await server.comments.listForAdmin()expect(data.find(c => c.text === 'just a normal comment with no trigger').automaticTags).to.have.lengthOf(0)@@ -68,6 +95,8 @@ describe('Test plugin auto taggers', function () {text: 'another comment with plugin-tag-comment trigger'})+ await waitJobs([ server ])+{const { data } = await server.comments.listForAdmin()const c = (text: string) => data.find(c => c.text === text)@@ -137,12 +166,35 @@ describe('Test plugin auto taggers', function () {it('Should list available auto tag names for videos', async function () {const { available } = await server.autoTags.getServerAvailable()- expect(available.map(t => t.name)).to.have.members([ 'plugin video auto tag', 'external-link' ])+ expect(available.map(t => t.name)).to.have.members([+ 'plugin video auto tag',+ 'plugin video file auto tag',+ 'external-link'+ ])+ })++ it('Should assign a tag decided by an auto tagger that analyzed the video file', async function () {+ this.timeout(60000)++ await server.videos.upload({ attributes: { name: 'video with analyze-video-file', description: 'no trigger' } })++ await waitJobs([ server ])++ {+ const { data } = await server.videos.listAllForAdmin()+ const video = data.find(v => v.name === 'video with analyze-video-file')++ expect(video.automaticTags).to.have.members([ 'plugin video file auto tag' ])+ }++ await server.videos.removeAll()})it('Should not assign plugin tag if no trigger word is in video name or description', async function () {await server.videos.upload({ attributes: { name: 'normal video', description: 'normal description' } })+ await waitJobs([ server ])+{const { data } = await server.videos.listAllForAdmin()expect(data.find(v => v.name === 'normal video').automaticTags).to.have.lengthOf(0)@@ -156,6 +208,8 @@ describe('Test plugin auto taggers', function () {attributes: { name: 'video with plugin-tag-video in name', description: 'no trigger' }})+ await waitJobs([ server ])+{const { data } = await server.videos.listAllForAdmin()expect(data.find(v => v.name === 'video with plugin-tag-video in name').automaticTags).to.have.members([ 'plugin video auto tag' ])@@ -169,6 +223,8 @@ describe('Test plugin auto taggers', function () {attributes: { name: 'video desc', description: 'contains plugin-tag-video in description' }})+ await waitJobs([ server ])+{const { data } = await server.videos.listAllForAdmin()expect(data.find(v => v.name === 'video desc').automaticTags).to.have.members([ 'plugin video auto tag' ])@@ -190,6 +246,8 @@ describe('Test plugin auto taggers', function () {text: 'plugin-tag-comment for helper test'})+ await waitJobs([ server ])+{const { data } = await server.comments.listForAdmin()const comment = data.find(c => c.text === 'plugin-tag-comment for helper test')@@ -229,6 +287,8 @@ describe('Test plugin auto taggers', function () {attributes: { name: 'plugin-tag-video for helper', description: 'test' }})+ await waitJobs([ server ])+{const { data } = await server.videos.listAllForAdmin()const video = data.find(v => v.name === 'plugin-tag-video for helper')diff --git a/server/core/controllers/api/videos/comment.ts b/server/core/controllers/api/videos/comment.tsindex e0d1ee1c7a..7df873fb2f 100644--- a/server/core/controllers/api/videos/comment.ts+++ b/server/core/controllers/api/videos/comment.ts@@ -287,14 +287,15 @@ async function listVideoCommentReplies (req: express.Request, res: express.Respoasync function addVideoCommentThread (req: express.Request, res: express.Response) {const videoCommentInfo: VideoCommentCreate = req.body- const comment = await createLocalVideoComment({+ const { comment, pendingAutomaticTags } = await createLocalVideoComment({text: videoCommentInfo.text,inReplyToComment: null,video: res.locals.videoWithRights,user: res.locals.oauth.token.User})- Notifier.Instance.notifyOnNewComment(comment)+ // The `build-object-automatic-tags` job notifies once the held status of the comment is final+ if (!pendingAutomaticTags) Notifier.Instance.notifyOnNewComment(comment)auditLogger.create(getAuditIdFromRes(res), new CommentAuditView(comment.toFormattedJSON()))Hooks.runAction('action:api.video-thread.created', { comment, req, res })@@ -305,14 +306,15 @@ async function addVideoCommentThread (req: express.Request, res: express.Responsasync function addVideoCommentReply (req: express.Request, res: express.Response) {const videoCommentInfo: VideoCommentCreate = req.body- const comment = await createLocalVideoComment({+ const { comment, pendingAutomaticTags } = await createLocalVideoComment({text: videoCommentInfo.text,inReplyToComment: res.locals.videoCommentFull,video: res.locals.videoWithRights,user: res.locals.oauth.token.User})- Notifier.Instance.notifyOnNewComment(comment)+ // The `build-object-automatic-tags` job notifies once the held status of the comment is final+ if (!pendingAutomaticTags) Notifier.Instance.notifyOnNewComment(comment)auditLogger.create(getAuditIdFromRes(res), new CommentAuditView(comment.toFormattedJSON()))Hooks.runAction('action:api.video-comment-reply.created', { comment, req, res })diff --git a/server/core/controllers/api/videos/source.ts b/server/core/controllers/api/videos/source.tsindex ca21e571bb..4e54894213 100644--- a/server/core/controllers/api/videos/source.ts+++ b/server/core/controllers/api/videos/source.ts@@ -6,6 +6,7 @@ import { CreateJobOptions, CreateJobTypeAndPayload, JobQueue } from '@server/libimport { Hooks } from '@server/lib/plugins/hooks.js'import { regenerateLocalVideoThumbnailsFromVideoIfNeeded } from '@server/lib/thumbnail.js'import { setupUploadResumableRoutes } from '@server/lib/uploadx.js'+import { buildNonDuplicatedVideoAutomaticTagsJob } from '@server/lib/automatic-tags/automatic-tags.js'import { autoBlacklistVideoIfNeeded } from '@server/lib/video-blacklist.js'import { regenerateTranscriptionTaskIfNeeded } from '@server/lib/video-captions.js'import { buildNewFile, createVideoSource } from '@server/lib/video-file.js'@@ -145,7 +146,8 @@ async function doReplaceVideoSourceResumable (req: express.Request, res: expressawait autoBlacklistVideoIfNeeded({video,user,- automaticTagsByAccount: null,+ // The name and the description of the video did not change, so its automatic tags are still up to date+ automaticTagsPending: false,isRemote: false,isNew: false,isNewFile: true,@@ -201,6 +203,9 @@ async function addVideoJobsAfterUpload (video: MVideoFull, videoFile: MVideoFileawait buildLocalStoryboardJobIfNeeded({ video, federate: false }),+ // The video has a new file to analyze: rebuild its automatic tags before re-federating it+ buildNonDuplicatedVideoAutomaticTagsJob({ video, moderation: 'apply' }),+buildNonDuplicatedFederateVideoJob({ video })]diff --git a/server/core/initializers/constants.ts b/server/core/initializers/constants.tsindex 0863e8e833..88cddde8a0 100644--- a/server/core/initializers/constants.ts+++ b/server/core/initializers/constants.ts@@ -230,6 +230,7 @@ export const REMOTE_SCHEME = {export const JOB_ATTEMPTS: { [id in JobType]: number } = {'build-automatic-tags': 1,+ 'build-object-automatic-tags': 2,'activitypub-http-broadcast': 1,'activitypub-http-broadcast-parallel': 1,'activitypub-http-unicast': 1,@@ -262,6 +263,8 @@ export const JOB_ATTEMPTS: { [id in JobType]: number } = {// Excluded keys are jobs that can be configured by adminsexport const JOB_CONCURRENCY: { [id in Exclude<JobType, 'video-transcoding' | 'video-import'>]: number } = {'build-automatic-tags': 1,+ // Auto taggers can be plugins calling a slow external service+ 'build-object-automatic-tags': 5,'activitypub-http-broadcast': 1,'activitypub-http-broadcast-parallel': 30,'activitypub-http-unicast': 30,@@ -290,7 +293,8 @@ export const JOB_CONCURRENCY: { [id in Exclude<JobType, 'video-transcoding' | 'v'video-transcription': 1}export const JOB_TTL: { [id in JobType]: number } = {- 'build-automatic-tags': 1000 * 60 * 30, // 30 minutes+ 'build-automatic-tags': 60000 * 60 * 24 * 7, // 7 days: plugin auto taggers can be slow and this job rebuilds every object+ 'build-object-automatic-tags': 1000 * 60 * 30, // 30 minutes'activitypub-http-broadcast': 60000 * 10, // 10 minutes'activitypub-http-broadcast-parallel': 60000 * 10, // 10 minutes'activitypub-http-unicast': 60000 * 10, // 10 minutesdiff --git a/server/core/lib/activitypub/process/process-create.ts b/server/core/lib/activitypub/process/process-create.tsindex 39a90ecff0..bab67a792b 100644--- a/server/core/lib/activitypub/process/process-create.ts+++ b/server/core/lib/activitypub/process/process-create.ts@@ -151,6 +151,7 @@ async function processCreateVideoComment (let video: MVideoAccountLightBlacklistAllFileslet created: booleanlet comment: MCommentOwnerVideo+ let heldPendingAutomaticTags: booleantry {const resolveThreadResult = await resolveThread({ url: commentObject.id, isVideo: false })@@ -159,6 +160,7 @@ async function processCreateVideoComment (video = resolveThreadResult.videocreated = resolveThreadResult.commentCreatedcomment = resolveThreadResult.comment+ heldPendingAutomaticTags = resolveThreadResult.heldPendingAutomaticTags} catch (err) {logger.debug('Cannot process video comment because we could not resolve thread %s. Maybe it was not a video thread, so skip it.',@@ -195,7 +197,8 @@ async function processCreateVideoComment (}}- if (created) Notifier.Instance.notifyOnNewComment(comment)+ // The `build-object-automatic-tags` job notifies once the held status of the comment is final+ if (created && !heldPendingAutomaticTags) Notifier.Instance.notifyOnNewComment(comment)}// The origin instance re-sends us the comment when we approved the replydiff --git a/server/core/lib/activitypub/video-comments.ts b/server/core/lib/activitypub/video-comments.tsindex 46f7d998cf..35339b7881 100644--- a/server/core/lib/activitypub/video-comments.ts+++ b/server/core/lib/activitypub/video-comments.ts@@ -1,6 +1,5 @@import { HttpStatusCode, VideoCommentPolicy } from '@peertube/peertube-models'import { CONFIG } from '@server/initializers/config.js'-import { getServerAccount } from '@server/models/application/application.js'import Bluebird from 'bluebird'import { sanitizeAndCheckVideoCommentObject } from '../../helpers/custom-validators/activitypub/video-comments.js'import { createLogger } from '../../helpers/logger.js'@@ -13,11 +12,10 @@ import {MVideoAccountLight,MVideoAccountLightBlacklistAllFiles} from '../../types/models/video/index.js'-import { AutomaticTagger } from '../automatic-tags/automatic-tagger.js'-import { setAndSaveCommentAutomaticTags } from '../automatic-tags/automatic-tags.js'+import { createCommentAutomaticTagsJob } from '../automatic-tags/automatic-tags.js'import { isRemoteVideoCommentAccepted } from '../moderation.js'import { Hooks } from '../plugins/hooks.js'-import { shouldCommentBeHeldForReview } from '../video-comment.js'+import { CommentReviewDecision, getCommentReviewDecision } from '../video-comment.js'import { fetchAP } from './activity.js'import { getOrCreateAPActor } from './actors/index.js'import { checkUrlsSameHost } from './url.js'@@ -31,7 +29,12 @@ type ResolveThreadParams = {isVideo?: booleancommentCreated?: boolean}-type ResolveThreadResult = Promise<{ video: MVideoAccountLightBlacklistAllFiles, comment: MCommentOwnerVideo, commentCreated: boolean }>+type ResolveThreadResult = Promise<{+ video: MVideoAccountLightBlacklistAllFiles+ comment: MCommentOwnerVideo+ commentCreated: boolean+ heldPendingAutomaticTags: boolean+}>export async function addVideoComments (commentUrls: string[]) {if (CONFIG.VIDEO_COMMENTS.ACCEPT_REMOTE_COMMENTS !== true) return@@ -123,6 +126,10 @@ async function tryToResolveThreadFromVideo (params: ResolveThreadParams) {}let resultComment: MCommentOwnerVideo+ // comments[0] is the comment the thread was resolved for: only its held status is reported back to the caller,+ // the other comments in the array are ancestors synthesized to complete the thread+ let resultCommentHeldPendingAutomaticTags = false+if (comments.length !== 0) {const firstReply = comments[comments.length - 1] as MCommentOwnerVideofirstReply.inReplyToCommentId = null@@ -135,10 +142,22 @@ async function tryToResolveThreadFromVideo (params: ResolveThreadParams) {return undefined}- const firstReplyAutomaticTagsByAccount = await getAutomaticTagsAndAssignReview(firstReply, video)+ const firstReplyDecision = await assignReviewIfNew(firstReply, video)comments[comments.length - 1] = await firstReply.save()- await setAndSaveCommentAutomaticTags({ comment: firstReply, automaticTagsByAccount: firstReplyAutomaticTagsByAccount })+ if (firstReplyDecision) {+ if (comments.length === 1) resultCommentHeldPendingAutomaticTags = firstReplyDecision.pendingAutomaticTags++ createCommentAutomaticTagsJob({+ comment: firstReply,+ moderation: firstReplyDecision.pendingAutomaticTags+ ? 'release-hold'+ : 'none',+ // Only the comment the thread was resolved for (comments[0]) must notify the video owner: the other ones+ // are ancestors synthesized to complete the thread, not what the Create activity was actually about+ notify: comments.length === 1+ })+ }for (let i = comments.length - 2; i >= 0; i--) {const comment = comments[i] as MCommentOwnerVideo@@ -152,44 +171,45 @@ async function tryToResolveThreadFromVideo (params: ResolveThreadParams) {return undefined}- const automaticTagsByAccount = await getAutomaticTagsAndAssignReview(comment, video)+ const decision = await assignReviewIfNew(comment, video)comments[i] = await comment.save()- await setAndSaveCommentAutomaticTags({ comment, automaticTagsByAccount })+ if (decision) {+ if (i === 0) resultCommentHeldPendingAutomaticTags = decision.pendingAutomaticTags++ createCommentAutomaticTagsJob({+ comment,+ moderation: decision.pendingAutomaticTags+ ? 'release-hold'+ : 'none',+ // comments[0] is the comment the thread was resolved for: only it must notify the video owner+ notify: i === 0+ })+ }}resultComment = comments[0] as MCommentOwnerVideo}- return { video, comment: resultComment, commentCreated }+ return { video, comment: resultComment, commentCreated, heldPendingAutomaticTags: resultCommentHeldPendingAutomaticTags }}-// Return the automatic tags by account id (owner and server accounts) and assign the held for review status to the comment if needed-async function getAutomaticTagsAndAssignReview (comment: MComment, video: MVideoAccountLight) {+// Assign the held for review status of the comment if it's a new one, and return the decision (undefined if not new)+// Automatic tags are built by a job, so a comment is held as soon as the account has a review policy+// The job then releases it if the tags it ends up with don't match any of them+async function assignReviewIfNew (comment: MComment, video: MVideoAccountLight): Promise<CommentReviewDecision> {// Remote comment already exists in database -> we don't need to rebuild automatic tags- if (comment.id) return {}-- const ownerAccount = video.VideoChannel.Account-- const automaticTagsByAccount = await new AutomaticTagger().buildCommentsAutomaticTags({- serverAccount: await getServerAccount(),- ownerAccount,- text: comment.text- })+ if (comment.id) return undefined// Third parties rely on origin, so if origin has the comment it's not held for review- if (video.isLocal() || comment.isLocal()) {- comment.heldForReview = await shouldCommentBeHeldForReview({- user: null,- video,- ownerAutomaticTags: automaticTagsByAccount[video.VideoChannel.accountId]- })- } else {- comment.heldForReview = false- }+ const decision = video.isLocal() || comment.isLocal()+ ? await getCommentReviewDecision({ user: null, video, automaticTagsPending: true })+ : { heldForReview: false, pendingAutomaticTags: false }++ comment.heldForReview = decision.heldForReview- return automaticTagsByAccount+ return decision}// ---------------------------------------------------------------------------diff --git a/server/core/lib/activitypub/videos/shared/abstract-builder.ts b/server/core/lib/activitypub/videos/shared/abstract-builder.tsindex 3208ebd668..63676bf7db 100644--- a/server/core/lib/activitypub/videos/shared/abstract-builder.ts+++ b/server/core/lib/activitypub/videos/shared/abstract-builder.ts@@ -4,12 +4,9 @@ import { isVideoChaptersObjectValid } from '@server/helpers/custom-validators/acimport { deleteAllModels, filterNonExistingModels, retryTransactionWrapper } from '@server/helpers/database-utils.js'import { createLogger } from '@server/helpers/logger.js'import { sequelizeTypescript } from '@server/initializers/database.js'-import { AutomaticTagger } from '@server/lib/automatic-tags/automatic-tagger.js'-import { setAndSaveVideoAutomaticTags } from '@server/lib/automatic-tags/automatic-tags.js'import { updateRemoteVideoThumbnail } from '@server/lib/thumbnail.js'import { replaceChapters } from '@server/lib/video-chapters.js'import { setVideoTags } from '@server/lib/video.js'-import { getServerAccount } from '@server/models/application/application.js'import { StoryboardModel } from '@server/models/video/storyboard.js'import { VideoCaptionModel } from '@server/models/video/video-caption.js'import { VideoFileModel } from '@server/models/video/video-file.js'@@ -237,23 +234,14 @@ export abstract class APVideoAbstractBuilder {playlistModel.VideoFiles = await this.saveFiles({ toCreate, oldFiles: oldStreamingPlaylistFiles, t, mode: 'streaming-playlist' })}- protected async setAutomaticTags (options: {+ // The automatic tags are built from the name and the description, so they only have to be rebuilt when one changed+ protected automaticTagsNeedRebuild (options: {video: MVideooldVideo?: Pick<MVideo, 'name' | 'description'>- transaction: Transaction}) {- const { video, transaction, oldVideo } = options+ const { video, oldVideo } = options- if (video.name === oldVideo?.name && video.description === oldVideo.description) return {}-- const automaticTagsByAccount = await new AutomaticTagger().buildVideoAutomaticTags({- serverAccount: await getServerAccount(),- video,- transaction- })- await setAndSaveVideoAutomaticTags({ video, automaticTagsByAccount, transaction })-- return automaticTagsByAccount+ return video.name !== oldVideo?.name || video.description !== oldVideo.description}protected async saveFiles (options: {diff --git a/server/core/lib/activitypub/videos/shared/creator.ts b/server/core/lib/activitypub/videos/shared/creator.tsindex 3ddd54fe28..647552807f 100644--- a/server/core/lib/activitypub/videos/shared/creator.ts+++ b/server/core/lib/activitypub/videos/shared/creator.ts@@ -1,6 +1,7 @@import { VideoObject } from '@peertube/peertube-models'import { createLogger } from '@server/helpers/logger.js'import { sequelizeTypescript } from '@server/initializers/database.js'+import { createVideoAutomaticTagsJob } from '@server/lib/automatic-tags/automatic-tags.js'import { Hooks } from '@server/lib/plugins/hooks.js'import { autoBlacklistVideoIfNeeded } from '@server/lib/video-blacklist.js'import { VideoModel } from '@server/models/video/video.js'@@ -42,14 +43,12 @@ export class APVideoCreator extends APVideoAbstractBuilder {await this.insertOrReplaceLive(videoCreated, t)await this.insertOrReplaceStoryboard(videoCreated, t)- const automaticTagsByAccount = await this.setAutomaticTags({ video: videoCreated, transaction: t })-// We added a video in this channel, set it as updatedawait channel.setAsUpdated(t)- const autoBlacklisted = await autoBlacklistVideoIfNeeded({+ const { blacklisted: autoBlacklisted, pendingAutomaticTags } = await autoBlacklistVideoIfNeeded({video: videoCreated,- automaticTagsByAccount,+ automaticTagsPending: true,user: undefined,isRemote: true,isNew: true,@@ -57,6 +56,14 @@ export class APVideoCreator extends APVideoAbstractBuilder {transaction: t})+ createVideoAutomaticTagsJob({+ video: videoCreated,+ moderation: pendingAutomaticTags+ ? 'release-hold'+ : 'apply',+ transaction: t+ })+logger.info('Remote video with uuid %s inserted.', this.videoObject.uuid)Hooks.runAction('action:activity-pub.remote-video.created', { video: videoCreated, videoAPObject: this.videoObject })diff --git a/server/core/lib/activitypub/videos/updater.ts b/server/core/lib/activitypub/videos/updater.tsindex 209b968406..c3c844fc05 100644--- a/server/core/lib/activitypub/videos/updater.ts+++ b/server/core/lib/activitypub/videos/updater.ts@@ -1,6 +1,7 @@import { VideoObject, VideoPrivacy } from '@peertube/peertube-models'import { resetSequelizeInstance, runInReadCommittedTransaction } from '@server/helpers/database-utils.js'import { createLogger } from '@server/helpers/logger.js'+import { createVideoAutomaticTagsJob } from '@server/lib/automatic-tags/automatic-tags.js'import { Notifier } from '@server/lib/notifier/index.js'import { PeerTubeSocket } from '@server/lib/peertube-socket.js'import { Hooks } from '@server/lib/plugins/hooks.js'@@ -83,18 +84,19 @@ export class APVideoUpdater extends APVideoAbstractBuilder {this.setOrDeleteLive(videoUpdated)])- const automaticTagsByAccount = await runInReadCommittedTransaction(t => {- return this.setAutomaticTags({ video: videoUpdated, transaction: t, oldVideo })- })+ const rebuildAutomaticTags = this.automaticTagsNeedRebuild({ video: videoUpdated, oldVideo })await runInReadCommittedTransaction(t => this.setCaptions(videoUpdated, t))await this.updateChapters(videoUpdated)await this.upsertPlayerSettings(videoUpdated)+ // The video is already published: don't hold it while its automatic tags are rebuilt, or a simple metadata+ // update would block it and re-announce it to the subscribers once released+ // The `build-object-automatic-tags` job applies the auto tag block policies afterwardsawait autoBlacklistVideoIfNeeded({video: videoUpdated,- automaticTagsByAccount,+ automaticTagsPending: false,user: undefined,isRemote: true,isNew: false,@@ -102,6 +104,10 @@ export class APVideoUpdater extends APVideoAbstractBuilder {transaction: undefined})+ if (rebuildAutomaticTags) {+ createVideoAutomaticTagsJob({ video: videoUpdated, moderation: 'apply' })+ }+await updateVideoRates(videoUpdated, this.videoObject)// Notify our users?diff --git a/server/core/lib/automatic-tags/automatic-tagger.ts b/server/core/lib/automatic-tags/automatic-tagger.tsindex 77a5958d5a..ea2c47bd58 100644--- a/server/core/lib/automatic-tags/automatic-tagger.ts+++ b/server/core/lib/automatic-tags/automatic-tagger.ts@@ -5,9 +5,8 @@ import { WEBSERVER } from '@server/initializers/constants.js'import { getServerAccount } from '@server/models/application/application.js'import { AccountAutomaticTagPolicyModel } from '@server/models/automatic-tag/account-automatic-tag-policy.js'import { WatchedWordsListModel } from '@server/models/watched-words/watched-words-list.js'-import { MAccount, MAccountId, MComment, MVideo } from '@server/types/models/index.js'+import { MAccount, MAccountId, MVideo } from '@server/types/models/index.js'import { LinkifyIt } from 'linkify-it'-import { Transaction } from 'sequelize'import { PluginManager } from '../plugins/plugin-manager.js'const logger = createLogger('automatic-tags')@@ -19,23 +18,26 @@ export class AutomaticTagger {EXTERNAL_LINK: 'external-link'}+ // Never run inside a transaction: plugin auto taggers can be slowasync buildCommentsAutomaticTags (options: {serverAccount: MAccount | nullownerAccount: MAccount | nulltext: string- transaction?: Transaction}) {- const { text, serverAccount, ownerAccount, transaction } = options+ const { text, serverAccount, ownerAccount } = options// accountId -> tagsconst result: Record<number, string[]> = {}try {- const pluginAutoTags = await this.buildPluginAutomaticTags({ video: null, comment: { text } })+ const pluginAutoTags = await this.runAutoTaggers(+ PluginManager.Instance.getCommentAutoTaggers(),+ ({ handler }) => handler({ comment: { text } })+ )if (serverAccount) {const tags = [- ...await this.buildAutomaticTags({ account: serverAccount, text, transaction }),+ ...await this.buildAutomaticTags({ account: serverAccount, text }),...pluginAutoTags]@@ -44,7 +46,7 @@ export class AutomaticTagger {if (ownerAccount) {const tags = [- ...await this.buildAutomaticTags({ account: ownerAccount, text, transaction }),+ ...await this.buildAutomaticTags({ account: ownerAccount, text }),...pluginAutoTags]@@ -61,18 +63,18 @@ export class AutomaticTagger {}}+ // See `buildCommentsAutomaticTags`: never run inside a transactionasync buildVideoAutomaticTags (options: {serverAccount: MAccount- video: Pick<MVideo, 'name' | 'description'>- transaction?: Transaction+ video: Pick<MVideo, 'id' | 'name' | 'description'>}) {- const { video, serverAccount, transaction } = options+ const { video, serverAccount } = optionstry {const [ videoNameTags, videoDescriptionTags, pluginTags ] = await Promise.all([- this.buildAutomaticTags({ account: serverAccount, text: video.name, transaction }),- this.buildAutomaticTags({ account: serverAccount, text: video.description, transaction }),- this.buildPluginAutomaticTags({ video, comment: null })+ this.buildAutomaticTags({ account: serverAccount, text: video.name }),+ this.buildAutomaticTags({ account: serverAccount, text: video.description }),+ this.runAutoTaggers(PluginManager.Instance.getVideoAutoTaggers(), ({ handler }) => handler({ video }))])logger.debug('Built automatic tags for video', {@@ -94,15 +96,14 @@ export class AutomaticTagger {private async buildAutomaticTags (options: {account: MAccounttext: string- transaction?: Transaction}) {- const { text, account, transaction } = options+ const { text, account } = optionsconst tagsDone = new Set<string>()const automaticTags: string[] = []// Watched words by account that published the video- const watchedWords = await WatchedWordsListModel.buildWatchedWordsRegexp({ accountId: account.id, transaction })+ const watchedWords = await WatchedWordsListModel.buildWatchedWordsRegexp({ accountId: account.id })logger.debug(`Got watched words regex for account ${account.id}`, {listNames: watchedWords.map(r => r.listName)@@ -131,33 +132,29 @@ export class AutomaticTagger {return automaticTags}- private async buildPluginAutomaticTags (options: {- video: Pick<MVideo, 'name' | 'description'> | null- comment: Pick<MComment, 'text'> | null- }) {- const { video, comment } = options-- const pluginTags: string[] = []-- const pluginWithAutoTags = video- ? PluginManager.Instance.getVideoAutoTaggers()- : PluginManager.Instance.getCommentAutoTaggers()-- for (const { npmName, autoTaggersPerTagName } of pluginWithAutoTags) {- for (const autoTagName of Object.keys(autoTaggersPerTagName)) {- for (const autoTagger of (autoTaggersPerTagName[autoTagName] || [])) {+ // Auto taggers of plugins can be slow (they may call an external service), so run them in parallel+ private async runAutoTaggers<T extends { autoTagNames: string[] }> (+ plugins: { npmName: string, autoTaggers: T[] }[],+ run: (autoTagger: T) => Promise<{ tags: string[] }>+ ) {+ const tagsPerAutoTagger = await Promise.all(+ plugins.flatMap(({ npmName, autoTaggers }) => {+ return autoTaggers.map(async autoTagger => {try {- const { result } = await autoTagger({ video, comment })+ const { tags } = await run(autoTagger)- if (result) pluginTags.push(autoTagName)+ // A plugin can only assign the tags it registered+ return (tags || []).filter(t => autoTagger.autoTagNames.includes(t))} catch (err) {logger.error('Cannot execute auto tagger of plugin ' + npmName, { err })++ return []}- }- }- }+ })+ })+ )- return pluginTags+ return tagsPerAutoTagger.flat()}private hasExternalLinks (text: string) {@@ -215,12 +212,7 @@ export class AutomaticTagger {? PluginManager.Instance.getVideoAutoTaggers(): PluginManager.Instance.getCommentAutoTaggers()- return toLoad- .flatMap(({ autoTaggersPerTagName }) => {- // Keys that have at least one active auto tagger function- return Object.keys(autoTaggersPerTagName)- .filter(k => (autoTaggersPerTagName[k] || []).length > 0)- })+ return uniqify(toLoad.flatMap(({ autoTaggers }) => autoTaggers.flatMap(a => a.autoTagNames))).map(name => ({ name, type: 'plugin' as const }))}}diff --git a/server/core/lib/automatic-tags/automatic-tags.ts b/server/core/lib/automatic-tags/automatic-tags.tsindex d054163c7a..631e5ae9f3 100644--- a/server/core/lib/automatic-tags/automatic-tags.ts+++ b/server/core/lib/automatic-tags/automatic-tags.ts@@ -1,4 +1,5 @@-import { AutomaticTagPolicyType } from '@peertube/peertube-models'+import { AutomaticTagPolicyType, AutomaticTagsModeration, BuildObjectAutomaticTagsPayload } from '@peertube/peertube-models'+import { afterCommitIfTransaction } from '@server/helpers/database-utils.js'import { getServerAccount } from '@server/models/application/application.js'import { AccountAutomaticTagPolicyModel } from '@server/models/automatic-tag/account-automatic-tag-policy.js'import { AutomaticTagModel } from '@server/models/automatic-tag/automatic-tag.js'@@ -9,41 +10,44 @@ import {MComment,MCommentAdminOrUserFormattable,MCommentAutomaticTagWithTag,+ MCommentId,+ MCommentVideo,MVideo,- MVideoAutomaticTagWithTag+ MVideoAutomaticTagWithTag,+ MVideoId} from '@server/types/models/index.js'import { Transaction } from 'sequelize'-import { JobQueue } from '../job-queue/job-queue.js'+import { CreateJobOptions, CreateJobTypeAndPayload, JobQueue } from '../job-queue/job-queue.js'+import { AutomaticTagger } from './automatic-tagger.js'export async function setAndSaveCommentAutomaticTags (options: {comment: MCommentautomaticTagsByAccount: Record<number, string[]>- transaction?: Transaction}) {- const { comment, automaticTagsByAccount, transaction } = options+ const { comment, automaticTagsByAccount } = optionsif (Object.keys(automaticTagsByAccount).length === 0) returnconst { toCreateItems, toDeleteItems } = await _buildAutomaticTagItems({automaticTagsByAccount,existingAutomaticTagsGetter: (accountIds: number[]) => {- return CommentAutomaticTagModel.listByAccountIdsAndCommentId({ commentId: comment.id, accountIds, transaction })+ return CommentAutomaticTagModel.listByAccountIdsAndCommentId({ commentId: comment.id, accountIds })}})for (const item of toDeleteItems) {- await item.destroy({ transaction })+ await item.destroy()}const commentAutomaticTags: MCommentAutomaticTagWithTag[] = []for (const tag of toCreateItems) {- const automaticTagInstance = await AutomaticTagModel.findOrCreateAutomaticTag({ tag: tag.name, transaction })+ const automaticTagInstance = await AutomaticTagModel.findOrCreateAutomaticTag({ tag: tag.name })const [ commentAutomaticTag ] = await CommentAutomaticTagModel.upsert({accountId: tag.accountId,automaticTagId: automaticTagInstance.id,commentId: comment.id- }, { transaction })+ })commentAutomaticTag.AutomaticTag = automaticTagInstance@@ -54,11 +58,10 @@ export async function setAndSaveCommentAutomaticTags (options: {}export async function setAndSaveVideoAutomaticTags (options: {- video: MVideo+ video: Pick<MVideo, 'id'>automaticTagsByAccount: Record<number, string[]>- transaction?: Transaction}) {- const { video, automaticTagsByAccount, transaction } = options+ const { video, automaticTagsByAccount } = optionsif (Object.keys(automaticTagsByAccount).length === 0) return@@ -66,24 +69,24 @@ export async function setAndSaveVideoAutomaticTags (options: {automaticTagsByAccount,existingAutomaticTagsGetter: accountIds => {- return VideoAutomaticTagModel.listByAccountIdsAndVideoId({ videoId: video.id, accountIds, transaction })+ return VideoAutomaticTagModel.listByAccountIdsAndVideoId({ videoId: video.id, accountIds })}})for (const item of toDeleteItems) {- await item.destroy({ transaction })+ await item.destroy()}const videoAutomaticTags: MVideoAutomaticTagWithTag[] = []for (const tag of toCreateItems) {- const automaticTagInstance = await AutomaticTagModel.findOrCreateAutomaticTag({ tag: tag.name, transaction })+ const automaticTagInstance = await AutomaticTagModel.findOrCreateAutomaticTag({ tag: tag.name })const [ videoAutomaticTag ] = await VideoAutomaticTagModel.upsert({accountId: tag.accountId,automaticTagId: automaticTagInstance.id,videoId: video.id- }, { transaction })+ })videoAutomaticTag.AutomaticTag = automaticTagInstance@@ -158,3 +161,111 @@ export async function createRebuildAutomaticTagsJob (options: {deduplicationId: `build-automatic-tags:${accountId}`})}++// ---------------------------------------------------------------------------+// Automatic tags are built by the `build-object-automatic-tags` job so plugin auto taggers never run inside a+// transaction and are allowed to take a long time (they may analyze the video files or call an external service)+// ---------------------------------------------------------------------------++export async function buildAndSaveVideoAutomaticTags (options: {+ video: Pick<MVideo, 'id' | 'name' | 'description'>+}) {+ const { video } = options++ const automaticTagsByAccount = await new AutomaticTagger().buildVideoAutomaticTags({+ serverAccount: await getServerAccount(),+ video+ })++ await setAndSaveVideoAutomaticTags({ video, automaticTagsByAccount })++ return automaticTagsByAccount+}++export async function buildAndSaveCommentAutomaticTags (options: {+ comment: MCommentVideo+ ofServerAccount?: boolean // default true+ ofOwnerAccount?: boolean // default true+}) {+ const { comment, ofServerAccount = true, ofOwnerAccount = true } = options++ const automaticTagsByAccount = await new AutomaticTagger().buildCommentsAutomaticTags({+ serverAccount: ofServerAccount+ ? await getServerAccount()+ : null,+ ownerAccount: ofOwnerAccount+ ? comment.Video.VideoChannel.Account+ : null,+ text: comment.text+ })++ await setAndSaveCommentAutomaticTags({ comment, automaticTagsByAccount })++ return automaticTagsByAccount+}++// ---------------------------------------------------------------------------++export function createVideoAutomaticTagsJob (options: {+ video: MVideoId+ moderation: AutomaticTagsModeration+ transaction?: Transaction+}) {+ const { video, moderation, transaction } = options++ createBuildObjectAutomaticTagsJob({+ payload: { objectType: 'video', objectId: video.id, moderation, notify: null },+ transaction+ })+}++// Same job, but returned instead of created so the caller can insert it in a job flow+// We can't use a deduplicated job in a job flow+export function buildNonDuplicatedVideoAutomaticTagsJob (options: {+ video: MVideoId+ moderation: AutomaticTagsModeration+}): CreateJobTypeAndPayload & CreateJobOptions {+ const { video, moderation } = options++ return {+ type: 'build-object-automatic-tags',+ payload: { objectType: 'video', objectId: video.id, moderation, notify: null }+ }+}++export function createCommentAutomaticTagsJob (options: {+ comment: MCommentId+ moderation: AutomaticTagsModeration+ notify: boolean+ transaction?: Transaction+}) {+ const { comment, moderation, notify, transaction } = options++ createBuildObjectAutomaticTagsJob({+ payload: { objectType: 'comment', objectId: comment.id, moderation, notify },+ transaction+ })+}++function createBuildObjectAutomaticTagsJob (options: {+ payload: BuildObjectAutomaticTagsPayload+ transaction?: Transaction+}) {+ const { payload, transaction } = options++ afterCommitIfTransaction(transaction, () => {+ JobQueue.Instance.createJobAsync({+ type: 'build-object-automatic-tags',+ payload,++ ...buildAutomaticTagsJobDeduplication(payload)+ })+ })+}++function buildAutomaticTagsJobDeduplication (options: Pick<BuildObjectAutomaticTagsPayload, 'objectType' | 'objectId'>) {+ return {+ deduplicationId: `build-object-automatic-tags-${options.objectType}-${options.objectId}`,+ deduplicationKeepLastIfActive: true+ }+}diff --git a/server/core/lib/job-queue/handlers/build-automatic-tags.ts b/server/core/lib/job-queue/handlers/build-automatic-tags.tsindex fc4a2a1735..0540d4816f 100644--- a/server/core/lib/job-queue/handlers/build-automatic-tags.ts+++ b/server/core/lib/job-queue/handlers/build-automatic-tags.ts@@ -1,11 +1,9 @@import { BuildAutomaticTagsPayload } from '@peertube/peertube-models'import { createLogger } from '@server/helpers/logger.js'-import { AutomaticTagger } from '@server/lib/automatic-tags/automatic-tagger.js'-import { setAndSaveCommentAutomaticTags, setAndSaveVideoAutomaticTags } from '@server/lib/automatic-tags/automatic-tags.js'+import { buildAndSaveCommentAutomaticTags, buildAndSaveVideoAutomaticTags } from '@server/lib/automatic-tags/automatic-tags.js'import { getServerAccount } from '@server/models/application/application.js'import { VideoCommentModel } from '@server/models/video/video-comment.js'import { VideoModel } from '@server/models/video/video.js'-import { MAccount } from '@server/types/models/index.js'import { Job } from 'bullmq'const logger = createLogger('job-queue')@@ -55,8 +53,6 @@ async function rebuildComments (options: {}) {const { videoOwnerId, isServerAccount } = options- const automaticTagger = new AutomaticTagger()-let rebuilt = 0let lastId = 0@@ -68,16 +64,14 @@ async function rebuildComments (options: {for (const id of ids) {const comment = await VideoCommentModel.loadByIdWithVideo(id)+ if (!comment) continue- let serverAccount: MAccount- let ownerAccount: MAccount-- if (isServerAccount) serverAccount = await getServerAccount()- else ownerAccount = comment.Video.VideoChannel.Account-- const automaticTagsByAccount = await automaticTagger.buildCommentsAutomaticTags({ text: comment.text, serverAccount, ownerAccount })-- await setAndSaveCommentAutomaticTags({ comment, automaticTagsByAccount })+ // Only the account this job runs for changed its watched words/policies: leave the tags of the other account alone+ await buildAndSaveCommentAutomaticTags({+ comment,+ ofServerAccount: isServerAccount,+ ofOwnerAccount: !isServerAccount+ })rebuilt++}@@ -87,8 +81,6 @@ async function rebuildComments (options: {}async function rebuildVideos () {- const automaticTagger = new AutomaticTagger()-let rebuilt = 0let lastId = 0@@ -100,10 +92,9 @@ async function rebuildVideos () {for (const id of ids) {const video = await VideoModel.load(id)+ if (!video) continue- const automaticTagsByAccount = await automaticTagger.buildVideoAutomaticTags({ video, serverAccount: await getServerAccount() })-- await setAndSaveVideoAutomaticTags({ video, automaticTagsByAccount })+ await buildAndSaveVideoAutomaticTags({ video })rebuilt++}diff --git a/server/core/lib/job-queue/handlers/build-object-automatic-tags.ts b/server/core/lib/job-queue/handlers/build-object-automatic-tags.tsnew file mode 100644index 0000000000..01090141bc--- /dev/null+++ b/server/core/lib/job-queue/handlers/build-object-automatic-tags.ts@@ -0,0 +1,111 @@+import { BuildObjectAutomaticTagsPayload } from '@peertube/peertube-models'+import { afterCommitIfTransaction, retryTransactionWrapper } from '@server/helpers/database-utils.js'+import { createLogger } from '@server/helpers/logger.js'+import { sequelizeTypescript } from '@server/initializers/database.js'+import { buildAndSaveCommentAutomaticTags, buildAndSaveVideoAutomaticTags } from '@server/lib/automatic-tags/automatic-tags.js'+import { Notifier } from '@server/lib/notifier/index.js'+import { autoBlacklistVideoByAutoTagPolicyIfNeeded, resolvePendingAutoTagBlacklist } from '@server/lib/video-blacklist.js'+import { approveComment, getCommentReviewDecision } from '@server/lib/video-comment.js'+import { UserModel } from '@server/models/user/user.js'+import { VideoCommentModel } from '@server/models/video/video-comment.js'+import { VideoModel } from '@server/models/video/video.js'+import { Job } from 'bullmq'++const logger = createLogger('job-queue')++// Auto taggers of plugins can be slow (they may analyze the video files or call an external service)+// Automatic tags of a video/comment are always built here and never in a transaction+export async function processBuildObjectAutomaticTags (job: Job): Promise<void> {+ const payload = job.data as BuildObjectAutomaticTagsPayload++ logger.info('Processing build automatic tags of %s %d in job %s.', payload.objectType, payload.objectId, job.id)++ if (payload.objectType === 'video') await buildOfVideo(payload)+ else await buildOfComment(payload)++ logger.info('Processed build automatic tags of %s %d in job %s.', payload.objectType, payload.objectId, job.id)+}++// ---------------------------------------------------------------------------++async function buildOfVideo (payload: BuildObjectAutomaticTagsPayload) {+ // Auto taggers only need the id/name/description: plugins load/analyze anything else (files included) themselves+ const video = await VideoModel.load(payload.objectId)+ if (!video) {+ logger.info('Do not build automatic tags of video %d that does not exist anymore.', payload.objectId)+ return+ }++ const automaticTagsByAccount = await buildAndSaveVideoAutomaticTags({ video })++ if (payload.moderation === 'none') return++ // Building the tags can take a long time: reload the video and apply the moderation decision against its current+ // state, all in a single retried transaction+ await retryTransactionWrapper(() =>+ sequelizeTypescript.transaction(async t => {+ const videoReloaded = await VideoModel.loadWithRights(payload.objectId, t)+ if (!videoReloaded) return++ if (payload.moderation === 'apply') {+ const user = await UserModel.loadByAccountId(videoReloaded.VideoChannel.accountId, t)++ await autoBlacklistVideoByAutoTagPolicyIfNeeded({ video: videoReloaded, user, automaticTagsByAccount, transaction: t })+ } else {+ await resolvePendingAutoTagBlacklist({ video: videoReloaded, automaticTagsByAccount, transaction: t })+ }+ })+ )+}++async function buildOfComment (payload: BuildObjectAutomaticTagsPayload) {+ const comment = await VideoCommentModel.loadByIdWithVideo(payload.objectId)+ if (!comment || comment.isDeleted()) {+ logger.info('Do not build automatic tags of comment %d that does not exist anymore.', payload.objectId)+ return+ }++ const automaticTagsByAccount = await buildAndSaveCommentAutomaticTags({ comment })++ // A comment is federated as soon as it is created, so it is only held while waiting for its tags+ if (payload.moderation !== 'release-hold') return++ // Building the tags can take a long time: reload the comment and apply the moderation decision against its current+ // state (a moderator may already have approved/rejected it), all in a single retried transaction+ await retryTransactionWrapper(() =>+ sequelizeTypescript.transaction(async t => {+ const commentReloaded = await VideoCommentModel.loadByIdAndPopulateVideoAndAccountAndReply(payload.objectId, t)+ if (!commentReloaded || commentReloaded.isDeleted()) return++ if (commentReloaded.heldForReview) {+ const video = commentReloaded.Video++ const { heldForReview } = await getCommentReviewDecision({+ user: commentReloaded.accountId+ ? await UserModel.loadByAccountId(commentReloaded.accountId, t)+ : null,+ video,+ automaticTagsPending: false,+ ownerAutomaticTags: automaticTagsByAccount[video.VideoChannel.accountId],+ transaction: t+ })++ if (!heldForReview) {+ // The comment was only held while waiting for its automatic tags: its author must not be notified of an approval+ await approveComment(commentReloaded, { notify: false, transaction: t })+ commentReloaded.heldForReview = false++ logger.info('Comment %d released from review: its automatic tags do not match any review policy.', commentReloaded.id)+ }+ }++ // The video owner has not been notified at creation, because the held status of the comment was not final yet+ // Notify it even if the comment is still held for review, so it can approve it (same as a comment held by+ // a `commentsPolicy: REQUIRES_APPROVAL` video, which is notified synchronously at creation)+ // `notify` is false for a comment that only exists to complete a federated thread (see `resolveThread`)+ if (payload.notify !== false) {+ afterCommitIfTransaction(t, () => Notifier.Instance.notifyOnNewComment(commentReloaded))+ }+ })+ )+}diff --git a/server/core/lib/job-queue/handlers/video-import.ts b/server/core/lib/job-queue/handlers/video-import.tsindex b66b0478af..28051d5f0f 100644--- a/server/core/lib/job-queue/handlers/video-import.ts+++ b/server/core/lib/job-queue/handlers/video-import.ts@@ -13,11 +13,13 @@ import {import { retryTransactionWrapper } from '@server/helpers/database-utils.js'import { YoutubeDLWrapper } from '@server/helpers/youtube-dl/index.js'import { CONFIG } from '@server/initializers/config.js'+import { createVideoAutomaticTagsJob } from '@server/lib/automatic-tags/automatic-tags.js'import { isPostImportVideoAccepted } from '@server/lib/moderation.js'import { Hooks } from '@server/lib/plugins/hooks.js'import { ServerConfigManager } from '@server/lib/server-config-manager.js'import { createOptimizeOrMergeAudioJobs } from '@server/lib/transcoding/create-transcoding-job.js'import { isUserQuotaValid } from '@server/lib/user.js'+import { autoBlacklistVideoIfNeeded } from '@server/lib/video-blacklist.js'import { createTranscriptionTaskIfNeeded } from '@server/lib/video-captions.js'import { replaceChaptersIfNotExist } from '@server/lib/video-chapters.js'import { buildNewFile } from '@server/lib/video-file.js'@@ -28,7 +30,7 @@ import { createTorrentForFile, downloadWebTorrentVideo } from '@server/lib/webtoimport { UserModel } from '@server/models/user/user.js'import { VideoCaptionModel } from '@server/models/video/video-caption.js'import { VideoInfohashModel } from '@server/models/video/video-infohash.js'-import { MUserId, MVideoFile, MVideoFull } from '@server/types/models/index.js'+import { MUser, MUserId, MVideoFile, MVideoFull } from '@server/types/models/index.js'import { MVideoImport, MVideoImportDefault, MVideoImportDefaultFiles, MVideoImportVideo } from '@server/types/models/video/video-import.js'import { Job } from 'bullmq'import { FfprobeData } from 'fluent-ffmpeg'@@ -300,20 +302,46 @@ async function afterImportSuccess (options: {videoImport: MVideoImportvideo: MVideoFullvideoFile: MVideoFile- user: MUserId+ user: MUsergenerateTranscription: boolean}) {const { video, videoFile, videoImport, user, generateTranscription } = options+ // The file now exists, so plugin auto taggers can analyze it+ // They can be slow, so let the `build-object-automatic-tags` job run them instead of blocking this queue+ // Hold the video until it has confirmed or released the hold: the video has not been announced yet+ // Note the video may already be blocked by the instance policy, applied by `insertFromImportIntoDB`+ const { pendingAutomaticTags } = await autoBlacklistVideoIfNeeded({+ video,+ user,+ automaticTagsPending: true,+ isRemote: false,+ isNew: true,+ isNewFile: true,+ notify: false,+ transaction: undefined+ })++ createVideoAutomaticTagsJob({+ video,+ moderation: pendingAutomaticTags+ ? 'release-hold'+ : 'apply'+ })+Notifier.Instance.notifyOnFinishedVideoImport({ videoImport: Object.assign(videoImport, { Video: video }), success: true })- if (video.isBlacklisted()) {- const videoBlacklist = Object.assign(video.VideoBlacklist, { Video: video })+ // A video held for its automatic tags is announced by the job instead, which also notifies the moderators if it+ // ends up confirming the block+ if (!pendingAutomaticTags) {+ if (video.isBlacklisted()) {+ const videoBlacklist = Object.assign(video.VideoBlacklist, { Video: video })- Notifier.Instance.notifyOnVideoAutoBlacklist(videoBlacklist)- } else {- Notifier.Instance.notifyOnNewVideoOrLiveIfNeeded(video)+ Notifier.Instance.notifyOnVideoAutoBlacklist(videoBlacklist)+ } else {+ Notifier.Instance.notifyOnNewVideoOrLiveIfNeeded(video)+ }}// Generate the storyboard in the job queue, and don't forget to federate an update afterdiff --git a/server/core/lib/job-queue/job-queue.ts b/server/core/lib/job-queue/job-queue.tsindex 5c1b00ebba..d734adcfd8 100644--- a/server/core/lib/job-queue/job-queue.ts+++ b/server/core/lib/job-queue/job-queue.ts@@ -7,6 +7,7 @@ import {ActorKeysPayload,AfterVideoChannelImportPayload,BuildAutomaticTagsPayload,+ BuildObjectAutomaticTagsPayload,CreateUserExportPayload,EmailPayload,FederateVideoPayload,@@ -59,6 +60,7 @@ import { refreshAPObject } from './handlers/activitypub-refresher.js'import { processActorKeys } from './handlers/actor-keys.js'import { processAfterVideoChannelImport } from './handlers/after-video-channel-import.js'import { processBuildAutomaticTags } from './handlers/build-automatic-tags.js'+import { processBuildObjectAutomaticTags } from './handlers/build-object-automatic-tags.js'import { processCreateUserExport } from './handlers/create-user-export.js'import { processEmail } from './handlers/email.js'import { processFederateVideo } from './handlers/federate-video.js'@@ -82,6 +84,7 @@ const logger = createLogger('job-queue')export type CreateJobTypeAndPayload =| { type: 'build-automatic-tags', payload: BuildAutomaticTagsPayload }+ | { type: 'build-object-automatic-tags', payload: BuildObjectAutomaticTagsPayload }| { type: 'activitypub-http-broadcast', payload: ActivitypubHttpBroadcastPayload }| { type: 'activitypub-http-broadcast-parallel', payload: ActivitypubHttpBroadcastPayload }| { type: 'activitypub-http-unicast', payload: ActivitypubHttpUnicastPayload }@@ -125,6 +128,7 @@ export type CreateJobOptions = {const handlers: { [id in JobType]: (job: Job, signal?: AbortSignal) => Promise<any> } = {'build-automatic-tags': processBuildAutomaticTags,+ 'build-object-automatic-tags': processBuildObjectAutomaticTags,'activitypub-cleaner': processActivityPubCleaner,'activitypub-follow': processActivityPubFollow,'activitypub-http-broadcast-parallel': processActivityPubParallelHttpBroadcast,@@ -162,6 +166,7 @@ const errorHandlers: { [id in JobType]?: (job: Job, err: any) => Promise<any> }const jobTypes: JobType[] = ['build-automatic-tags',+ 'build-object-automatic-tags','activitypub-cleaner','activitypub-follow','activitypub-http-broadcast-parallel',diff --git a/server/core/lib/local-video-creator.ts b/server/core/lib/local-video-creator.tsindex 912e5faa10..b8bf9291b5 100644--- a/server/core/lib/local-video-creator.ts+++ b/server/core/lib/local-video-creator.ts@@ -17,7 +17,6 @@ import { retryTransactionWrapper } from '@server/helpers/database-utils.js'import { createLogger } from '@server/helpers/logger.js'import { CONFIG } from '@server/initializers/config.js'import { sequelizeTypescript } from '@server/initializers/database.js'-import { getServerAccount } from '@server/models/application/application.js'import { ScheduleVideoUpdateModel } from '@server/models/video/schedule-video-update.js'import { VideoChannelActivityModel } from '@server/models/video/video-channel-activity.js'import { VideoLiveReplaySettingModel } from '@server/models/video/video-live-replay-setting.js'@@ -31,8 +30,7 @@ import { FfprobeData } from 'fluent-ffmpeg'import { move } from 'fs-extra/esm'import { getLocalVideoActivityPubUrl } from './activitypub/url.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 { createVideoAutomaticTagsJob } from './automatic-tags/automatic-tags.js'import { Hooks } from './plugins/hooks.js'import { createLocalVideoThumbnailsFromImage, createLocalVideoThumbnailsFromVideo } from './thumbnail.js'import { autoBlacklistVideoIfNeeded } from './video-blacklist.js'@@ -179,13 +177,6 @@ export class LocalVideoCreator {await setVideoTags({ video: this.video, tags: this.videoAttributes.tags, transaction })- const automaticTagsByAccount = await new AutomaticTagger().buildVideoAutomaticTags({- serverAccount: await getServerAccount(),- video: this.video,- transaction- })- await setAndSaveVideoAutomaticTags({ video: this.video, automaticTagsByAccount, transaction })-// Schedule an update in the future?if (this.videoAttributes.scheduleUpdate) {await ScheduleVideoUpdateModel.create({@@ -203,16 +194,30 @@ export class LocalVideoCreator {}}- await autoBlacklistVideoIfNeeded({+ // A video with a file has its automatic tags built by `addVideoJobsAfterCreation`, before it is announced.+ // A video without one (live) is federated right away, so hold it until the tagging job has run+ const holdForAutomaticTags = !this.videoFile++ const { pendingAutomaticTags } = await autoBlacklistVideoIfNeeded({video: this.video,user: this.options.user,- automaticTagsByAccount,+ automaticTagsPending: holdForAutomaticTags,isRemote: false,isNew: true,isNewFile: true,transaction})+ if (holdForAutomaticTags) {+ createVideoAutomaticTagsJob({+ video: this.video,+ moderation: pendingAutomaticTags+ ? 'release-hold'+ : 'apply',+ transaction+ })+ }+if (this.videoAttributes.privacy === VideoPrivacy.PASSWORD_PROTECTED) {await VideoPasswordModel.addPasswords(this.videoAttributes.videoPasswords, this.video.id, transaction)}diff --git a/server/core/lib/local-video-updater.ts b/server/core/lib/local-video-updater.tsindex aaf4e0e84f..9a04c83de9 100644--- a/server/core/lib/local-video-updater.ts+++ b/server/core/lib/local-video-updater.ts@@ -10,7 +10,6 @@ import {import { auditLoggerFactory, getAuditIdFromUser, VideoAuditView } from '@server/helpers/audit-logger.js'import { createLogger } from '@server/helpers/logger.js'import { sequelizeTypescript } from '@server/initializers/database.js'-import { getServerAccount } from '@server/models/application/application.js'import { ScheduleVideoUpdateModel } from '@server/models/video/schedule-video-update.js'import { VideoChannelActivityModel } from '@server/models/video/video-channel-activity.js'import { VideoPasswordModel } from '@server/models/video/video-password.js'@@ -21,8 +20,7 @@ import { Transaction } from 'sequelize'import { sendDeleteVideo } from './activitypub/send/send-delete.js'import { changeVideoChannelShare } from './activitypub/share.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 { createVideoAutomaticTagsJob } from './automatic-tags/automatic-tags.js'import { CreateJobTypeAndPayload, JobQueue } from './job-queue/job-queue.js'import { autoBlacklistVideoIfNeeded } from './video-blacklist.js'import { replaceChaptersFromDescriptionIfNeeded } from './video-chapters.js'@@ -101,6 +99,8 @@ export class LocalVideoUpdater {video.sitemapContentUpdatedAt = new Date()}+ const rebuildAutomaticTags = oldName !== video.name || oldDescription !== video.description+if (video.nsfw !== true) {video.nsfwFlags = NSFWFlag.NONEvideo.nsfwSummary = null@@ -201,26 +201,22 @@ export class LocalVideoUpdater {})}- 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 })- }-+ // The video is already published: don't hold it while its automatic tags are rebuilt, or a simple metadata+ // edit would block it and re-announce it to the subscribers once releasedawait autoBlacklistVideoIfNeeded({video,user: this.user,isRemote: false,isNew: false,isNewFile: false,- automaticTagsByAccount,+ automaticTagsPending: false,transaction: t})+ if (rebuildAutomaticTags) {+ createVideoAutomaticTagsJob({ video, moderation: 'apply', transaction: t })+ }+if (this.user) {auditLogger.update(getAuditIdFromUser(this.user),diff --git a/server/core/lib/plugins/plugin-manager.ts b/server/core/lib/plugins/plugin-manager.tsindex faf95c28b8..b5d3aea4bf 100644--- a/server/core/lib/plugins/plugin-manager.ts+++ b/server/core/lib/plugins/plugin-manager.ts@@ -193,9 +193,9 @@ export class PluginManager implements ServerHook {npmName: p.npmName,name: p.name,version: p.version,- autoTaggersPerTagName: p.registerHelpers.getVideoAutoTagger()+ autoTaggers: p.registerHelpers.getVideoAutoTaggers()}))- .filter(p => Object.keys(p.autoTaggersPerTagName).length !== 0)+ .filter(p => p.autoTaggers.length !== 0)}getCommentAutoTaggers () {@@ -204,9 +204,9 @@ export class PluginManager implements ServerHook {npmName: p.npmName,name: p.name,version: p.version,- autoTaggersPerTagName: p.registerHelpers.getCommentAutoTagger()+ autoTaggers: p.registerHelpers.getCommentAutoTaggers()}))- .filter(p => Object.keys(p.autoTaggersPerTagName).length !== 0)+ .filter(p => p.autoTaggers.length !== 0)}// ---------------------------------------------------------------------------diff --git a/server/core/lib/plugins/register-helpers.ts b/server/core/lib/plugins/register-helpers.tsindex c2c25df911..84f4aa672c 100644--- a/server/core/lib/plugins/register-helpers.ts+++ b/server/core/lib/plugins/register-helpers.ts@@ -51,8 +51,8 @@ export class RegisterHelpers {private idAndPassAuths: RegisterServerAuthPassOptions[] = []private externalAuths: RegisterServerAuthExternalOptions[] = []- private readonly commentAutoTagger: Record<string, RegisterCommentAutoTaggerOptions['handler'][]> = {}- private readonly videoAutoTagger: Record<string, RegisterVideoAutoTaggerOptions['handler'][]> = {}+ private commentAutoTaggers: RegisterCommentAutoTaggerOptions[] = []+ private videoAutoTaggers: RegisterVideoAutoTaggerOptions[] = []private readonly onSettingsChangeCallbacks: SettingsChangeCallback[] = []@@ -198,12 +198,12 @@ export class RegisterHelpers {// ---------------------------------------------------------------------------- getCommentAutoTagger () {- return this.commentAutoTagger+ getCommentAutoTaggers () {+ return this.commentAutoTaggers}- getVideoAutoTagger () {- return this.videoAutoTagger+ getVideoAutoTaggers () {+ return this.videoAutoTaggers}// ---------------------------------------------------------------------------@@ -363,33 +363,41 @@ export class RegisterHelpers {// ---------------------------------------------------------------------------+ private isAutoTaggerValid (options: RegisterCommentAutoTaggerOptions | RegisterVideoAutoTaggerOptions) {+ if (!Array.isArray(options?.autoTagNames) || options.autoTagNames.length === 0 || typeof options.handler !== 'function') {+ logger.error('Cannot register auto tagger of plugin %s: autoTagNames or handler are not valid.', this.npmName, { options })++ return false+ }++ return true+ }+private buildRegisterCommentAutoTagger () {return (options: RegisterCommentAutoTaggerOptions) => {- if (!this.commentAutoTagger[options.autoTagName]) this.commentAutoTagger[options.autoTagName] = []+ if (!this.isAutoTaggerValid(options)) return- this.commentAutoTagger[options.autoTagName].push(options.handler)+ this.commentAutoTaggers.push(options)}}private buildRegisterVideoAutoTagger () {return (options: RegisterVideoAutoTaggerOptions) => {- if (!this.videoAutoTagger[options.autoTagName]) this.videoAutoTagger[options.autoTagName] = []+ if (!this.isAutoTaggerValid(options)) return- this.videoAutoTagger[options.autoTagName].push(options.handler)+ this.videoAutoTaggers.push(options)}}private buildUnregisterCommentAutoTagger () {return (options: RegisterCommentAutoTaggerOptions) => {- this.commentAutoTagger[options.autoTagName] = (this.commentAutoTagger[options.autoTagName] || [])- .filter(f => f !== options.handler)+ this.commentAutoTaggers = this.commentAutoTaggers.filter(a => a.handler !== options.handler)}}private buildUnregisterVideoAutoTagger () {return (options: RegisterVideoAutoTaggerOptions) => {- this.videoAutoTagger[options.autoTagName] = (this.videoAutoTagger[options.autoTagName] || [])- .filter(f => f !== options.handler)+ this.videoAutoTaggers = this.videoAutoTaggers.filter(a => a.handler !== options.handler)}}}diff --git a/server/core/lib/video-blacklist.ts b/server/core/lib/video-blacklist.tsindex 795d741286..c3e9a101a0 100644--- a/server/core/lib/video-blacklist.ts+++ b/server/core/lib/video-blacklist.ts@@ -38,15 +38,19 @@ export async function autoBlacklistVideoIfNeeded (parameters: {isRemote: booleanisNew: booleanisNewFile: boolean- automaticTagsByAccount: Record<number, string[]>++ // The automatic tags of the video have not been built yet: we don't know which tags it will have+ automaticTagsPending: boolean+ automaticTagsByAccount?: Record<number, string[]>+user?: MUsernotify?: booleantransaction?: Transaction}) {- const { video, user, isRemote, isNew, isNewFile, automaticTagsByAccount, notify = true, transaction } = parameters+ const { video, user, isRemote, isNew, isNewFile, automaticTagsPending, automaticTagsByAccount, notify = true, transaction } = parameters// Already blacklisted- if (video.VideoBlacklist) return false+ if (video.VideoBlacklist) return { blacklisted: true, pendingAutomaticTags: false }const doAutoBlacklistByInstancePolicy = await Hooks.wrapFun(_autoBlacklistByInstancePolicyNeeded,@@ -57,16 +61,81 @@ export async function autoBlacklistVideoIfNeeded (parameters: {if (doAutoBlacklistByInstancePolicy) {await _autoBlacklist({ video, notify, user, transaction, type: VideoBlacklistType.AUTO_BY_INSTANCE_POLICY })- return true+ return { blacklisted: true, pendingAutomaticTags: false }}- if (await _autoBlacklistByAutoTagPolicyNeeded({ video, user, automaticTagsByAccount, transaction })) {- await _autoBlacklist({ video, notify, user, transaction, type: VideoBlacklistType.AUTO_BY_AUTO_TAG_POLICY })+ if (await _autoBlacklistByAutoTagPolicyNeeded({ video, user, automaticTagsPending, automaticTagsByAccount, transaction })) {+ // We don't know yet if the video really matches a policy+ // So don't notify the moderators before the `build-object-automatic-tags` job has confirmed it+ await _autoBlacklist({+ video,+ notify: notify && !automaticTagsPending,+ user,+ transaction,+ type: VideoBlacklistType.AUTO_BY_AUTO_TAG_POLICY+ })++ return { blacklisted: true, pendingAutomaticTags: automaticTagsPending }+ }++ return { blacklisted: false, pendingAutomaticTags: false }+}- return true+// Called by the `build-object-automatic-tags` job for a video that was not put on hold+// Apply the auto tag block policies using the tags the job has just built+export async function autoBlacklistVideoByAutoTagPolicyIfNeeded (options: {+ video: MVideoWithRights+ automaticTagsByAccount: Record<number, string[]>+ user?: MUser+ notify?: boolean+ transaction?: Transaction+}) {+ const { video, automaticTagsByAccount, user, notify = true, transaction } = options++ if (video.VideoBlacklist) return false+ if (!await _autoBlacklistByAutoTagPolicyNeeded({ video, user, automaticTagsPending: false, automaticTagsByAccount, transaction })) {+ return false}- return false+ await _autoBlacklist({+ video,+ notify,+ user,+ transaction,+ type: VideoBlacklistType.AUTO_BY_AUTO_TAG_POLICY+ })++ logger.info('Video %s auto-blocked by its automatic tags.', video.uuid)++ return true+}++// Called by the `build-object-automatic-tags` job for a video that has been auto blocked while waiting for its tags+// Confirm the block and notify the moderators, or remove it+export async function resolvePendingAutoTagBlacklist (options: {+ video: MVideoWithRights+ automaticTagsByAccount: Record<number, string[]>+ transaction?: Transaction+}) {+ const { video, automaticTagsByAccount, transaction } = options++ const videoBlacklist = await VideoBlacklistModel.loadByVideoId(video.id, transaction)+ if (!videoBlacklist || videoBlacklist.type !== VideoBlacklistType.AUTO_BY_AUTO_TAG_POLICY) return++ if (await _autoBlacklistByAutoTagPolicyNeeded({ video, automaticTagsPending: false, automaticTagsByAccount, transaction })) {+ const videoBlacklistWithVideo = videoBlacklist as MVideoBlacklistVideo+ videoBlacklistWithVideo.Video = video++ afterCommitIfTransaction(transaction, () => Notifier.Instance.notifyOnVideoAutoBlacklist(videoBlacklistWithVideo))++ logger.info('Video %s auto-blocking confirmed by its automatic tags.', video.uuid)+ return+ }++ await _removeBlacklist(videoBlacklist, video, transaction)+ await _releasePendingAutoTagHold(video, transaction)++ logger.info('Removed auto-blocking of video %s: its automatic tags do not match any auto block policy.', video.uuid)}async function _autoBlacklist (options: {@@ -121,24 +190,36 @@ function _autoBlacklistByInstancePolicyNeeded (parameters: {async function _autoBlacklistByAutoTagPolicyNeeded (options: {video: MVideoWithBlacklistLight- transaction: Transaction- automaticTagsByAccount: Record<number, string[]>+ automaticTagsPending: boolean+ automaticTagsByAccount?: Record<number, string[]>+ transaction?: Transactionuser?: MUser}) {- const { user, video, transaction, automaticTagsByAccount } = options+ const { user, video, transaction, automaticTagsPending, automaticTagsByAccount } = options- if (!automaticTagsByAccount || Object.keys(automaticTagsByAccount).length === 0) return false+ if (!automaticTagsPending && !automaticTagsByAccount) return falseif (video.isLocal() && user?.hasRight(UserRight.MANAGE_VIDEO_BLACKLIST)) return falseconst accountId = (await getServerAccount()).id- const tags = automaticTagsByAccount[accountId]++ // Block the video if the instance could want to review it+ // Let the `build-object-automatic-tags` job release it if the tags it ends up with don't match any of its auto block policies+ if (automaticTagsPending) {+ return AccountAutomaticTagPolicyModel.hasPolicy({+ accountId,+ policy: AutomaticTagPolicy.AUTO_BLACKLIST_VIDEO,+ transaction+ })+ }++ const tags = automaticTagsByAccount?.[accountId]if (!tags || tags.length === 0) return falsereturn AccountAutomaticTagPolicyModel.hasPolicyOnTags({accountId,policy: AutomaticTagPolicy.AUTO_BLACKLIST_VIDEO,- tags: automaticTagsByAccount[accountId],+ tags,transaction})}@@ -168,7 +249,17 @@ export async function blacklistVideo (videoInstance: MVideoAccountLight, options}export async function unblacklistVideo (videoBlacklist: MVideoBlacklist, video: MVideoWithRights) {- const videoBlacklistType = await sequelizeTypescript.transaction(async t => {+ const videoBlacklistType = await _removeBlacklist(videoBlacklist, video)++ Notifier.Instance.notifyOnVideoUnblacklist(video)++ if (videoBlacklistType === VideoBlacklistType.AUTO_BY_INSTANCE_POLICY) {+ await _notifyVideoReleasedFromAutoBlacklist(video)+ }+}++function _removeBlacklist (videoBlacklist: MVideoBlacklist, video: MVideoWithRights, transaction?: Transaction) {+ const run = async (t: Transaction) => {const unfederated = videoBlacklist.unfederatedconst videoBlacklistType = videoBlacklist.type@@ -181,18 +272,35 @@ export async function unblacklistVideo (videoBlacklist: MVideoBlacklist, video:}return videoBlacklistType- })+ }- Notifier.Instance.notifyOnVideoUnblacklist(video)+ if (transaction) return run(transaction)- if (videoBlacklistType === VideoBlacklistType.AUTO_BY_INSTANCE_POLICY) {- const videoWithSchedule = video as MVideoWithRights & MVideoWithSchedule- videoWithSchedule.ScheduleVideoUpdate = await videoWithSchedule.$get('ScheduleVideoUpdate')+ return sequelizeTypescript.transaction(run)+}++async function _notifyVideoReleasedFromAutoBlacklist (video: MVideoWithRights, transaction?: Transaction) {+ const videoWithSchedule = video as MVideoWithRights & MVideoWithSchedule+ videoWithSchedule.ScheduleVideoUpdate = await videoWithSchedule.$get('ScheduleVideoUpdate', { transaction })++ // Delete on object so new video notifications will send+ delete video.VideoBlacklist+ afterCommitIfTransaction(transaction, () => {Notifier.Instance.notifyOnVideoPublishedAfterRemovedFromAutoBlacklist(videoWithSchedule)+ Notifier.Instance.notifyOnNewVideoOrLiveIfNeeded(videoWithSchedule)+ })+}++// Same as _notifyVideoReleasedFromAutoBlacklist but do not send "removed from auto blacklist" notification+async function _releasePendingAutoTagHold (video: MVideoWithRights, transaction?: Transaction) {+ const videoWithSchedule = video as MVideoWithRights & MVideoWithSchedule+ videoWithSchedule.ScheduleVideoUpdate = await videoWithSchedule.$get('ScheduleVideoUpdate', { transaction })- // Delete on object so new video notifications will send- delete video.VideoBlacklist+ // Delete on object so new video notifications will send+ delete video.VideoBlacklist++ afterCommitIfTransaction(transaction, () => {Notifier.Instance.notifyOnNewVideoOrLiveIfNeeded(videoWithSchedule)- }+ })}diff --git a/server/core/lib/video-comment.ts b/server/core/lib/video-comment.tsindex e85dfa7fcd..7abc37fedf 100644--- a/server/core/lib/video-comment.ts+++ b/server/core/lib/video-comment.ts@@ -3,7 +3,6 @@ import { afterCommitIfTransaction } from '@server/helpers/database-utils.js'import { createLogger } from '@server/helpers/logger.js'import { sequelizeTypescript } from '@server/initializers/database.js'import { AccountModel } from '@server/models/account/account.js'-import { getServerAccount } from '@server/models/application/application.js'import { AccountAutomaticTagPolicyModel } from '@server/models/automatic-tag/account-automatic-tag-policy.js'import express from 'express'import cloneDeep from 'lodash-es/cloneDeep.js'@@ -15,12 +14,12 @@ import {MCommentOwnerVideo,MCommentOwnerVideoReply,MUserAccountId,+ MVideoAccountIdUrl,MVideoAccountLight} from '../types/models/index.js'import { sendCreateVideoCommentIfNeeded, sendDeleteVideoComment, sendReplyApproval } from './activitypub/send/index.js'import { getLocalVideoCommentActivityPubUrl } from './activitypub/url.js'-import { AutomaticTagger } from './automatic-tags/automatic-tagger.js'-import { setAndSaveCommentAutomaticTags } from './automatic-tags/automatic-tags.js'+import { createCommentAutomaticTagsJob } from './automatic-tags/automatic-tags.js'import { Notifier } from './notifier/notifier.js'import { Hooks } from './plugins/hooks.js'@@ -48,8 +47,10 @@ export async function removeComment (commentArg: MComment, req: express.Request,Hooks.runAction('action:api.video-comment.deleted', { comment: videoCommentInstanceBefore, req, res })}-export async function approveComment (commentArg: MComment) {- await sequelizeTypescript.transaction(async t => {+export async function approveComment (commentArg: MComment, options: { notify?: boolean, transaction?: Transaction } = {}) {+ const { notify = true, transaction } = options++ const run = async (t: Transaction) => {const comment = await VideoCommentModel.loadByIdAndPopulateVideoAndAccountAndReply(commentArg.id, t)const oldHeldForReview = comment.heldForReview@@ -63,12 +64,15 @@ export async function approveComment (commentArg: MComment) {afterCommitIfTransaction(t, () => sendReplyApproval(comment, 'ApproveReply'))}- if (oldHeldForReview !== comment.heldForReview) {+ if (notify && oldHeldForReview !== comment.heldForReview) {afterCommitIfTransaction(t, () => Notifier.Instance.notifyOnNewCommentApproval(comment))}logger.info('Video comment %d approved.', comment.id)- })+ }++ if (transaction) await run(transaction)+ else await sequelizeTypescript.transaction(run)}export async function createLocalVideoComment (options: {@@ -90,17 +94,10 @@ export async function createLocalVideoComment (options: {return sequelizeTypescript.transaction(async transaction => {const account = await AccountModel.load(user.Account.id, transaction)- const automaticTagsByAccount = await new AutomaticTagger().buildCommentsAutomaticTags({- serverAccount: await getServerAccount(),- ownerAccount: video.VideoChannel.Account,- text,- transaction- })-- const heldForReview = await shouldCommentBeHeldForReview({+ const { heldForReview, pendingAutomaticTags } = await getCommentReviewDecision({user,video,- ownerAutomaticTags: automaticTagsByAccount[video.VideoChannel.accountId],+ automaticTagsPending: true,transaction})@@ -118,7 +115,14 @@ export async function createLocalVideoComment (options: {const savedComment: MCommentOwnerVideoReply = await comment.save({ transaction })- await setAndSaveCommentAutomaticTags({ comment: savedComment, automaticTagsByAccount, transaction })+ createCommentAutomaticTagsJob({+ comment: savedComment,+ moderation: pendingAutomaticTags+ ? 'release-hold'+ : 'none',+ notify: true,+ transaction+ })savedComment.InReplyToVideoComment = inReplyToCommentsavedComment.Video = video@@ -126,7 +130,7 @@ export async function createLocalVideoComment (options: {await sendCreateVideoCommentIfNeeded(savedComment, transaction)- return savedComment+ return { comment: savedComment, pendingAutomaticTags }})}@@ -182,28 +186,59 @@ export function buildFormattedCommentTree (options: {}}-export async function shouldCommentBeHeldForReview (options: {+export type CommentReviewDecision = {+ heldForReview: boolean++ // The comment is only held because its automatic tags are not built yet+ // The `build-object-automatic-tags` job confirms or releases that hold, and is in charge of notifying the video owner of the new comment+ pendingAutomaticTags: boolean+}++export async function getCommentReviewDecision (options: {user: MUserAccountId- video: MVideoAccountLight- ownerAutomaticTags: string[]+ video: MVideoAccountIdUrl++ // The automatic tags of the comment have not been built yet: we don't know which tags it will have+ automaticTagsPending: boolean+ ownerAutomaticTags?: string[]+transaction?: Transaction-}) {- const { user, video, transaction, ownerAutomaticTags } = options+}): Promise<CommentReviewDecision> {+ const { user, video, transaction, automaticTagsPending, ownerAutomaticTags } = options++ const notHeld = { heldForReview: false, pendingAutomaticTags: false }if (video.isLocal() && user) {- if (user.hasRight(UserRight.MANAGE_ANY_VIDEO_COMMENT)) return false- if (user.Account.id === video.VideoChannel.accountId) return false+ if (user.hasRight(UserRight.MANAGE_ANY_VIDEO_COMMENT)) return notHeld+ if (user.Account.id === video.VideoChannel.accountId) return notHeld}- if (video.commentsPolicy === VideoCommentPolicy.REQUIRES_APPROVAL) return true- if (video.isLocal() !== true) return false+ if (video.commentsPolicy === VideoCommentPolicy.REQUIRES_APPROVAL) {+ return { heldForReview: true, pendingAutomaticTags: false }+ }- if (!ownerAutomaticTags || ownerAutomaticTags.length === 0) return false+ if (video.isLocal() !== true) return notHeld- return AccountAutomaticTagPolicyModel.hasPolicyOnTags({+ // Hold the comment if the account could want to review it+ // Let the `build-object-automatic-tags` job release it if the tags it ends up with don't match any of its review policies+ if (automaticTagsPending) {+ const hasPolicy = await AccountAutomaticTagPolicyModel.hasPolicy({+ accountId: video.VideoChannel.accountId,+ policy: AutomaticTagPolicy.REVIEW_COMMENT,+ transaction+ })++ return { heldForReview: hasPolicy, pendingAutomaticTags: hasPolicy }+ }++ if (!ownerAutomaticTags || ownerAutomaticTags.length === 0) return notHeld++ const heldForReview = await AccountAutomaticTagPolicyModel.hasPolicyOnTags({accountId: video.VideoChannel.accountId,policy: AutomaticTagPolicy.REVIEW_COMMENT,tags: ownerAutomaticTags,transaction})++ return { heldForReview, pendingAutomaticTags: false }}diff --git a/server/core/lib/video-jobs.ts b/server/core/lib/video-jobs.tsindex 497b0b3e0e..f04ab7783f 100644--- a/server/core/lib/video-jobs.ts+++ b/server/core/lib/video-jobs.ts@@ -4,6 +4,7 @@ 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 { buildNonDuplicatedVideoAutomaticTagsJob } from './automatic-tags/automatic-tags.js'import { CreateJobOptions, CreateJobTypeAndPayload, JobQueue } from './job-queue/job-queue.js'import { VideoStoryboardJobHandler } from './runners/index.js'import { createTranscriptionTaskIfNeeded } from './video-captions.js'@@ -104,6 +105,10 @@ export async function addVideoJobsAfterCreation (options: {await buildLocalStoryboardJobIfNeeded({ video, federate: false }),+ // Before the notify/federate jobs so an auto tag block policy is applied before the video is announced+ // And before the transcoding jobs so a plugin auto tagger analyzes the input file while it is still untouched+ buildNonDuplicatedVideoAutomaticTagsJob({ video, moderation: 'apply' }),+{type: 'notify',payload: {diff --git a/server/core/lib/video-pre-import.ts b/server/core/lib/video-pre-import.tsindex 9029bc24d7..111e1d4fa7 100644--- a/server/core/lib/video-pre-import.ts+++ b/server/core/lib/video-pre-import.ts@@ -20,7 +20,6 @@ import { Hooks } from '@server/lib/plugins/hooks.js'import { ServerConfigManager } from '@server/lib/server-config-manager.js'import { autoBlacklistVideoIfNeeded } from '@server/lib/video-blacklist.js'import { setVideoTags } from '@server/lib/video.js'-import { getServerAccount } from '@server/models/application/application.js'import { VideoChannelActivityModel } from '@server/models/video/video-channel-activity.js'import { VideoImportModel } from '@server/models/video/video-import.js'import { VideoPasswordModel } from '@server/models/video/video-password.js'@@ -41,8 +40,6 @@ import {import express from 'express'import { remove } from 'fs-extra/esm'import { getLocalVideoActivityPubUrl } from './activitypub/url.js'-import { AutomaticTagger } from './automatic-tags/automatic-tagger.js'-import { setAndSaveVideoAutomaticTags } from './automatic-tags/automatic-tags.js'import { createLocalVideoThumbnailsFromImage, createLocalVideoThumbnailsFromUrl } from './thumbnail.js'import { createLocalCaption } from './video-captions.js'import { replaceChapters, replaceChaptersFromDescriptionIfNeeded } from './video-chapters.js'@@ -78,16 +75,11 @@ export async function insertFromImportIntoDB (parameters: {await VideoPasswordModel.addPasswords(videoPasswords, video.id, t)}- const automaticTagsByAccount = await new AutomaticTagger().buildVideoAutomaticTags({- serverAccount: await getServerAccount(),- video,- transaction: t- })- await setAndSaveVideoAutomaticTags({ video, automaticTagsByAccount, transaction: t })-+ // The automatic tags of the video are built once its file has been downloaded, so plugin auto taggers can analyze it+ // The video stays in the `TO_IMPORT` state until then, so it is neither federated nor notifiedawait autoBlacklistVideoIfNeeded({video: videoCreated,- automaticTagsByAccount,+ automaticTagsPending: false,user,notify: false,isRemote: false,diff --git a/server/core/models/automatic-tag/account-automatic-tag-policy.ts b/server/core/models/automatic-tag/account-automatic-tag-policy.tsindex dcb5b33eef..32d015fe7d 100644--- a/server/core/models/automatic-tag/account-automatic-tag-policy.ts+++ b/server/core/models/automatic-tag/account-automatic-tag-policy.ts@@ -77,6 +77,21 @@ export class AccountAutomaticTagPolicyModel extends SequelizeModel<AccountAutoma})}+ // Whether the account has at least one policy of this type, whatever the tag+ static hasPolicy (options: {+ accountId: number+ policy: AutomaticTagPolicyType+ transaction?: Transaction+ }) {+ const { accountId, policy, transaction } = options++ const query = `SELECT 1 FROM "accountAutomaticTagPolicy" ` ++ `WHERE "accountId" = $accountId AND "policy" = $policy ` ++ `LIMIT 1`++ return doesExist({ sequelize: this.sequelize, query, bind: { accountId, policy }, transaction })+ }+static hasPolicyOnTags (options: {accountId: numbertags: string[]diff --git a/server/core/models/user/user.ts b/server/core/models/user/user.tsindex 71de459d1b..b2aec3991f 100644--- a/server/core/models/user/user.ts+++ b/server/core/models/user/user.ts@@ -28,7 +28,7 @@ import {MUserNotifSettingChannelDefault,MUserWithNotificationSetting} from '@server/types/models/index.js'-import { col, FindOptions, fn, literal, Op, QueryTypes, ScopeOptions, where, WhereOptions } from 'sequelize'+import { col, FindOptions, fn, literal, Op, QueryTypes, ScopeOptions, Transaction, where, WhereOptions } from 'sequelize'import {AfterDestroy,AfterUpdate,@@ -859,7 +859,7 @@ export class UserModel extends SequelizeModel<UserModel> {})}- static loadByAccountId (accountId: number): Promise<MUserDefault> {+ static loadByAccountId (accountId: number, transaction?: Transaction): Promise<MUserDefault> {const query = {include: [{@@ -869,7 +869,8 @@ export class UserModel extends SequelizeModel<UserModel> {id: accountId}}- ]+ ],+ transaction}return UserModel.findOne(query)diff --git a/server/core/models/video/video-blacklist.ts b/server/core/models/video/video-blacklist.tsindex e0fa4bddb9..b1f619649d 100644--- a/server/core/models/video/video-blacklist.ts+++ b/server/core/models/video/video-blacklist.ts@@ -1,6 +1,6 @@import { VideoBlacklist, type VideoBlacklistType_Type } from '@peertube/peertube-models'import { MVideoBlacklist, MVideoBlacklistFormattable } from '@server/types/models/index.js'-import { FindOptions } from 'sequelize'+import { FindOptions, Transaction } from 'sequelize'import { AllowNull, BelongsTo, Column, CreatedAt, DataType, Default, ForeignKey, Is, Table, UpdatedAt } from 'sequelize-typescript'import {isVideoBlacklistReasonValid,@@ -116,11 +116,12 @@ export class VideoBlacklistModel extends SequelizeModel<VideoBlacklistModel> {})}- static loadByVideoId (id: number): Promise<MVideoBlacklist> {+ static loadByVideoId (id: number, transaction?: Transaction): Promise<MVideoBlacklist> {const query = {where: {videoId: id- }+ },+ transaction}return VideoBlacklistModel.findOne(query)diff --git a/server/core/models/watched-words/watched-words-list.ts b/server/core/models/watched-words/watched-words-list.tsindex b8b0057e10..bab26bb3dd 100644--- a/server/core/models/watched-words/watched-words-list.ts+++ b/server/core/models/watched-words/watched-words-list.ts@@ -182,19 +182,18 @@ export class WatchedWordsListModel extends SequelizeModel<WatchedWordsListModel>// ---------------------------------------------------------------------------+ // Automatic tags are never built inside a transaction, so this doesn't take onestatic async buildWatchedWordsRegexp (options: {accountId: number- transaction: Transaction}) {- const { accountId, transaction } = options+ const { accountId } = optionsif (WatchedWordsListModel.regexCache.has(accountId)) {return WatchedWordsListModel.regexCache.get(accountId)}const models = await WatchedWordsListModel.findAll<MWatchedWordsList>({- where: { accountId },- transaction+ where: { accountId }})const result = models.map(m => ({ listName: m.listName, regex: wordsToRegExp(m.words) }))diff --git a/server/core/types/plugins/register-auto-tagger.model.ts b/server/core/types/plugins/register-auto-tagger.model.tsindex 23cc908a64..7db24789fc 100644--- a/server/core/types/plugins/register-auto-tagger.model.ts+++ b/server/core/types/plugins/register-auto-tagger.model.ts@@ -1,14 +1,21 @@import { MComment } from '../models/video/video-comment.js'import { MVideo } from '../models/video/video.js'+// Auto taggers are executed by the `build-object-automatic-tags` job, outside of any transaction: they are allowed to+// take a long time to return their result, for example to analyze the video files or to call an external service+// The handler can only assign tags it declared in `autoTagNames`, other returned tags are ignored+export type RegisterCommentAutoTaggerOptions = {- autoTagName: string+ autoTagNames: string[]- handler: (options: { comment: Pick<MComment, 'text'> }) => Promise<{ result: boolean }>+ handler: (options: { comment: Pick<MComment, 'text'> }) => Promise<{ tags: string[] }>}export type RegisterVideoAutoTaggerOptions = {- autoTagName: string+ autoTagNames: string[]- handler: (options: { video: Pick<MVideo, 'name' | 'description'> }) => Promise<{ result: boolean }>+ // Only the video id/name/description are provided: use `peertubeHelpers.videos.getFiles(video.id)` and+ // `peertubeHelpers.videos.ffprobe(path)` to load and analyze its files if needed+ // A live video has no file yet when its auto taggers run+ handler: (options: { video: Pick<MVideo, 'id' | 'name' | 'description'> }) => Promise<{ tags: string[] }>}diff --git a/support/doc/api/openapi.yaml b/support/doc/api/openapi.yamlindex 9e9832a8b7..4ce9eea631 100644--- a/support/doc/api/openapi.yaml+++ b/support/doc/api/openapi.yaml@@ -12466,6 +12466,7 @@ components:- actor-keys- after-video-channel-import- build-automatic-tags+ - build-object-automatic-tags- create-user-export- federate-video