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 { forwardActivity, getActorsInvolvedInVideo } from '../send/misc'
13 import { resolveThread } from '../video-comments'
14 import { getOrCreateAccountAndVideoAndChannel } from '../videos'
16 async function processCreateActivity (activity: ActivityCreate) {
17 const activityObject = activity.object
18 const activityType = activityObject.type
19 const actor = await getOrCreateActorAndServerAndModel(activity.actor)
21 if (activityType === 'View') {
22 return processCreateView(actor, activity)
23 } else if (activityType === 'Dislike') {
24 return processCreateDislike(actor, activity)
25 } else if (activityType === 'Video') {
26 return processCreateVideo(actor, activity)
27 } else if (activityType === 'Flag') {
28 return processCreateVideoAbuse(actor, activityObject as VideoAbuseObject)
29 } else if (activityType === 'Note') {
30 return processCreateVideoComment(actor, activity)
33 logger.warn('Unknown activity object type %s when creating activity.', activityType, { activity: activity.id })
34 return Promise.resolve(undefined)
37 // ---------------------------------------------------------------------------
43 // ---------------------------------------------------------------------------
45 async function processCreateVideo (
47 activity: ActivityCreate
49 const videoToCreateData = activity.object as VideoTorrentObject
51 const { video } = await getOrCreateAccountAndVideoAndChannel(videoToCreateData, actor)
56 async function processCreateDislike (byActor: ActorModel, activity: ActivityCreate) {
58 arguments: [ byActor, activity ],
59 errorMessage: 'Cannot dislike the video with many retries.'
62 return retryTransactionWrapper(createVideoDislike, options)
65 async function createVideoDislike (byActor: ActorModel, activity: ActivityCreate) {
66 const dislike = activity.object as DislikeObject
67 const byAccount = byActor.Account
69 if (!byAccount) throw new Error('Cannot create dislike with the non account actor ' + byActor.url)
71 const { video } = await getOrCreateAccountAndVideoAndChannel(dislike.object)
73 return sequelizeTypescript.transaction(async t => {
75 type: 'dislike' as 'dislike',
77 accountId: byAccount.id
79 const [ , created ] = await AccountVideoRateModel.findOrCreate({
84 if (created === true) await video.increment('dislikes', { transaction: t })
86 if (video.isOwned() && created === true) {
87 // Don't resend the activity to the sender
88 const exceptions = [ byActor ]
89 await forwardActivity(activity, t, exceptions)
94 async function processCreateView (byActor: ActorModel, activity: ActivityCreate) {
95 const view = activity.object as ViewObject
97 const { video } = await getOrCreateAccountAndVideoAndChannel(view.object)
99 const actor = await ActorModel.loadByUrl(view.actor)
100 if (!actor) throw new Error('Unknown actor ' + view.actor)
102 await video.increment('views')
104 if (video.isOwned()) {
105 // Don't resend the activity to the sender
106 const exceptions = [ byActor ]
107 await forwardActivity(activity, undefined, exceptions)
111 function processCreateVideoAbuse (actor: ActorModel, videoAbuseToCreateData: VideoAbuseObject) {
113 arguments: [ actor, videoAbuseToCreateData ],
114 errorMessage: 'Cannot insert the remote video abuse with many retries.'
117 return retryTransactionWrapper(addRemoteVideoAbuse, options)
120 async function addRemoteVideoAbuse (actor: ActorModel, videoAbuseToCreateData: VideoAbuseObject) {
121 logger.debug('Reporting remote abuse for video %s.', videoAbuseToCreateData.object)
123 const account = actor.Account
124 if (!account) throw new Error('Cannot create dislike with the non account actor ' + actor.url)
126 const { video } = await getOrCreateAccountAndVideoAndChannel(videoAbuseToCreateData.object)
128 return sequelizeTypescript.transaction(async t => {
129 const videoAbuseData = {
130 reporterAccountId: account.id,
131 reason: videoAbuseToCreateData.content,
135 await VideoAbuseModel.create(videoAbuseData)
137 logger.info('Remote abuse for video uuid %s created', videoAbuseToCreateData.object)
141 function processCreateVideoComment (byActor: ActorModel, activity: ActivityCreate) {
143 arguments: [ byActor, activity ],
144 errorMessage: 'Cannot create video comment with many retries.'
147 return retryTransactionWrapper(createVideoComment, options)
150 async function createVideoComment (byActor: ActorModel, activity: ActivityCreate) {
151 const comment = activity.object as VideoCommentObject
152 const byAccount = byActor.Account
154 if (!byAccount) throw new Error('Cannot create video comment with the non account actor ' + byActor.url)
156 const { video, parents } = await resolveThread(comment.inReplyTo)
158 return sequelizeTypescript.transaction(async t => {
159 let originCommentId = null
160 let inReplyToCommentId = null
162 if (parents.length !== 0) {
163 const parent = parents[0]
165 originCommentId = parent.getThreadId()
166 inReplyToCommentId = parent.id
169 // This is a new thread
170 const objectToCreate = {
172 text: comment.content,
176 accountId: byAccount.id
181 url: objectToCreate.url
183 defaults: objectToCreate,
186 const [ ,created ] = await VideoCommentModel.findOrCreate(options)
188 if (video.isOwned() && created === true) {
189 // Don't resend the activity to the sender
190 const exceptions = [ byActor ]
192 // Mastodon does not add our announces in audience, so we forward to them manually
193 const additionalActors = await getActorsInvolvedInVideo(video, t)
194 const additionalFollowerUrls = additionalActors.map(a => a.followersUrl)
196 await forwardActivity(activity, t, exceptions, additionalFollowerUrls)