123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365 |
- import { Video, VideoDetails, VideoFile } from '../../../shared/models/videos'
- import { VideoModel } from './video'
- import { VideoFileModel } from './video-file'
- import {
- ActivityPlaylistInfohashesObject,
- ActivityPlaylistSegmentHashesObject,
- ActivityUrlObject,
- VideoTorrentObject
- } from '../../../shared/models/activitypub/objects'
- import { MIMETYPES, WEBSERVER } from '../../initializers/constants'
- import { VideoCaptionModel } from './video-caption'
- import {
- getVideoCommentsActivityPubUrl,
- getVideoDislikesActivityPubUrl,
- getVideoLikesActivityPubUrl,
- getVideoSharesActivityPubUrl
- } from '../../lib/activitypub'
- import { isArray } from '../../helpers/custom-validators/misc'
- import { VideoStreamingPlaylist } from '../../../shared/models/videos/video-streaming-playlist.model'
- import { VideoStreamingPlaylistModel } from './video-streaming-playlist'
- export type VideoFormattingJSONOptions = {
- completeDescription?: boolean
- additionalAttributes: {
- state?: boolean,
- waitTranscoding?: boolean,
- scheduledUpdate?: boolean,
- blacklistInfo?: boolean
- playlistInfo?: boolean
- }
- }
- function videoModelToFormattedJSON (video: VideoModel, options?: VideoFormattingJSONOptions): Video {
- const userHistory = isArray(video.UserVideoHistories) ? video.UserVideoHistories[0] : undefined
- const videoObject: Video = {
- id: video.id,
- uuid: video.uuid,
- name: video.name,
- category: {
- id: video.category,
- label: VideoModel.getCategoryLabel(video.category)
- },
- licence: {
- id: video.licence,
- label: VideoModel.getLicenceLabel(video.licence)
- },
- language: {
- id: video.language,
- label: VideoModel.getLanguageLabel(video.language)
- },
- privacy: {
- id: video.privacy,
- label: VideoModel.getPrivacyLabel(video.privacy)
- },
- nsfw: video.nsfw,
- description: options && options.completeDescription === true ? video.description : video.getTruncatedDescription(),
- isLocal: video.isOwned(),
- duration: video.duration,
- views: video.views,
- likes: video.likes,
- dislikes: video.dislikes,
- thumbnailPath: video.getMiniatureStaticPath(),
- previewPath: video.getPreviewStaticPath(),
- embedPath: video.getEmbedStaticPath(),
- createdAt: video.createdAt,
- updatedAt: video.updatedAt,
- publishedAt: video.publishedAt,
- originallyPublishedAt: video.originallyPublishedAt,
- account: video.VideoChannel.Account.toFormattedSummaryJSON(),
- channel: video.VideoChannel.toFormattedSummaryJSON(),
- userHistory: userHistory ? {
- currentTime: userHistory.currentTime
- } : undefined
- }
- if (options) {
- if (options.additionalAttributes.state === true) {
- videoObject.state = {
- id: video.state,
- label: VideoModel.getStateLabel(video.state)
- }
- }
- if (options.additionalAttributes.waitTranscoding === true) {
- videoObject.waitTranscoding = video.waitTranscoding
- }
- if (options.additionalAttributes.scheduledUpdate === true && video.ScheduleVideoUpdate) {
- videoObject.scheduledUpdate = {
- updateAt: video.ScheduleVideoUpdate.updateAt,
- privacy: video.ScheduleVideoUpdate.privacy || undefined
- }
- }
- if (options.additionalAttributes.blacklistInfo === true) {
- videoObject.blacklisted = !!video.VideoBlacklist
- videoObject.blacklistedReason = video.VideoBlacklist ? video.VideoBlacklist.reason : null
- }
- if (options.additionalAttributes.playlistInfo === true) {
- // We filtered on a specific videoId/videoPlaylistId, that is unique
- const playlistElement = video.VideoPlaylistElements[0]
- videoObject.playlistElement = {
- position: playlistElement.position,
- startTimestamp: playlistElement.startTimestamp,
- stopTimestamp: playlistElement.stopTimestamp
- }
- }
- }
- return videoObject
- }
- function videoModelToFormattedDetailsJSON (video: VideoModel): VideoDetails {
- const formattedJson = video.toFormattedJSON({
- additionalAttributes: {
- scheduledUpdate: true,
- blacklistInfo: true
- }
- })
- const { baseUrlHttp, baseUrlWs } = video.getBaseUrls()
- const tags = video.Tags ? video.Tags.map(t => t.name) : []
- const streamingPlaylists = streamingPlaylistsModelToFormattedJSON(video, video.VideoStreamingPlaylists)
- const detailsJson = {
- support: video.support,
- descriptionPath: video.getDescriptionAPIPath(),
- channel: video.VideoChannel.toFormattedJSON(),
- account: video.VideoChannel.Account.toFormattedJSON(),
- tags,
- commentsEnabled: video.commentsEnabled,
- downloadEnabled: video.downloadEnabled,
- waitTranscoding: video.waitTranscoding,
- state: {
- id: video.state,
- label: VideoModel.getStateLabel(video.state)
- },
- trackerUrls: video.getTrackerUrls(baseUrlHttp, baseUrlWs),
- files: [],
- streamingPlaylists
- }
- // Format and sort video files
- detailsJson.files = videoFilesModelToFormattedJSON(video, video.VideoFiles)
- return Object.assign(formattedJson, detailsJson)
- }
- function streamingPlaylistsModelToFormattedJSON (video: VideoModel, playlists: VideoStreamingPlaylistModel[]): VideoStreamingPlaylist[] {
- if (isArray(playlists) === false) return []
- return playlists
- .map(playlist => {
- const redundancies = isArray(playlist.RedundancyVideos)
- ? playlist.RedundancyVideos.map(r => ({ baseUrl: r.fileUrl }))
- : []
- return {
- id: playlist.id,
- type: playlist.type,
- playlistUrl: playlist.playlistUrl,
- segmentsSha256Url: playlist.segmentsSha256Url,
- redundancies
- } as VideoStreamingPlaylist
- })
- }
- function videoFilesModelToFormattedJSON (video: VideoModel, videoFiles: VideoFileModel[]): VideoFile[] {
- const { baseUrlHttp, baseUrlWs } = video.getBaseUrls()
- return videoFiles
- .map(videoFile => {
- let resolutionLabel = videoFile.resolution + 'p'
- return {
- resolution: {
- id: videoFile.resolution,
- label: resolutionLabel
- },
- magnetUri: video.generateMagnetUri(videoFile, baseUrlHttp, baseUrlWs),
- size: videoFile.size,
- fps: videoFile.fps,
- torrentUrl: video.getTorrentUrl(videoFile, baseUrlHttp),
- torrentDownloadUrl: video.getTorrentDownloadUrl(videoFile, baseUrlHttp),
- fileUrl: video.getVideoFileUrl(videoFile, baseUrlHttp),
- fileDownloadUrl: video.getVideoFileDownloadUrl(videoFile, baseUrlHttp)
- } as VideoFile
- })
- .sort((a, b) => {
- if (a.resolution.id < b.resolution.id) return 1
- if (a.resolution.id === b.resolution.id) return 0
- return -1
- })
- }
- function videoModelToActivityPubObject (video: VideoModel): VideoTorrentObject {
- const { baseUrlHttp, baseUrlWs } = video.getBaseUrls()
- if (!video.Tags) video.Tags = []
- const tag = video.Tags.map(t => ({
- type: 'Hashtag' as 'Hashtag',
- name: t.name
- }))
- let language
- if (video.language) {
- language = {
- identifier: video.language,
- name: VideoModel.getLanguageLabel(video.language)
- }
- }
- let category
- if (video.category) {
- category = {
- identifier: video.category + '',
- name: VideoModel.getCategoryLabel(video.category)
- }
- }
- let licence
- if (video.licence) {
- licence = {
- identifier: video.licence + '',
- name: VideoModel.getLicenceLabel(video.licence)
- }
- }
- const url: ActivityUrlObject[] = []
- for (const file of video.VideoFiles) {
- url.push({
- type: 'Link',
- mimeType: MIMETYPES.VIDEO.EXT_MIMETYPE[ file.extname ] as any,
- mediaType: MIMETYPES.VIDEO.EXT_MIMETYPE[ file.extname ] as any,
- href: video.getVideoFileUrl(file, baseUrlHttp),
- height: file.resolution,
- size: file.size,
- fps: file.fps
- })
- url.push({
- type: 'Link',
- mimeType: 'application/x-bittorrent' as 'application/x-bittorrent',
- mediaType: 'application/x-bittorrent' as 'application/x-bittorrent',
- href: video.getTorrentUrl(file, baseUrlHttp),
- height: file.resolution
- })
- url.push({
- type: 'Link',
- mimeType: 'application/x-bittorrent;x-scheme-handler/magnet' as 'application/x-bittorrent;x-scheme-handler/magnet',
- mediaType: 'application/x-bittorrent;x-scheme-handler/magnet' as 'application/x-bittorrent;x-scheme-handler/magnet',
- href: video.generateMagnetUri(file, baseUrlHttp, baseUrlWs),
- height: file.resolution
- })
- }
- for (const playlist of (video.VideoStreamingPlaylists || [])) {
- let tag: (ActivityPlaylistSegmentHashesObject | ActivityPlaylistInfohashesObject)[]
- tag = playlist.p2pMediaLoaderInfohashes
- .map(i => ({ type: 'Infohash' as 'Infohash', name: i }))
- tag.push({
- type: 'Link',
- name: 'sha256',
- mimeType: 'application/json' as 'application/json',
- mediaType: 'application/json' as 'application/json',
- href: playlist.segmentsSha256Url
- })
- url.push({
- type: 'Link',
- mimeType: 'application/x-mpegURL' as 'application/x-mpegURL',
- mediaType: 'application/x-mpegURL' as 'application/x-mpegURL',
- href: playlist.playlistUrl,
- tag
- })
- }
- // Add video url too
- url.push({
- type: 'Link',
- mimeType: 'text/html',
- mediaType: 'text/html',
- href: WEBSERVER.URL + '/videos/watch/' + video.uuid
- })
- const subtitleLanguage = []
- for (const caption of video.VideoCaptions) {
- subtitleLanguage.push({
- identifier: caption.language,
- name: VideoCaptionModel.getLanguageLabel(caption.language)
- })
- }
- const miniature = video.getMiniature()
- return {
- type: 'Video' as 'Video',
- id: video.url,
- name: video.name,
- duration: getActivityStreamDuration(video.duration),
- uuid: video.uuid,
- tag,
- category,
- licence,
- language,
- views: video.views,
- sensitive: video.nsfw,
- waitTranscoding: video.waitTranscoding,
- state: video.state,
- commentsEnabled: video.commentsEnabled,
- downloadEnabled: video.downloadEnabled,
- published: video.publishedAt.toISOString(),
- originallyPublishedAt: video.originallyPublishedAt ? video.originallyPublishedAt.toISOString() : null,
- updated: video.updatedAt.toISOString(),
- mediaType: 'text/markdown',
- content: video.getTruncatedDescription(),
- support: video.support,
- subtitleLanguage,
- icon: {
- type: 'Image',
- url: miniature.getFileUrl(),
- mediaType: 'image/jpeg',
- width: miniature.width,
- height: miniature.height
- },
- url,
- likes: getVideoLikesActivityPubUrl(video),
- dislikes: getVideoDislikesActivityPubUrl(video),
- shares: getVideoSharesActivityPubUrl(video),
- comments: getVideoCommentsActivityPubUrl(video),
- attributedTo: [
- {
- type: 'Person',
- id: video.VideoChannel.Account.Actor.url
- },
- {
- type: 'Group',
- id: video.VideoChannel.Actor.url
- }
- ]
- }
- }
- function getActivityStreamDuration (duration: number) {
- // https://www.w3.org/TR/activitystreams-vocabulary/#dfn-duration
- return 'PT' + duration + 'S'
- }
- export {
- videoModelToFormattedJSON,
- videoModelToFormattedDetailsJSON,
- videoFilesModelToFormattedJSON,
- videoModelToActivityPubObject,
- getActivityStreamDuration
- }
|