From 6d8524702874120a4667269a81a61e3c7c5e300d Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Fri, 22 Dec 2017 09:14:50 +0100 Subject: Create comment on replied mastodon statutes --- server/lib/activitypub/process/process-create.ts | 53 +++++++++++++++++++++++- 1 file changed, 51 insertions(+), 2 deletions(-) (limited to 'server/lib') diff --git a/server/lib/activitypub/process/process-create.ts b/server/lib/activitypub/process/process-create.ts index 1ddd817db..102e54b19 100644 --- a/server/lib/activitypub/process/process-create.ts +++ b/server/lib/activitypub/process/process-create.ts @@ -1,6 +1,7 @@ import * as Bluebird from 'bluebird' import { ActivityCreate, VideoTorrentObject } from '../../../../shared' import { DislikeObject, VideoAbuseObject, ViewObject } from '../../../../shared/models/activitypub/objects' +import { VideoCommentObject } from '../../../../shared/models/activitypub/objects/video-comment-object' import { VideoRateType } from '../../../../shared/models/videos' import { logger, retryTransactionWrapper } from '../../../helpers' import { sequelizeTypescript } from '../../../initializers' @@ -9,6 +10,7 @@ import { ActorModel } from '../../../models/activitypub/actor' import { TagModel } from '../../../models/video/tag' import { VideoModel } from '../../../models/video/video' import { VideoAbuseModel } from '../../../models/video/video-abuse' +import { VideoCommentModel } from '../../../models/video/video-comment' import { VideoFileModel } from '../../../models/video/video-file' import { getOrCreateActorAndServerAndModel } from '../actor' import { forwardActivity } from '../send/misc' @@ -28,6 +30,8 @@ async function processCreateActivity (activity: ActivityCreate) { return processCreateVideo(actor, activity) } else if (activityType === 'Flag') { return processCreateVideoAbuse(actor, activityObject as VideoAbuseObject) + } else if (activityType === 'Note') { + return processCreateVideoComment(actor, activity) } logger.warn('Unknown activity object type %s when creating activity.', activityType, { activity: activity.id }) @@ -184,7 +188,7 @@ function createVideoDislike (byActor: ActorModel, activity: ActivityCreate) { }) } -async function processCreateView (byAccount: ActorModel, activity: ActivityCreate) { +async function processCreateView (byActor: ActorModel, activity: ActivityCreate) { const view = activity.object as ViewObject const video = await VideoModel.loadByUrlAndPopulateAccount(view.object) @@ -198,7 +202,7 @@ async function processCreateView (byAccount: ActorModel, activity: ActivityCreat if (video.isOwned()) { // Don't resend the activity to the sender - const exceptions = [ byAccount ] + const exceptions = [ byActor ] await forwardActivity(activity, undefined, exceptions) } } @@ -236,3 +240,48 @@ function addRemoteVideoAbuse (actor: ActorModel, videoAbuseToCreateData: VideoAb logger.info('Remote abuse for video uuid %s created', videoAbuseToCreateData.object) }) } + +function processCreateVideoComment (byActor: ActorModel, activity: ActivityCreate) { + const options = { + arguments: [ byActor, activity ], + errorMessage: 'Cannot create video comment with many retries.' + } + + return retryTransactionWrapper(createVideoComment, options) +} + +function createVideoComment (byActor: ActorModel, activity: ActivityCreate) { + const comment = activity.object as VideoCommentObject + const byAccount = byActor.Account + + if (!byAccount) throw new Error('Cannot create video comment with the non account actor ' + byActor.url) + + return sequelizeTypescript.transaction(async t => { + const video = await VideoModel.loadByUrl(comment.inReplyTo, t) + + // This is a new thread + if (video) { + return VideoCommentModel.create({ + url: comment.id, + text: comment.content, + originCommentId: null, + inReplyToComment: null, + videoId: video.id, + actorId: byActor.id + }, { transaction: t }) + } + + const inReplyToComment = await VideoCommentModel.loadByUrl(comment.inReplyTo, t) + if (!inReplyToComment) throw new Error('Unknown replied comment ' + comment.inReplyTo) + + const originCommentId = inReplyToComment.originCommentId || inReplyToComment.id + return VideoCommentModel.create({ + url: comment.id, + text: comment.content, + originCommentId, + inReplyToCommentId: inReplyToComment.id, + videoId: inReplyToComment.videoId, + actorId: byActor.id + }, { transaction: t }) + }) +} -- cgit v1.2.3