]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/activitypub/video-comments.ts
beff557bcc6999304d5caa188cf30f6654a6f309
[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
13 async function videoCommentActivityObjectToDBAttributes (video: VideoModel, actor: ActorModel, comment: VideoCommentObject) {
14 let originCommentId: number = null
15 let inReplyToCommentId: number = null
16
17 // If this is not a reply to the video (thread), create or get the parent comment
18 if (video.url !== comment.inReplyTo) {
19 const [ parent ] = await addVideoComment(video, comment.inReplyTo)
20 if (!parent) {
21 logger.warn('Cannot fetch or get parent comment %s of comment %s.', comment.inReplyTo, comment.id)
22 return undefined
23 }
24
25 originCommentId = parent.originCommentId || parent.id
26 inReplyToCommentId = parent.id
27 }
28
29 return {
30 url: comment.url,
31 text: comment.content,
32 videoId: video.id,
33 accountId: actor.Account.id,
34 inReplyToCommentId,
35 originCommentId,
36 createdAt: new Date(comment.published),
37 updatedAt: new Date(comment.updated)
38 }
39 }
40
41 async function addVideoComments (commentUrls: string[], instance: VideoModel) {
42 return Bluebird.map(commentUrls, commentUrl => {
43 return addVideoComment(instance, commentUrl)
44 }, { concurrency: CRAWL_REQUEST_CONCURRENCY })
45 }
46
47 async 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
56 if (sanitizeAndCheckVideoCommentObject(body) === false) {
57 logger.debug('Remote video comment JSON is not valid.', { body })
58 return undefined
59 }
60
61 const actorUrl = body.attributedTo
62 if (!actorUrl) return []
63
64 const actor = await getOrCreateActorAndServerAndModel(actorUrl)
65 const entry = await videoCommentActivityObjectToDBAttributes(videoInstance, actor, body)
66 if (!entry) return []
67
68 return VideoCommentModel.findOrCreate({
69 where: {
70 url: body.id
71 },
72 defaults: entry
73 })
74 }
75
76 async function resolveThread (url: string, comments: VideoCommentModel[] = []) {
77 // Already have this comment?
78 const commentFromDatabase = await VideoCommentModel.loadByUrlAndPopulateReplyAndVideo(url)
79 if (commentFromDatabase) {
80 let parentComments = comments.concat([ commentFromDatabase ])
81
82 // Speed up things and resolve directly the thread
83 if (commentFromDatabase.InReplyToVideoComment) {
84 const data = await VideoCommentModel.listThreadParentComments(commentFromDatabase, undefined, 'DESC')
85
86 parentComments = parentComments.concat(data)
87 }
88
89 return resolveThread(commentFromDatabase.Video.url, parentComments)
90 }
91
92 try {
93 // Maybe it's a reply to a video?
94 const { video } = await getOrCreateVideoAndAccountAndChannel(url)
95
96 if (comments.length !== 0) {
97 const firstReply = comments[ comments.length - 1 ]
98 firstReply.inReplyToCommentId = null
99 firstReply.originCommentId = null
100 firstReply.videoId = video.id
101 comments[comments.length - 1] = await firstReply.save()
102
103 for (let i = comments.length - 2; i >= 0; i--) {
104 const comment = comments[ i ]
105 comment.originCommentId = firstReply.id
106 comment.inReplyToCommentId = comments[ i + 1 ].id
107 comment.videoId = video.id
108
109 comments[i] = await comment.save()
110 }
111 }
112
113 return { video, parents: comments }
114 } catch (err) {
115 logger.debug('Cannot get or create account and video and channel for reply %s, fetch comment', url, { err })
116
117 if (comments.length > ACTIVITY_PUB.MAX_RECURSION_COMMENTS) {
118 throw new Error('Recursion limit reached when resolving a thread')
119 }
120
121 const { body } = await doRequest({
122 uri: url,
123 json: true,
124 activityPub: true
125 })
126
127 if (sanitizeAndCheckVideoCommentObject(body) === false) {
128 throw new Error('Remote video comment JSON is not valid :' + JSON.stringify(body))
129 }
130
131 const actorUrl = body.attributedTo
132 if (!actorUrl) throw new Error('Miss attributed to in comment')
133
134 const actor = await getOrCreateActorAndServerAndModel(actorUrl)
135 const comment = new VideoCommentModel({
136 url: body.url,
137 text: body.content,
138 videoId: null,
139 accountId: actor.Account.id,
140 inReplyToCommentId: null,
141 originCommentId: null,
142 createdAt: new Date(body.published),
143 updatedAt: new Date(body.updated)
144 })
145
146 return resolveThread(body.inReplyTo, comments.concat([ comment ]))
147 }
148
149 }
150
151 export {
152 videoCommentActivityObjectToDBAttributes,
153 addVideoComments,
154 addVideoComment,
155 resolveThread
156 }