Split types and typings
[oweals/peertube.git] / server / lib / activitypub / video-comments.ts
1 import { sanitizeAndCheckVideoCommentObject } from '../../helpers/custom-validators/activitypub/video-comments'
2 import { logger } from '../../helpers/logger'
3 import { doRequest } from '../../helpers/requests'
4 import { ACTIVITY_PUB, CRAWL_REQUEST_CONCURRENCY } from '../../initializers/constants'
5 import { VideoCommentModel } from '../../models/video/video-comment'
6 import { getOrCreateActorAndServerAndModel } from './actor'
7 import { getOrCreateVideoAndAccountAndChannel } from './videos'
8 import * as Bluebird from 'bluebird'
9 import { checkUrlsSameHost } from '../../helpers/activitypub'
10 import { MCommentOwner, MCommentOwnerVideo, MVideoAccountLightBlacklistAllFiles } from '../../types/models/video'
11
12 type ResolveThreadParams = {
13   url: string
14   comments?: MCommentOwner[]
15   isVideo?: boolean
16   commentCreated?: boolean
17 }
18 type ResolveThreadResult = Promise<{ video: MVideoAccountLightBlacklistAllFiles, comment: MCommentOwnerVideo, commentCreated: boolean }>
19
20 async function addVideoComments (commentUrls: string[]) {
21   return Bluebird.map(commentUrls, commentUrl => {
22     return resolveThread({ url: commentUrl, isVideo: false })
23   }, { concurrency: CRAWL_REQUEST_CONCURRENCY })
24 }
25
26 async function resolveThread (params: ResolveThreadParams): ResolveThreadResult {
27   const { url, isVideo } = params
28   if (params.commentCreated === undefined) params.commentCreated = false
29   if (params.comments === undefined) params.comments = []
30
31   // Already have this comment?
32   if (isVideo !== true) {
33     const result = await resolveCommentFromDB(params)
34     if (result) return result
35   }
36
37   try {
38     if (isVideo !== false) return await tryResolveThreadFromVideo(params)
39
40     return resolveParentComment(params)
41   } catch (err) {
42     logger.debug('Cannot get or create account and video and channel for reply %s, fetch comment', url, { err })
43
44     return resolveParentComment(params)
45   }
46 }
47
48 export {
49   addVideoComments,
50   resolveThread
51 }
52
53 // ---------------------------------------------------------------------------
54
55 async function resolveCommentFromDB (params: ResolveThreadParams) {
56   const { url, comments, commentCreated } = params
57
58   const commentFromDatabase = await VideoCommentModel.loadByUrlAndPopulateReplyAndVideoUrlAndAccount(url)
59   if (commentFromDatabase) {
60     let parentComments = comments.concat([ commentFromDatabase ])
61
62     // Speed up things and resolve directly the thread
63     if (commentFromDatabase.InReplyToVideoComment) {
64       const data = await VideoCommentModel.listThreadParentComments(commentFromDatabase, undefined, 'DESC')
65
66       parentComments = parentComments.concat(data)
67     }
68
69     return resolveThread({
70       url: commentFromDatabase.Video.url,
71       comments: parentComments,
72       isVideo: true,
73       commentCreated
74     })
75   }
76
77   return undefined
78 }
79
80 async function tryResolveThreadFromVideo (params: ResolveThreadParams) {
81   const { url, comments, commentCreated } = params
82
83   // Maybe it's a reply to a video?
84   // If yes, it's done: we resolved all the thread
85   const syncParam = { likes: true, dislikes: true, shares: true, comments: false, thumbnail: true, refreshVideo: false }
86   const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: url, syncParam })
87
88   let resultComment: MCommentOwnerVideo
89   if (comments.length !== 0) {
90     const firstReply = comments[comments.length - 1] as MCommentOwnerVideo
91     firstReply.inReplyToCommentId = null
92     firstReply.originCommentId = null
93     firstReply.videoId = video.id
94     firstReply.changed('updatedAt', true)
95     firstReply.Video = video
96
97     comments[comments.length - 1] = await firstReply.save()
98
99     for (let i = comments.length - 2; i >= 0; i--) {
100       const comment = comments[i] as MCommentOwnerVideo
101       comment.originCommentId = firstReply.id
102       comment.inReplyToCommentId = comments[i + 1].id
103       comment.videoId = video.id
104       comment.changed('updatedAt', true)
105       comment.Video = video
106
107       comments[i] = await comment.save()
108     }
109
110     resultComment = comments[0] as MCommentOwnerVideo
111   }
112
113   return { video, comment: resultComment, commentCreated }
114 }
115
116 async function resolveParentComment (params: ResolveThreadParams) {
117   const { url, comments } = params
118
119   if (comments.length > ACTIVITY_PUB.MAX_RECURSION_COMMENTS) {
120     throw new Error('Recursion limit reached when resolving a thread')
121   }
122
123   const { body } = await doRequest<any>({
124     uri: url,
125     json: true,
126     activityPub: true
127   })
128
129   if (sanitizeAndCheckVideoCommentObject(body) === false) {
130     throw new Error('Remote video comment JSON is not valid:' + JSON.stringify(body))
131   }
132
133   const actorUrl = body.attributedTo
134   if (!actorUrl && body.type !== 'Tombstone') throw new Error('Miss attributed to in comment')
135
136   if (actorUrl && checkUrlsSameHost(url, actorUrl) !== true) {
137     throw new Error(`Actor url ${actorUrl} has not the same host than the comment url ${url}`)
138   }
139
140   if (checkUrlsSameHost(body.id, url) !== true) {
141     throw new Error(`Comment url ${url} host is different from the AP object id ${body.id}`)
142   }
143
144   const actor = actorUrl
145     ? await getOrCreateActorAndServerAndModel(actorUrl, 'all')
146     : null
147
148   const comment = new VideoCommentModel({
149     url: body.id,
150     text: body.content ? body.content : '',
151     videoId: null,
152     accountId: actor ? actor.Account.id : null,
153     inReplyToCommentId: null,
154     originCommentId: null,
155     createdAt: new Date(body.published),
156     updatedAt: new Date(body.updated),
157     deletedAt: body.deleted ? new Date(body.deleted) : null
158   }) as MCommentOwner
159   comment.Account = actor ? actor.Account : null
160
161   return resolveThread({
162     url: body.inReplyTo,
163     comments: comments.concat([ comment ]),
164     commentCreated: true
165   })
166 }