diff options
Diffstat (limited to 'server/helpers/activitypub.ts')
-rw-r--r-- | server/helpers/activitypub.ts | 45 |
1 files changed, 43 insertions, 2 deletions
diff --git a/server/helpers/activitypub.ts b/server/helpers/activitypub.ts index ecb509b66..75de2278c 100644 --- a/server/helpers/activitypub.ts +++ b/server/helpers/activitypub.ts | |||
@@ -2,10 +2,48 @@ import * as url from 'url' | |||
2 | 2 | ||
3 | import { database as db } from '../initializers' | 3 | import { database as db } from '../initializers' |
4 | import { logger } from './logger' | 4 | import { logger } from './logger' |
5 | import { doRequest } from './requests' | 5 | import { doRequest, doRequestAndSaveToFile } from './requests' |
6 | import { isRemoteAccountValid } from './custom-validators' | 6 | import { isRemoteAccountValid } from './custom-validators' |
7 | import { ActivityPubActor } from '../../shared/models/activitypub/activitypub-actor' | 7 | import { ActivityPubActor } from '../../shared/models/activitypub/activitypub-actor' |
8 | import { ResultList } from '../../shared/models/result-list.model' | 8 | import { ResultList } from '../../shared/models/result-list.model' |
9 | import { CONFIG } from '../initializers/constants' | ||
10 | import { VideoInstance } from '../models/video/video-interface' | ||
11 | import { ActivityIconObject } from '../../shared/index' | ||
12 | import { join } from 'path' | ||
13 | |||
14 | function generateThumbnailFromUrl (video: VideoInstance, icon: ActivityIconObject) { | ||
15 | const thumbnailName = video.getThumbnailName() | ||
16 | const thumbnailPath = join(CONFIG.STORAGE.THUMBNAILS_DIR, thumbnailName) | ||
17 | |||
18 | const options = { | ||
19 | method: 'GET', | ||
20 | uri: icon.url | ||
21 | } | ||
22 | return doRequestAndSaveToFile(options, thumbnailPath) | ||
23 | } | ||
24 | |||
25 | function getActivityPubUrl (type: 'video' | 'videoChannel', uuid: string) { | ||
26 | if (type === 'video') return CONFIG.WEBSERVER.URL + '/videos/watch/' + uuid | ||
27 | else if (type === 'videoChannel') return CONFIG.WEBSERVER.URL + '/video-channels/' + uuid | ||
28 | |||
29 | return '' | ||
30 | } | ||
31 | |||
32 | async function getOrCreateAccount (accountUrl: string) { | ||
33 | let account = await db.Account.loadByUrl(accountUrl) | ||
34 | |||
35 | // We don't have this account in our database, fetch it on remote | ||
36 | if (!account) { | ||
37 | const { account } = await fetchRemoteAccountAndCreatePod(accountUrl) | ||
38 | |||
39 | if (!account) throw new Error('Cannot fetch remote account.') | ||
40 | |||
41 | // Save our new account in database | ||
42 | await account.save() | ||
43 | } | ||
44 | |||
45 | return account | ||
46 | } | ||
9 | 47 | ||
10 | async function fetchRemoteAccountAndCreatePod (accountUrl: string) { | 48 | async function fetchRemoteAccountAndCreatePod (accountUrl: string) { |
11 | const options = { | 49 | const options = { |
@@ -100,7 +138,10 @@ function activityPubCollectionPagination (url: string, page: number, result: Res | |||
100 | export { | 138 | export { |
101 | fetchRemoteAccountAndCreatePod, | 139 | fetchRemoteAccountAndCreatePod, |
102 | activityPubContextify, | 140 | activityPubContextify, |
103 | activityPubCollectionPagination | 141 | activityPubCollectionPagination, |
142 | getActivityPubUrl, | ||
143 | generateThumbnailFromUrl, | ||
144 | getOrCreateAccount | ||
104 | } | 145 | } |
105 | 146 | ||
106 | // --------------------------------------------------------------------------- | 147 | // --------------------------------------------------------------------------- |