1 import * as url from 'url'
3 import { database as db } from '../initializers'
4 import { logger } from './logger'
5 import { doRequest, doRequestAndSaveToFile } from './requests'
6 import { isRemoteAccountValid } from './custom-validators'
7 import { ActivityPubActor } from '../../shared/models/activitypub/activitypub-actor'
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'
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', 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
32 async function getOrCreateAccount (accountUrl: string) {
33 let account = await db.Account.loadByUrl(accountUrl)
35 // We don't have this account in our database, fetch it on remote
37 const { account } = await fetchRemoteAccountAndCreatePod(accountUrl)
39 if (!account) throw new Error('Cannot fetch remote account.')
41 // Save our new account in database
48 async function fetchRemoteAccountAndCreatePod (accountUrl: string) {
56 requestResult = await doRequest(options)
58 logger.warning('Cannot fetch remote account %s.', accountUrl, err)
62 const accountJSON: ActivityPubActor = requestResult.body
63 if (isRemoteAccountValid(accountJSON) === false) return undefined
65 const followersCount = await fetchAccountCount(accountJSON.followers)
66 const followingCount = await fetchAccountCount(accountJSON.following)
68 const account = db.Account.build({
69 uuid: accountJSON.uuid,
70 name: accountJSON.preferredUsername,
72 publicKey: accountJSON.publicKey.publicKeyPem,
74 followersCount: followersCount,
75 followingCount: followingCount,
76 inboxUrl: accountJSON.inbox,
77 outboxUrl: accountJSON.outbox,
78 sharedInboxUrl: accountJSON.endpoints.sharedInbox,
79 followersUrl: accountJSON.followers,
80 followingUrl: accountJSON.following
83 const accountHost = url.parse(account.url).host
92 const pod = await db.Pod.findOrCreate(podOptions)
94 return { account, pod }
97 function activityPubContextify (data: object) {
98 return Object.assign(data,{
100 'https://www.w3.org/ns/activitystreams',
101 'https://w3id.org/security/v1',
103 'Hashtag': 'as:Hashtag',
104 'uuid': 'http://schema.org/identifier',
105 'category': 'http://schema.org/category',
106 'licence': 'http://schema.org/license',
107 'nsfw': 'as:sensitive',
108 'language': 'http://schema.org/inLanguage',
109 'views': 'http://schema.org/Number',
110 'size': 'http://schema.org/Number'
116 function activityPubCollectionPagination (url: string, page: number, result: ResultList<any>) {
117 const baseUrl = url.split('?').shift
122 totalItems: result.total,
124 id: baseUrl + '?page=' + page,
125 type: 'CollectionPage',
126 totalItems: result.total,
127 next: baseUrl + '?page=' + (page + 1),
133 return activityPubContextify(obj)
136 // ---------------------------------------------------------------------------
139 fetchRemoteAccountAndCreatePod,
140 activityPubContextify,
141 activityPubCollectionPagination,
143 generateThumbnailFromUrl,
147 // ---------------------------------------------------------------------------
149 async function fetchAccountCount (url: string) {
157 requestResult = await doRequest(options)
159 logger.warning('Cannot fetch remote account count %s.', url, err)
163 return requestResult.totalItems ? requestResult.totalItems : 0