From efc32059d980c51793e8e9ac0fb6a885a8026f94 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Thu, 16 Nov 2017 11:08:25 +0100 Subject: Send server announce when users upload a video --- server/lib/activitypub/misc.ts | 6 +-- server/lib/activitypub/process-create.ts | 4 +- server/lib/activitypub/send-request.ts | 56 +++++++++++++++++----- .../video-file-optimizer-handler.ts | 2 + server/lib/video-channel.ts | 5 +- 5 files changed, 55 insertions(+), 18 deletions(-) (limited to 'server/lib') diff --git a/server/lib/activitypub/misc.ts b/server/lib/activitypub/misc.ts index 2cf0c4fd1..43d26c328 100644 --- a/server/lib/activitypub/misc.ts +++ b/server/lib/activitypub/misc.ts @@ -28,9 +28,9 @@ async function videoActivityObjectToDBAttributes ( description: videoObject.content, channelId: videoChannel.id, duration: parseInt(duration, 10), - createdAt: videoObject.published, + createdAt: new Date(videoObject.published), // FIXME: updatedAt does not seems to be considered by Sequelize - updatedAt: videoObject.updated, + updatedAt: new Date(videoObject.updated), views: videoObject.views, likes: 0, dislikes: 0, @@ -46,7 +46,7 @@ async function videoActivityObjectToDBAttributes ( function videoFileActivityUrlToDBAttributes (videoCreated: VideoInstance, videoObject: VideoTorrentObject) { const fileUrls = videoObject.url - .filter(u => Object.keys(VIDEO_MIMETYPE_EXT).indexOf(u.mimeType) !== -1) + .filter(u => Object.keys(VIDEO_MIMETYPE_EXT).indexOf(u.mimeType) !== -1 && u.url.startsWith('video/')) const attributes: VideoFileAttributes[] = [] for (const url of fileUrls) { diff --git a/server/lib/activitypub/process-create.ts b/server/lib/activitypub/process-create.ts index 1b825ebbc..4e4c9f703 100644 --- a/server/lib/activitypub/process-create.ts +++ b/server/lib/activitypub/process-create.ts @@ -48,8 +48,8 @@ async function addRemoteVideoChannel (account: AccountInstance, videoChannelToCr name: videoChannelToCreateData.name, description: videoChannelToCreateData.content, uuid: videoChannelToCreateData.uuid, - createdAt: videoChannelToCreateData.published, - updatedAt: videoChannelToCreateData.updated, + createdAt: new Date(videoChannelToCreateData.published), + updatedAt: new Date(videoChannelToCreateData.updated), remote: true, accountId: account.id } diff --git a/server/lib/activitypub/send-request.ts b/server/lib/activitypub/send-request.ts index 1dad51828..664b9d826 100644 --- a/server/lib/activitypub/send-request.ts +++ b/server/lib/activitypub/send-request.ts @@ -17,46 +17,67 @@ async function sendCreateVideoChannel (videoChannel: VideoChannelInstance, t: Se const videoChannelObject = videoChannel.toActivityPubObject() const data = await createActivityData(videoChannel.url, videoChannel.Account, videoChannelObject) - return broadcastToFollowers(data, videoChannel.Account, t) + return broadcastToFollowers(data, [ videoChannel.Account ], t) } async function sendUpdateVideoChannel (videoChannel: VideoChannelInstance, t: Sequelize.Transaction) { const videoChannelObject = videoChannel.toActivityPubObject() const data = await updateActivityData(videoChannel.url, videoChannel.Account, videoChannelObject) - return broadcastToFollowers(data, videoChannel.Account, t) + return broadcastToFollowers(data, [ videoChannel.Account ], t) } async function sendDeleteVideoChannel (videoChannel: VideoChannelInstance, t: Sequelize.Transaction) { const data = await deleteActivityData(videoChannel.url, videoChannel.Account) - return broadcastToFollowers(data, videoChannel.Account, t) + return broadcastToFollowers(data, [ videoChannel.Account ], t) } async function sendAddVideo (video: VideoInstance, t: Sequelize.Transaction) { const videoObject = video.toActivityPubObject() const data = await addActivityData(video.url, video.VideoChannel.Account, video.VideoChannel.url, videoObject) - return broadcastToFollowers(data, video.VideoChannel.Account, t) + return broadcastToFollowers(data, [ video.VideoChannel.Account ], t) } async function sendUpdateVideo (video: VideoInstance, t: Sequelize.Transaction) { const videoObject = video.toActivityPubObject() const data = await updateActivityData(video.url, video.VideoChannel.Account, videoObject) - return broadcastToFollowers(data, video.VideoChannel.Account, t) + return broadcastToFollowers(data, [ video.VideoChannel.Account ], t) } async function sendDeleteVideo (video: VideoInstance, t: Sequelize.Transaction) { const data = await deleteActivityData(video.url, video.VideoChannel.Account) - return broadcastToFollowers(data, video.VideoChannel.Account, t) + return broadcastToFollowers(data, [ video.VideoChannel.Account ], t) } async function sendDeleteAccount (account: AccountInstance, t: Sequelize.Transaction) { const data = await deleteActivityData(account.url, account) - return broadcastToFollowers(data, account, t) + return broadcastToFollowers(data, [ account ], t) +} + +async function sendAnnounce (byAccount: AccountInstance, instance: VideoInstance | VideoChannelInstance, t: Sequelize.Transaction) { + const object = instance.toActivityPubObject() + + let url = '' + let objectActorUrl: string + if ((instance as any).VideoChannel !== undefined) { + objectActorUrl = (instance as VideoInstance).VideoChannel.Account.url + url = getActivityPubUrl('video', instance.uuid) + '#announce' + } else { + objectActorUrl = (instance as VideoChannelInstance).Account.url + url = getActivityPubUrl('videoChannel', instance.uuid) + '#announce' + } + + const objectWithActor = Object.assign(object, { + actor: objectActorUrl + }) + + const data = await announceActivityData(url, byAccount, objectWithActor) + return broadcastToFollowers(data, [ byAccount ], t) } async function sendVideoAbuse ( @@ -95,15 +116,17 @@ export { sendDeleteAccount, sendAccept, sendFollow, - sendVideoAbuse + sendVideoAbuse, + sendAnnounce } // --------------------------------------------------------------------------- -async function broadcastToFollowers (data: any, fromAccount: AccountInstance, t: Sequelize.Transaction) { - const result = await db.AccountFollow.listAcceptedFollowerUrlsForApi(fromAccount.id) +async function broadcastToFollowers (data: any, toAccountFollowers: AccountInstance[], t: Sequelize.Transaction) { + const toAccountFollowerIds = toAccountFollowers.map(a => a.id) + const result = await db.AccountFollow.listAcceptedFollowerSharedInboxUrls(toAccountFollowerIds) if (result.data.length === 0) { - logger.info('Not broadcast because of 0 followers.') + logger.info('Not broadcast because of 0 followers for %s.', toAccountFollowerIds.join(', ')) return } @@ -186,6 +209,17 @@ async function addActivityData (url: string, byAccount: AccountInstance, target: return buildSignedActivity(byAccount, base) } +async function announceActivityData (url: string, byAccount: AccountInstance, object: any) { + const base = { + type: 'Announce', + id: url, + actor: byAccount.url, + object + } + + return buildSignedActivity(byAccount, base) +} + async function followActivityData (url: string, byAccount: AccountInstance) { const base = { type: 'Follow', diff --git a/server/lib/jobs/transcoding-job-scheduler/video-file-optimizer-handler.ts b/server/lib/jobs/transcoding-job-scheduler/video-file-optimizer-handler.ts index f6d9627a5..6443899d3 100644 --- a/server/lib/jobs/transcoding-job-scheduler/video-file-optimizer-handler.ts +++ b/server/lib/jobs/transcoding-job-scheduler/video-file-optimizer-handler.ts @@ -6,6 +6,7 @@ import { VideoInstance } from '../../../models' import { sendAddVideo } from '../../activitypub/send-request' import { JobScheduler } from '../job-scheduler' import { TranscodingJobPayload } from './transcoding-job-scheduler' +import { shareVideoByServer } from '../../../helpers/activitypub' async function process (data: TranscodingJobPayload, jobId: number) { const video = await db.Video.loadByUUIDAndPopulateAccountAndServerAndTags(data.videoUUID) @@ -37,6 +38,7 @@ async function onSuccess (jobId: number, video: VideoInstance, jobScheduler: Job // Now we'll add the video's meta data to our followers await sendAddVideo(video, undefined) + await shareVideoByServer(video, undefined) const originalFileHeight = await videoDatabase.getOriginalFileHeight() diff --git a/server/lib/video-channel.ts b/server/lib/video-channel.ts index 80303fb83..e69ec062f 100644 --- a/server/lib/video-channel.ts +++ b/server/lib/video-channel.ts @@ -5,7 +5,7 @@ import { logger } from '../helpers' import { AccountInstance } from '../models' import { VideoChannelCreate } from '../../shared/models' import { sendCreateVideoChannel } from './activitypub/send-request' -import { getActivityPubUrl } from '../helpers/activitypub' +import { getActivityPubUrl, shareVideoChannelByServer } from '../helpers/activitypub' async function createVideoChannel (videoChannelInfo: VideoChannelCreate, account: AccountInstance, t: Sequelize.Transaction) { const videoChannelData = { @@ -25,7 +25,8 @@ async function createVideoChannel (videoChannelInfo: VideoChannelCreate, account // Do not forget to add Account information to the created video channel videoChannelCreated.Account = account - sendCreateVideoChannel(videoChannelCreated, t) + await sendCreateVideoChannel(videoChannelCreated, t) + await shareVideoChannelByServer(videoChannelCreated, t) return videoChannelCreated } -- cgit v1.2.3