import { generateMagnetUri } from '@server/helpers/webtorrent'
+import { getActivityStreamDuration } from '@server/lib/activitypub/activity'
+import { tracer } from '@server/lib/opentelemetry/tracing'
import { getLocalVideoFileMetadataUrl } from '@server/lib/video-urls'
-import { VideoViews } from '@server/lib/video-views'
+import { VideoViewsManager } from '@server/lib/views/video-views-manager'
import { uuidToShort } from '@shared/extra-utils'
-import { VideoFile, VideosCommonQueryAfterSanitize } from '@shared/models'
-import { ActivityTagObject, ActivityUrlObject, VideoObject } from '../../../../shared/models/activitypub/objects'
-import { Video, VideoDetails, VideoInclude } from '../../../../shared/models/videos'
-import { VideoStreamingPlaylist } from '../../../../shared/models/videos/video-streaming-playlist.model'
+import {
+ ActivityTagObject,
+ ActivityUrlObject,
+ Video,
+ VideoDetails,
+ VideoFile,
+ VideoInclude,
+ VideoObject,
+ VideosCommonQueryAfterSanitize,
+ VideoStreamingPlaylist
+} from '@shared/models'
import { isArray } from '../../../helpers/custom-validators/misc'
import {
MIMETYPES,
import {
MServer,
MStreamingPlaylistRedundanciesOpt,
+ MUserId,
MVideo,
MVideoAP,
MVideoFile,
}
function guessAdditionalAttributesFromQuery (query: VideosCommonQueryAfterSanitize): VideoFormattingJSONOptions {
- if (!query || !query.include) return {}
+ if (!query?.include) return {}
return {
additionalAttributes: {
}
function videoModelToFormattedJSON (video: MVideoFormattable, options: VideoFormattingJSONOptions = {}): Video {
+ const span = tracer.startSpan('peertube.VideoModel.toFormattedJSON')
+
const userHistory = isArray(video.UserVideoHistories) ? video.UserVideoHistories[0] : undefined
const videoObject: Video = {
},
nsfw: video.nsfw,
+ truncatedDescription: video.getTruncatedDescription(),
description: options && options.completeDescription === true
? video.description
: video.getTruncatedDescription(),
isLocal: video.isOwned(),
duration: video.duration,
+
views: video.views,
+ viewers: VideoViewsManager.Instance.getViewers(video),
+
likes: video.likes,
dislikes: video.dislikes,
thumbnailPath: video.getMiniatureStaticPath(),
pluginData: (video as any).pluginData
}
- if (video.isLive) {
- videoObject.viewers = VideoViews.Instance.getViewers(video)
- }
-
const add = options.additionalAttributes
if (add?.state === true) {
videoObject.state = {
videoObject.files = videoFilesModelToFormattedJSON(video, video.VideoFiles)
}
+ span.end()
+
return videoObject
}
function videoModelToFormattedDetailsJSON (video: MVideoFormattableDetails): VideoDetails {
+ const span = tracer.startSpan('peertube.VideoModel.toFormattedDetailsJSON')
+
const videoJSON = video.toFormattedJSON({
+ completeDescription: true,
additionalAttributes: {
scheduledUpdate: true,
blacklistInfo: true,
trackerUrls: video.getTrackerUrls()
}
+ span.end()
+
return Object.assign(videoJSON, detailsJSON)
}
function videoFilesModelToFormattedJSON (
video: MVideoFormattable,
videoFiles: MVideoFileRedundanciesOpt[],
- includeMagnet = true
+ options: {
+ includeMagnet?: boolean // default true
+ } = {}
): VideoFile[] {
+ const { includeMagnet = true } = options
+
const trackerUrls = includeMagnet
? video.getTrackerUrls()
: []
.sort(sortByResolutionDesc)
.map(videoFile => {
return {
+ id: videoFile.id,
+
resolution: {
id: videoFile.resolution,
label: videoFile.resolution === 0 ? 'Audio' : `${videoFile.resolution}p`
})
}
-function addVideoFilesInAPAcc (
- acc: ActivityUrlObject[] | ActivityTagObject[],
- video: MVideo,
+function addVideoFilesInAPAcc (options: {
+ acc: ActivityUrlObject[] | ActivityTagObject[]
+ video: MVideo
files: MVideoFile[]
-) {
+ user?: MUserId
+}) {
+ const { acc, video, files } = options
+
const trackerUrls = video.getTrackerUrls()
const sortedFiles = (files || [])
}
]
- addVideoFilesInAPAcc(url, video, video.VideoFiles || [])
+ addVideoFilesInAPAcc({ acc: url, video, files: video.VideoFiles || [] })
for (const playlist of (video.VideoStreamingPlaylists || [])) {
const tag = playlist.p2pMediaLoaderInfohashes
href: playlist.getSha256SegmentsUrl(video)
})
- addVideoFilesInAPAcc(tag, video, playlist.VideoFiles || [])
+ addVideoFilesInAPAcc({ acc: tag, video, files: playlist.VideoFiles || [] })
url.push({
type: 'Link',
}
}
-function getActivityStreamDuration (duration: number) {
- // https://www.w3.org/TR/activitystreams-vocabulary/#dfn-duration
- return 'PT' + duration + 'S'
-}
-
function getCategoryLabel (id: number) {
- return VIDEO_CATEGORIES[id] || 'Misc'
+ return VIDEO_CATEGORIES[id] || 'Unknown'
}
function getLicenceLabel (id: number) {
videoModelToFormattedDetailsJSON,
videoFilesModelToFormattedJSON,
videoModelToActivityPubObject,
- getActivityStreamDuration,
guessAdditionalAttributesFromQuery,