1 import { join } from 'path'
2 import * as request from 'request'
3 import * as Sequelize from 'sequelize'
4 import * as url from 'url'
5 import { ActivityIconObject } from '../../shared/index'
6 import { ActivityPubActor } from '../../shared/models/activitypub/activitypub-actor'
7 import { ResultList } from '../../shared/models/result-list.model'
8 import { database as db, REMOTE_SCHEME } from '../initializers'
9 import { ACTIVITY_PUB_ACCEPT_HEADER, CONFIG, STATIC_PATHS } from '../initializers/constants'
10 import { sendAnnounce } from '../lib/activitypub/send-request'
11 import { VideoChannelInstance } from '../models/video/video-channel-interface'
12 import { VideoInstance } from '../models/video/video-interface'
13 import { isRemoteAccountValid } from './custom-validators'
14 import { logger } from './logger'
15 import { doRequest, doRequestAndSaveToFile } from './requests'
16 import { getServerAccount } from './utils'
18 function generateThumbnailFromUrl (video: VideoInstance, icon: ActivityIconObject) {
19 const thumbnailName = video.getThumbnailName()
20 const thumbnailPath = join(CONFIG.STORAGE.THUMBNAILS_DIR, thumbnailName)
26 return doRequestAndSaveToFile(options, thumbnailPath)
29 async function shareVideoChannelByServer (videoChannel: VideoChannelInstance, t: Sequelize.Transaction) {
30 const serverAccount = await getServerAccount()
32 await db.VideoChannelShare.create({
33 accountId: serverAccount.id,
34 videoChannelId: videoChannel.id
35 }, { transaction: t })
37 return sendAnnounce(serverAccount, videoChannel, t)
40 async function shareVideoByServer (video: VideoInstance, t: Sequelize.Transaction) {
41 const serverAccount = await getServerAccount()
43 await db.VideoShare.create({
44 accountId: serverAccount.id,
46 }, { transaction: t })
48 return sendAnnounce(serverAccount, video, t)
51 function getActivityPubUrl (type: 'video' | 'videoChannel' | 'account' | 'videoAbuse', id: string) {
52 if (type === 'video') return CONFIG.WEBSERVER.URL + '/videos/watch/' + id
53 else if (type === 'videoChannel') return CONFIG.WEBSERVER.URL + '/video-channels/' + id
54 else if (type === 'account') return CONFIG.WEBSERVER.URL + '/account/' + id
55 else if (type === 'videoAbuse') return CONFIG.WEBSERVER.URL + '/admin/video-abuses/' + id
60 async function getOrCreateAccount (accountUrl: string) {
61 let account = await db.Account.loadByUrl(accountUrl)
63 // We don't have this account in our database, fetch it on remote
65 const res = await fetchRemoteAccountAndCreateServer(accountUrl)
66 if (res === undefined) throw new Error('Cannot fetch remote account.')
68 // Save our new account in database
69 const account = res.account
76 async function fetchRemoteAccountAndCreateServer (accountUrl: string) {
81 'Accept': ACTIVITY_PUB_ACCEPT_HEADER
85 logger.info('Fetching remote account %s.', accountUrl)
89 requestResult = await doRequest(options)
91 logger.warn('Cannot fetch remote account %s.', accountUrl, err)
95 const accountJSON: ActivityPubActor = JSON.parse(requestResult.body)
96 if (isRemoteAccountValid(accountJSON) === false) {
97 logger.debug('Remote account JSON is not valid.', { accountJSON })
101 const followersCount = await fetchAccountCount(accountJSON.followers)
102 const followingCount = await fetchAccountCount(accountJSON.following)
104 const account = db.Account.build({
105 uuid: accountJSON.uuid,
106 name: accountJSON.preferredUsername,
107 url: accountJSON.url,
108 publicKey: accountJSON.publicKey.publicKeyPem,
110 followersCount: followersCount,
111 followingCount: followingCount,
112 inboxUrl: accountJSON.inbox,
113 outboxUrl: accountJSON.outbox,
114 sharedInboxUrl: accountJSON.endpoints.sharedInbox,
115 followersUrl: accountJSON.followers,
116 followingUrl: accountJSON.following
119 const accountHost = url.parse(account.url).host
120 const serverOptions = {
128 const [ server ] = await db.Server.findOrCreate(serverOptions)
129 account.set('serverId', server.id)
131 return { account, server }
134 function fetchRemoteVideoPreview (video: VideoInstance) {
136 const host = video.VideoChannel.Account.Server.host
137 const path = join(STATIC_PATHS.PREVIEWS, video.getPreviewName())
139 return request.get(REMOTE_SCHEME.HTTP + '://' + host + path)
142 async function fetchRemoteVideoDescription (video: VideoInstance) {
147 const { body } = await doRequest(options)
148 return body.description ? body.description : ''
151 function activityPubContextify <T> (data: T) {
152 return Object.assign(data,{
154 'https://www.w3.org/ns/activitystreams',
155 'https://w3id.org/security/v1',
157 'Hashtag': 'as:Hashtag',
158 'uuid': 'http://schema.org/identifier',
159 'category': 'http://schema.org/category',
160 'licence': 'http://schema.org/license',
161 'nsfw': 'as:sensitive',
162 'language': 'http://schema.org/inLanguage',
163 'views': 'http://schema.org/Number',
164 'size': 'http://schema.org/Number',
165 'VideoChannel': 'https://peertu.be/ns/VideoChannel'
171 function activityPubCollectionPagination (url: string, page: number, result: ResultList<any>) {
172 const baseUrl = url.split('?').shift
177 totalItems: result.total,
179 id: baseUrl + '?page=' + page,
180 type: 'CollectionPage',
181 totalItems: result.total,
182 next: baseUrl + '?page=' + (page + 1),
188 return activityPubContextify(obj)
191 // ---------------------------------------------------------------------------
194 fetchRemoteAccountAndCreateServer,
195 activityPubContextify,
196 activityPubCollectionPagination,
198 generateThumbnailFromUrl,
200 fetchRemoteVideoPreview,
201 fetchRemoteVideoDescription,
202 shareVideoChannelByServer,
206 // ---------------------------------------------------------------------------
208 async function fetchAccountCount (url: string) {
216 requestResult = await doRequest(options)
218 logger.warn('Cannot fetch remote account count %s.', url, err)
222 return requestResult.totalItems ? requestResult.totalItems : 0