1
0
Fork 0
peertube/server/core/lib/video.ts
Chocobozzz 29329d6c45 Implement auto tag on comments and videos
* Comments and videos can be automatically tagged using core rules or
   watched word lists
 * These tags can be used to automatically filter videos and comments
 * Introduce a new video comment policy where comments must be approved
   first
 * Comments may have to be approved if the user auto block them using
   core rules or watched word lists
 * Implement FEP-5624 to federate reply control policies
2024-05-29 15:03:14 +02:00

56 lines
1.8 KiB
TypeScript

import { VideoCommentPolicy, VideoCommentPolicyType } from '@peertube/peertube-models'
import { CONFIG } from '@server/initializers/config.js'
import { MEMOIZE_LENGTH, MEMOIZE_TTL } from '@server/initializers/constants.js'
import { TagModel } from '@server/models/video/tag.js'
import { VideoModel } from '@server/models/video/video.js'
import { MVideoTag } from '@server/types/models/index.js'
import memoizee from 'memoizee'
import { Transaction } from 'sequelize'
// ---------------------------------------------------------------------------
export async function setVideoTags (options: {
video: MVideoTag
tags: string[]
transaction?: Transaction
}) {
const { video, tags, transaction } = options
const internalTags = tags || []
const tagInstances = await TagModel.findOrCreateMultiple({ tags: internalTags, transaction })
await video.$set('Tags', tagInstances, { transaction })
video.Tags = tagInstances
}
// ---------------------------------------------------------------------------
async function getVideoDuration (videoId: number | string) {
const video = await VideoModel.load(videoId)
const duration = video.isLive
? undefined
: video.duration
return { duration, isLive: video.isLive }
}
export const getCachedVideoDuration = memoizee(getVideoDuration, {
promise: true,
max: MEMOIZE_LENGTH.VIDEO_DURATION,
maxAge: MEMOIZE_TTL.VIDEO_DURATION
})
// ---------------------------------------------------------------------------
export function buildCommentsPolicy (options: {
commentsEnabled?: boolean
commentsPolicy?: VideoCommentPolicyType
}) {
if (options.commentsPolicy) return options.commentsPolicy
if (options.commentsEnabled === true) return VideoCommentPolicy.ENABLED
if (options.commentsEnabled === false) return VideoCommentPolicy.DISABLED
return CONFIG.DEFAULTS.PUBLISH.COMMENTS_POLICY
}