]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/activitypub/video-comments.ts
Add import finished and video published notifs
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / video-comments.ts
1 import { VideoCommentObject } from '../../../shared/models/activitypub/objects/video-comment-object'
2 import { sanitizeAndCheckVideoCommentObject } from '../../helpers/custom-validators/activitypub/video-comments'
3 import { logger } from '../../helpers/logger'
4 import { doRequest } from '../../helpers/requests'
5 import { ACTIVITY_PUB, CRAWL_REQUEST_CONCURRENCY } from '../../initializers'
6 import { ActorModel } from '../../models/activitypub/actor'
7 import { VideoModel } from '../../models/video/video'
8 import { VideoCommentModel } from '../../models/video/video-comment'
9 import { getOrCreateActorAndServerAndModel } from './actor'
10 import { getOrCreateVideoAndAccountAndChannel } from './videos'
11 import * as Bluebird from 'bluebird'
12 import { checkUrlsSameHost } from '../../helpers/activitypub'
13
14 async 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) {
20 const { comment: parent } = await addVideoComment(video, comment.inReplyTo)
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 {
31 url: comment.id,
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
42 async function addVideoComments (commentUrls: string[], instance: VideoModel) {
43 return Bluebird.map(commentUrls, commentUrl => {
44 return addVideoComment(instance, commentUrl)
45 }, { concurrency: CRAWL_REQUEST_CONCURRENCY })
46 }
47
48 async 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
57 if (sanitizeAndCheckVideoCommentObject(body) === false) {
58 logger.debug('Remote video comment JSON is not valid.', { body })
59 return { created: false }
60 }
61
62 const actorUrl = body.attributedTo
63 if (!actorUrl) return { created: false }
64
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
73 const actor = await getOrCreateActorAndServerAndModel(actorUrl, 'all')
74 const entry = await videoCommentActivityObjectToDBAttributes(videoInstance, actor, body)
75 if (!entry) return { created: false }
76
77 const [ comment, created ] = await VideoCommentModel.findOrCreate({
78 where: {
79 url: body.id
80 },
81 defaults: entry
82 })
83 comment.Account = actor.Account
84 comment.Video = videoInstance
85
86 return { comment, created }
87 }
88
89 async function resolveThread (url: string, comments: VideoCommentModel[] = []) {
90 // Already have this comment?
91 const commentFromDatabase = await VideoCommentModel.loadByUrlAndPopulateReplyAndVideo(url)
92 if (commentFromDatabase) {
93 let parentComments = comments.concat([ commentFromDatabase ])
94
95 // Speed up things and resolve directly the thread
96 if (commentFromDatabase.InReplyToVideoComment) {
97 const data = await VideoCommentModel.listThreadParentComments(commentFromDatabase, undefined, 'DESC')
98
99 parentComments = parentComments.concat(data)
100 }
101
102 return resolveThread(commentFromDatabase.Video.url, parentComments)
103 }
104
105 try {
106 // Maybe it's a reply to a video?
107 // If yes, it's done: we resolved all the thread
108 const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: url })
109
110 if (comments.length !== 0) {
111 const firstReply = comments[ comments.length - 1 ]
112 firstReply.inReplyToCommentId = null
113 firstReply.originCommentId = null
114 firstReply.videoId = video.id
115 comments[comments.length - 1] = await firstReply.save()
116
117 for (let i = comments.length - 2; i >= 0; i--) {
118 const comment = comments[ i ]
119 comment.originCommentId = firstReply.id
120 comment.inReplyToCommentId = comments[ i + 1 ].id
121 comment.videoId = video.id
122
123 comments[i] = await comment.save()
124 }
125 }
126
127 return { video, parents: comments }
128 } catch (err) {
129 logger.debug('Cannot get or create account and video and channel for reply %s, fetch comment', url, { err })
130
131 if (comments.length > ACTIVITY_PUB.MAX_RECURSION_COMMENTS) {
132 throw new Error('Recursion limit reached when resolving a thread')
133 }
134
135 const { body } = await doRequest({
136 uri: url,
137 json: true,
138 activityPub: true
139 })
140
141 if (sanitizeAndCheckVideoCommentObject(body) === false) {
142 throw new Error('Remote video comment JSON is not valid :' + JSON.stringify(body))
143 }
144
145 const actorUrl = body.attributedTo
146 if (!actorUrl) throw new Error('Miss attributed to in comment')
147
148 if (checkUrlsSameHost(url, actorUrl) !== true) {
149 throw new Error(`Actor url ${actorUrl} has not the same host than the comment url ${url}`)
150 }
151
152 if (checkUrlsSameHost(body.id, url) !== true) {
153 throw new Error(`Comment url ${url} host is different from the AP object id ${body.id}`)
154 }
155
156 const actor = await getOrCreateActorAndServerAndModel(actorUrl)
157 const comment = new VideoCommentModel({
158 url: body.id,
159 text: body.content,
160 videoId: null,
161 accountId: actor.Account.id,
162 inReplyToCommentId: null,
163 originCommentId: null,
164 createdAt: new Date(body.published),
165 updatedAt: new Date(body.updated)
166 })
167
168 return resolveThread(body.inReplyTo, comments.concat([ comment ]))
169 }
170
171 }
172
173 export {
174 videoCommentActivityObjectToDBAttributes,
175 addVideoComments,
176 addVideoComment,
177 resolveThread
178 }