1 import * as magnetUtil from 'magnet-uri'
2 import { VideoTorrentObject } from '../../../../shared'
3 import { VideoCommentObject } from '../../../../shared/models/activitypub/objects/video-comment-object'
4 import { VideoPrivacy } from '../../../../shared/models/videos'
5 import { isVideoFileInfoHashValid } from '../../../helpers/custom-validators/videos'
6 import { logger } from '../../../helpers/logger'
7 import { doRequest } from '../../../helpers/requests'
8 import { ACTIVITY_PUB, VIDEO_MIMETYPE_EXT } from '../../../initializers'
9 import { ActorModel } from '../../../models/activitypub/actor'
10 import { VideoModel } from '../../../models/video/video'
11 import { VideoChannelModel } from '../../../models/video/video-channel'
12 import { VideoCommentModel } from '../../../models/video/video-comment'
13 import { VideoShareModel } from '../../../models/video/video-share'
14 import { getOrCreateActorAndServerAndModel } from '../actor'
16 async function videoActivityObjectToDBAttributes (
17 videoChannel: VideoChannelModel,
18 videoObject: VideoTorrentObject,
22 let privacy = VideoPrivacy.PRIVATE
23 if (to.indexOf(ACTIVITY_PUB.PUBLIC) !== -1) privacy = VideoPrivacy.PUBLIC
24 else if (cc.indexOf(ACTIVITY_PUB.PUBLIC) !== -1) privacy = VideoPrivacy.UNLISTED
26 const duration = videoObject.duration.replace(/[^\d]+/, '')
28 if (videoObject.language) {
29 language = parseInt(videoObject.language.identifier, 10)
33 if (videoObject.category) {
34 category = parseInt(videoObject.category.identifier, 10)
38 if (videoObject.licence) {
39 licence = parseInt(videoObject.licence.identifier, 10)
42 let description = null
43 if (videoObject.content) {
44 description = videoObject.content
48 name: videoObject.name,
49 uuid: videoObject.uuid,
55 nsfw: videoObject.nsfw,
56 commentsEnabled: videoObject.commentsEnabled,
57 channelId: videoChannel.id,
58 duration: parseInt(duration, 10),
59 createdAt: new Date(videoObject.published),
60 // FIXME: updatedAt does not seems to be considered by Sequelize
61 updatedAt: new Date(videoObject.updated),
62 views: videoObject.views,
70 function videoFileActivityUrlToDBAttributes (videoCreated: VideoModel, videoObject: VideoTorrentObject) {
71 const mimeTypes = Object.keys(VIDEO_MIMETYPE_EXT)
72 const fileUrls = videoObject.url.filter(u => {
73 return mimeTypes.indexOf(u.mimeType) !== -1 && u.mimeType.startsWith('video/')
76 if (fileUrls.length === 0) {
77 throw new Error('Cannot find video files for ' + videoCreated.url)
81 for (const fileUrl of fileUrls) {
82 // Fetch associated magnet uri
83 const magnet = videoObject.url.find(u => {
84 return u.mimeType === 'application/x-bittorrent;x-scheme-handler/magnet' && u.width === fileUrl.width
87 if (!magnet) throw new Error('Cannot find associated magnet uri for file ' + fileUrl.url)
89 const parsed = magnetUtil.decode(magnet.url)
90 if (!parsed || isVideoFileInfoHashValid(parsed.infoHash) === false) throw new Error('Cannot parse magnet URI ' + magnet.url)
93 extname: VIDEO_MIMETYPE_EXT[fileUrl.mimeType],
94 infoHash: parsed.infoHash,
95 resolution: fileUrl.width,
97 videoId: videoCreated.id
99 attributes.push(attribute)
105 async function videoCommentActivityObjectToDBAttributes (video: VideoModel, actor: ActorModel, comment: VideoCommentObject) {
106 let originCommentId: number = null
107 let inReplyToCommentId: number = null
109 // If this is not a reply to the video (thread), create or get the parent comment
110 if (video.url !== comment.inReplyTo) {
111 const [ parent ] = await addVideoComment(video, comment.inReplyTo)
113 logger.warn('Cannot fetch or get parent comment %s of comment %s.', comment.inReplyTo, comment.id)
117 originCommentId = parent.originCommentId || parent.id
118 inReplyToCommentId = parent.id
123 text: comment.content,
125 accountId: actor.Account.id,
128 createdAt: new Date(comment.published),
129 updatedAt: new Date(comment.updated)
133 async function addVideoShares (instance: VideoModel, shareUrls: string[]) {
134 for (const shareUrl of shareUrls) {
136 const { body } = await doRequest({
141 const actorUrl = body.actor
142 if (!actorUrl) continue
144 const actor = await getOrCreateActorAndServerAndModel(actorUrl)
151 await VideoShareModel.findOrCreate({
158 async function addVideoComments (instance: VideoModel, commentUrls: string[]) {
159 for (const commentUrl of commentUrls) {
160 await addVideoComment(instance, commentUrl)
164 async function addVideoComment (instance: VideoModel, commentUrl: string) {
166 const { body } = await doRequest({
172 const actorUrl = body.attributedTo
173 if (!actorUrl) return []
175 const actor = await getOrCreateActorAndServerAndModel(actorUrl)
176 const entry = await videoCommentActivityObjectToDBAttributes(instance, actor, body)
177 if (!entry) return []
179 return VideoCommentModel.findOrCreate({
187 // ---------------------------------------------------------------------------
190 videoFileActivityUrlToDBAttributes,
191 videoActivityObjectToDBAttributes,