From 0f320037e689b2778959c12ddd4ce790f6e4ae4f Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Fri, 11 May 2018 15:10:13 +0200 Subject: Add ability to update a video channel --- server/lib/activitypub/process/process-undo.ts | 25 +++++++++++++++++++++++- server/lib/activitypub/process/process-update.ts | 10 ++++++++-- 2 files changed, 32 insertions(+), 3 deletions(-) (limited to 'server/lib/activitypub/process') diff --git a/server/lib/activitypub/process/process-undo.ts b/server/lib/activitypub/process/process-undo.ts index 565e70289..9b024d15f 100644 --- a/server/lib/activitypub/process/process-undo.ts +++ b/server/lib/activitypub/process/process-undo.ts @@ -1,4 +1,4 @@ -import { ActivityFollow, ActivityLike, ActivityUndo } from '../../../../shared/models/activitypub' +import { ActivityAnnounce, ActivityFollow, ActivityLike, ActivityUndo } from '../../../../shared/models/activitypub' import { DislikeObject } from '../../../../shared/models/activitypub/objects' import { getActorUrl } from '../../../helpers/activitypub' import { retryTransactionWrapper } from '../../../helpers/database-utils' @@ -10,6 +10,7 @@ import { ActorModel } from '../../../models/activitypub/actor' import { ActorFollowModel } from '../../../models/activitypub/actor-follow' import { forwardActivity } from '../send/misc' import { getOrCreateAccountAndVideoAndChannel } from '../videos' +import { VideoShareModel } from '../../../models/video/video-share' async function processUndoActivity (activity: ActivityUndo) { const activityToUndo = activity.object @@ -22,6 +23,8 @@ async function processUndoActivity (activity: ActivityUndo) { return processUndoDislike(actorUrl, activity) } else if (activityToUndo.type === 'Follow') { return processUndoFollow(actorUrl, activityToUndo) + } else if (activityToUndo.type === 'Announce') { + return processUndoAnnounce(actorUrl, activityToUndo) } logger.warn('Unknown activity object type %s -> %s when undo activity.', activityToUndo.type, { activity: activity.id }) @@ -123,3 +126,23 @@ function undoFollow (actorUrl: string, followActivity: ActivityFollow) { return undefined }) } + +function processUndoAnnounce (actorUrl: string, announceActivity: ActivityAnnounce) { + const options = { + arguments: [ actorUrl, announceActivity ], + errorMessage: 'Cannot undo announce with many retries.' + } + + return retryTransactionWrapper(undoAnnounce, options) +} + +function undoAnnounce (actorUrl: string, announceActivity: ActivityAnnounce) { + return sequelizeTypescript.transaction(async t => { + const share = await VideoShareModel.loadByUrl(announceActivity.id, t) + if (!share) throw new Error(`'Unknown video share ${announceActivity.id}.`) + + await share.destroy({ transaction: t }) + + return undefined + }) +} diff --git a/server/lib/activitypub/process/process-update.ts b/server/lib/activitypub/process/process-update.ts index 51e3cc4e3..0dd657c2b 100644 --- a/server/lib/activitypub/process/process-update.ts +++ b/server/lib/activitypub/process/process-update.ts @@ -14,7 +14,7 @@ import { VideoFileModel } from '../../../models/video/video-file' import { fetchAvatarIfExists, getOrCreateActorAndServerAndModel, updateActorAvatarInstance, updateActorInstance } from '../actor' import { generateThumbnailFromUrl, - getOrCreateAccountAndVideoAndChannel, + getOrCreateAccountAndVideoAndChannel, getOrCreateVideoChannel, videoActivityObjectToDBAttributes, videoFileActivityUrlToDBAttributes } from '../videos' @@ -54,6 +54,10 @@ async function updateRemoteVideo (actor: ActorModel, activity: ActivityUpdate) { const res = await getOrCreateAccountAndVideoAndChannel(videoAttributesToUpdate.id) + // Fetch video channel outside the transaction + const newVideoChannelActor = await getOrCreateVideoChannel(videoAttributesToUpdate) + const newVideoChannel = newVideoChannelActor.VideoChannel + logger.debug('Updating remote video "%s".', videoAttributesToUpdate.uuid) let videoInstance = res.video let videoFieldsSave: any @@ -66,12 +70,13 @@ async function updateRemoteVideo (actor: ActorModel, activity: ActivityUpdate) { videoFieldsSave = videoInstance.toJSON() + // Check actor has the right to update the video const videoChannel = videoInstance.VideoChannel if (videoChannel.Account.Actor.id !== actor.id) { throw new Error('Account ' + actor.url + ' does not own video channel ' + videoChannel.Actor.url) } - const videoData = await videoActivityObjectToDBAttributes(videoChannel, videoAttributesToUpdate, activity.to) + const videoData = await videoActivityObjectToDBAttributes(newVideoChannel, videoAttributesToUpdate, activity.to) videoInstance.set('name', videoData.name) videoInstance.set('uuid', videoData.uuid) videoInstance.set('url', videoData.url) @@ -87,6 +92,7 @@ async function updateRemoteVideo (actor: ActorModel, activity: ActivityUpdate) { videoInstance.set('updatedAt', videoData.updatedAt) videoInstance.set('views', videoData.views) videoInstance.set('privacy', videoData.privacy) + videoInstance.set('channelId', videoData.channelId) await videoInstance.save(sequelizeOptions) -- cgit v1.2.3