]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/activitypub/video-comments.ts
Merge from upstream
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / video-comments.ts
1 import { VideoCommentObject } from '../../../shared/models/activitypub/objects/video-comment-object'
2 import { sanitizeAndCheckVideoCommentObject } from '../../helpers/custom-validators/activitypub/video-comments'
3 import { logger } from '../../helpers/logger'
4 import { doRequest } from '../../helpers/requests'
5 import { ACTIVITY_PUB, CRAWL_REQUEST_CONCURRENCY } 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 { getOrCreateVideoAndAccountAndChannel } from './videos'
11 import * as Bluebird from 'bluebird'
12 import { checkUrlsSameHost } from '../../helpers/activitypub'
13
14 async function videoCommentActivityObjectToDBAttributes (video: VideoModel, actor: ActorModel, comment: VideoCommentObject) {
15 let originCommentId: number = null
16 let inReplyToCommentId: number = null
17
18 // If this is not a reply to the video (thread), create or get the parent comment
19 if (video.url !== comment.inReplyTo) {
20 const { comment: parent } = await addVideoComment(video, comment.inReplyTo)
21 if (!parent) {
22 logger.warn('Cannot fetch or get parent comment %s of comment %s.', comment.inReplyTo, comment.id)
23 return undefined
24 }
25
26 originCommentId = parent.originCommentId || parent.id
27 inReplyToCommentId = parent.id
28 }
29
30 return {
31 url: comment.id,
32 text: comment.content,
33 videoId: video.id,
34 accountId: actor.Account.id,
35 inReplyToCommentId,
36 originCommentId,
37 createdAt: new Date(comment.published),
38 updatedAt: new Date(comment.updated)
39 }
40 }
41
42 async function addVideoComments (commentUrls: string[], instance: VideoModel) {
43 return Bluebird.map(commentUrls, commentUrl => {
44 return addVideoComment(instance, commentUrl)
45 }, { concurrency: CRAWL_REQUEST_CONCURRENCY })
46 }
47
48 async function addVideoComment (videoInstance: VideoModel, commentUrl: string) {
49 logger.info('Fetching remote video comment %s.', commentUrl)
50
51 const { body } = await doRequest({
52 uri: commentUrl,
53 json: true,
54 activityPub: true
55 })
56
57 if (sanitizeAndCheckVideoCommentObject(body) === false) {
58 logger.debug('Remote video comment JSON is not valid.', { body })
59 return { created: false }
60 }
61
62 const actorUrl = body.attributedTo
63 if (!actorUrl) return { created: false }
64
65 if (checkUrlsSameHost(commentUrl, actorUrl) !== true) {
66 throw new Error(`Actor url ${actorUrl} has not the same host than the comment url ${commentUrl}`)
67 }
68
69 if (checkUrlsSameHost(body.id, commentUrl) !== true) {
70 throw new Error(`Comment url ${commentUrl} host is different from the AP object id ${body.id}`)
71 }
72
73 const actor = await getOrCreateActorAndServerAndModel(actorUrl)
74 const entry = await videoCommentActivityObjectToDBAttributes(videoInstance, actor, body)
75 if (!entry) return { created: false }
76
77 const [ comment, created ] = await VideoCommentModel.findOrCreate({
78 where: {
79 url: body.id
80 },
81 defaults: entry
82 })
83
84 return { comment, created }
85 }
86
87 async function resolveThread (url: string, comments: VideoCommentModel[] = []) {
88 // Already have this comment?
89 const commentFromDatabase = await VideoCommentModel.loadByUrlAndPopulateReplyAndVideo(url)
90 if (commentFromDatabase) {
91 let parentComments = comments.concat([ commentFromDatabase ])
92
93 // Speed up things and resolve directly the thread
94 if (commentFromDatabase.InReplyToVideoComment) {
95 const data = await VideoCommentModel.listThreadParentComments(commentFromDatabase, undefined, 'DESC')
96
97 parentComments = parentComments.concat(data)
98 }
99
100 return resolveThread(commentFromDatabase.Video.url, parentComments)
101 }
102
103 try {
104 // Maybe it's a reply to a video?
105 // If yes, it's done: we resolved all the thread
106 const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: url })
107
108 if (comments.length !== 0) {
109 const firstReply = comments[ comments.length - 1 ]
110 firstReply.inReplyToCommentId = null
111 firstReply.originCommentId = null
112 firstReply.videoId = video.id
113 comments[comments.length - 1] = await firstReply.save()
114
115 for (let i = comments.length - 2; i >= 0; i--) {
116 const comment = comments[ i ]
117 comment.originCommentId = firstReply.id
118 comment.inReplyToCommentId = comments[ i + 1 ].id
119 comment.videoId = video.id
120
121 comments[i] = await comment.save()
122 }
123 }
124
125 return { video, parents: comments }
126 } catch (err) {
127 logger.debug('Cannot get or create account and video and channel for reply %s, fetch comment', url, { err })
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 doRequest({
134 uri: url,
135 json: true,
136 activityPub: true
137 })
138
139 if (sanitizeAndCheckVideoCommentObject(body) === false) {
140 throw new Error('Remote video comment JSON is not valid :' + JSON.stringify(body))
141 }
142
143 const actorUrl = body.attributedTo
144 if (!actorUrl) throw new Error('Miss attributed to in comment')
145
146 if (checkUrlsSameHost(url, actorUrl) !== true) {
147 throw new Error(`Actor url ${actorUrl} has not the same host than the comment url ${url}`)
148 }
149
150 if (checkUrlsSameHost(body.id, url) !== true) {
151 throw new Error(`Comment url ${url} host is different from the AP object id ${body.id}`)
152 }
153
154 const actor = await getOrCreateActorAndServerAndModel(actorUrl)
155 const comment = new VideoCommentModel({
156 url: body.id,
157 text: body.content,
158 videoId: null,
159 accountId: actor.Account.id,
160 inReplyToCommentId: null,
161 originCommentId: null,
162 createdAt: new Date(body.published),
163 updatedAt: new Date(body.updated)
164 })
165
166 return resolveThread(body.inReplyTo, comments.concat([ comment ]))
167 }
168
169 }
170
171 export {
172 videoCommentActivityObjectToDBAttributes,
173 addVideoComments,
174 addVideoComment,
175 resolveThread
176 }