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