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