]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/activitypub/video-comments.ts
Merge branch 'hotfix/docker' into develop
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / video-comments.ts
CommitLineData
2ccaeeb3 1import { VideoCommentObject } from '../../../shared/models/activitypub/objects/video-comment-object'
5cf13500 2import { sanitizeAndCheckVideoCommentObject } from '../../helpers/custom-validators/activitypub/video-comments'
2ccaeeb3
C
3import { logger } from '../../helpers/logger'
4import { doRequest } from '../../helpers/requests'
f6eebcb3 5import { ACTIVITY_PUB, CRAWL_REQUEST_CONCURRENCY } from '../../initializers'
2ccaeeb3
C
6import { ActorModel } from '../../models/activitypub/actor'
7import { VideoModel } from '../../models/video/video'
8import { VideoCommentModel } from '../../models/video/video-comment'
9import { getOrCreateActorAndServerAndModel } from './actor'
1297eb5d 10import { getOrCreateVideoAndAccountAndChannel } from './videos'
f6eebcb3 11import * as Bluebird from 'bluebird'
5c6d985f 12import { checkUrlsSameHost } from '../../helpers/activitypub'
2ccaeeb3
C
13
14async 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) {
83e6519b 20 const { comment: parent } = await addVideoComment(video, comment.inReplyTo)
2ccaeeb3
C
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 {
47d0b3ee 31 url: comment.id,
2ccaeeb3
C
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
8fffe21a 42async function addVideoComments (commentUrls: string[], instance: VideoModel) {
f6eebcb3
C
43 return Bluebird.map(commentUrls, commentUrl => {
44 return addVideoComment(instance, commentUrl)
45 }, { concurrency: CRAWL_REQUEST_CONCURRENCY })
2ccaeeb3
C
46}
47
48async 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
5cf13500 57 if (sanitizeAndCheckVideoCommentObject(body) === false) {
2ccaeeb3 58 logger.debug('Remote video comment JSON is not valid.', { body })
83e6519b 59 return { created: false }
2ccaeeb3
C
60 }
61
62 const actorUrl = body.attributedTo
83e6519b 63 if (!actorUrl) return { created: false }
2ccaeeb3 64
5c6d985f
C
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
2ccaeeb3
C
73 const actor = await getOrCreateActorAndServerAndModel(actorUrl)
74 const entry = await videoCommentActivityObjectToDBAttributes(videoInstance, actor, body)
83e6519b 75 if (!entry) return { created: false }
2ccaeeb3 76
83e6519b 77 const [ comment, created ] = await VideoCommentModel.findOrCreate({
2ccaeeb3
C
78 where: {
79 url: body.id
80 },
81 defaults: entry
82 })
83e6519b
C
83
84 return { comment, created }
2ccaeeb3
C
85}
86
87async 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')
2ccaeeb3
C
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?
83e6519b 105 // If yes, it's done: we resolved all the thread
4157cdb1 106 const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: url })
2ccaeeb3
C
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) {
d5b7d911 127 logger.debug('Cannot get or create account and video and channel for reply %s, fetch comment', url, { err })
2ccaeeb3
C
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
5cf13500 139 if (sanitizeAndCheckVideoCommentObject(body) === false) {
2ccaeeb3
C
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
5c6d985f
C
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
2ccaeeb3
C
154 const actor = await getOrCreateActorAndServerAndModel(actorUrl)
155 const comment = new VideoCommentModel({
8578e3b5 156 url: body.id,
2ccaeeb3
C
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
171export {
172 videoCommentActivityObjectToDBAttributes,
173 addVideoComments,
174 addVideoComment,
175 resolveThread
176}