From 40ff57078e15d5b86ee6b71e198b95d3feb78eaf Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Wed, 22 Nov 2017 16:25:03 +0100 Subject: Federate video views --- server/controllers/activitypub/outbox.ts | 14 +++++++++----- server/controllers/api/server/follows.ts | 9 ++++++++- server/controllers/api/videos/index.ts | 26 +++++++++++++++----------- 3 files changed, 32 insertions(+), 17 deletions(-) (limited to 'server/controllers') diff --git a/server/controllers/activitypub/outbox.ts b/server/controllers/activitypub/outbox.ts index 74d399763..8c63eeb2e 100644 --- a/server/controllers/activitypub/outbox.ts +++ b/server/controllers/activitypub/outbox.ts @@ -1,14 +1,14 @@ import * as express from 'express' import { Activity, ActivityAdd } from '../../../shared/models/activitypub/activity' -import { activityPubCollectionPagination, activityPubContextify } from '../../helpers/activitypub' +import { activityPubCollectionPagination } from '../../helpers/activitypub' +import { pageToStartAndCount } from '../../helpers/core-utils' import { database as db } from '../../initializers' +import { ACTIVITY_PUB } from '../../initializers/constants' import { addActivityData } from '../../lib/activitypub/send/send-add' import { getAnnounceActivityPubUrl } from '../../lib/activitypub/url' import { announceActivityData } from '../../lib/index' import { asyncMiddleware, localAccountValidator } from '../../middlewares' import { AccountInstance } from '../../models/account/account-interface' -import { pageToStartAndCount } from '../../helpers/core-utils' -import { ACTIVITY_PUB } from '../../initializers/constants' const outboxRouter = express.Router() @@ -36,14 +36,18 @@ async function outboxController (req: express.Request, res: express.Response, ne for (const video of data.data) { const videoObject = video.toActivityPubObject() - let addActivity: ActivityAdd = await addActivityData(video.url, account, video, video.VideoChannel.url, videoObject) // This is a shared video - if (video.VideoShare !== undefined) { + if (video.VideoShares !== undefined && video.VideoShares.length !== 0) { + const addActivity = await addActivityData(video.url, video.VideoChannel.Account, video, video.VideoChannel.url, videoObject) + const url = getAnnounceActivityPubUrl(video.url, account) const announceActivity = await announceActivityData(url, account, addActivity) + activities.push(announceActivity) } else { + const addActivity = await addActivityData(video.url, account, video, video.VideoChannel.url, videoObject) + activities.push(addActivity) } } diff --git a/server/controllers/api/server/follows.ts b/server/controllers/api/server/follows.ts index 391f8bdca..535d530f7 100644 --- a/server/controllers/api/server/follows.ts +++ b/server/controllers/api/server/follows.ts @@ -148,10 +148,17 @@ async function removeFollow (req: express.Request, res: express.Response, next: const follow: AccountFollowInstance = res.locals.follow await db.sequelize.transaction(async t => { - await sendUndoFollow(follow, t) + if (follow.state === 'accepted') await sendUndoFollow(follow, t) + await follow.destroy({ transaction: t }) }) + // Destroy the account that will destroy video channels, videos and video files too + // This could be long so don't wait this task + const following = follow.AccountFollowing + following.destroy() + .catch(err => logger.error('Cannot destroy account that we do not follow anymore %s.', following.url, err)) + return res.status(204).end() } diff --git a/server/controllers/api/videos/index.ts b/server/controllers/api/videos/index.ts index 0d114dcd2..2b5afd632 100644 --- a/server/controllers/api/videos/index.ts +++ b/server/controllers/api/videos/index.ts @@ -11,10 +11,15 @@ import { resetSequelizeInstance, retryTransactionWrapper } from '../../../helpers' +import { getServerAccount } from '../../../helpers/utils' import { CONFIG, VIDEO_CATEGORIES, VIDEO_LANGUAGES, VIDEO_LICENCES, VIDEO_MIMETYPE_EXT, VIDEO_PRIVACIES } from '../../../initializers' import { database as db } from '../../../initializers/database' import { sendAddVideo } from '../../../lib/activitypub/send/send-add' import { sendUpdateVideo } from '../../../lib/activitypub/send/send-update' +import { shareVideoByServer } from '../../../lib/activitypub/share' +import { getVideoActivityPubUrl } from '../../../lib/activitypub/url' +import { fetchRemoteVideoDescription } from '../../../lib/activitypub/videos' +import { sendCreateViewToVideoFollowers } from '../../../lib/index' import { transcodingJobScheduler } from '../../../lib/jobs/transcoding-job-scheduler/transcoding-job-scheduler' import { asyncMiddleware, @@ -35,9 +40,7 @@ import { abuseVideoRouter } from './abuse' import { blacklistRouter } from './blacklist' import { videoChannelRouter } from './channel' import { rateVideoRouter } from './rate' -import { getVideoActivityPubUrl } from '../../../lib/activitypub/url' -import { shareVideoByServer } from '../../../lib/activitypub/share' -import { fetchRemoteVideoDescription } from '../../../lib/activitypub/videos' +import { sendCreateViewToOrigin } from '../../../lib/activitypub/send/send-create' const videosRouter = express.Router() @@ -311,17 +314,18 @@ async function updateVideo (req: express.Request, res: express.Response) { async function getVideo (req: express.Request, res: express.Response) { const videoInstance = res.locals.video + const baseIncrementPromise = videoInstance.increment('views') + .then(() => getServerAccount()) + if (videoInstance.isOwned()) { // The increment is done directly in the database, not using the instance value - // FIXME: make a real view system - // For example, only add a view when a user watch a video during 30s etc - videoInstance.increment('views') - .then(() => { - // TODO: send to followers a notification - }) - .catch(err => logger.error('Cannot add view to video %s.', videoInstance.uuid, err)) + baseIncrementPromise + .then(serverAccount => sendCreateViewToVideoFollowers(serverAccount, videoInstance, undefined)) + .catch(err => logger.error('Cannot add view to video/send view to followers for %s.', videoInstance.uuid, err)) } else { - // TODO: send view event to followers + baseIncrementPromise + .then(serverAccount => sendCreateViewToOrigin(serverAccount, videoInstance, undefined)) + .catch(err => logger.error('Cannot send view to origin server for %s.', videoInstance.uuid, err)) } // Do not wait the view system -- cgit v1.2.3