]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/activitypub/video-comments.ts
Adding frontend peertubeHelpers.getBaseRouterRoute. (#4153)
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / video-comments.ts
CommitLineData
db4b15f2
C
1import * as Bluebird from 'bluebird'
2import { checkUrlsSameHost } from '../../helpers/activitypub'
5cf13500 3import { sanitizeAndCheckVideoCommentObject } from '../../helpers/custom-validators/activitypub/video-comments'
2ccaeeb3 4import { logger } from '../../helpers/logger'
db4b15f2 5import { doJSONRequest } from '../../helpers/requests'
74dc3bca 6import { ACTIVITY_PUB, CRAWL_REQUEST_CONCURRENCY } from '../../initializers/constants'
2ccaeeb3 7import { VideoCommentModel } from '../../models/video/video-comment'
db4b15f2 8import { MCommentOwner, MCommentOwnerVideo, MVideoAccountLightBlacklistAllFiles } from '../../types/models/video'
2ccaeeb3 9import { getOrCreateActorAndServerAndModel } from './actor'
304a84d5 10import { getOrCreateAPVideo } from './videos'
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[]) {
266131e0
C
21 return Bluebird.map(commentUrls, async commentUrl => {
22 try {
23 await resolveThread({ url: commentUrl, isVideo: false })
24 } catch (err) {
25 logger.warn('Cannot resolve thread %s.', commentUrl, { err })
26 }
f6eebcb3 27 }, { concurrency: CRAWL_REQUEST_CONCURRENCY })
2ccaeeb3
C
28}
29
6b9c966f
C
30async function resolveThread (params: ResolveThreadParams): ResolveThreadResult {
31 const { url, isVideo } = params
32 if (params.commentCreated === undefined) params.commentCreated = false
33 if (params.comments === undefined) params.comments = []
2ccaeeb3 34
10c8b0b7
C
35 // If it is not a video, or if we don't know if it's a video
36 if (isVideo === false || isVideo === undefined) {
6b9c966f
C
37 const result = await resolveCommentFromDB(params)
38 if (result) return result
2ccaeeb3
C
39 }
40
6b9c966f 41 try {
10c8b0b7
C
42 // If it is a video, or if we don't know if it's a video
43 if (isVideo === true || isVideo === undefined) {
44 // Keep await so we catch the exception
45 return await tryResolveThreadFromVideo(params)
46 }
6b9c966f 47 } catch (err) {
74d249bc 48 logger.debug('Cannot resolve thread from video %s, maybe because it was not a video', url, { err })
5c6d985f 49 }
403c69c5 50
74d249bc 51 return resolveRemoteParentComment(params)
6b9c966f 52}
5c6d985f 53
6b9c966f
C
54export {
55 addVideoComments,
56 resolveThread
57}
2ccaeeb3 58
6b9c966f 59// ---------------------------------------------------------------------------
83e6519b 60
6b9c966f
C
61async function resolveCommentFromDB (params: ResolveThreadParams) {
62 const { url, comments, commentCreated } = params
2ccaeeb3 63
6b9c966f 64 const commentFromDatabase = await VideoCommentModel.loadByUrlAndPopulateReplyAndVideoUrlAndAccount(url)
2ccaeeb3
C
65 if (commentFromDatabase) {
66 let parentComments = comments.concat([ commentFromDatabase ])
67
68 // Speed up things and resolve directly the thread
69 if (commentFromDatabase.InReplyToVideoComment) {
70 const data = await VideoCommentModel.listThreadParentComments(commentFromDatabase, undefined, 'DESC')
2ccaeeb3
C
71
72 parentComments = parentComments.concat(data)
73 }
74
6b9c966f
C
75 return resolveThread({
76 url: commentFromDatabase.Video.url,
77 comments: parentComments,
78 isVideo: true,
79 commentCreated
80 })
2ccaeeb3
C
81 }
82
6b9c966f
C
83 return undefined
84}
85
86async function tryResolveThreadFromVideo (params: ResolveThreadParams) {
87 const { url, comments, commentCreated } = params
88
89 // Maybe it's a reply to a video?
90 // If yes, it's done: we resolved all the thread
91 const syncParam = { likes: true, dislikes: true, shares: true, comments: false, thumbnail: true, refreshVideo: false }
304a84d5 92 const { video } = await getOrCreateAPVideo({ videoObject: url, syncParam })
6b9c966f 93
403c69c5
C
94 if (video.isOwned() && !video.hasPrivacyForFederation()) {
95 throw new Error('Cannot resolve thread of video with privacy that is not compatible with federation')
96 }
97
453e83ea 98 let resultComment: MCommentOwnerVideo
6b9c966f 99 if (comments.length !== 0) {
a1587156 100 const firstReply = comments[comments.length - 1] as MCommentOwnerVideo
6b9c966f
C
101 firstReply.inReplyToCommentId = null
102 firstReply.originCommentId = null
103 firstReply.videoId = video.id
104 firstReply.changed('updatedAt', true)
105 firstReply.Video = video
106
107 comments[comments.length - 1] = await firstReply.save()
108
109 for (let i = comments.length - 2; i >= 0; i--) {
a1587156 110 const comment = comments[i] as MCommentOwnerVideo
6b9c966f 111 comment.originCommentId = firstReply.id
a1587156 112 comment.inReplyToCommentId = comments[i + 1].id
6b9c966f
C
113 comment.videoId = video.id
114 comment.changed('updatedAt', true)
115 comment.Video = video
116
117 comments[i] = await comment.save()
2ccaeeb3
C
118 }
119
453e83ea 120 resultComment = comments[0] as MCommentOwnerVideo
6b9c966f 121 }
2ccaeeb3 122
6b9c966f
C
123 return { video, comment: resultComment, commentCreated }
124}
2ccaeeb3 125
74d249bc 126async function resolveRemoteParentComment (params: ResolveThreadParams) {
6b9c966f 127 const { url, comments } = params
2ccaeeb3 128
6b9c966f
C
129 if (comments.length > ACTIVITY_PUB.MAX_RECURSION_COMMENTS) {
130 throw new Error('Recursion limit reached when resolving a thread')
131 }
2ccaeeb3 132
db4b15f2 133 const { body } = await doJSONRequest<any>(url, { activityPub: true })
2ccaeeb3 134
6b9c966f 135 if (sanitizeAndCheckVideoCommentObject(body) === false) {
74d249bc 136 throw new Error(`Remote video comment JSON ${url} is not valid:` + JSON.stringify(body))
6b9c966f 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}