1 import { ActivityCreate, VideoTorrentObject } from '../../../../shared'
2 import { DislikeObject, VideoAbuseObject, ViewObject } from '../../../../shared/models/activitypub/objects'
3 import { VideoCommentObject } from '../../../../shared/models/activitypub/objects/video-comment-object'
4 import { retryTransactionWrapper } from '../../../helpers/database-utils'
5 import { logger } from '../../../helpers/logger'
6 import { sequelizeTypescript } from '../../../initializers'
7 import { AccountVideoRateModel } from '../../../models/account/account-video-rate'
8 import { ActorModel } from '../../../models/activitypub/actor'
9 import { VideoAbuseModel } from '../../../models/video/video-abuse'
10 import { VideoCommentModel } from '../../../models/video/video-comment'
11 import { getOrCreateActorAndServerAndModel } from '../actor'
12 import { getActorsInvolvedInVideo } from '../audience'
13 import { resolveThread } from '../video-comments'
14 import { getOrCreateAccountAndVideoAndChannel } from '../videos'
15 import { forwardActivity } from '../send/utils'
17 async function processCreateActivity (activity: ActivityCreate) {
18 const activityObject = activity.object
19 const activityType = activityObject.type
20 const actor = await getOrCreateActorAndServerAndModel(activity.actor)
22 if (activityType === 'View') {
23 return processCreateView(actor, activity)
24 } else if (activityType === 'Dislike') {
25 return processCreateDislike(actor, activity)
26 } else if (activityType === 'Video') {
27 return processCreateVideo(actor, activity)
28 } else if (activityType === 'Flag') {
29 return processCreateVideoAbuse(actor, activityObject as VideoAbuseObject)
30 } else if (activityType === 'Note') {
31 return processCreateVideoComment(actor, activity)
34 logger.warn('Unknown activity object type %s when creating activity.', activityType, { activity: activity.id })
35 return Promise.resolve(undefined)
38 // ---------------------------------------------------------------------------
44 // ---------------------------------------------------------------------------
46 async function processCreateVideo (
48 activity: ActivityCreate
50 const videoToCreateData = activity.object as VideoTorrentObject
52 const { video } = await getOrCreateAccountAndVideoAndChannel(videoToCreateData, actor)
57 async function processCreateDislike (byActor: ActorModel, activity: ActivityCreate) {
59 arguments: [ byActor, activity ],
60 errorMessage: 'Cannot dislike the video with many retries.'
63 return retryTransactionWrapper(createVideoDislike, options)
66 async function createVideoDislike (byActor: ActorModel, activity: ActivityCreate) {
67 const dislike = activity.object as DislikeObject
68 const byAccount = byActor.Account
70 if (!byAccount) throw new Error('Cannot create dislike with the non account actor ' + byActor.url)
72 const { video } = await getOrCreateAccountAndVideoAndChannel(dislike.object)
74 return sequelizeTypescript.transaction(async t => {
76 type: 'dislike' as 'dislike',
78 accountId: byAccount.id
80 const [ , created ] = await AccountVideoRateModel.findOrCreate({
85 if (created === true) await video.increment('dislikes', { transaction: t })
87 if (video.isOwned() && created === true) {
88 // Don't resend the activity to the sender
89 const exceptions = [ byActor ]
90 await forwardActivity(activity, t, exceptions)
95 async function processCreateView (byActor: ActorModel, activity: ActivityCreate) {
96 const view = activity.object as ViewObject
98 const { video } = await getOrCreateAccountAndVideoAndChannel(view.object)
100 const actor = await ActorModel.loadByUrl(view.actor)
101 if (!actor) throw new Error('Unknown actor ' + view.actor)
103 await video.increment('views')
105 if (video.isOwned()) {
106 // Don't resend the activity to the sender
107 const exceptions = [ byActor ]
108 await forwardActivity(activity, undefined, exceptions)
112 function processCreateVideoAbuse (actor: ActorModel, videoAbuseToCreateData: VideoAbuseObject) {
114 arguments: [ actor, videoAbuseToCreateData ],
115 errorMessage: 'Cannot insert the remote video abuse with many retries.'
118 return retryTransactionWrapper(addRemoteVideoAbuse, options)
121 async function addRemoteVideoAbuse (actor: ActorModel, videoAbuseToCreateData: VideoAbuseObject) {
122 logger.debug('Reporting remote abuse for video %s.', videoAbuseToCreateData.object)
124 const account = actor.Account
125 if (!account) throw new Error('Cannot create dislike with the non account actor ' + actor.url)
127 const { video } = await getOrCreateAccountAndVideoAndChannel(videoAbuseToCreateData.object)
129 return sequelizeTypescript.transaction(async t => {
130 const videoAbuseData = {
131 reporterAccountId: account.id,
132 reason: videoAbuseToCreateData.content,
136 await VideoAbuseModel.create(videoAbuseData)
138 logger.info('Remote abuse for video uuid %s created', videoAbuseToCreateData.object)
142 function processCreateVideoComment (byActor: ActorModel, activity: ActivityCreate) {
144 arguments: [ byActor, activity ],
145 errorMessage: 'Cannot create video comment with many retries.'
148 return retryTransactionWrapper(createVideoComment, options)
151 async function createVideoComment (byActor: ActorModel, activity: ActivityCreate) {
152 const comment = activity.object as VideoCommentObject
153 const byAccount = byActor.Account
155 if (!byAccount) throw new Error('Cannot create video comment with the non account actor ' + byActor.url)
157 const { video, parents } = await resolveThread(comment.inReplyTo)
159 return sequelizeTypescript.transaction(async t => {
160 let originCommentId = null
161 let inReplyToCommentId = null
163 if (parents.length !== 0) {
164 const parent = parents[0]
166 originCommentId = parent.getThreadId()
167 inReplyToCommentId = parent.id
170 // This is a new thread
171 const objectToCreate = {
173 text: comment.content,
177 accountId: byAccount.id
182 url: objectToCreate.url
184 defaults: objectToCreate,
187 const [ ,created ] = await VideoCommentModel.findOrCreate(options)
189 if (video.isOwned() && created === true) {
190 // Don't resend the activity to the sender
191 const exceptions = [ byActor ]
193 // Mastodon does not add our announces in audience, so we forward to them manually
194 const additionalActors = await getActorsInvolvedInVideo(video, t)
195 const additionalFollowerUrls = additionalActors.map(a => a.followersUrl)
197 await forwardActivity(activity, t, exceptions, additionalFollowerUrls)