2017-12-20 15:36:29 +01:00
|
|
|
import { ActivityAnnounce } from '../../../../shared/models/activitypub'
|
2017-12-28 11:16:08 +01:00
|
|
|
import { retryTransactionWrapper } from '../../../helpers/database-utils'
|
2017-12-12 17:53:50 +01:00
|
|
|
import { sequelizeTypescript } from '../../../initializers'
|
2017-12-14 17:38:41 +01:00
|
|
|
import { ActorModel } from '../../../models/activitypub/actor'
|
2017-12-12 17:53:50 +01:00
|
|
|
import { VideoShareModel } from '../../../models/video/video-share'
|
2018-05-31 10:23:56 +02:00
|
|
|
import { forwardVideoRelatedActivity } from '../send/utils'
|
2018-08-22 16:15:35 +02:00
|
|
|
import { getOrCreateVideoAndAccountAndChannel } from '../videos'
|
2018-12-26 10:36:24 +01:00
|
|
|
import { Notifier } from '../../notifier'
|
2019-05-31 15:14:40 +02:00
|
|
|
import { VideoModel } from '../../../models/video/video'
|
|
|
|
import { logger } from '../../../helpers/logger'
|
2019-08-02 10:53:36 +02:00
|
|
|
import { APProcessorOptions } from '../../../typings/activitypub-processor.model'
|
2017-11-15 17:56:21 +01:00
|
|
|
|
2019-08-02 10:53:36 +02:00
|
|
|
async function processAnnounceActivity (options: APProcessorOptions<ActivityAnnounce>) {
|
|
|
|
const { activity, byActor: actorAnnouncer } = options
|
|
|
|
// Only notify if it is not from a fetcher job
|
|
|
|
const notify = options.fromFetch !== true
|
|
|
|
|
|
|
|
return retryTransactionWrapper(processVideoShare, actorAnnouncer, activity, notify)
|
2017-11-15 17:56:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
export {
|
|
|
|
processAnnounceActivity
|
|
|
|
}
|
2017-11-27 14:44:51 +01:00
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2019-08-02 10:53:36 +02:00
|
|
|
async function processVideoShare (actorAnnouncer: ActorModel, activity: ActivityAnnounce, notify: boolean) {
|
2018-01-26 12:02:18 +01:00
|
|
|
const objectUri = typeof activity.object === 'string' ? activity.object : activity.object.id
|
2018-01-10 17:18:12 +01:00
|
|
|
|
2019-05-31 15:14:40 +02:00
|
|
|
let video: VideoModel
|
|
|
|
let videoCreated: boolean
|
|
|
|
|
|
|
|
try {
|
|
|
|
const result = await getOrCreateVideoAndAccountAndChannel({ videoObject: objectUri })
|
|
|
|
video = result.video
|
|
|
|
videoCreated = result.created
|
|
|
|
} catch (err) {
|
|
|
|
logger.debug('Cannot process share of %s. Maybe this is not a video object, so just skipping.', objectUri, { err })
|
|
|
|
return
|
|
|
|
}
|
2017-11-27 14:44:51 +01:00
|
|
|
|
2018-12-26 10:36:24 +01:00
|
|
|
await sequelizeTypescript.transaction(async t => {
|
2017-11-27 14:44:51 +01:00
|
|
|
// Add share entry
|
|
|
|
|
|
|
|
const share = {
|
2017-12-14 17:38:41 +01:00
|
|
|
actorId: actorAnnouncer.id,
|
2018-01-26 15:49:57 +01:00
|
|
|
videoId: video.id,
|
|
|
|
url: activity.id
|
2017-11-27 14:44:51 +01:00
|
|
|
}
|
|
|
|
|
2017-12-12 17:53:50 +01:00
|
|
|
const [ , created ] = await VideoShareModel.findOrCreate({
|
2018-01-26 15:49:57 +01:00
|
|
|
where: {
|
|
|
|
url: activity.id
|
|
|
|
},
|
2017-11-27 14:44:51 +01:00
|
|
|
defaults: share,
|
|
|
|
transaction: t
|
|
|
|
})
|
|
|
|
|
|
|
|
if (video.isOwned() && created === true) {
|
|
|
|
// Don't resend the activity to the sender
|
2017-12-14 17:38:41 +01:00
|
|
|
const exceptions = [ actorAnnouncer ]
|
2018-05-31 10:23:56 +02:00
|
|
|
|
|
|
|
await forwardVideoRelatedActivity(activity, t, exceptions, video)
|
2017-11-27 14:44:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return undefined
|
|
|
|
})
|
2018-12-26 10:36:24 +01:00
|
|
|
|
2019-08-02 10:53:36 +02:00
|
|
|
if (videoCreated && notify) Notifier.Instance.notifyOnNewVideoIfNeeded(video)
|
2017-11-27 14:44:51 +01:00
|
|
|
}
|