2017-11-20 02:43:39 -06:00
|
|
|
import { ActivityCreate, VideoChannelObject } from '../../../../shared'
|
|
|
|
import { VideoAbuseObject } from '../../../../shared/models/activitypub/objects/video-abuse-object'
|
2017-11-22 09:25:03 -06:00
|
|
|
import { ViewObject } from '../../../../shared/models/activitypub/objects/view-object'
|
2017-11-20 02:43:39 -06:00
|
|
|
import { logger, retryTransactionWrapper } from '../../../helpers'
|
|
|
|
import { database as db } from '../../../initializers'
|
|
|
|
import { AccountInstance } from '../../../models/account/account-interface'
|
2017-11-21 06:43:29 -06:00
|
|
|
import { getOrCreateAccountAndServer } from '../account'
|
2017-11-23 07:19:55 -06:00
|
|
|
import { sendCreateDislikeToVideoFollowers, sendCreateViewToVideoFollowers } from '../send/send-create'
|
2017-11-20 03:24:29 -06:00
|
|
|
import { getVideoChannelActivityPubUrl } from '../url'
|
2017-11-16 08:22:39 -06:00
|
|
|
import { videoChannelActivityObjectToDBAttributes } from './misc'
|
2017-11-23 07:19:55 -06:00
|
|
|
import { DislikeObject } from '../../../../shared/models/activitypub/objects/dislike-object'
|
2017-11-09 10:51:58 -06:00
|
|
|
|
2017-11-10 07:34:45 -06:00
|
|
|
async function processCreateActivity (activity: ActivityCreate) {
|
2017-11-09 10:51:58 -06:00
|
|
|
const activityObject = activity.object
|
|
|
|
const activityType = activityObject.type
|
2017-11-21 06:43:29 -06:00
|
|
|
const account = await getOrCreateAccountAndServer(activity.actor)
|
2017-11-09 10:51:58 -06:00
|
|
|
|
2017-11-22 09:25:03 -06:00
|
|
|
if (activityType === 'View') {
|
|
|
|
return processCreateView(activityObject as ViewObject)
|
2017-11-23 07:19:55 -06:00
|
|
|
} else if (activityType === 'Dislike') {
|
|
|
|
return processCreateDislike(account, activityObject as DislikeObject)
|
2017-11-22 09:25:03 -06:00
|
|
|
} else if (activityType === 'VideoChannel') {
|
2017-11-10 07:34:45 -06:00
|
|
|
return processCreateVideoChannel(account, activityObject as VideoChannelObject)
|
2017-11-15 08:12:23 -06:00
|
|
|
} else if (activityType === 'Flag') {
|
|
|
|
return processCreateVideoAbuse(account, activityObject as VideoAbuseObject)
|
2017-11-09 10:51:58 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
logger.warn('Unknown activity object type %s when creating activity.', activityType, { activity: activity.id })
|
2017-11-10 07:34:45 -06:00
|
|
|
return Promise.resolve(undefined)
|
2017-11-09 10:51:58 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
export {
|
|
|
|
processCreateActivity
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2017-11-23 07:19:55 -06:00
|
|
|
async function processCreateDislike (byAccount: AccountInstance, dislike: DislikeObject) {
|
|
|
|
const options = {
|
|
|
|
arguments: [ byAccount, dislike ],
|
|
|
|
errorMessage: 'Cannot dislike the video with many retries.'
|
|
|
|
}
|
|
|
|
|
|
|
|
return retryTransactionWrapper(createVideoDislike, options)
|
|
|
|
}
|
|
|
|
|
|
|
|
function createVideoDislike (byAccount: AccountInstance, dislike: DislikeObject) {
|
|
|
|
return db.sequelize.transaction(async t => {
|
|
|
|
const video = await db.Video.loadByUrlAndPopulateAccount(dislike.object)
|
|
|
|
|
|
|
|
if (!video) throw new Error('Unknown video ' + dislike.object)
|
|
|
|
|
|
|
|
const rate = {
|
|
|
|
type: 'dislike' as 'dislike',
|
|
|
|
videoId: video.id,
|
|
|
|
accountId: byAccount.id
|
|
|
|
}
|
|
|
|
const [ , created ] = await db.AccountVideoRate.findOrCreate({
|
|
|
|
where: rate,
|
|
|
|
defaults: rate
|
|
|
|
})
|
|
|
|
await video.increment('dislikes')
|
|
|
|
|
|
|
|
if (video.isOwned() && created === true) await sendCreateDislikeToVideoFollowers(byAccount, video, undefined)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-11-22 09:25:03 -06:00
|
|
|
async function processCreateView (view: ViewObject) {
|
|
|
|
const video = await db.Video.loadByUrlAndPopulateAccount(view.object)
|
|
|
|
|
|
|
|
if (!video) throw new Error('Unknown video ' + view.object)
|
|
|
|
|
|
|
|
const account = await db.Account.loadByUrl(view.actor)
|
|
|
|
if (!account) throw new Error('Unknown account ' + view.actor)
|
|
|
|
|
|
|
|
await video.increment('views')
|
|
|
|
|
|
|
|
if (video.isOwned()) await sendCreateViewToVideoFollowers(account, video, undefined)
|
|
|
|
}
|
|
|
|
|
2017-11-10 07:34:45 -06:00
|
|
|
function processCreateVideoChannel (account: AccountInstance, videoChannelToCreateData: VideoChannelObject) {
|
2017-11-09 10:51:58 -06:00
|
|
|
const options = {
|
2017-11-10 07:34:45 -06:00
|
|
|
arguments: [ account, videoChannelToCreateData ],
|
|
|
|
errorMessage: 'Cannot insert the remote video channel with many retries.'
|
2017-11-09 10:51:58 -06:00
|
|
|
}
|
|
|
|
|
2017-11-10 07:34:45 -06:00
|
|
|
return retryTransactionWrapper(addRemoteVideoChannel, options)
|
2017-11-09 10:51:58 -06:00
|
|
|
}
|
|
|
|
|
2017-11-16 08:22:39 -06:00
|
|
|
function addRemoteVideoChannel (account: AccountInstance, videoChannelToCreateData: VideoChannelObject) {
|
2017-11-10 07:34:45 -06:00
|
|
|
logger.debug('Adding remote video channel "%s".', videoChannelToCreateData.uuid)
|
2017-11-09 10:51:58 -06:00
|
|
|
|
2017-11-15 10:56:21 -06:00
|
|
|
return db.sequelize.transaction(async t => {
|
2017-11-10 07:34:45 -06:00
|
|
|
let videoChannel = await db.VideoChannel.loadByUUIDOrUrl(videoChannelToCreateData.uuid, videoChannelToCreateData.id, t)
|
|
|
|
if (videoChannel) throw new Error('Video channel with this URL/UUID already exists.')
|
|
|
|
|
2017-11-16 08:22:39 -06:00
|
|
|
const videoChannelData = videoChannelActivityObjectToDBAttributes(videoChannelToCreateData, account)
|
2017-11-10 07:34:45 -06:00
|
|
|
videoChannel = db.VideoChannel.build(videoChannelData)
|
2017-11-20 02:43:39 -06:00
|
|
|
videoChannel.url = getVideoChannelActivityPubUrl(videoChannel)
|
2017-11-09 10:51:58 -06:00
|
|
|
|
2017-11-15 10:56:21 -06:00
|
|
|
videoChannel = await videoChannel.save({ transaction: t })
|
|
|
|
logger.info('Remote video channel with uuid %s inserted.', videoChannelToCreateData.uuid)
|
2017-11-09 10:51:58 -06:00
|
|
|
|
2017-11-15 10:56:21 -06:00
|
|
|
return videoChannel
|
|
|
|
})
|
2017-11-09 10:51:58 -06:00
|
|
|
}
|
2017-11-15 08:12:23 -06:00
|
|
|
|
|
|
|
function processCreateVideoAbuse (account: AccountInstance, videoAbuseToCreateData: VideoAbuseObject) {
|
|
|
|
const options = {
|
|
|
|
arguments: [ account, videoAbuseToCreateData ],
|
|
|
|
errorMessage: 'Cannot insert the remote video abuse with many retries.'
|
|
|
|
}
|
|
|
|
|
|
|
|
return retryTransactionWrapper(addRemoteVideoAbuse, options)
|
|
|
|
}
|
|
|
|
|
2017-11-16 08:22:39 -06:00
|
|
|
function addRemoteVideoAbuse (account: AccountInstance, videoAbuseToCreateData: VideoAbuseObject) {
|
2017-11-15 08:12:23 -06:00
|
|
|
logger.debug('Reporting remote abuse for video %s.', videoAbuseToCreateData.object)
|
|
|
|
|
|
|
|
return db.sequelize.transaction(async t => {
|
2017-11-16 08:55:01 -06:00
|
|
|
const video = await db.Video.loadByUrlAndPopulateAccount(videoAbuseToCreateData.object, t)
|
2017-11-15 08:12:23 -06:00
|
|
|
if (!video) {
|
|
|
|
logger.warn('Unknown video %s for remote video abuse.', videoAbuseToCreateData.object)
|
2017-11-17 08:52:26 -06:00
|
|
|
return undefined
|
2017-11-15 08:12:23 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
const videoAbuseData = {
|
|
|
|
reporterAccountId: account.id,
|
|
|
|
reason: videoAbuseToCreateData.content,
|
|
|
|
videoId: video.id
|
|
|
|
}
|
|
|
|
|
|
|
|
await db.VideoAbuse.create(videoAbuseData)
|
|
|
|
|
|
|
|
logger.info('Remote abuse for video uuid %s created', videoAbuseToCreateData.object)
|
|
|
|
})
|
|
|
|
}
|