]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/activitypub/video-comments.ts
92e1a9020788d570676fa6e1628e5f23b1453219
[github/Chocobozzz/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 { VideoModel } from '../../models/video/video'
6 import { VideoCommentModel } from '../../models/video/video-comment'
7 import { getOrCreateActorAndServerAndModel } from './actor'
8 import { getOrCreateVideoAndAccountAndChannel } from './videos'
9 import * as Bluebird from 'bluebird'
10 import { checkUrlsSameHost } from '../../helpers/activitypub'
11
12 type ResolveThreadParams = {
13 url: string,
14 comments?: VideoCommentModel[],
15 isVideo?: boolean,
16 commentCreated?: boolean
17 }
18 type ResolveThreadResult = Promise<{ video: VideoModel, comment: VideoCommentModel, 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: VideoCommentModel
89 if (comments.length !== 0) {
90 const firstReply = comments[ comments.length - 1 ]
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 ]
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]
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({
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) throw new Error('Miss attributed to in comment')
135
136 if (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 = await getOrCreateActorAndServerAndModel(actorUrl)
145 const comment = new VideoCommentModel({
146 url: body.id,
147 text: body.content,
148 videoId: null,
149 accountId: actor.Account.id,
150 inReplyToCommentId: null,
151 originCommentId: null,
152 createdAt: new Date(body.published),
153 updatedAt: new Date(body.updated)
154 })
155 comment.Account = actor.Account
156
157 return resolveThread({
158 url: body.inReplyTo,
159 comments: comments.concat([ comment ]),
160 commentCreated: true
161 })
162 }