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