2017-12-20 09:36:29 -05:00
|
|
|
import { ActivityAnnounce } from '../../../../shared/models/activitypub'
|
2017-12-28 05:16:08 -05:00
|
|
|
import { retryTransactionWrapper } from '../../../helpers/database-utils'
|
2020-05-07 08:58:24 -04:00
|
|
|
import { sequelizeTypescript } from '../../../initializers/database'
|
2017-12-12 11:53:50 -05:00
|
|
|
import { VideoShareModel } from '../../../models/video/video-share'
|
2018-05-31 04:23:56 -04:00
|
|
|
import { forwardVideoRelatedActivity } from '../send/utils'
|
2021-06-02 09:47:05 -04:00
|
|
|
import { getOrCreateAPVideo } from '../videos'
|
2018-12-26 04:36:24 -05:00
|
|
|
import { Notifier } from '../../notifier'
|
2019-05-31 09:14:40 -04:00
|
|
|
import { logger } from '../../../helpers/logger'
|
2020-06-18 04:45:25 -04:00
|
|
|
import { APProcessorOptions } from '../../../types/activitypub-processor.model'
|
|
|
|
import { MActorSignature, MVideoAccountLightBlacklistAllFiles } from '../../../types/models'
|
2021-11-16 04:05:12 -05:00
|
|
|
import { getAPId } from '@server/helpers/activitypub'
|
2017-11-15 11:56:21 -05:00
|
|
|
|
2019-08-02 04:53:36 -04: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
|
|
|
|
|
2021-11-16 04:05:12 -05:00
|
|
|
// Announces on accounts are not supported
|
|
|
|
if (actorAnnouncer.type !== 'Application' && actorAnnouncer.type !== 'Group') return
|
|
|
|
|
2019-08-02 04:53:36 -04:00
|
|
|
return retryTransactionWrapper(processVideoShare, actorAnnouncer, activity, notify)
|
2017-11-15 11:56:21 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
export {
|
|
|
|
processAnnounceActivity
|
|
|
|
}
|
2017-11-27 08:44:51 -05:00
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2019-08-15 05:53:26 -04:00
|
|
|
async function processVideoShare (actorAnnouncer: MActorSignature, activity: ActivityAnnounce, notify: boolean) {
|
2021-11-16 04:05:12 -05:00
|
|
|
const objectUri = getAPId(activity.object)
|
2018-01-10 11:18:12 -05:00
|
|
|
|
2019-08-20 07:52:49 -04:00
|
|
|
let video: MVideoAccountLightBlacklistAllFiles
|
2019-05-31 09:14:40 -04:00
|
|
|
let videoCreated: boolean
|
|
|
|
|
|
|
|
try {
|
2021-06-02 09:47:05 -04:00
|
|
|
const result = await getOrCreateAPVideo({ videoObject: objectUri })
|
2019-05-31 09:14:40 -04:00
|
|
|
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 08:44:51 -05:00
|
|
|
|
2018-12-26 04:36:24 -05:00
|
|
|
await sequelizeTypescript.transaction(async t => {
|
2017-11-27 08:44:51 -05:00
|
|
|
// Add share entry
|
|
|
|
|
|
|
|
const share = {
|
2017-12-14 11:38:41 -05:00
|
|
|
actorId: actorAnnouncer.id,
|
2018-01-26 09:49:57 -05:00
|
|
|
videoId: video.id,
|
|
|
|
url: activity.id
|
2017-11-27 08:44:51 -05:00
|
|
|
}
|
|
|
|
|
2017-12-12 11:53:50 -05:00
|
|
|
const [ , created ] = await VideoShareModel.findOrCreate({
|
2018-01-26 09:49:57 -05:00
|
|
|
where: {
|
|
|
|
url: activity.id
|
|
|
|
},
|
2017-11-27 08:44:51 -05:00
|
|
|
defaults: share,
|
|
|
|
transaction: t
|
|
|
|
})
|
|
|
|
|
|
|
|
if (video.isOwned() && created === true) {
|
|
|
|
// Don't resend the activity to the sender
|
2017-12-14 11:38:41 -05:00
|
|
|
const exceptions = [ actorAnnouncer ]
|
2018-05-31 04:23:56 -04:00
|
|
|
|
|
|
|
await forwardVideoRelatedActivity(activity, t, exceptions, video)
|
2017-11-27 08:44:51 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return undefined
|
|
|
|
})
|
2018-12-26 04:36:24 -05:00
|
|
|
|
2019-08-02 04:53:36 -04:00
|
|
|
if (videoCreated && notify) Notifier.Instance.notifyOnNewVideoIfNeeded(video)
|
2017-11-27 08:44:51 -05:00
|
|
|
}
|