Fix video announces processing
[oweals/peertube.git] / server / lib / activitypub / video-comments.ts
1 import { VideoCommentObject } from '../../../shared/models/activitypub/objects/video-comment-object'
2 import { isVideoCommentObjectValid } from '../../helpers/custom-validators/activitypub/video-comments'
3 import { logger } from '../../helpers/logger'
4 import { doRequest } from '../../helpers/requests'
5 import { ACTIVITY_PUB } from '../../initializers'
6 import { ActorModel } from '../../models/activitypub/actor'
7 import { VideoModel } from '../../models/video/video'
8 import { VideoCommentModel } from '../../models/video/video-comment'
9 import { getOrCreateActorAndServerAndModel } from './actor'
10 import { getOrCreateAccountAndVideoAndChannel } from './videos'
11
12 async function videoCommentActivityObjectToDBAttributes (video: VideoModel, actor: ActorModel, comment: VideoCommentObject) {
13   let originCommentId: number = null
14   let inReplyToCommentId: number = null
15
16   // If this is not a reply to the video (thread), create or get the parent comment
17   if (video.url !== comment.inReplyTo) {
18     const [ parent ] = await addVideoComment(video, comment.inReplyTo)
19     if (!parent) {
20       logger.warn('Cannot fetch or get parent comment %s of comment %s.', comment.inReplyTo, comment.id)
21       return undefined
22     }
23
24     originCommentId = parent.originCommentId || parent.id
25     inReplyToCommentId = parent.id
26   }
27
28   return {
29     url: comment.url,
30     text: comment.content,
31     videoId: video.id,
32     accountId: actor.Account.id,
33     inReplyToCommentId,
34     originCommentId,
35     createdAt: new Date(comment.published),
36     updatedAt: new Date(comment.updated)
37   }
38 }
39
40 async function addVideoComments (instance: VideoModel, commentUrls: string[]) {
41   for (const commentUrl of commentUrls) {
42     await addVideoComment(instance, commentUrl)
43   }
44 }
45
46 async function addVideoComment (videoInstance: VideoModel, commentUrl: string) {
47   logger.info('Fetching remote video comment %s.', commentUrl)
48
49   const { body } = await doRequest({
50     uri: commentUrl,
51     json: true,
52     activityPub: true
53   })
54
55   if (isVideoCommentObjectValid(body) === false) {
56     logger.debug('Remote video comment JSON is not valid.', { body })
57     return undefined
58   }
59
60   const actorUrl = body.attributedTo
61   if (!actorUrl) return []
62
63   const actor = await getOrCreateActorAndServerAndModel(actorUrl)
64   const entry = await videoCommentActivityObjectToDBAttributes(videoInstance, actor, body)
65   if (!entry) return []
66
67   return VideoCommentModel.findOrCreate({
68     where: {
69       url: body.id
70     },
71     defaults: entry
72   })
73 }
74
75 async function resolveThread (url: string, comments: VideoCommentModel[] = []) {
76    // Already have this comment?
77   const commentFromDatabase = await VideoCommentModel.loadByUrlAndPopulateReplyAndVideo(url)
78   if (commentFromDatabase) {
79     let parentComments = comments.concat([ commentFromDatabase ])
80
81     // Speed up things and resolve directly the thread
82     if (commentFromDatabase.InReplyToVideoComment) {
83       const data = await VideoCommentModel.listThreadParentComments(commentFromDatabase, undefined, 'DESC')
84
85       parentComments = parentComments.concat(data)
86     }
87
88     return resolveThread(commentFromDatabase.Video.url, parentComments)
89   }
90
91   try {
92     // Maybe it's a reply to a video?
93     const { video } = await getOrCreateAccountAndVideoAndChannel(url)
94
95     if (comments.length !== 0) {
96       const firstReply = comments[ comments.length - 1 ]
97       firstReply.inReplyToCommentId = null
98       firstReply.originCommentId = null
99       firstReply.videoId = video.id
100       comments[comments.length - 1] = await firstReply.save()
101
102       for (let i = comments.length - 2; i >= 0; i--) {
103         const comment = comments[ i ]
104         comment.originCommentId = firstReply.id
105         comment.inReplyToCommentId = comments[ i + 1 ].id
106         comment.videoId = video.id
107
108         comments[i] = await comment.save()
109       }
110     }
111
112     return { video, parents: comments }
113   } catch (err) {
114     logger.debug('Cannot get or create account and video and channel for reply %s, fetch comment', url, err)
115
116     if (comments.length > ACTIVITY_PUB.MAX_RECURSION_COMMENTS) {
117       throw new Error('Recursion limit reached when resolving a thread')
118     }
119
120     const { body } = await doRequest({
121       uri: url,
122       json: true,
123       activityPub: true
124     })
125
126     if (isVideoCommentObjectValid(body) === false) {
127       throw new Error('Remote video comment JSON is not valid :' + JSON.stringify(body))
128     }
129
130     const actorUrl = body.attributedTo
131     if (!actorUrl) throw new Error('Miss attributed to in comment')
132
133     const actor = await getOrCreateActorAndServerAndModel(actorUrl)
134     const comment = new VideoCommentModel({
135       url: body.url,
136       text: body.content,
137       videoId: null,
138       accountId: actor.Account.id,
139       inReplyToCommentId: null,
140       originCommentId: null,
141       createdAt: new Date(body.published),
142       updatedAt: new Date(body.updated)
143     })
144
145     return resolveThread(body.inReplyTo, comments.concat([ comment ]))
146   }
147
148 }
149
150 export {
151   videoCommentActivityObjectToDBAttributes,
152   addVideoComments,
153   addVideoComment,
154   resolveThread
155 }