1 import { ActivityCreate, VideoChannelObject } from '../../../../shared'
2 import { DislikeObject } from '../../../../shared/models/activitypub/objects/dislike-object'
3 import { VideoAbuseObject } from '../../../../shared/models/activitypub/objects/video-abuse-object'
4 import { ViewObject } from '../../../../shared/models/activitypub/objects/view-object'
5 import { logger, retryTransactionWrapper } from '../../../helpers'
6 import { database as db } from '../../../initializers'
7 import { AccountInstance } from '../../../models/account/account-interface'
8 import { getOrCreateAccountAndServer } from '../account'
9 import { forwardActivity } from '../send/misc'
10 import { getVideoChannelActivityPubUrl } from '../url'
11 import { addVideoChannelShares, videoChannelActivityObjectToDBAttributes } from './misc'
13 async function processCreateActivity (activity: ActivityCreate) {
14 const activityObject = activity.object
15 const activityType = activityObject.type
16 const account = await getOrCreateAccountAndServer(activity.actor)
18 if (activityType === 'View') {
19 return processCreateView(account, activity)
20 } else if (activityType === 'Dislike') {
21 return processCreateDislike(account, activity)
22 } else if (activityType === 'VideoChannel') {
23 return processCreateVideoChannel(account, activityObject as VideoChannelObject)
24 } else if (activityType === 'Flag') {
25 return processCreateVideoAbuse(account, activityObject as VideoAbuseObject)
28 logger.warn('Unknown activity object type %s when creating activity.', activityType, { activity: activity.id })
29 return Promise.resolve(undefined)
32 // ---------------------------------------------------------------------------
38 // ---------------------------------------------------------------------------
40 async function processCreateDislike (byAccount: AccountInstance, activity: ActivityCreate) {
42 arguments: [ byAccount, activity ],
43 errorMessage: 'Cannot dislike the video with many retries.'
46 return retryTransactionWrapper(createVideoDislike, options)
49 function createVideoDislike (byAccount: AccountInstance, activity: ActivityCreate) {
50 const dislike = activity.object as DislikeObject
52 return db.sequelize.transaction(async t => {
53 const video = await db.Video.loadByUrlAndPopulateAccount(dislike.object, t)
54 if (!video) throw new Error('Unknown video ' + dislike.object)
57 type: 'dislike' as 'dislike',
59 accountId: byAccount.id
61 const [ , created ] = await db.AccountVideoRate.findOrCreate({
66 if (created === true) await video.increment('dislikes', { transaction: t })
68 if (video.isOwned() && created === true) {
69 // Don't resend the activity to the sender
70 const exceptions = [ byAccount ]
71 await forwardActivity(activity, t, exceptions)
76 async function processCreateView (byAccount: AccountInstance, activity: ActivityCreate) {
77 const view = activity.object as ViewObject
79 const video = await db.Video.loadByUrlAndPopulateAccount(view.object)
81 if (!video) throw new Error('Unknown video ' + view.object)
83 const account = await db.Account.loadByUrl(view.actor)
84 if (!account) throw new Error('Unknown account ' + view.actor)
86 await video.increment('views')
88 if (video.isOwned()) {
89 // Don't resend the activity to the sender
90 const exceptions = [ byAccount ]
91 await forwardActivity(activity, undefined, exceptions)
95 async function processCreateVideoChannel (account: AccountInstance, videoChannelToCreateData: VideoChannelObject) {
97 arguments: [ account, videoChannelToCreateData ],
98 errorMessage: 'Cannot insert the remote video channel with many retries.'
101 const videoChannel = await retryTransactionWrapper(addRemoteVideoChannel, options)
103 if (videoChannelToCreateData.shares && Array.isArray(videoChannelToCreateData.shares.orderedItems)) {
104 await addVideoChannelShares(videoChannel, videoChannelToCreateData.shares.orderedItems)
110 function addRemoteVideoChannel (account: AccountInstance, videoChannelToCreateData: VideoChannelObject) {
111 logger.debug('Adding remote video channel "%s".', videoChannelToCreateData.uuid)
113 return db.sequelize.transaction(async t => {
114 let videoChannel = await db.VideoChannel.loadByUUIDOrUrl(videoChannelToCreateData.uuid, videoChannelToCreateData.id, t)
115 if (videoChannel) return videoChannel
117 const videoChannelData = videoChannelActivityObjectToDBAttributes(videoChannelToCreateData, account)
118 videoChannel = db.VideoChannel.build(videoChannelData)
119 videoChannel.url = getVideoChannelActivityPubUrl(videoChannel)
121 videoChannel = await videoChannel.save({ transaction: t })
122 logger.info('Remote video channel with uuid %s inserted.', videoChannelToCreateData.uuid)
128 function processCreateVideoAbuse (account: AccountInstance, videoAbuseToCreateData: VideoAbuseObject) {
130 arguments: [ account, videoAbuseToCreateData ],
131 errorMessage: 'Cannot insert the remote video abuse with many retries.'
134 return retryTransactionWrapper(addRemoteVideoAbuse, options)
137 function addRemoteVideoAbuse (account: AccountInstance, videoAbuseToCreateData: VideoAbuseObject) {
138 logger.debug('Reporting remote abuse for video %s.', videoAbuseToCreateData.object)
140 return db.sequelize.transaction(async t => {
141 const video = await db.Video.loadByUrlAndPopulateAccount(videoAbuseToCreateData.object, t)
143 logger.warn('Unknown video %s for remote video abuse.', videoAbuseToCreateData.object)
147 const videoAbuseData = {
148 reporterAccountId: account.id,
149 reason: videoAbuseToCreateData.content,
153 await db.VideoAbuse.create(videoAbuseData)
155 logger.info('Remote abuse for video uuid %s created', videoAbuseToCreateData.object)