1 import { join } from 'path'
2 import * as request from 'request'
3 import * as url from 'url'
4 import { ActivityIconObject } from '../../shared/index'
5 import { ActivityPubActor } from '../../shared/models/activitypub/activitypub-actor'
6 import { ResultList } from '../../shared/models/result-list.model'
7 import { database as db, REMOTE_SCHEME } from '../initializers'
8 import { CONFIG, STATIC_PATHS } from '../initializers/constants'
9 import { VideoInstance } from '../models/video/video-interface'
10 import { isRemoteAccountValid } from './custom-validators'
11 import { logger } from './logger'
12 import { doRequest, doRequestAndSaveToFile } from './requests'
14 function generateThumbnailFromUrl (video: VideoInstance, icon: ActivityIconObject) {
15 const thumbnailName = video.getThumbnailName()
16 const thumbnailPath = join(CONFIG.STORAGE.THUMBNAILS_DIR, thumbnailName)
22 return doRequestAndSaveToFile(options, thumbnailPath)
25 function getActivityPubUrl (type: 'video' | 'videoChannel' | 'account', id: string) {
26 if (type === 'video') return CONFIG.WEBSERVER.URL + '/videos/watch/' + id
27 else if (type === 'videoChannel') return CONFIG.WEBSERVER.URL + '/video-channels/' + id
28 else if (type === 'account') return CONFIG.WEBSERVER.URL + '/account/' + id
33 async function getOrCreateAccount (accountUrl: string) {
34 let account = await db.Account.loadByUrl(accountUrl)
36 // We don't have this account in our database, fetch it on remote
38 const { account } = await fetchRemoteAccountAndCreatePod(accountUrl)
40 if (!account) throw new Error('Cannot fetch remote account.')
42 // Save our new account in database
49 async function fetchRemoteAccountAndCreatePod (accountUrl: string) {
57 requestResult = await doRequest(options)
59 logger.warning('Cannot fetch remote account %s.', accountUrl, err)
63 const accountJSON: ActivityPubActor = requestResult.body
64 if (isRemoteAccountValid(accountJSON) === false) return undefined
66 const followersCount = await fetchAccountCount(accountJSON.followers)
67 const followingCount = await fetchAccountCount(accountJSON.following)
69 const account = db.Account.build({
70 uuid: accountJSON.uuid,
71 name: accountJSON.preferredUsername,
73 publicKey: accountJSON.publicKey.publicKeyPem,
75 followersCount: followersCount,
76 followingCount: followingCount,
77 inboxUrl: accountJSON.inbox,
78 outboxUrl: accountJSON.outbox,
79 sharedInboxUrl: accountJSON.endpoints.sharedInbox,
80 followersUrl: accountJSON.followers,
81 followingUrl: accountJSON.following
84 const accountHost = url.parse(account.url).host
93 const pod = await db.Pod.findOrCreate(podOptions)
95 return { account, pod }
98 function fetchRemoteVideoPreview (video: VideoInstance) {
100 const host = video.VideoChannel.Account.Pod.host
101 const path = join(STATIC_PATHS.PREVIEWS, video.getPreviewName())
103 return request.get(REMOTE_SCHEME.HTTP + '://' + host + path)
106 async function fetchRemoteVideoDescription (video: VideoInstance) {
111 const { body } = await doRequest(options)
112 return body.description ? body.description : ''
115 function activityPubContextify <T> (data: T) {
116 return Object.assign(data,{
118 'https://www.w3.org/ns/activitystreams',
119 'https://w3id.org/security/v1',
121 'Hashtag': 'as:Hashtag',
122 'uuid': 'http://schema.org/identifier',
123 'category': 'http://schema.org/category',
124 'licence': 'http://schema.org/license',
125 'nsfw': 'as:sensitive',
126 'language': 'http://schema.org/inLanguage',
127 'views': 'http://schema.org/Number',
128 'size': 'http://schema.org/Number'
134 function activityPubCollectionPagination (url: string, page: number, result: ResultList<any>) {
135 const baseUrl = url.split('?').shift
140 totalItems: result.total,
142 id: baseUrl + '?page=' + page,
143 type: 'CollectionPage',
144 totalItems: result.total,
145 next: baseUrl + '?page=' + (page + 1),
151 return activityPubContextify(obj)
154 // ---------------------------------------------------------------------------
157 fetchRemoteAccountAndCreatePod,
158 activityPubContextify,
159 activityPubCollectionPagination,
161 generateThumbnailFromUrl,
163 fetchRemoteVideoPreview,
164 fetchRemoteVideoDescription
167 // ---------------------------------------------------------------------------
169 async function fetchAccountCount (url: string) {
177 requestResult = await doRequest(options)
179 logger.warning('Cannot fetch remote account count %s.', url, err)
183 return requestResult.totalItems ? requestResult.totalItems : 0