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 channelId: videoChannel.id,
57 duration: parseInt(duration, 10),
58 createdAt: new Date(videoObject.published),
59 // FIXME: updatedAt does not seems to be considered by Sequelize
60 updatedAt: new Date(videoObject.updated),
61 views: videoObject.views,
69 function videoFileActivityUrlToDBAttributes (videoCreated: VideoModel, videoObject: VideoTorrentObject) {
70 const mimeTypes = Object.keys(VIDEO_MIMETYPE_EXT)
71 const fileUrls = videoObject.url.filter(u => {
72 return mimeTypes.indexOf(u.mimeType) !== -1 && u.mimeType.startsWith('video/')
75 if (fileUrls.length === 0) {
76 throw new Error('Cannot find video files for ' + videoCreated.url)
80 for (const fileUrl of fileUrls) {
81 // Fetch associated magnet uri
82 const magnet = videoObject.url.find(u => {
83 return u.mimeType === 'application/x-bittorrent;x-scheme-handler/magnet' && u.width === fileUrl.width
86 if (!magnet) throw new Error('Cannot find associated magnet uri for file ' + fileUrl.url)
88 const parsed = magnetUtil.decode(magnet.url)
89 if (!parsed || isVideoFileInfoHashValid(parsed.infoHash) === false) throw new Error('Cannot parse magnet URI ' + magnet.url)
92 extname: VIDEO_MIMETYPE_EXT[fileUrl.mimeType],
93 infoHash: parsed.infoHash,
94 resolution: fileUrl.width,
96 videoId: videoCreated.id
98 attributes.push(attribute)
104 async function videoCommentActivityObjectToDBAttributes (video: VideoModel, actor: ActorModel, comment: VideoCommentObject) {
105 let originCommentId: number = null
106 let inReplyToCommentId: number = null
108 // If this is not a reply to the video (thread), create or get the parent comment
109 if (video.url !== comment.inReplyTo) {
110 const [ parent ] = await addVideoComment(video, comment.inReplyTo)
112 logger.warn('Cannot fetch or get parent comment %s of comment %s.', comment.inReplyTo, comment.id)
116 originCommentId = parent.originCommentId || parent.id
117 inReplyToCommentId = parent.id
122 text: comment.content,
124 accountId: actor.Account.id,
127 createdAt: new Date(comment.published),
128 updatedAt: new Date(comment.updated)
132 async function addVideoShares (instance: VideoModel, shareUrls: string[]) {
133 for (const shareUrl of shareUrls) {
135 const { body } = await doRequest({
140 const actorUrl = body.actor
141 if (!actorUrl) continue
143 const actor = await getOrCreateActorAndServerAndModel(actorUrl)
150 await VideoShareModel.findOrCreate({
157 async function addVideoComments (instance: VideoModel, commentUrls: string[]) {
158 for (const commentUrl of commentUrls) {
159 await addVideoComment(instance, commentUrl)
163 async function addVideoComment (instance: VideoModel, commentUrl: string) {
165 const { body } = await doRequest({
171 const actorUrl = body.attributedTo
172 if (!actorUrl) return []
174 const actor = await getOrCreateActorAndServerAndModel(actorUrl)
175 const entry = await videoCommentActivityObjectToDBAttributes(instance, actor, body)
176 if (!entry) return []
178 return VideoCommentModel.findOrCreate({
186 // ---------------------------------------------------------------------------
189 videoFileActivityUrlToDBAttributes,
190 videoActivityObjectToDBAttributes,