diff options
Diffstat (limited to 'server/lib/activitypub/share.ts')
-rw-r--r-- | server/lib/activitypub/share.ts | 38 |
1 files changed, 38 insertions, 0 deletions
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' | |||
6 | import { sendUndoAnnounce, sendVideoAnnounce } from './send' | 6 | import { sendUndoAnnounce, sendVideoAnnounce } from './send' |
7 | import { getAnnounceActivityPubUrl } from './url' | 7 | import { getAnnounceActivityPubUrl } from './url' |
8 | import { VideoChannelModel } from '../../models/video/video-channel' | 8 | import { VideoChannelModel } from '../../models/video/video-channel' |
9 | import * as Bluebird from 'bluebird' | ||
10 | import { doRequest } from '../../helpers/requests' | ||
11 | import { getOrCreateActorAndServerAndModel } from './actor' | ||
12 | import { logger } from '../../helpers/logger' | ||
13 | import { CRAWL_REQUEST_CONCURRENCY } from '../../initializers' | ||
9 | 14 | ||
10 | async function shareVideoByServerAndChannel (video: VideoModel, t: Transaction) { | 15 | async function shareVideoByServerAndChannel (video: VideoModel, t: Transaction) { |
11 | if (video.privacy === VideoPrivacy.PRIVATE) return undefined | 16 | if (video.privacy === VideoPrivacy.PRIVATE) return undefined |
@@ -22,8 +27,41 @@ async function changeVideoChannelShare (video: VideoModel, oldVideoChannel: Vide | |||
22 | await shareByVideoChannel(video, t) | 27 | await shareByVideoChannel(video, t) |
23 | } | 28 | } |
24 | 29 | ||
30 | async function addVideoShares (shareUrls: string[], instance: VideoModel) { | ||
31 | await Bluebird.map(shareUrls, async shareUrl => { | ||
32 | try { | ||
33 | // Fetch url | ||
34 | const { body } = await doRequest({ | ||
35 | uri: shareUrl, | ||
36 | json: true, | ||
37 | activityPub: true | ||
38 | }) | ||
39 | if (!body || !body.actor) throw new Error('Body of body actor is invalid') | ||
40 | |||
41 | const actorUrl = body.actor | ||
42 | const actor = await getOrCreateActorAndServerAndModel(actorUrl) | ||
43 | |||
44 | const entry = { | ||
45 | actorId: actor.id, | ||
46 | videoId: instance.id, | ||
47 | url: shareUrl | ||
48 | } | ||
49 | |||
50 | await VideoShareModel.findOrCreate({ | ||
51 | where: { | ||
52 | url: shareUrl | ||
53 | }, | ||
54 | defaults: entry | ||
55 | }) | ||
56 | } catch (err) { | ||
57 | logger.warn('Cannot add share %s.', shareUrl, { err }) | ||
58 | } | ||
59 | }, { concurrency: CRAWL_REQUEST_CONCURRENCY }) | ||
60 | } | ||
61 | |||
25 | export { | 62 | export { |
26 | changeVideoChannelShare, | 63 | changeVideoChannelShare, |
64 | addVideoShares, | ||
27 | shareVideoByServerAndChannel | 65 | shareVideoByServerAndChannel |
28 | } | 66 | } |
29 | 67 | ||