From 1297eb5db651a230474670c5da1517862fb9cc3e Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Wed, 22 Aug 2018 16:15:35 +0200 Subject: Add refresh video on search --- server/lib/activitypub/share.ts | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) (limited to 'server/lib/activitypub/share.ts') diff --git a/server/lib/activitypub/share.ts b/server/lib/activitypub/share.ts index 698414867..fe3d73e9b 100644 --- a/server/lib/activitypub/share.ts +++ b/server/lib/activitypub/share.ts @@ -6,6 +6,11 @@ import { VideoShareModel } from '../../models/video/video-share' import { sendUndoAnnounce, sendVideoAnnounce } from './send' import { getAnnounceActivityPubUrl } from './url' import { VideoChannelModel } from '../../models/video/video-channel' +import * as Bluebird from 'bluebird' +import { doRequest } from '../../helpers/requests' +import { getOrCreateActorAndServerAndModel } from './actor' +import { logger } from '../../helpers/logger' +import { CRAWL_REQUEST_CONCURRENCY } from '../../initializers' async function shareVideoByServerAndChannel (video: VideoModel, t: Transaction) { if (video.privacy === VideoPrivacy.PRIVATE) return undefined @@ -22,8 +27,41 @@ async function changeVideoChannelShare (video: VideoModel, oldVideoChannel: Vide await shareByVideoChannel(video, t) } +async function addVideoShares (shareUrls: string[], instance: VideoModel) { + await Bluebird.map(shareUrls, async shareUrl => { + try { + // Fetch url + const { body } = await doRequest({ + uri: shareUrl, + json: true, + activityPub: true + }) + if (!body || !body.actor) throw new Error('Body of body actor is invalid') + + const actorUrl = body.actor + const actor = await getOrCreateActorAndServerAndModel(actorUrl) + + const entry = { + actorId: actor.id, + videoId: instance.id, + url: shareUrl + } + + await VideoShareModel.findOrCreate({ + where: { + url: shareUrl + }, + defaults: entry + }) + } catch (err) { + logger.warn('Cannot add share %s.', shareUrl, { err }) + } + }, { concurrency: CRAWL_REQUEST_CONCURRENCY }) +} + export { changeVideoChannelShare, + addVideoShares, shareVideoByServerAndChannel } -- cgit v1.2.3