From df0b219d36bf6852cdf2a7ad09ed4a41c6bccefa Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Tue, 5 Mar 2019 10:58:44 +0100 Subject: Add playlist rest tests --- server/lib/activitypub/playlist.ts | 4 +++- server/lib/activitypub/process/process-delete.ts | 24 ++++++++++++++++++++ server/lib/activitypub/send/send-create.ts | 2 +- server/lib/activitypub/send/send-delete.ts | 7 +++++- server/lib/activitypub/send/send-update.ts | 4 ++-- server/lib/user.ts | 9 +++++--- server/lib/video-playlist.ts | 29 ++++++++++++++++++++++++ 7 files changed, 71 insertions(+), 8 deletions(-) create mode 100644 server/lib/video-playlist.ts (limited to 'server/lib') diff --git a/server/lib/activitypub/playlist.ts b/server/lib/activitypub/playlist.ts index c9b428c92..70389044e 100644 --- a/server/lib/activitypub/playlist.ts +++ b/server/lib/activitypub/playlist.ts @@ -28,7 +28,9 @@ function playlistObjectToDBAttributes (playlistObject: PlaylistObject, byAccount url: playlistObject.id, uuid: playlistObject.uuid, ownerAccountId: byAccount.id, - videoChannelId: null + videoChannelId: null, + createdAt: new Date(playlistObject.published), + updatedAt: new Date(playlistObject.updated) } } diff --git a/server/lib/activitypub/process/process-delete.ts b/server/lib/activitypub/process/process-delete.ts index 155d2ffcc..76f07fd8a 100644 --- a/server/lib/activitypub/process/process-delete.ts +++ b/server/lib/activitypub/process/process-delete.ts @@ -8,6 +8,7 @@ import { VideoModel } from '../../../models/video/video' import { VideoChannelModel } from '../../../models/video/video-channel' import { VideoCommentModel } from '../../../models/video/video-comment' import { forwardVideoRelatedActivity } from '../send/utils' +import { VideoPlaylistModel } from '../../../models/video/video-playlist' async function processDeleteActivity (activity: ActivityDelete, byActor: ActorModel) { const objectUrl = typeof activity.object === 'string' ? activity.object : activity.object.id @@ -45,6 +46,15 @@ async function processDeleteActivity (activity: ActivityDelete, byActor: ActorMo } } + { + const videoPlaylist = await VideoPlaylistModel.loadByUrlAndPopulateAccount(objectUrl) + if (videoPlaylist) { + if (videoPlaylist.isOwned()) throw new Error(`Remote instance cannot delete owned playlist ${videoPlaylist.url}.`) + + return retryTransactionWrapper(processDeleteVideoPlaylist, byActor, videoPlaylist) + } + } + return undefined } @@ -70,6 +80,20 @@ async function processDeleteVideo (actor: ActorModel, videoToDelete: VideoModel) logger.info('Remote video with uuid %s removed.', videoToDelete.uuid) } +async function processDeleteVideoPlaylist (actor: ActorModel, playlistToDelete: VideoPlaylistModel) { + logger.debug('Removing remote video playlist "%s".', playlistToDelete.uuid) + + await sequelizeTypescript.transaction(async t => { + if (playlistToDelete.OwnerAccount.Actor.id !== actor.id) { + throw new Error('Account ' + actor.url + ' does not own video playlist ' + playlistToDelete.url) + } + + await playlistToDelete.destroy({ transaction: t }) + }) + + logger.info('Remote video playlist with uuid %s removed.', playlistToDelete.uuid) +} + async function processDeleteAccount (accountToRemove: AccountModel) { logger.debug('Removing remote account "%s".', accountToRemove.Actor.uuid) diff --git a/server/lib/activitypub/send/send-create.ts b/server/lib/activitypub/send/send-create.ts index bacdb97e3..28f18595b 100644 --- a/server/lib/activitypub/send/send-create.ts +++ b/server/lib/activitypub/send/send-create.ts @@ -45,7 +45,7 @@ async function sendCreateVideoPlaylist (playlist: VideoPlaylistModel, t: Transac const byActor = playlist.OwnerAccount.Actor const audience = getAudience(byActor, playlist.privacy === VideoPlaylistPrivacy.PUBLIC) - const object = await playlist.toActivityPubObject() + const object = await playlist.toActivityPubObject(null, t) const createActivity = buildCreateActivity(playlist.url, byActor, object, audience) const serverActor = await getServerActor() diff --git a/server/lib/activitypub/send/send-delete.ts b/server/lib/activitypub/send/send-delete.ts index 016811e60..7bf5ca520 100644 --- a/server/lib/activitypub/send/send-delete.ts +++ b/server/lib/activitypub/send/send-delete.ts @@ -31,7 +31,12 @@ async function sendDeleteActor (byActor: ActorModel, t: Transaction) { const url = getDeleteActivityPubUrl(byActor.url) const activity = buildDeleteActivity(url, byActor.url, byActor) - const actorsInvolved = await VideoShareModel.loadActorsByVideoOwner(byActor.id, t) + const actorsInvolved = await VideoShareModel.loadActorsWhoSharedVideosOf(byActor.id, t) + + // In case the actor did not have any videos + const serverActor = await getServerActor() + actorsInvolved.push(serverActor) + actorsInvolved.push(byActor) return broadcastToFollowers(activity, byActor, actorsInvolved, t) diff --git a/server/lib/activitypub/send/send-update.ts b/server/lib/activitypub/send/send-update.ts index 3eb2704fd..7411c08d5 100644 --- a/server/lib/activitypub/send/send-update.ts +++ b/server/lib/activitypub/send/send-update.ts @@ -52,7 +52,7 @@ async function sendUpdateActor (accountOrChannel: AccountModel | VideoChannelMod let actorsInvolved: ActorModel[] if (accountOrChannel instanceof AccountModel) { // Actors that shared my videos are involved too - actorsInvolved = await VideoShareModel.loadActorsByVideoOwner(byActor.id, t) + actorsInvolved = await VideoShareModel.loadActorsWhoSharedVideosOf(byActor.id, t) } else { // Actors that shared videos of my channel are involved too actorsInvolved = await VideoShareModel.loadActorsByVideoChannel(accountOrChannel.id, t) @@ -87,7 +87,7 @@ async function sendUpdateVideoPlaylist (videoPlaylist: VideoPlaylistModel, t: Tr const url = getUpdateActivityPubUrl(videoPlaylist.url, videoPlaylist.updatedAt.toISOString()) - const object = await videoPlaylist.toActivityPubObject() + const object = await videoPlaylist.toActivityPubObject(null, t) const audience = getAudience(byActor, videoPlaylist.privacy === VideoPlaylistPrivacy.PUBLIC) const updateActivity = buildUpdateActivity(url, byActor, object, audience) diff --git a/server/lib/user.ts b/server/lib/user.ts index a39ef6c3d..02a84f15b 100644 --- a/server/lib/user.ts +++ b/server/lib/user.ts @@ -11,8 +11,9 @@ import { FilteredModelAttributes } from 'sequelize-typescript/lib/models/Model' import { ActorModel } from '../models/activitypub/actor' import { UserNotificationSettingModel } from '../models/account/user-notification-setting' import { UserNotificationSetting, UserNotificationSettingValue } from '../../shared/models/users' +import { createWatchLaterPlaylist } from './video-playlist' -async function createUserAccountAndChannel (userToCreate: UserModel, validateUser = true) { +async function createUserAccountAndChannelAndPlaylist (userToCreate: UserModel, validateUser = true) { const { user, account, videoChannel } = await sequelizeTypescript.transaction(async t => { const userOptions = { transaction: t, @@ -38,7 +39,9 @@ async function createUserAccountAndChannel (userToCreate: UserModel, validateUse } const videoChannel = await createVideoChannel(videoChannelInfo, accountCreated, t) - return { user: userCreated, account: accountCreated, videoChannel } + const videoPlaylist = await createWatchLaterPlaylist(accountCreated, t) + + return { user: userCreated, account: accountCreated, videoChannel, videoPlaylist } }) const [ accountKeys, channelKeys ] = await Promise.all([ @@ -89,7 +92,7 @@ async function createApplicationActor (applicationId: number) { export { createApplicationActor, - createUserAccountAndChannel, + createUserAccountAndChannelAndPlaylist, createLocalAccountWithoutKeys } diff --git a/server/lib/video-playlist.ts b/server/lib/video-playlist.ts new file mode 100644 index 000000000..6e214e60f --- /dev/null +++ b/server/lib/video-playlist.ts @@ -0,0 +1,29 @@ +import * as Sequelize from 'sequelize' +import { AccountModel } from '../models/account/account' +import { VideoPlaylistModel } from '../models/video/video-playlist' +import { VideoPlaylistPrivacy } from '../../shared/models/videos/playlist/video-playlist-privacy.model' +import { getVideoPlaylistActivityPubUrl } from './activitypub' +import { VideoPlaylistType } from '../../shared/models/videos/playlist/video-playlist-type.model' + +async function createWatchLaterPlaylist (account: AccountModel, t: Sequelize.Transaction) { + const videoPlaylist = new VideoPlaylistModel({ + name: 'Watch later', + privacy: VideoPlaylistPrivacy.PRIVATE, + type: VideoPlaylistType.WATCH_LATER, + ownerAccountId: account.id + }) + + videoPlaylist.url = getVideoPlaylistActivityPubUrl(videoPlaylist) // We use the UUID, so set the URL after building the object + + await videoPlaylist.save({ transaction: t }) + + videoPlaylist.OwnerAccount = account + + return videoPlaylist +} + +// --------------------------------------------------------------------------- + +export { + createWatchLaterPlaylist +} -- cgit v1.2.3