diff options
author | Chocobozzz <me@florianbigard.com> | 2018-08-22 16:59:55 +0200 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2018-08-27 09:41:54 +0200 |
commit | 83e6519ba4ee752dc3148a16c69effbfccb13e6b (patch) | |
tree | f2d043c549c46c45556965269aec5aebbda9386d /server/lib/activitypub | |
parent | 1297eb5db651a230474670c5da1517862fb9cc3e (diff) | |
download | PeerTube-83e6519ba4ee752dc3148a16c69effbfccb13e6b.tar.gz PeerTube-83e6519ba4ee752dc3148a16c69effbfccb13e6b.tar.zst PeerTube-83e6519ba4ee752dc3148a16c69effbfccb13e6b.zip |
Refractor comment creation from federation
Diffstat (limited to 'server/lib/activitypub')
-rw-r--r-- | server/lib/activitypub/process/process-create.ts | 47 | ||||
-rw-r--r-- | server/lib/activitypub/video-comments.ts | 13 |
2 files changed, 17 insertions, 43 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' | |||
9 | import { VideoAbuseModel } from '../../../models/video/video-abuse' | 9 | import { VideoAbuseModel } from '../../../models/video/video-abuse' |
10 | import { VideoCommentModel } from '../../../models/video/video-comment' | 10 | import { VideoCommentModel } from '../../../models/video/video-comment' |
11 | import { getOrCreateActorAndServerAndModel } from '../actor' | 11 | import { getOrCreateActorAndServerAndModel } from '../actor' |
12 | import { resolveThread } from '../video-comments' | 12 | import { addVideoComment, resolveThread } from '../video-comments' |
13 | import { getOrCreateVideoAndAccountAndChannel } from '../videos' | 13 | import { getOrCreateVideoAndAccountAndChannel } from '../videos' |
14 | import { forwardActivity, forwardVideoRelatedActivity } from '../send/utils' | 14 | import { forwardActivity, forwardVideoRelatedActivity } from '../send/utils' |
15 | 15 | ||
@@ -120,48 +120,19 @@ async function processCreateVideoAbuse (actor: ActorModel, videoAbuseToCreateDat | |||
120 | } | 120 | } |
121 | 121 | ||
122 | async function processCreateVideoComment (byActor: ActorModel, activity: ActivityCreate) { | 122 | async 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 | ||
76 | async function resolveThread (url: string, comments: VideoCommentModel[] = []) { | 78 | async 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) { |