X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Flib%2Factivitypub%2Fprocess%2Fprocess-announce.ts;h=9cc87ee27a13fc60a5aa60efa0d323685c1bcc6f;hb=7e98a7df7d04e19ba67163a86c7b876d78d76839;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..9cc87ee27 100644 --- a/server/lib/activitypub/process/process-announce.ts +++ b/server/lib/activitypub/process/process-announce.ts @@ -1,32 +1,24 @@ -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 { getAPId } from '@server/lib/activitypub/activity' +import { ActivityAnnounce } from '../../../../shared/models/activitypub' +import { retryTransactionWrapper } from '../../../helpers/database-utils' +import { logger } from '../../../helpers/logger' +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) - } +import { APProcessorOptions } from '../../../types/activitypub-processor.model' +import { MActorSignature, MVideoAccountLightBlacklistAllFiles } from '../../../types/models' +import { Notifier } from '../../notifier' +import { forwardVideoRelatedActivity } from '../send/shared/send-utils' +import { getOrCreateAPVideo } from '../videos' + +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 - logger.warn( - 'Unknown activity object type %s -> %s when announcing activity.', announcedActivity.type, announcedActivity.object.type, - { activity: activity.id } - ) + // Announces on accounts are not supported + if (actorAnnouncer.type !== 'Application' && actorAnnouncer.type !== 'Group') return - return undefined + return retryTransactionWrapper(processVideoShare, actorAnnouncer, activity, notify) } // --------------------------------------------------------------------------- @@ -37,75 +29,47 @@ export { // --------------------------------------------------------------------------- -function processVideoChannelShare (accountAnnouncer: AccountModel, activity: ActivityAnnounce) { - const options = { - arguments: [ accountAnnouncer, activity ], - errorMessage: 'Cannot share the video channel with many retries.' - } +async function processVideoShare (actorAnnouncer: MActorSignature, activity: ActivityAnnounce, notify: boolean) { + const objectUri = getAPId(activity.object) - 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 - }) + let video: MVideoAccountLightBlacklistAllFiles + let videoCreated: boolean - if (videoChannel.isOwned() && created === true) { - // Don't resend the activity to the sender - const exceptions = [ accountAnnouncer ] - await forwardActivity(activity, t, exceptions) - } - - return undefined - }) -} - -function processVideoShare (accountAnnouncer: AccountModel, activity: ActivityAnnounce) { - const options = { - arguments: [ accountAnnouncer, activity ], - errorMessage: 'Cannot share the video with many retries.' + try { + const result = await getOrCreateAPVideo({ 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) }