1 import * as Bluebird from 'bluebird'
2 import { ActivityCreate, VideoTorrentObject } from '../../../../shared'
3 import { DislikeObject, VideoAbuseObject, ViewObject } from '../../../../shared/models/activitypub/objects'
4 import { VideoCommentObject } from '../../../../shared/models/activitypub/objects/video-comment-object'
5 import { VideoRateType } from '../../../../shared/models/videos'
6 import { retryTransactionWrapper } from '../../../helpers/database-utils'
7 import { logger } from '../../../helpers/logger'
8 import { sequelizeTypescript } from '../../../initializers'
9 import { AccountVideoRateModel } from '../../../models/account/account-video-rate'
10 import { ActorModel } from '../../../models/activitypub/actor'
11 import { VideoModel } from '../../../models/video/video'
12 import { VideoAbuseModel } from '../../../models/video/video-abuse'
13 import { VideoCommentModel } from '../../../models/video/video-comment'
14 import { getOrCreateActorAndServerAndModel } from '../actor'
15 import { forwardActivity, getActorsInvolvedInVideo } from '../send/misc'
16 import { addVideoComments, resolveThread } from '../video-comments'
17 import { addVideoShares, getOrCreateAccountAndVideoAndChannel } from '../videos'
19 async function processCreateActivity (activity: ActivityCreate) {
20 const activityObject = activity.object
21 const activityType = activityObject.type
22 const actor = await getOrCreateActorAndServerAndModel(activity.actor)
24 if (activityType === 'View') {
25 return processCreateView(actor, activity)
26 } else if (activityType === 'Dislike') {
27 return processCreateDislike(actor, activity)
28 } else if (activityType === 'Video') {
29 return processCreateVideo(actor, activity)
30 } else if (activityType === 'Flag') {
31 return processCreateVideoAbuse(actor, activityObject as VideoAbuseObject)
32 } else if (activityType === 'Note') {
33 return processCreateVideoComment(actor, activity)
36 logger.warn('Unknown activity object type %s when creating activity.', activityType, { activity: activity.id })
37 return Promise.resolve(undefined)
40 // ---------------------------------------------------------------------------
46 // ---------------------------------------------------------------------------
48 async function processCreateVideo (
50 activity: ActivityCreate
52 const videoToCreateData = activity.object as VideoTorrentObject
54 const { video } = await getOrCreateAccountAndVideoAndChannel(videoToCreateData, actor)
56 // Process outside the transaction because we could fetch remote data
57 if (videoToCreateData.likes && Array.isArray(videoToCreateData.likes.orderedItems)) {
58 logger.info('Adding likes of video %s.', video.uuid)
59 await createRates(videoToCreateData.likes.orderedItems, video, 'like')
62 if (videoToCreateData.dislikes && Array.isArray(videoToCreateData.dislikes.orderedItems)) {
63 logger.info('Adding dislikes of video %s.', video.uuid)
64 await createRates(videoToCreateData.dislikes.orderedItems, video, 'dislike')
67 if (videoToCreateData.shares && Array.isArray(videoToCreateData.shares.orderedItems)) {
68 logger.info('Adding shares of video %s.', video.uuid)
69 await addVideoShares(video, videoToCreateData.shares.orderedItems)
72 if (videoToCreateData.comments && Array.isArray(videoToCreateData.comments.orderedItems)) {
73 logger.info('Adding comments of video %s.', video.uuid)
74 await addVideoComments(video, videoToCreateData.comments.orderedItems)
80 async function createRates (actorUrls: string[], video: VideoModel, rate: VideoRateType) {
82 const tasks: Bluebird<number>[] = []
84 for (const actorUrl of actorUrls) {
85 const actor = await getOrCreateActorAndServerAndModel(actorUrl)
86 const p = AccountVideoRateModel
89 accountId: actor.Account.id,
92 .then(() => rateCounts += 1)
97 await Promise.all(tasks)
99 logger.info('Adding %d %s to video %s.', rateCounts, rate, video.uuid)
101 // This is "likes" and "dislikes"
102 await video.increment(rate + 's', { by: rateCounts })
107 async function processCreateDislike (byActor: ActorModel, activity: ActivityCreate) {
109 arguments: [ byActor, activity ],
110 errorMessage: 'Cannot dislike the video with many retries.'
113 return retryTransactionWrapper(createVideoDislike, options)
116 async function createVideoDislike (byActor: ActorModel, activity: ActivityCreate) {
117 const dislike = activity.object as DislikeObject
118 const byAccount = byActor.Account
120 if (!byAccount) throw new Error('Cannot create dislike with the non account actor ' + byActor.url)
122 const { video } = await getOrCreateAccountAndVideoAndChannel(dislike.object)
124 return sequelizeTypescript.transaction(async t => {
126 type: 'dislike' as 'dislike',
128 accountId: byAccount.id
130 const [ , created ] = await AccountVideoRateModel.findOrCreate({
135 if (created === true) await video.increment('dislikes', { transaction: t })
137 if (video.isOwned() && created === true) {
138 // Don't resend the activity to the sender
139 const exceptions = [ byActor ]
140 await forwardActivity(activity, t, exceptions)
145 async function processCreateView (byActor: ActorModel, activity: ActivityCreate) {
146 const view = activity.object as ViewObject
148 const { video } = await getOrCreateAccountAndVideoAndChannel(view.object)
150 const actor = await ActorModel.loadByUrl(view.actor)
151 if (!actor) throw new Error('Unknown actor ' + view.actor)
153 await video.increment('views')
155 if (video.isOwned()) {
156 // Don't resend the activity to the sender
157 const exceptions = [ byActor ]
158 await forwardActivity(activity, undefined, exceptions)
162 function processCreateVideoAbuse (actor: ActorModel, videoAbuseToCreateData: VideoAbuseObject) {
164 arguments: [ actor, videoAbuseToCreateData ],
165 errorMessage: 'Cannot insert the remote video abuse with many retries.'
168 return retryTransactionWrapper(addRemoteVideoAbuse, options)
171 async function addRemoteVideoAbuse (actor: ActorModel, videoAbuseToCreateData: VideoAbuseObject) {
172 logger.debug('Reporting remote abuse for video %s.', videoAbuseToCreateData.object)
174 const account = actor.Account
175 if (!account) throw new Error('Cannot create dislike with the non account actor ' + actor.url)
177 const { video } = await getOrCreateAccountAndVideoAndChannel(videoAbuseToCreateData.object)
179 return sequelizeTypescript.transaction(async t => {
180 const videoAbuseData = {
181 reporterAccountId: account.id,
182 reason: videoAbuseToCreateData.content,
186 await VideoAbuseModel.create(videoAbuseData)
188 logger.info('Remote abuse for video uuid %s created', videoAbuseToCreateData.object)
192 function processCreateVideoComment (byActor: ActorModel, activity: ActivityCreate) {
194 arguments: [ byActor, activity ],
195 errorMessage: 'Cannot create video comment with many retries.'
198 return retryTransactionWrapper(createVideoComment, options)
201 async function createVideoComment (byActor: ActorModel, activity: ActivityCreate) {
202 const comment = activity.object as VideoCommentObject
203 const byAccount = byActor.Account
205 if (!byAccount) throw new Error('Cannot create video comment with the non account actor ' + byActor.url)
207 const { video, parents } = await resolveThread(comment.inReplyTo)
209 return sequelizeTypescript.transaction(async t => {
210 let originCommentId = null
211 let inReplyToCommentId = null
213 if (parents.length !== 0) {
214 const parent = parents[0]
216 originCommentId = parent.getThreadId()
217 inReplyToCommentId = parent.id
220 // This is a new thread
221 const objectToCreate = {
223 text: comment.content,
227 accountId: byAccount.id
232 url: objectToCreate.url
234 defaults: objectToCreate,
237 const [ ,created ] = await VideoCommentModel.findOrCreate(options)
239 if (video.isOwned() && created === true) {
240 // Don't resend the activity to the sender
241 const exceptions = [ byActor ]
243 // Mastodon does not add our announces in audience, so we forward to them manually
244 const additionalActors = await getActorsInvolvedInVideo(video, t)
245 const additionalFollowerUrls = additionalActors.map(a => a.followersUrl)
247 await forwardActivity(activity, t, exceptions, additionalFollowerUrls)