+
+async function processCreateVideoComment (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)
+
+ const { video, parents } = await resolveThread(comment.inReplyTo)
+
+ return sequelizeTypescript.transaction(async t => {
+ let originCommentId = null
+ let inReplyToCommentId = null
+
+ if (parents.length !== 0) {
+ const parent = parents[0]
+
+ originCommentId = parent.getThreadId()
+ inReplyToCommentId = parent.id
+ }
+
+ // This is a new thread
+ const objectToCreate = {
+ url: comment.id,
+ text: comment.content,
+ originCommentId,
+ inReplyToCommentId,
+ videoId: video.id,
+ accountId: byAccount.id
+ }
+
+ const options = {
+ where: {
+ url: objectToCreate.url
+ },
+ defaults: objectToCreate,
+ transaction: t
+ }
+ const [ ,created ] = await VideoCommentModel.findOrCreate(options)
+
+ if (video.isOwned() && created === true) {
+ // Don't resend the activity to the sender
+ const exceptions = [ byActor ]
+
+ await forwardVideoRelatedActivity(activity, t, exceptions, video)
+ }
+ })
+}