X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Flib%2Factivitypub%2Fprocess%2Fprocess-announce.ts;h=9cc87ee27a13fc60a5aa60efa0d323685c1bcc6f;hb=7e98a7df7d04e19ba67163a86c7b876d78d76839;hp=2aa9ad5c745ed7892094fea53aaabddb6a5b7e69;hpb=4e50b6a1c9a3eb261e04ede73241648e6edf21d6;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/lib/activitypub/process/process-announce.ts b/server/lib/activitypub/process/process-announce.ts index 2aa9ad5c7..9cc87ee27 100644 --- a/server/lib/activitypub/process/process-announce.ts +++ b/server/lib/activitypub/process/process-announce.ts @@ -1,31 +1,24 @@ -import { ActivityAdd, ActivityAnnounce, ActivityCreate } from '../../../../shared/models/activitypub/activity' +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 { database as db } from '../../../initializers/index' -import { AccountInstance } from '../../../models/account/account-interface' -import { VideoInstance } from '../../../models/index' -import { VideoChannelInstance } from '../../../models/video/video-channel-interface' -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 { sequelizeTypescript } from '../../../initializers/database' +import { VideoShareModel } from '../../../models/video/video-share' +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 + + // Announces on accounts are not supported + if (actorAnnouncer.type !== 'Application' && actorAnnouncer.type !== 'Group') return + + return retryTransactionWrapper(processVideoShare, actorAnnouncer, activity, notify) } // --------------------------------------------------------------------------- @@ -36,75 +29,47 @@ export { // --------------------------------------------------------------------------- -function processVideoChannelShare (accountAnnouncer: AccountInstance, activity: ActivityAnnounce) { - const options = { - arguments: [ accountAnnouncer, activity ], - errorMessage: 'Cannot share the video channel with many retries.' - } - - return retryTransactionWrapper(shareVideoChannel, options) -} - -async function shareVideoChannel (accountAnnouncer: AccountInstance, activity: ActivityAnnounce) { - const announcedActivity = activity.object as ActivityCreate - - return db.sequelize.transaction(async t => { - // Add share entry - const videoChannel: VideoChannelInstance = await processCreateActivity(announcedActivity) - const share = { - accountId: accountAnnouncer.id, - videoChannelId: videoChannel.id - } - - const [ , created ] = await db.VideoChannelShare.findOrCreate({ - where: share, - defaults: share, - transaction: t - }) - - if (videoChannel.isOwned() && created === true) { - // Don't resend the activity to the sender - const exceptions = [ accountAnnouncer ] - await forwardActivity(activity, t, exceptions) - } +async function processVideoShare (actorAnnouncer: MActorSignature, activity: ActivityAnnounce, notify: boolean) { + const objectUri = getAPId(activity.object) - return undefined - }) -} + let video: MVideoAccountLightBlacklistAllFiles + let videoCreated: boolean -function processVideoShare (accountAnnouncer: AccountInstance, 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: AccountInstance, activity: ActivityAnnounce) { - const announcedActivity = activity.object as ActivityAdd - - return db.sequelize.transaction(async t => { + await sequelizeTypescript.transaction(async t => { // Add share entry - const video: VideoInstance = await processAddActivity(announcedActivity) const share = { - accountId: accountAnnouncer.id, - videoId: video.id + actorId: actorAnnouncer.id, + videoId: video.id, + url: activity.id } - const [ , created ] = await db.VideoShare.findOrCreate({ - where: share, + const [ , created ] = await VideoShareModel.findOrCreate({ + 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) }