Skip to content

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

Add ability for plugins to set auto tags

Chocobozzz86b0319f2b29
diff --git a/client/src/app/shared/shared-moderation/automatic-tag.service.ts b/client/src/app/shared/shared-moderation/automatic-tag.service.ts
index afdbdf4608..42eb2ef348 100644
--- a/client/src/app/shared/shared-moderation/automatic-tag.service.ts
+++ b/client/src/app/shared/shared-moderation/automatic-tag.service.ts
@@ -1,7 +1,12 @@
import { HttpClient } from '@angular/common/http'
import { Injectable, inject } from '@angular/core'
import { RestExtractor } from '@app/core'
-import { AutomaticTagAvailable, CommentAutomaticTagPolicies, VideoAutoTagPolicies, VideoAutomaticTagPoliciesUpdate } from '@peertube/peertube-models'
+import {
+ AutomaticTagAvailable,
+ CommentAutomaticTagPolicies,
+ VideoAutoTagPolicies,
+ VideoAutomaticTagPoliciesUpdate
+} from '@peertube/peertube-models'
import { catchError } from 'rxjs/operators'
import { environment } from '../../../environments/environment'
diff --git a/packages/models/src/moderation/automatic-tag-available.model.ts b/packages/models/src/moderation/automatic-tag-available.model.ts
index 407866316b..4d7966e067 100644
--- a/packages/models/src/moderation/automatic-tag-available.model.ts
+++ b/packages/models/src/moderation/automatic-tag-available.model.ts
@@ -1,4 +1,4 @@
-export type AutomaticTagAvailableType = 'core' | 'watched-words-list'
+export type AutomaticTagAvailableType = 'core' | 'watched-words-list' | 'plugin'
export interface AutomaticTagAvailable {
available: {
diff --git a/packages/tests/fixtures/peertube-plugin-test-seven/main.js b/packages/tests/fixtures/peertube-plugin-test-seven/main.js
new file mode 100644
index 0000000000..43f49b57bf
--- /dev/null
+++ b/packages/tests/fixtures/peertube-plugin-test-seven/main.js
@@ -0,0 +1,77 @@
+async function register({
+ peertubeHelpers,
+ registerCommentAutoTagger,
+ registerVideoAutoTagger,
+ getRouter
+}) {
+ registerCommentAutoTagger({
+ autoTagName: 'plugin comment auto tag',
+
+ handler: async ({ comment }) => {
+ if (comment && comment.text && comment.text.includes('plugin-tag-comment')) {
+ return { result: true }
+ }
+
+ return { result: false }
+ }
+ })
+
+ registerVideoAutoTagger({
+ autoTagName: '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 { result: false }
+ }
+ })
+
+ const router = getRouter()
+
+ router.get('/server-comment-tags/:commentId', async (req, res) => {
+ const commentId = parseInt(req.params.commentId, 10)
+
+ const tags = await peertubeHelpers.automaticTags.getServerCommentAutomaticTags({ commentId })
+
+ return res.json({
+ tags: tags.map(t => t.name)
+ })
+ })
+
+ router.get('/account-comment-tags/:commentId', async (req, res) => {
+ const commentId = parseInt(req.params.commentId, 10)
+ const accountId = parseInt(req.query.accountId, 10)
+
+ const tags = await peertubeHelpers.automaticTags.getAccountCommentAutomaticTags({ commentId, accountId })
+
+ return res.json({
+ tags: tags.map(t => t.name)
+ })
+ })
+
+ router.get('/server-video-tags/:videoId', async (req, res) => {
+ const videoId = parseInt(req.params.videoId, 10)
+
+ const tags = await peertubeHelpers.automaticTags.getServerVideoAutomaticTags({ videoId })
+
+ return res.json({
+ tags: tags.map(t => t.name)
+ })
+ })
+}
+
+async function unregister() {
+ return
+}
+
+module.exports = {
+ register,
+ unregister
+}
diff --git a/packages/tests/fixtures/peertube-plugin-test-seven/package.json b/packages/tests/fixtures/peertube-plugin-test-seven/package.json
new file mode 100644
index 0000000000..04923fe433
--- /dev/null
+++ b/packages/tests/fixtures/peertube-plugin-test-seven/package.json
@@ -0,0 +1,17 @@
+{
+ "name": "peertube-plugin-test-seven",
+ "version": "0.0.1",
+ "description": "Plugin test seven - auto taggers",
+ "engine": {
+ "peertube": ">=1.3.0"
+ },
+ "keywords": [ "peertube", "plugin" ],
+ "homepage": "https://github.com/Chocobozzz/PeerTube",
+ "author": "Chocobozzz",
+ "bugs": "https://github.com/Chocobozzz/PeerTube/issues",
+ "library": "./main.js",
+ "staticDirs": {},
+ "css": [],
+ "clientScripts": [],
+ "translations": {}
+}
diff --git a/packages/tests/src/plugins/plugin-auto-taggers.ts b/packages/tests/src/plugins/plugin-auto-taggers.ts
new file mode 100644
index 0000000000..767d40c175
--- /dev/null
+++ b/packages/tests/src/plugins/plugin-auto-taggers.ts
@@ -0,0 +1,254 @@
+/* oxlint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
+
+import {
+ cleanupTests,
+ createSingleServer,
+ doubleFollow,
+ makeGetRequest,
+ PeerTubeServer,
+ PluginsCommand,
+ setAccessTokensToServers,
+ waitJobs
+} from '@peertube/peertube-server-commands'
+import { HttpStatusCode } from '@peertube/peertube-models'
+import { expect } from 'chai'
+
+describe('Test plugin auto taggers', function () {
+ let server: PeerTubeServer
+ let anotherServer: PeerTubeServer
+
+ before(async function () {
+ this.timeout(120000)
+
+ server = await createSingleServer(1)
+ anotherServer = await createSingleServer(2)
+
+ await setAccessTokensToServers([ server, anotherServer ])
+ await doubleFollow(server, anotherServer)
+
+ await server.plugins.install({ path: PluginsCommand.getPluginTestPath('-seven') })
+ })
+
+ describe('Plugin comment auto tagger', function () {
+ let videoUUID: string
+
+ before(async function () {
+ ;({ uuid: videoUUID } = await server.videos.quickUpload({ name: 'video' }))
+
+ await waitJobs([ server, anotherServer ])
+ })
+
+ 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' ])
+ })
+
+ it('Should not assign plugin tag if the plugin does not trigger', async function () {
+ await server.comments.createThread({
+ videoId: videoUUID,
+ text: 'just a normal comment with no trigger'
+ })
+
+ {
+ const { data } = await server.comments.listForAdmin()
+ expect(data.find(c => c.text === 'just a normal comment with no trigger').automaticTags).to.have.lengthOf(0)
+ }
+
+ await server.comments.deleteAllComments({ videoUUID })
+ })
+
+ it('Should assign the plugin tag as automatic tag when the plugin triggers', async function () {
+ await server.comments.createThread({
+ videoId: videoUUID,
+ text: 'this comment triggers plugin-tag-comment'
+ })
+
+ await server.comments.createThread({
+ videoId: videoUUID,
+ text: 'another comment with plugin-tag-comment trigger'
+ })
+
+ {
+ const { data } = await server.comments.listForAdmin()
+ const c = (text: string) => data.find(c => c.text === text)
+
+ expect(c('this comment triggers plugin-tag-comment').automaticTags).to.have.members([ 'plugin comment auto tag' ])
+
+ expect(c('another comment with plugin-tag-comment trigger').automaticTags).to.have.members([ 'plugin comment auto tag' ])
+ }
+
+ await server.comments.deleteAllComments({ videoUUID })
+ })
+
+ it('Should assign plugin tag along with watched words and external-link tags', async function () {
+ await server.watchedWordsLists.createList({ listName: 'test list', words: [ 'trigger' ], accountName: 'root' })
+
+ await server.comments.createThread({
+ videoId: videoUUID,
+ text: 'plugin-tag-comment with trigger example.com'
+ })
+
+ await anotherServer.comments.createThread({
+ videoId: videoUUID,
+ text: 'plugin-tag-comment with trigger example.com from remote'
+ })
+
+ await waitJobs([ server, anotherServer ])
+
+ {
+ const { data } = await server.comments.listForAdmin()
+ const t = (text: string) => data.find(c => c.text === text).automaticTags
+
+ expect(t('plugin-tag-comment with trigger example.com')).to.have.members([ 'plugin comment auto tag', 'external-link' ])
+
+ expect(t('plugin-tag-comment with trigger example.com from remote')).to.have.members([
+ 'plugin comment auto tag',
+ 'external-link'
+ ])
+ }
+
+ {
+ const { data } = await server.comments.listCommentsOnMyVideos()
+ const t = (text: string) => data.find(c => c.text === text).automaticTags
+
+ expect(t('plugin-tag-comment with trigger example.com')).to.have.members([
+ 'plugin comment auto tag',
+ 'external-link',
+ 'test list'
+ ])
+
+ expect(
+ t('plugin-tag-comment with trigger example.com from remote')
+ ).to.have.members([
+ 'plugin comment auto tag',
+ 'external-link',
+ 'test list'
+ ])
+ }
+
+ await server.comments.deleteAllComments({ videoUUID })
+ })
+ })
+
+ describe('Plugin video auto tagger', function () {
+ before(async function () {
+ await server.videos.removeAll()
+ })
+
+ 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' ])
+ })
+
+ 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' } })
+
+ {
+ const { data } = await server.videos.listAllForAdmin()
+ expect(data.find(v => v.name === 'normal video').automaticTags).to.have.lengthOf(0)
+ }
+
+ await server.videos.removeAll()
+ })
+
+ it('Should assign plugin npmName as automatic tag on video with trigger in name', async function () {
+ await server.videos.upload({
+ attributes: { name: 'video with plugin-tag-video in name', description: 'no trigger' }
+ })
+
+ {
+ 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' ])
+ }
+
+ await server.videos.removeAll()
+ })
+
+ it('Should assign plugin npmName as automatic tag on video with trigger in description', async function () {
+ await server.videos.upload({
+ attributes: { name: 'video desc', description: 'contains plugin-tag-video in description' }
+ })
+
+ {
+ const { data } = await server.videos.listAllForAdmin()
+ expect(data.find(v => v.name === 'video desc').automaticTags).to.have.members([ 'plugin video auto tag' ])
+ }
+ })
+ })
+
+ describe('Plugin automatic tags helpers', function () {
+ let videoUUID: string
+ let commentId: number
+
+ before(async function () {
+ ;({ uuid: videoUUID } = await server.videos.quickUpload({ name: 'video for helpers' }))
+ })
+
+ it('Should get server comment automatic tags via peertubeHelpers', async function () {
+ await server.comments.createThread({
+ videoId: videoUUID,
+ text: 'plugin-tag-comment for helper test'
+ })
+
+ {
+ const { data } = await server.comments.listForAdmin()
+ const comment = data.find(c => c.text === 'plugin-tag-comment for helper test')
+ expect(comment).to.exist
+ expect(comment.automaticTags).to.include('plugin comment auto tag')
+
+ commentId = comment.id
+ }
+
+ const res = await makeGetRequest({
+ url: server.url,
+ path: `/plugins/test-seven/0.0.1/router/server-comment-tags/${commentId}`,
+ expectedStatus: HttpStatusCode.OK_200
+ })
+
+ expect(res.body.tags).to.include('plugin comment auto tag')
+ })
+
+ it('Should get account comment automatic tags via peertubeHelpers', async function () {
+ const { data } = await server.comments.listForAdmin()
+ const comment = data.find(c => c.text === 'plugin-tag-comment for helper test')
+ expect(comment).to.exist
+
+ const accountId = comment.account.id
+
+ const res = await makeGetRequest({
+ url: server.url,
+ path: `/plugins/test-seven/0.0.1/router/account-comment-tags/${commentId}?accountId=${accountId}`,
+ expectedStatus: HttpStatusCode.OK_200
+ })
+
+ expect(res.body.tags).to.include('plugin comment auto tag')
+ })
+
+ it('Should get server video automatic tags via peertubeHelpers', async function () {
+ await server.videos.upload({
+ attributes: { name: 'plugin-tag-video for helper', description: 'test' }
+ })
+
+ {
+ const { data } = await server.videos.listAllForAdmin()
+ const video = data.find(v => v.name === 'plugin-tag-video for helper')
+ expect(video).to.exist
+ expect(video.automaticTags).to.include('plugin video auto tag')
+
+ const res = await makeGetRequest({
+ url: server.url,
+ path: `/plugins/test-seven/0.0.1/router/server-video-tags/${video.id}`,
+ expectedStatus: HttpStatusCode.OK_200
+ })
+
+ expect(res.body.tags).to.include('plugin video auto tag')
+ }
+ })
+ })
+
+ after(async function () {
+ await cleanupTests([ server ])
+
+ if (anotherServer) await cleanupTests([ anotherServer ])
+ })
+})
diff --git a/server/core/lib/automatic-tags/automatic-tagger.ts b/server/core/lib/automatic-tags/automatic-tagger.ts
index 3b11bec87d..b827c912bf 100644
--- a/server/core/lib/automatic-tags/automatic-tagger.ts
+++ b/server/core/lib/automatic-tags/automatic-tagger.ts
@@ -1,11 +1,14 @@
+import { uniqify } from '@peertube/peertube-core-utils'
import { AutomaticTagAvailable, AutomaticTagPolicy, CommentAutomaticTagPolicies, VideoAutoTagPolicies } from '@peertube/peertube-models'
import { logger, loggerTagsFactory } from '@server/helpers/logger.js'
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, MVideo } from '@server/types/models/index.js'
+import { MAccount, MAccountId, MComment, MVideo } from '@server/types/models/index.js'
import Linkifyit from 'linkify-it'
import { Transaction } from 'sequelize'
+import { PluginManager } from '../plugins/plugin-manager.js'
const lTags = loggerTagsFactory('automatic-tags')
@@ -28,12 +31,24 @@ export class AutomaticTagger {
const result: Record<number, string[]> = {}
try {
+ const pluginAutoTags = await this.buildPluginAutomaticTags({ video: null, comment: { text } })
+
if (serverAccount) {
- result[serverAccount.id] = await this.buildAutomaticTags({ account: serverAccount, text, transaction })
+ const tags = [
+ ...await this.buildAutomaticTags({ account: serverAccount, text, transaction }),
+ ...pluginAutoTags
+ ]
+
+ result[serverAccount.id] = uniqify(tags)
}
if (ownerAccount) {
- result[ownerAccount.id] = await this.buildAutomaticTags({ account: ownerAccount, text, transaction })
+ const tags = [
+ ...await this.buildAutomaticTags({ account: ownerAccount, text, transaction }),
+ ...pluginAutoTags
+ ]
+
+ result[ownerAccount.id] = uniqify(tags)
}
logger.debug('Built automatic tags for comment', { text, result, ...lTags() })
@@ -54,9 +69,10 @@ export class AutomaticTagger {
const { video, serverAccount, transaction } = options
try {
- const [ videoNameTags, videoDescriptionTags ] = await Promise.all([
+ const [ videoNameTags, videoDescriptionTags, pluginTags ] = await Promise.all([
this.buildAutomaticTags({ account: serverAccount, text: video.name, transaction }),
- this.buildAutomaticTags({ account: serverAccount, text: video.description, transaction })
+ this.buildAutomaticTags({ account: serverAccount, text: video.description, transaction }),
+ this.buildPluginAutomaticTags({ video, comment: null })
])
logger.debug('Built automatic tags for video', {
@@ -64,10 +80,11 @@ export class AutomaticTagger {
videoDescription: video.description,
videoNameTags,
videoDescriptionTags,
+ pluginTags,
...lTags()
})
- return { [serverAccount.id]: [ ...videoNameTags, ...videoDescriptionTags ] }
+ return { [serverAccount.id]: uniqify([ ...videoNameTags, ...videoDescriptionTags, ...pluginTags ]) }
} catch (err) {
logger.error('Cannot build video automatic tags', { video, err, ...lTags() })
@@ -116,6 +133,35 @@ 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] || [])) {
+ try {
+ const { result } = await autoTagger({ video, comment })
+
+ if (result) pluginTags.push(autoTagName)
+ } catch (err) {
+ logger.error('Cannot execute auto tagger of plugin ' + npmName, { err, ...lTags() })
+ }
+ }
+ }
+ }
+
+ return pluginTags
+ }
+
private hasExternalLinks (text: string) {
if (!text) return false
@@ -152,12 +198,31 @@ export class AutomaticTagger {
static async getAutomaticTagAvailable (account: MAccountId) {
const result: AutomaticTagAvailable = {
available: [
- ...(await WatchedWordsListModel.listNamesOf(account)).map(t => ({ name: t, type: 'watched-words-list' as 'watched-words-list' })),
+ ...(await WatchedWordsListModel.listNamesOf(account)).map(t => ({ name: t, type: 'watched-words-list' as const })),
+
+ ...Object.values(AutomaticTagger.SPECIAL_TAGS).map(t => ({ name: t, type: 'core' as const })),
- ...Object.values(AutomaticTagger.SPECIAL_TAGS).map(t => ({ name: t, type: 'core' as 'core' }))
+ ...await this.getAvailablePluginAutomaticTagNames(account)
]
}
return result
}
+
+ private static async getAvailablePluginAutomaticTagNames (account: MAccountId) {
+ const serverAccountId = (await getServerAccount()).id
+
+ // The instance can only blacklist videos, it doesn't act on comments
+ const toLoad = serverAccountId === account.id
+ ? 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)
+ })
+ .map(name => ({ name, type: 'plugin' as const }))
+ }
}
diff --git a/server/core/lib/plugins/plugin-helpers-builder.ts b/server/core/lib/plugins/plugin-helpers-builder.ts
index 46e186a9da..a049b3126f 100644
--- a/server/core/lib/plugins/plugin-helpers-builder.ts
+++ b/server/core/lib/plugins/plugin-helpers-builder.ts
@@ -5,9 +5,11 @@ import { buildLogger } from '@server/helpers/logger.js'
import { CONFIG } from '@server/initializers/config.js'
import { WEBSERVER } from '@server/initializers/constants.js'
import { sequelizeTypescript } from '@server/initializers/database.js'
-import { AccountBlocklistModel } from '@server/models/blocklist/account-blocklist.js'
import { AccountModel } from '@server/models/account/account.js'
-import { getServerActor } from '@server/models/application/application.js'
+import { getServerAccount, getServerActor } from '@server/models/application/application.js'
+import { CommentAutomaticTagModel } from '@server/models/automatic-tag/comment-automatic-tag.js'
+import { VideoAutomaticTagModel } from '@server/models/automatic-tag/video-automatic-tag.js'
+import { AccountBlocklistModel } from '@server/models/blocklist/account-blocklist.js'
import { ServerBlocklistModel } from '@server/models/blocklist/server-blocklist.js'
import { ServerModel } from '@server/models/server/server.js'
import { UserModel } from '@server/models/user/user.js'
@@ -25,33 +27,25 @@ import { blacklistVideo, unblacklistVideo } from '../video-blacklist.js'
import { VideoPathManager } from '../video-path-manager.js'
function buildPluginHelpers (httpServer: Server, pluginModel: MPlugin, npmName: string): PeerTubeHelpers {
- const logger = buildPluginLogger(npmName)
+ return {
+ logger: buildPluginLogger(npmName),
- const database = buildDatabaseHelpers()
- const videos = buildVideosHelpers()
+ database: buildDatabaseHelpers(),
+ videos: buildVideosHelpers(),
- const config = buildConfigHelpers()
+ config: buildConfigHelpers(),
- const server = buildServerHelpers(httpServer)
+ server: buildServerHelpers(httpServer),
- const moderation = buildModerationHelpers()
+ moderation: buildModerationHelpers(),
- const plugin = buildPluginRelatedHelpers(pluginModel, npmName)
+ plugin: buildPluginRelatedHelpers(pluginModel, npmName),
- const socket = buildSocketHelpers()
+ socket: buildSocketHelpers(),
- const user = buildUserHelpers()
+ user: buildUserHelpers(),
- return {
- logger,
- database,
- videos,
- config,
- moderation,
- plugin,
- server,
- socket,
- user
+ automaticTags: buildAutomaticTagsHelpers()
}
}
@@ -279,3 +273,39 @@ function buildUserHelpers () {
}
}
}
+
+function buildAutomaticTagsHelpers () {
+ return {
+ getServerCommentAutomaticTags: async (options: {
+ commentId: number
+ }) => {
+ const result = await CommentAutomaticTagModel.listByAccountIdsAndCommentId({
+ commentId: options.commentId,
+ accountIds: [ (await getServerAccount()).id ]
+ })
+
+ return result.map(r => r.AutomaticTag)
+ },
+
+ getAccountCommentAutomaticTags: async (options: {
+ accountId: number
+ commentId: number
+ }) => {
+ const result = await CommentAutomaticTagModel.listByAccountIdsAndCommentId({
+ commentId: options.commentId,
+ accountIds: [ options.accountId ]
+ })
+
+ return result.map(r => r.AutomaticTag)
+ },
+
+ getServerVideoAutomaticTags: async (options: { videoId: number }) => {
+ const result = await VideoAutomaticTagModel.listByAccountIdsAndVideoId({
+ videoId: options.videoId,
+ accountIds: [ (await getServerAccount()).id ]
+ })
+
+ return result.map(r => r.AutomaticTag)
+ }
+ }
+}
diff --git a/server/core/lib/plugins/plugin-manager.ts b/server/core/lib/plugins/plugin-manager.ts
index 179fbdeb66..176ee0f381 100644
--- a/server/core/lib/plugins/plugin-manager.ts
+++ b/server/core/lib/plugins/plugin-manager.ts
@@ -155,6 +155,8 @@ export class PluginManager implements ServerHook {
return this.getRegisteredPluginsOrThemes(PluginType.THEME)
}
+ // ---------------------------------------------------------------------------
+
getIdAndPassAuths () {
return this.getRegisteredPlugins()
.map(p => ({
@@ -177,6 +179,32 @@ export class PluginManager implements ServerHook {
.filter(v => v.externalAuths.length !== 0)
}
+ // ---------------------------------------------------------------------------
+
+ getVideoAutoTaggers () {
+ return this.getRegisteredPlugins()
+ .map(p => ({
+ npmName: p.npmName,
+ name: p.name,
+ version: p.version,
+ autoTaggersPerTagName: p.registerHelpers.getVideoAutoTagger()
+ }))
+ .filter(p => Object.keys(p.autoTaggersPerTagName).length !== 0)
+ }
+
+ getCommentAutoTaggers () {
+ return this.getRegisteredPlugins()
+ .map(p => ({
+ npmName: p.npmName,
+ name: p.name,
+ version: p.version,
+ autoTaggersPerTagName: p.registerHelpers.getCommentAutoTagger()
+ }))
+ .filter(p => Object.keys(p.autoTaggersPerTagName).length !== 0)
+ }
+
+ // ---------------------------------------------------------------------------
+
getRegisteredSettings (npmName: string) {
const result = this.getRegisteredPluginOrTheme(npmName)
if (result?.type !== PluginType.PLUGIN) return []
diff --git a/server/core/lib/plugins/register-helpers.ts b/server/core/lib/plugins/register-helpers.ts
index 1d8ba1f0e8..6d5acd68e2 100644
--- a/server/core/lib/plugins/register-helpers.ts
+++ b/server/core/lib/plugins/register-helpers.ts
@@ -1,5 +1,3 @@
-import express from 'express'
-import { Server } from 'http'
import {
EncoderOptionsBuilder,
PluginSettingsManager,
@@ -23,6 +21,9 @@ import {
RegisterServerOptions,
RegisterServerWebSocketRouteOptions
} from '@server/types/plugins/index.js'
+import { RegisterCommentAutoTaggerOptions, RegisterVideoAutoTaggerOptions } from '@server/types/plugins/register-auto-tagger.model.js'
+import express from 'express'
+import { Server } from 'http'
import { VideoTranscodingProfilesManager } from '../transcoding/default-transcoding-profiles.js'
import { buildPluginHelpers } from './plugin-helpers-builder.js'
@@ -49,6 +50,9 @@ export class RegisterHelpers {
private idAndPassAuths: RegisterServerAuthPassOptions[] = []
private externalAuths: RegisterServerAuthExternalOptions[] = []
+ private readonly commentAutoTagger: Record<string, RegisterCommentAutoTaggerOptions['handler'][]> = {}
+ private readonly videoAutoTagger: Record<string, RegisterVideoAutoTaggerOptions['handler'][]> = {}
+
private readonly onSettingsChangeCallbacks: SettingsChangeCallback[] = []
private readonly webSocketRoutes: RegisterServerWebSocketRouteOptions[] = []
@@ -86,11 +90,6 @@ export class RegisterHelpers {
const transcodingManager = this.buildTranscodingManager()
- const registerIdAndPassAuth = this.buildRegisterIdAndPassAuth()
- const registerExternalAuth = this.buildRegisterExternalAuth()
- const unregisterIdAndPassAuth = this.buildUnregisterIdAndPassAuth()
- const unregisterExternalAuth = this.buildUnregisterExternalAuth()
-
const peertubeHelpers = buildPluginHelpers(this.server, this.plugin, this.npmName)
return {
@@ -138,10 +137,15 @@ export class RegisterHelpers {
transcodingManager,
- registerIdAndPassAuth,
- registerExternalAuth,
- unregisterIdAndPassAuth,
- unregisterExternalAuth,
+ registerIdAndPassAuth: this.buildRegisterIdAndPassAuth(),
+ registerExternalAuth: this.buildRegisterExternalAuth(),
+ unregisterIdAndPassAuth: this.buildUnregisterIdAndPassAuth(),
+ unregisterExternalAuth: this.buildUnregisterExternalAuth(),
+
+ registerCommentAutoTagger: this.buildRegisterCommentAutoTagger(),
+ registerVideoAutoTagger: this.buildRegisterVideoAutoTagger(),
+ unregisterCommentAutoTagger: this.buildUnregisterCommentAutoTagger(),
+ unregisterVideoAutoTagger: this.buildUnregisterVideoAutoTagger(),
peertubeHelpers
}
@@ -191,6 +195,18 @@ export class RegisterHelpers {
return this.webSocketRoutes
}
+ // ---------------------------------------------------------------------------
+
+ getCommentAutoTagger () {
+ return this.commentAutoTagger
+ }
+
+ getVideoAutoTagger () {
+ return this.videoAutoTagger
+ }
+
+ // ---------------------------------------------------------------------------
+
private buildGetRouter () {
return () => this.router
}
@@ -343,4 +359,36 @@ export class RegisterHelpers {
}
}
}
+
+ // ---------------------------------------------------------------------------
+
+ private buildRegisterCommentAutoTagger () {
+ return (options: RegisterCommentAutoTaggerOptions) => {
+ if (!this.commentAutoTagger[options.autoTagName]) this.commentAutoTagger[options.autoTagName] = []
+
+ this.commentAutoTagger[options.autoTagName].push(options.handler)
+ }
+ }
+
+ private buildRegisterVideoAutoTagger () {
+ return (options: RegisterVideoAutoTaggerOptions) => {
+ if (!this.videoAutoTagger[options.autoTagName]) this.videoAutoTagger[options.autoTagName] = []
+
+ this.videoAutoTagger[options.autoTagName].push(options.handler)
+ }
+ }
+
+ private buildUnregisterCommentAutoTagger () {
+ return (options: RegisterCommentAutoTaggerOptions) => {
+ this.commentAutoTagger[options.autoTagName] = (this.commentAutoTagger[options.autoTagName] || [])
+ .filter(f => f !== options.handler)
+ }
+ }
+
+ private buildUnregisterVideoAutoTagger () {
+ return (options: RegisterVideoAutoTaggerOptions) => {
+ this.videoAutoTagger[options.autoTagName] = (this.videoAutoTagger[options.autoTagName] || [])
+ .filter(f => f !== options.handler)
+ }
+ }
}
diff --git a/server/core/types/plugins/register-auto-tagger.model.ts b/server/core/types/plugins/register-auto-tagger.model.ts
new file mode 100644
index 0000000000..23cc908a64
--- /dev/null
+++ b/server/core/types/plugins/register-auto-tagger.model.ts
@@ -0,0 +1,14 @@
+import { MComment } from '../models/video/video-comment.js'
+import { MVideo } from '../models/video/video.js'
+
+export type RegisterCommentAutoTaggerOptions = {
+ autoTagName: string
+
+ handler: (options: { comment: Pick<MComment, 'text'> }) => Promise<{ result: boolean }>
+}
+
+export type RegisterVideoAutoTaggerOptions = {
+ autoTagName: string
+
+ handler: (options: { video: Pick<MVideo, 'name' | 'description'> }) => Promise<{ result: boolean }>
+}
diff --git a/server/core/types/plugins/register-server-option.model.ts b/server/core/types/plugins/register-server-option.model.ts
index 930e055082..cd60321774 100644
--- a/server/core/types/plugins/register-server-option.model.ts
+++ b/server/core/types/plugins/register-server-option.model.ts
@@ -1,6 +1,3 @@
-import { Response, Router } from 'express'
-import { Server } from 'http'
-import { Logger } from 'winston'
import {
PluginPlaylistPrivacyManager,
PluginSettingsManager,
@@ -16,7 +13,11 @@ import {
VideoBlacklistCreate
} from '@peertube/peertube-models'
import { ActorModel } from '@server/models/actor/actor.js'
-import { MUserDefault, MVideo, MVideoThumbnails, MVideoWithAllFiles, UserNotificationModelForApi } from '../models/index.js'
+import { Response, Router } from 'express'
+import { Server } from 'http'
+import { Logger } from 'winston'
+import { MAutomaticTag, MUserDefault, MVideo, MVideoThumbnails, MVideoWithAllFiles, UserNotificationModelForApi } from '../models/index.js'
+import { RegisterCommentAutoTaggerOptions, RegisterVideoAutoTaggerOptions } from './register-auto-tagger.model.js'
import {
RegisterServerAuthExternalOptions,
RegisterServerAuthExternalResult,
@@ -123,6 +124,16 @@ export type PeerTubeHelpers = {
// PeerTube >= 4.3
loadById: (id: number) => Promise<MUserDefault>
}
+
+ automaticTags: {
+ // PeerTube >= 8.3
+ getServerCommentAutomaticTags: (options: { commentId: number }) => Promise<MAutomaticTag[]>
+ // PeerTube >= 8.3
+ getAccountCommentAutomaticTags: (options: { accountId: number, commentId: number }) => Promise<MAutomaticTag[]>
+
+ // PeerTube >= 8.3
+ getServerVideoAutomaticTags: (options: { videoId: number }) => Promise<MAutomaticTag[]>
+ }
}
export type RegisterServerOptions = {
@@ -148,6 +159,11 @@ export type RegisterServerOptions = {
unregisterIdAndPassAuth: (authName: string) => void
unregisterExternalAuth: (authName: string) => void
+ registerCommentAutoTagger: (options: RegisterCommentAutoTaggerOptions) => void
+ registerVideoAutoTagger: (options: RegisterVideoAutoTaggerOptions) => void
+ unregisterCommentAutoTagger: (options: RegisterCommentAutoTaggerOptions) => void
+ unregisterVideoAutoTagger: (options: RegisterVideoAutoTaggerOptions) => void
+
// Get plugin router to create custom routes
// Base routes of this router are
// * /plugins/:pluginName/:pluginVersion/router/...