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 { ACTIVITY_PUB_ACCEPT_HEADER, 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 res = await fetchRemoteAccountAndCreateServer(accountUrl)
39 if (res === undefined) throw new Error('Cannot fetch remote account.')
41 // Save our new account in database
42 const account = res.account
49 async function fetchRemoteAccountAndCreateServer (accountUrl: string) {
54 'Accept': ACTIVITY_PUB_ACCEPT_HEADER
58 logger.info('Fetching remote account %s.', accountUrl)
62 requestResult = await doRequest(options)
64 logger.warn('Cannot fetch remote account %s.', accountUrl, err)
68 const accountJSON: ActivityPubActor = JSON.parse(requestResult.body)
69 if (isRemoteAccountValid(accountJSON) === false) {
70 logger.debug('Remote account JSON is not valid.', { accountJSON })
74 const followersCount = await fetchAccountCount(accountJSON.followers)
75 const followingCount = await fetchAccountCount(accountJSON.following)
77 const account = db.Account.build({
78 uuid: accountJSON.uuid,
79 name: accountJSON.preferredUsername,
81 publicKey: accountJSON.publicKey.publicKeyPem,
83 followersCount: followersCount,
84 followingCount: followingCount,
85 inboxUrl: accountJSON.inbox,
86 outboxUrl: accountJSON.outbox,
87 sharedInboxUrl: accountJSON.endpoints.sharedInbox,
88 followersUrl: accountJSON.followers,
89 followingUrl: accountJSON.following
92 const accountHost = url.parse(account.url).host
93 const serverOptions = {
101 const [ server ] = await db.Server.findOrCreate(serverOptions)
102 account.set('serverId', server.id)
104 return { account, server }
107 function fetchRemoteVideoPreview (video: VideoInstance) {
109 const host = video.VideoChannel.Account.Server.host
110 const path = join(STATIC_PATHS.PREVIEWS, video.getPreviewName())
112 return request.get(REMOTE_SCHEME.HTTP + '://' + host + path)
115 async function fetchRemoteVideoDescription (video: VideoInstance) {
120 const { body } = await doRequest(options)
121 return body.description ? body.description : ''
124 function activityPubContextify <T> (data: T) {
125 return Object.assign(data,{
127 'https://www.w3.org/ns/activitystreams',
128 'https://w3id.org/security/v1',
130 'Hashtag': 'as:Hashtag',
131 'uuid': 'http://schema.org/identifier',
132 'category': 'http://schema.org/category',
133 'licence': 'http://schema.org/license',
134 'nsfw': 'as:sensitive',
135 'language': 'http://schema.org/inLanguage',
136 'views': 'http://schema.org/Number',
137 'size': 'http://schema.org/Number'
143 function activityPubCollectionPagination (url: string, page: number, result: ResultList<any>) {
144 const baseUrl = url.split('?').shift
149 totalItems: result.total,
151 id: baseUrl + '?page=' + page,
152 type: 'CollectionPage',
153 totalItems: result.total,
154 next: baseUrl + '?page=' + (page + 1),
160 return activityPubContextify(obj)
163 // ---------------------------------------------------------------------------
166 fetchRemoteAccountAndCreateServer,
167 activityPubContextify,
168 activityPubCollectionPagination,
170 generateThumbnailFromUrl,
172 fetchRemoteVideoPreview,
173 fetchRemoteVideoDescription
176 // ---------------------------------------------------------------------------
178 async function fetchAccountCount (url: string) {
186 requestResult = await doRequest(options)
188 logger.warn('Cannot fetch remote account count %s.', url, err)
192 return requestResult.totalItems ? requestResult.totalItems : 0