aboutsummaryrefslogtreecommitdiffhomepage
path: root/server
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2018-08-22 16:59:55 +0200
committerChocobozzz <me@florianbigard.com>2018-08-27 09:41:54 +0200
commit83e6519ba4ee752dc3148a16c69effbfccb13e6b (patch)
treef2d043c549c46c45556965269aec5aebbda9386d /server
parent1297eb5db651a230474670c5da1517862fb9cc3e (diff)
downloadPeerTube-83e6519ba4ee752dc3148a16c69effbfccb13e6b.tar.gz
PeerTube-83e6519ba4ee752dc3148a16c69effbfccb13e6b.tar.zst
PeerTube-83e6519ba4ee752dc3148a16c69effbfccb13e6b.zip
Refractor comment creation from federation
Diffstat (limited to 'server')
-rw-r--r--server/lib/activitypub/process/process-create.ts47
-rw-r--r--server/lib/activitypub/video-comments.ts13
-rw-r--r--server/tests/api/videos/index.ts4
3 files changed, 19 insertions, 45 deletions
diff --git a/server/lib/activitypub/process/process-create.ts b/server/lib/activitypub/process/process-create.ts
index e8f5ade06..75f07d131 100644
--- a/server/lib/activitypub/process/process-create.ts
+++ b/server/lib/activitypub/process/process-create.ts
@@ -9,7 +9,7 @@ import { ActorModel } from '../../../models/activitypub/actor'
9import { VideoAbuseModel } from '../../../models/video/video-abuse' 9import { VideoAbuseModel } from '../../../models/video/video-abuse'
10import { VideoCommentModel } from '../../../models/video/video-comment' 10import { VideoCommentModel } from '../../../models/video/video-comment'
11import { getOrCreateActorAndServerAndModel } from '../actor' 11import { getOrCreateActorAndServerAndModel } from '../actor'
12import { resolveThread } from '../video-comments' 12import { addVideoComment, resolveThread } from '../video-comments'
13import { getOrCreateVideoAndAccountAndChannel } from '../videos' 13import { getOrCreateVideoAndAccountAndChannel } from '../videos'
14import { forwardActivity, forwardVideoRelatedActivity } from '../send/utils' 14import { forwardActivity, forwardVideoRelatedActivity } from '../send/utils'
15 15
@@ -120,48 +120,19 @@ async function processCreateVideoAbuse (actor: ActorModel, videoAbuseToCreateDat
120} 120}
121 121
122async function processCreateVideoComment (byActor: ActorModel, activity: ActivityCreate) { 122async function processCreateVideoComment (byActor: ActorModel, activity: ActivityCreate) {
123 const comment = activity.object as VideoCommentObject 123 const commentObject = activity.object as VideoCommentObject
124 const byAccount = byActor.Account 124 const byAccount = byActor.Account
125 125
126 if (!byAccount) throw new Error('Cannot create video comment with the non account actor ' + byActor.url) 126 if (!byAccount) throw new Error('Cannot create video comment with the non account actor ' + byActor.url)
127 127
128 const { video, parents } = await resolveThread(comment.inReplyTo) 128 const { video } = await resolveThread(commentObject.inReplyTo)
129 129
130 return sequelizeTypescript.transaction(async t => { 130 const { created } = await addVideoComment(video, commentObject.id)
131 let originCommentId = null
132 let inReplyToCommentId = null
133
134 if (parents.length !== 0) {
135 const parent = parents[0]
136
137 originCommentId = parent.getThreadId()
138 inReplyToCommentId = parent.id
139 }
140
141 // This is a new thread
142 const objectToCreate = {
143 url: comment.id,
144 text: comment.content,
145 originCommentId,
146 inReplyToCommentId,
147 videoId: video.id,
148 accountId: byAccount.id
149 }
150
151 const options = {
152 where: {
153 url: objectToCreate.url
154 },
155 defaults: objectToCreate,
156 transaction: t
157 }
158 const [ ,created ] = await VideoCommentModel.findOrCreate(options)
159 131
160 if (video.isOwned() && created === true) { 132 if (video.isOwned() && created === true) {
161 // Don't resend the activity to the sender 133 // Don't resend the activity to the sender
162 const exceptions = [ byActor ] 134 const exceptions = [ byActor ]
163 135
164 await forwardVideoRelatedActivity(activity, t, exceptions, video) 136 await forwardVideoRelatedActivity(activity, undefined, exceptions, video)
165 } 137 }
166 })
167} 138}
diff --git a/server/lib/activitypub/video-comments.ts b/server/lib/activitypub/video-comments.ts
index beff557bc..ffbd3a64e 100644
--- a/server/lib/activitypub/video-comments.ts
+++ b/server/lib/activitypub/video-comments.ts
@@ -16,7 +16,7 @@ async function videoCommentActivityObjectToDBAttributes (video: VideoModel, acto
16 16
17 // If this is not a reply to the video (thread), create or get the parent comment 17 // If this is not a reply to the video (thread), create or get the parent comment
18 if (video.url !== comment.inReplyTo) { 18 if (video.url !== comment.inReplyTo) {
19 const [ parent ] = await addVideoComment(video, comment.inReplyTo) 19 const { comment: parent } = await addVideoComment(video, comment.inReplyTo)
20 if (!parent) { 20 if (!parent) {
21 logger.warn('Cannot fetch or get parent comment %s of comment %s.', comment.inReplyTo, comment.id) 21 logger.warn('Cannot fetch or get parent comment %s of comment %s.', comment.inReplyTo, comment.id)
22 return undefined 22 return undefined
@@ -55,22 +55,24 @@ async function addVideoComment (videoInstance: VideoModel, commentUrl: string) {
55 55
56 if (sanitizeAndCheckVideoCommentObject(body) === false) { 56 if (sanitizeAndCheckVideoCommentObject(body) === false) {
57 logger.debug('Remote video comment JSON is not valid.', { body }) 57 logger.debug('Remote video comment JSON is not valid.', { body })
58 return undefined 58 return { created: false }
59 } 59 }
60 60
61 const actorUrl = body.attributedTo 61 const actorUrl = body.attributedTo
62 if (!actorUrl) return [] 62 if (!actorUrl) return { created: false }
63 63
64 const actor = await getOrCreateActorAndServerAndModel(actorUrl) 64 const actor = await getOrCreateActorAndServerAndModel(actorUrl)
65 const entry = await videoCommentActivityObjectToDBAttributes(videoInstance, actor, body) 65 const entry = await videoCommentActivityObjectToDBAttributes(videoInstance, actor, body)
66 if (!entry) return [] 66 if (!entry) return { created: false }
67 67
68 return VideoCommentModel.findOrCreate({ 68 const [ comment, created ] = await VideoCommentModel.findOrCreate({
69 where: { 69 where: {
70 url: body.id 70 url: body.id
71 }, 71 },
72 defaults: entry 72 defaults: entry
73 }) 73 })
74
75 return { comment, created }
74} 76}
75 77
76async function resolveThread (url: string, comments: VideoCommentModel[] = []) { 78async function resolveThread (url: string, comments: VideoCommentModel[] = []) {
@@ -91,6 +93,7 @@ async function resolveThread (url: string, comments: VideoCommentModel[] = []) {
91 93
92 try { 94 try {
93 // Maybe it's a reply to a video? 95 // Maybe it's a reply to a video?
96 // If yes, it's done: we resolved all the thread
94 const { video } = await getOrCreateVideoAndAccountAndChannel(url) 97 const { video } = await getOrCreateVideoAndAccountAndChannel(url)
95 98
96 if (comments.length !== 0) { 99 if (comments.length !== 0) {
diff --git a/server/tests/api/videos/index.ts b/server/tests/api/videos/index.ts
index 9f1230767..bc66a7824 100644
--- a/server/tests/api/videos/index.ts
+++ b/server/tests/api/videos/index.ts
@@ -6,9 +6,9 @@ import './video-blacklist'
6import './video-blacklist-management' 6import './video-blacklist-management'
7import './video-captions' 7import './video-captions'
8import './video-channels' 8import './video-channels'
9import './video-comme' 9import './video-comments'
10import './video-description' 10import './video-description'
11import './video-impo' 11import './video-imports'
12import './video-nsfw' 12import './video-nsfw'
13import './video-privacy' 13import './video-privacy'
14import './video-schedule-update' 14import './video-schedule-update'