X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Flib%2Factivitypub%2Fprocess%2Fprocess-announce.ts;h=63082466e6e923a7eb779263b2852a2bc3ccde6d;hb=543442a3be9d7740749eb3918dc59f502ff042f9;hp=ff2c6d708e688cb59d33d8f1b87876265a11fc7a;hpb=3fd3ab2d34d512b160a5e6084d7609be7b4f4452;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/lib/activitypub/process/process-announce.ts b/server/lib/activitypub/process/process-announce.ts index ff2c6d708..63082466e 100644 --- a/server/lib/activitypub/process/process-announce.ts +++ b/server/lib/activitypub/process/process-announce.ts @@ -1,32 +1,20 @@ -import { ActivityAdd, ActivityAnnounce, ActivityCreate } from '../../../../shared/models/activitypub' -import { logger, retryTransactionWrapper } from '../../../helpers' -import { sequelizeTypescript } from '../../../initializers' -import { AccountModel } from '../../../models/account/account' -import { VideoModel } from '../../../models/video/video' -import { VideoChannelModel } from '../../../models/video/video-channel' -import { VideoChannelShareModel } from '../../../models/video/video-channel-share' +import { ActivityAnnounce } from '../../../../shared/models/activitypub' +import { retryTransactionWrapper } from '../../../helpers/database-utils' +import { sequelizeTypescript } from '../../../initializers/database' import { VideoShareModel } from '../../../models/video/video-share' -import { getOrCreateAccountAndServer } from '../account' -import { forwardActivity } from '../send/misc' -import { processAddActivity } from './process-add' -import { processCreateActivity } from './process-create' - -async function processAnnounceActivity (activity: ActivityAnnounce) { - const announcedActivity = activity.object - const accountAnnouncer = await getOrCreateAccountAndServer(activity.actor) - - if (announcedActivity.type === 'Create' && announcedActivity.object.type === 'VideoChannel') { - return processVideoChannelShare(accountAnnouncer, activity) - } else if (announcedActivity.type === 'Add' && announcedActivity.object.type === 'Video') { - return processVideoShare(accountAnnouncer, activity) - } - - logger.warn( - 'Unknown activity object type %s -> %s when announcing activity.', announcedActivity.type, announcedActivity.object.type, - { activity: activity.id } - ) - - return undefined +import { forwardVideoRelatedActivity } from '../send/utils' +import { getOrCreateVideoAndAccountAndChannel } from '../videos' +import { Notifier } from '../../notifier' +import { logger } from '../../../helpers/logger' +import { APProcessorOptions } from '../../../types/activitypub-processor.model' +import { MActorSignature, MVideoAccountLightBlacklistAllFiles } from '../../../types/models' + +async function processAnnounceActivity (options: APProcessorOptions) { + 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) } // --------------------------------------------------------------------------- @@ -37,75 +25,47 @@ export { // --------------------------------------------------------------------------- -function processVideoChannelShare (accountAnnouncer: AccountModel, activity: ActivityAnnounce) { - const options = { - arguments: [ accountAnnouncer, activity ], - errorMessage: 'Cannot share the video channel with many retries.' - } - - return retryTransactionWrapper(shareVideoChannel, options) -} - -async function shareVideoChannel (accountAnnouncer: AccountModel, activity: ActivityAnnounce) { - const announcedActivity = activity.object as ActivityCreate - - return sequelizeTypescript.transaction(async t => { - // Add share entry - const videoChannel: VideoChannelModel = await processCreateActivity(announcedActivity) - const share = { - accountId: accountAnnouncer.id, - videoChannelId: videoChannel.id - } - - const [ , created ] = await VideoChannelShareModel.findOrCreate({ - where: share, - defaults: share, - transaction: t - }) +async function processVideoShare (actorAnnouncer: MActorSignature, activity: ActivityAnnounce, notify: boolean) { + const objectUri = typeof activity.object === 'string' ? activity.object : activity.object.id - if (videoChannel.isOwned() && created === true) { - // Don't resend the activity to the sender - const exceptions = [ accountAnnouncer ] - await forwardActivity(activity, t, exceptions) - } - - return undefined - }) -} + let video: MVideoAccountLightBlacklistAllFiles + let videoCreated: boolean -function processVideoShare (accountAnnouncer: AccountModel, activity: ActivityAnnounce) { - const options = { - arguments: [ accountAnnouncer, activity ], - errorMessage: 'Cannot share the video with many retries.' + 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 } - return retryTransactionWrapper(shareVideo, options) -} - -function shareVideo (accountAnnouncer: AccountModel, activity: ActivityAnnounce) { - const announcedActivity = activity.object as ActivityAdd - - return sequelizeTypescript.transaction(async t => { + await sequelizeTypescript.transaction(async t => { // Add share entry - const video: VideoModel = await processAddActivity(announcedActivity) const share = { - accountId: accountAnnouncer.id, - videoId: video.id + actorId: actorAnnouncer.id, + videoId: video.id, + url: activity.id } const [ , created ] = await VideoShareModel.findOrCreate({ - where: share, + where: { + url: activity.id + }, defaults: share, transaction: t }) if (video.isOwned() && created === true) { // Don't resend the activity to the sender - const exceptions = [ accountAnnouncer ] - await forwardActivity(activity, t, exceptions) + const exceptions = [ actorAnnouncer ] + + await forwardVideoRelatedActivity(activity, t, exceptions, video) } return undefined }) + + if (videoCreated && notify) Notifier.Instance.notifyOnNewVideoIfNeeded(video) }