]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/activitypub/video-comments.ts
Fix HLS federation
[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,
2ba92871 37 createdAt: new Date(comment.published)
2ccaeeb3
C
38 }
39}
40
8fffe21a 41async function addVideoComments (commentUrls: string[], instance: VideoModel) {
f6eebcb3
C
42 return Bluebird.map(commentUrls, commentUrl => {
43 return addVideoComment(instance, commentUrl)
44 }, { concurrency: CRAWL_REQUEST_CONCURRENCY })
2ccaeeb3
C
45}
46
47async function addVideoComment (videoInstance: VideoModel, commentUrl: string) {
48 logger.info('Fetching remote video comment %s.', commentUrl)
49
50 const { body } = await doRequest({
51 uri: commentUrl,
52 json: true,
53 activityPub: true
54 })
55
5cf13500 56 if (sanitizeAndCheckVideoCommentObject(body) === false) {
2ccaeeb3 57 logger.debug('Remote video comment JSON is not valid.', { body })
83e6519b 58 return { created: false }
2ccaeeb3
C
59 }
60
61 const actorUrl = body.attributedTo
83e6519b 62 if (!actorUrl) return { created: false }
2ccaeeb3 63
5c6d985f
C
64 if (checkUrlsSameHost(commentUrl, actorUrl) !== true) {
65 throw new Error(`Actor url ${actorUrl} has not the same host than the comment url ${commentUrl}`)
66 }
67
68 if (checkUrlsSameHost(body.id, commentUrl) !== true) {
69 throw new Error(`Comment url ${commentUrl} host is different from the AP object id ${body.id}`)
70 }
71
cef534ed 72 const actor = await getOrCreateActorAndServerAndModel(actorUrl, 'all')
2ccaeeb3 73 const entry = await videoCommentActivityObjectToDBAttributes(videoInstance, actor, body)
83e6519b 74 if (!entry) return { created: false }
2ccaeeb3 75
2ba92871 76 const [ comment, created ] = await VideoCommentModel.upsert<VideoCommentModel>(entry, { returning: true })
cef534ed
C
77 comment.Account = actor.Account
78 comment.Video = videoInstance
83e6519b
C
79
80 return { comment, created }
2ccaeeb3
C
81}
82
83async function resolveThread (url: string, comments: VideoCommentModel[] = []) {
84 // Already have this comment?
85 const commentFromDatabase = await VideoCommentModel.loadByUrlAndPopulateReplyAndVideo(url)
86 if (commentFromDatabase) {
87 let parentComments = comments.concat([ commentFromDatabase ])
88
89 // Speed up things and resolve directly the thread
90 if (commentFromDatabase.InReplyToVideoComment) {
91 const data = await VideoCommentModel.listThreadParentComments(commentFromDatabase, undefined, 'DESC')
2ccaeeb3
C
92
93 parentComments = parentComments.concat(data)
94 }
95
96 return resolveThread(commentFromDatabase.Video.url, parentComments)
97 }
98
99 try {
100 // Maybe it's a reply to a video?
83e6519b 101 // If yes, it's done: we resolved all the thread
4157cdb1 102 const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: url })
2ccaeeb3
C
103
104 if (comments.length !== 0) {
105 const firstReply = comments[ comments.length - 1 ]
106 firstReply.inReplyToCommentId = null
107 firstReply.originCommentId = null
108 firstReply.videoId = video.id
109 comments[comments.length - 1] = await firstReply.save()
110
111 for (let i = comments.length - 2; i >= 0; i--) {
112 const comment = comments[ i ]
113 comment.originCommentId = firstReply.id
114 comment.inReplyToCommentId = comments[ i + 1 ].id
115 comment.videoId = video.id
116
117 comments[i] = await comment.save()
118 }
119 }
120
121 return { video, parents: comments }
122 } catch (err) {
d5b7d911 123 logger.debug('Cannot get or create account and video and channel for reply %s, fetch comment', url, { err })
2ccaeeb3
C
124
125 if (comments.length > ACTIVITY_PUB.MAX_RECURSION_COMMENTS) {
126 throw new Error('Recursion limit reached when resolving a thread')
127 }
128
129 const { body } = await doRequest({
130 uri: url,
131 json: true,
132 activityPub: true
133 })
134
5cf13500 135 if (sanitizeAndCheckVideoCommentObject(body) === false) {
2ccaeeb3
C
136 throw new Error('Remote video comment JSON is not valid :' + JSON.stringify(body))
137 }
138
139 const actorUrl = body.attributedTo
140 if (!actorUrl) throw new Error('Miss attributed to in comment')
141
5c6d985f
C
142 if (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
2ccaeeb3
C
150 const actor = await getOrCreateActorAndServerAndModel(actorUrl)
151 const comment = new VideoCommentModel({
8578e3b5 152 url: body.id,
2ccaeeb3
C
153 text: body.content,
154 videoId: null,
155 accountId: actor.Account.id,
156 inReplyToCommentId: null,
157 originCommentId: null,
158 createdAt: new Date(body.published),
159 updatedAt: new Date(body.updated)
160 })
161
162 return resolveThread(body.inReplyTo, comments.concat([ comment ]))
163 }
164
165}
166
167export {
168 videoCommentActivityObjectToDBAttributes,
169 addVideoComments,
170 addVideoComment,
171 resolveThread
172}