From d846501818c2d29e66e6fd141789cb04fd55a437 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Wed, 15 Nov 2017 17:56:21 +0100 Subject: Handle announces in inbox --- server/lib/activitypub/index.ts | 5 +++ server/lib/activitypub/process-add.ts | 7 ++-- server/lib/activitypub/process-announce.ts | 52 ++++++++++++++++++++++++++++++ server/lib/activitypub/process-create.ts | 9 +++--- 4 files changed, 67 insertions(+), 6 deletions(-) create mode 100644 server/lib/activitypub/process-announce.ts (limited to 'server/lib') diff --git a/server/lib/activitypub/index.ts b/server/lib/activitypub/index.ts index f8d56528a..dca446fd4 100644 --- a/server/lib/activitypub/index.ts +++ b/server/lib/activitypub/index.ts @@ -1,4 +1,9 @@ +export * from './process-accept' +export * from './process-add' +export * from './process-announce' export * from './process-create' +export * from './process-delete' export * from './process-flag' +export * from './process-follow' export * from './process-update' export * from './send-request' diff --git a/server/lib/activitypub/process-add.ts b/server/lib/activitypub/process-add.ts index 06d23a2ea..98e414dbb 100644 --- a/server/lib/activitypub/process-add.ts +++ b/server/lib/activitypub/process-add.ts @@ -39,7 +39,7 @@ function processAddVideo (account: AccountInstance, videoChannelUrl: string, vid async function addRemoteVideo (account: AccountInstance, videoChannelUrl: string, videoToCreateData: VideoTorrentObject) { logger.debug('Adding remote video %s.', videoToCreateData.url) - await db.sequelize.transaction(async t => { + return db.sequelize.transaction(async t => { const sequelizeOptions = { transaction: t } @@ -66,7 +66,10 @@ async function addRemoteVideo (account: AccountInstance, videoChannelUrl: string const tags = videoToCreateData.tag.map(t => t.name) const tagInstances = await db.Tag.findOrCreateTags(tags, t) await videoCreated.setTags(tagInstances, sequelizeOptions) + + logger.info('Remote video with uuid %s inserted.', videoToCreateData.uuid) + + return videoCreated }) - logger.info('Remote video with uuid %s inserted.', videoToCreateData.uuid) } diff --git a/server/lib/activitypub/process-announce.ts b/server/lib/activitypub/process-announce.ts new file mode 100644 index 000000000..d67958aec --- /dev/null +++ b/server/lib/activitypub/process-announce.ts @@ -0,0 +1,52 @@ +import { ActivityAnnounce } from '../../../shared/models/activitypub/activity' +import { VideoChannelObject } from '../../../shared/models/activitypub/objects/video-channel-object' +import { VideoTorrentObject } from '../../../shared/models/activitypub/objects/video-torrent-object' +import { logger } from '../../helpers/logger' +import { processAddActivity } from './process-add' +import { processCreateActivity } from './process-create' +import { database as db } from '../../initializers/index' +import { getOrCreateAccount } from '../../helpers/activitypub' +import { VideoChannelInstance } from '../../models/video/video-channel-interface' +import { VideoInstance } from '../../models/index' + +async function processAnnounceActivity (activity: ActivityAnnounce) { + const activityType = activity.object.type + const accountAnnouncer = await getOrCreateAccount(activity.actor) + + if (activityType === 'VideoChannel') { + const activityCreate = Object.assign(activity, { + type: 'Create' as 'Create', + actor: activity.object.actor, + object: activity.object as VideoChannelObject + }) + + // Add share entry + const videoChannel: VideoChannelInstance = await processCreateActivity(activityCreate) + await db.VideoChannelShare.create({ + accountId: accountAnnouncer.id, + videoChannelId: videoChannel.id + }) + } else if (activityType === 'Video') { + const activityAdd = Object.assign(activity, { + type: 'Add' as 'Add', + actor: activity.object.actor, + object: activity.object as VideoTorrentObject + }) + + // Add share entry + const video: VideoInstance = await processAddActivity(activityAdd) + await db.VideoShare.create({ + accountId: accountAnnouncer.id, + videoId: video.id + }) + } + + logger.warn('Unknown activity object type %s when announcing activity.', activityType, { activity: activity.id }) + return Promise.resolve(undefined) +} + +// --------------------------------------------------------------------------- + +export { + processAnnounceActivity +} diff --git a/server/lib/activitypub/process-create.ts b/server/lib/activitypub/process-create.ts index 8d842b822..1b825ebbc 100644 --- a/server/lib/activitypub/process-create.ts +++ b/server/lib/activitypub/process-create.ts @@ -40,7 +40,7 @@ function processCreateVideoChannel (account: AccountInstance, videoChannelToCrea async function addRemoteVideoChannel (account: AccountInstance, videoChannelToCreateData: VideoChannelObject) { logger.debug('Adding remote video channel "%s".', videoChannelToCreateData.uuid) - await db.sequelize.transaction(async t => { + return db.sequelize.transaction(async t => { let videoChannel = await db.VideoChannel.loadByUUIDOrUrl(videoChannelToCreateData.uuid, videoChannelToCreateData.id, t) if (videoChannel) throw new Error('Video channel with this URL/UUID already exists.') @@ -57,10 +57,11 @@ async function addRemoteVideoChannel (account: AccountInstance, videoChannelToCr videoChannel = db.VideoChannel.build(videoChannelData) videoChannel.url = getActivityPubUrl('videoChannel', videoChannel.uuid) - await videoChannel.save({ transaction: t }) - }) + videoChannel = await videoChannel.save({ transaction: t }) + logger.info('Remote video channel with uuid %s inserted.', videoChannelToCreateData.uuid) - logger.info('Remote video channel with uuid %s inserted.', videoChannelToCreateData.uuid) + return videoChannel + }) } function processCreateVideoAbuse (account: AccountInstance, videoAbuseToCreateData: VideoAbuseObject) { -- cgit v1.2.3