X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Flib%2Factivitypub%2Fprocess%2Fprocess-announce.ts;h=b08156aa165ad7cdd67d6b0d633c7fe8a12dca65;hb=f6eebcb336c067e160a62020a5140d8d992ba384;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..b08156aa1 100644 --- a/server/lib/activitypub/process/process-announce.ts +++ b/server/lib/activitypub/process/process-announce.ts @@ -1,32 +1,17 @@ -import { ActivityAdd, ActivityAnnounce, ActivityCreate } from '../../../../shared/models/activitypub' -import { logger, retryTransactionWrapper } from '../../../helpers' +import { ActivityAnnounce } from '../../../../shared/models/activitypub' +import { retryTransactionWrapper } from '../../../helpers/database-utils' import { sequelizeTypescript } from '../../../initializers' -import { AccountModel } from '../../../models/account/account' +import { ActorModel } from '../../../models/activitypub/actor' import { VideoModel } from '../../../models/video/video' -import { VideoChannelModel } from '../../../models/video/video-channel' -import { VideoChannelShareModel } from '../../../models/video/video-channel-share' 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' +import { getOrCreateActorAndServerAndModel } from '../actor' +import { forwardVideoRelatedActivity } from '../send/utils' +import { getOrCreateAccountAndVideoAndChannel } from '../videos' async function processAnnounceActivity (activity: ActivityAnnounce) { - const announcedActivity = activity.object - const accountAnnouncer = await getOrCreateAccountAndServer(activity.actor) + const actorAnnouncer = await getOrCreateActorAndServerAndModel(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 + return retryTransactionWrapper(processVideoShare, actorAnnouncer, activity) } // --------------------------------------------------------------------------- @@ -37,73 +22,33 @@ 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 processVideoShare (actorAnnouncer: ActorModel, activity: ActivityAnnounce) { + const objectUri = typeof activity.object === 'string' ? activity.object : activity.object.id -async function shareVideoChannel (accountAnnouncer: AccountModel, activity: ActivityAnnounce) { - const announcedActivity = activity.object as ActivityCreate + const { video } = await getOrCreateAccountAndVideoAndChannel(objectUri) 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 - }) - - 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.' - } - - return retryTransactionWrapper(shareVideo, options) -} - -function shareVideo (accountAnnouncer: AccountModel, activity: ActivityAnnounce) { - const announcedActivity = activity.object as ActivityAdd - - return 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