2017-12-20 08:36:29 -06:00
|
|
|
import { ActivityAnnounce } from '../../../../shared/models/activitypub'
|
2017-12-28 04:16:08 -06:00
|
|
|
import { retryTransactionWrapper } from '../../../helpers/database-utils'
|
|
|
|
import { logger } from '../../../helpers/logger'
|
2017-12-12 10:53:50 -06:00
|
|
|
import { sequelizeTypescript } from '../../../initializers'
|
2017-12-14 10:38:41 -06:00
|
|
|
import { ActorModel } from '../../../models/activitypub/actor'
|
2017-12-12 10:53:50 -06:00
|
|
|
import { VideoModel } from '../../../models/video/video'
|
|
|
|
import { VideoShareModel } from '../../../models/video/video-share'
|
2017-12-14 10:38:41 -06:00
|
|
|
import { getOrCreateActorAndServerAndModel } from '../actor'
|
2017-11-27 07:44:51 -06:00
|
|
|
import { forwardActivity } from '../send/misc'
|
2018-01-10 10:18:12 -06:00
|
|
|
import { getOrCreateAccountAndVideoAndChannel } from '../videos'
|
2017-11-17 08:52:26 -06:00
|
|
|
import { processCreateActivity } from './process-create'
|
2017-11-15 10:56:21 -06:00
|
|
|
|
|
|
|
async function processAnnounceActivity (activity: ActivityAnnounce) {
|
2017-11-16 08:22:39 -06:00
|
|
|
const announcedActivity = activity.object
|
2017-12-14 10:38:41 -06:00
|
|
|
const actorAnnouncer = await getOrCreateActorAndServerAndModel(activity.actor)
|
2017-11-15 10:56:21 -06:00
|
|
|
|
2017-12-19 10:07:58 -06:00
|
|
|
if (typeof announcedActivity === 'string') {
|
|
|
|
return processVideoShare(actorAnnouncer, activity)
|
|
|
|
} else if (announcedActivity.type === 'Create' && announcedActivity.object.type === 'Video') {
|
2017-12-14 10:38:41 -06:00
|
|
|
return processVideoShare(actorAnnouncer, activity)
|
2017-11-15 10:56:21 -06:00
|
|
|
}
|
|
|
|
|
2017-11-16 08:22:39 -06:00
|
|
|
logger.warn(
|
|
|
|
'Unknown activity object type %s -> %s when announcing activity.', announcedActivity.type, announcedActivity.object.type,
|
|
|
|
{ activity: activity.id }
|
|
|
|
)
|
2017-11-20 02:43:39 -06:00
|
|
|
|
|
|
|
return undefined
|
2017-11-15 10:56:21 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
export {
|
|
|
|
processAnnounceActivity
|
|
|
|
}
|
2017-11-27 07:44:51 -06:00
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2017-12-14 10:38:41 -06:00
|
|
|
function processVideoShare (actorAnnouncer: ActorModel, activity: ActivityAnnounce) {
|
2017-11-27 07:44:51 -06:00
|
|
|
const options = {
|
2017-12-14 10:38:41 -06:00
|
|
|
arguments: [ actorAnnouncer, activity ],
|
2017-12-19 10:07:58 -06:00
|
|
|
errorMessage: 'Cannot share the video activity with many retries.'
|
2017-11-27 07:44:51 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
return retryTransactionWrapper(shareVideo, options)
|
|
|
|
}
|
|
|
|
|
2018-01-10 10:18:12 -06:00
|
|
|
async function shareVideo (actorAnnouncer: ActorModel, activity: ActivityAnnounce) {
|
2017-12-19 10:07:58 -06:00
|
|
|
const announced = activity.object
|
2018-01-10 10:18:12 -06:00
|
|
|
let video: VideoModel
|
|
|
|
|
|
|
|
if (typeof announced === 'string') {
|
|
|
|
const res = await getOrCreateAccountAndVideoAndChannel(announced)
|
|
|
|
video = res.video
|
|
|
|
} else {
|
|
|
|
video = await processCreateActivity(announced)
|
|
|
|
}
|
2017-11-27 07:44:51 -06:00
|
|
|
|
2017-12-12 10:53:50 -06:00
|
|
|
return sequelizeTypescript.transaction(async t => {
|
2017-11-27 07:44:51 -06:00
|
|
|
// Add share entry
|
|
|
|
|
|
|
|
const share = {
|
2017-12-14 10:38:41 -06:00
|
|
|
actorId: actorAnnouncer.id,
|
2017-11-27 07:44:51 -06:00
|
|
|
videoId: video.id
|
|
|
|
}
|
|
|
|
|
2017-12-12 10:53:50 -06:00
|
|
|
const [ , created ] = await VideoShareModel.findOrCreate({
|
2017-11-27 07:44:51 -06:00
|
|
|
where: share,
|
|
|
|
defaults: share,
|
|
|
|
transaction: t
|
|
|
|
})
|
|
|
|
|
|
|
|
if (video.isOwned() && created === true) {
|
|
|
|
// Don't resend the activity to the sender
|
2017-12-14 10:38:41 -06:00
|
|
|
const exceptions = [ actorAnnouncer ]
|
2017-11-27 07:44:51 -06:00
|
|
|
await forwardActivity(activity, t, exceptions)
|
|
|
|
}
|
|
|
|
|
|
|
|
return undefined
|
|
|
|
})
|
|
|
|
}
|