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/lib/activitypub/process/misc.ts | 7 ++++++- server/lib/activitypub/process/process-create.ts | 19 ++++++++++++++++++- server/lib/activitypub/process/process-follow.ts | 6 ++++++ 3 files changed, 30 insertions(+), 2 deletions(-) (limited to 'server/lib/activitypub/process') diff --git a/server/lib/activitypub/process/misc.ts b/server/lib/activitypub/process/misc.ts index e90a793fc..eefbe2884 100644 --- a/server/lib/activitypub/process/misc.ts +++ b/server/lib/activitypub/process/misc.ts @@ -33,13 +33,18 @@ async function videoActivityObjectToDBAttributes ( else if (cc.indexOf(ACTIVITY_PUB.PUBLIC) !== -1) privacy = VideoPrivacy.UNLISTED const duration = videoObject.duration.replace(/[^\d]+/, '') + let language = null + if (videoObject.language) { + language = parseInt(videoObject.language.identifier, 10) + } + const videoData: VideoAttributes = { name: videoObject.name, uuid: videoObject.uuid, url: videoObject.id, category: parseInt(videoObject.category.identifier, 10), licence: parseInt(videoObject.licence.identifier, 10), - language: parseInt(videoObject.language.identifier, 10), + language, nsfw: videoObject.nsfw, description: videoObject.content, channelId: videoChannel.id, diff --git a/server/lib/activitypub/process/process-create.ts b/server/lib/activitypub/process/process-create.ts index ddf7c74f6..1777733a0 100644 --- a/server/lib/activitypub/process/process-create.ts +++ b/server/lib/activitypub/process/process-create.ts @@ -1,9 +1,11 @@ import { ActivityCreate, VideoChannelObject } from '../../../../shared' import { VideoAbuseObject } from '../../../../shared/models/activitypub/objects/video-abuse-object' +import { ViewObject } from '../../../../shared/models/activitypub/objects/view-object' import { logger, retryTransactionWrapper } from '../../../helpers' import { database as db } from '../../../initializers' import { AccountInstance } from '../../../models/account/account-interface' import { getOrCreateAccountAndServer } from '../account' +import { sendCreateViewToVideoFollowers } from '../send/send-create' import { getVideoChannelActivityPubUrl } from '../url' import { videoChannelActivityObjectToDBAttributes } from './misc' @@ -12,7 +14,9 @@ async function processCreateActivity (activity: ActivityCreate) { const activityType = activityObject.type const account = await getOrCreateAccountAndServer(activity.actor) - if (activityType === 'VideoChannel') { + if (activityType === 'View') { + return processCreateView(activityObject as ViewObject) + } else if (activityType === 'VideoChannel') { return processCreateVideoChannel(account, activityObject as VideoChannelObject) } else if (activityType === 'Flag') { return processCreateVideoAbuse(account, activityObject as VideoAbuseObject) @@ -30,6 +34,19 @@ export { // --------------------------------------------------------------------------- +async function processCreateView (view: ViewObject) { + const video = await db.Video.loadByUrlAndPopulateAccount(view.object) + + if (!video) throw new Error('Unknown video ' + view.object) + + const account = await db.Account.loadByUrl(view.actor) + if (!account) throw new Error('Unknown account ' + view.actor) + + await video.increment('views') + + if (video.isOwned()) await sendCreateViewToVideoFollowers(account, video, undefined) +} + function processCreateVideoChannel (account: AccountInstance, videoChannelToCreateData: VideoChannelObject) { const options = { arguments: [ account, videoChannelToCreateData ], diff --git a/server/lib/activitypub/process/process-follow.ts b/server/lib/activitypub/process/process-follow.ts index 248004226..320dc1138 100644 --- a/server/lib/activitypub/process/process-follow.ts +++ b/server/lib/activitypub/process/process-follow.ts @@ -49,6 +49,12 @@ async function follow (account: AccountInstance, targetAccountURL: string) { }, transaction: t }) + + if (accountFollow.state !== 'accepted') { + accountFollow.state = 'accepted' + await accountFollow.save({ transaction: t }) + } + accountFollow.AccountFollower = account accountFollow.AccountFollowing = targetAccount -- cgit v1.2.3