2024-03-29 14:25:03 +01:00
|
|
|
import { VideoCommentPolicy, VideoCommentPolicyType } from '@peertube/peertube-models'
|
|
|
|
import { CONFIG } from '@server/initializers/config.js'
|
2023-07-31 14:34:36 +02:00
|
|
|
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'
|
2024-02-13 14:23:32 +01:00
|
|
|
import { MVideoTag } from '@server/types/models/index.js'
|
2024-03-29 14:25:03 +01:00
|
|
|
import memoizee from 'memoizee'
|
|
|
|
import { Transaction } from 'sequelize'
|
2020-09-17 10:00:46 +02:00
|
|
|
|
2022-03-22 14:35:04 +01:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2023-10-31 12:15:40 +01:00
|
|
|
export async function setVideoTags (options: {
|
2020-09-17 10:00:46 +02:00
|
|
|
video: MVideoTag
|
|
|
|
tags: string[]
|
|
|
|
transaction?: Transaction
|
|
|
|
}) {
|
2021-03-03 11:03:30 +01:00
|
|
|
const { video, tags, transaction } = options
|
2020-09-17 10:00:46 +02:00
|
|
|
|
2021-03-03 11:03:30 +01:00
|
|
|
const internalTags = tags || []
|
2024-03-29 14:25:03 +01:00
|
|
|
const tagInstances = await TagModel.findOrCreateMultiple({ tags: internalTags, transaction })
|
2021-03-03 11:03:30 +01:00
|
|
|
|
|
|
|
await video.$set('Tags', tagInstances, { transaction })
|
|
|
|
video.Tags = tagInstances
|
2020-09-17 10:00:46 +02:00
|
|
|
}
|
|
|
|
|
2022-03-22 14:35:04 +01:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2024-02-13 14:23:32 +01:00
|
|
|
async function getVideoDuration (videoId: number | string) {
|
2022-06-17 14:34:37 +02:00
|
|
|
const video = await VideoModel.load(videoId)
|
|
|
|
|
|
|
|
const duration = video.isLive
|
|
|
|
? undefined
|
|
|
|
: video.duration
|
|
|
|
|
|
|
|
return { duration, isLive: video.isLive }
|
|
|
|
}
|
|
|
|
|
2023-10-31 12:15:40 +01:00
|
|
|
export const getCachedVideoDuration = memoizee(getVideoDuration, {
|
2022-06-17 14:34:37 +02:00
|
|
|
promise: true,
|
|
|
|
max: MEMOIZE_LENGTH.VIDEO_DURATION,
|
|
|
|
maxAge: MEMOIZE_TTL.VIDEO_DURATION
|
|
|
|
})
|
2024-03-29 14:25:03 +01:00
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|