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 { VideoChannelObject } from '../../shared/models/activitypub/objects/video-channel-object'
8 import { ResultList } from '../../shared/models/result-list.model'
9 import { database as db, REMOTE_SCHEME } from '../initializers'
10 import { ACTIVITY_PUB_ACCEPT_HEADER, CONFIG, STATIC_PATHS } from '../initializers/constants'
11 import { videoChannelActivityObjectToDBAttributes } from '../lib/activitypub/misc'
12 import { sendVideoAnnounce } from '../lib/activitypub/send-request'
13 import { sendVideoChannelAnnounce } from '../lib/index'
14 import { AccountInstance } from '../models/account/account-interface'
15 import { VideoChannelInstance } from '../models/video/video-channel-interface'
16 import { VideoInstance } from '../models/video/video-interface'
17 import { isRemoteAccountValid } from './custom-validators'
18 import { isVideoChannelObjectValid } from './custom-validators/activitypub/videos'
19 import { logger } from './logger'
20 import { doRequest, doRequestAndSaveToFile } from './requests'
21 import { getServerAccount } from './utils'
23 function generateThumbnailFromUrl (video: VideoInstance, icon: ActivityIconObject) {
24 const thumbnailName = video.getThumbnailName()
25 const thumbnailPath = join(CONFIG.STORAGE.THUMBNAILS_DIR, thumbnailName)
31 return doRequestAndSaveToFile(options, thumbnailPath)
34 async function shareVideoChannelByServer (videoChannel: VideoChannelInstance, t: Sequelize.Transaction) {
35 const serverAccount = await getServerAccount()
37 await db.VideoChannelShare.create({
38 accountId: serverAccount.id,
39 videoChannelId: videoChannel.id
40 }, { transaction: t })
42 return sendVideoChannelAnnounce(serverAccount, videoChannel, t)
45 async function shareVideoByServer (video: VideoInstance, t: Sequelize.Transaction) {
46 const serverAccount = await getServerAccount()
48 await db.VideoShare.create({
49 accountId: serverAccount.id,
51 }, { transaction: t })
53 return sendVideoAnnounce(serverAccount, video, t)
56 function getActivityPubUrl (type: 'video' | 'videoChannel' | 'account' | 'videoAbuse', id: string) {
57 if (type === 'video') return CONFIG.WEBSERVER.URL + '/videos/watch/' + id
58 else if (type === 'videoChannel') return CONFIG.WEBSERVER.URL + '/video-channels/' + id
59 else if (type === 'account') return CONFIG.WEBSERVER.URL + '/account/' + id
60 else if (type === 'videoAbuse') return CONFIG.WEBSERVER.URL + '/admin/video-abuses/' + id
65 async function getOrCreateAccount (accountUrl: string) {
66 let account = await db.Account.loadByUrl(accountUrl)
68 // We don't have this account in our database, fetch it on remote
70 const res = await fetchRemoteAccountAndCreateServer(accountUrl)
71 if (res === undefined) throw new Error('Cannot fetch remote account.')
73 // Save our new account in database
74 account = await res.account.save()
80 async function getOrCreateVideoChannel (ownerAccount: AccountInstance, videoChannelUrl: string) {
81 let videoChannel = await db.VideoChannel.loadByUrl(videoChannelUrl)
83 // We don't have this account in our database, fetch it on remote
85 videoChannel = await fetchRemoteVideoChannel(ownerAccount, videoChannelUrl)
86 if (videoChannel === undefined) throw new Error('Cannot fetch remote video channel.')
88 // Save our new video channel in database
89 await videoChannel.save()
95 async function fetchRemoteAccountAndCreateServer (accountUrl: string) {
100 'Accept': ACTIVITY_PUB_ACCEPT_HEADER
104 logger.info('Fetching remote account %s.', accountUrl)
108 requestResult = await doRequest(options)
110 logger.warn('Cannot fetch remote account %s.', accountUrl, err)
114 const accountJSON: ActivityPubActor = JSON.parse(requestResult.body)
115 if (isRemoteAccountValid(accountJSON) === false) {
116 logger.debug('Remote account JSON is not valid.', { accountJSON })
120 const followersCount = await fetchAccountCount(accountJSON.followers)
121 const followingCount = await fetchAccountCount(accountJSON.following)
123 const account = db.Account.build({
124 uuid: accountJSON.uuid,
125 name: accountJSON.preferredUsername,
126 url: accountJSON.url,
127 publicKey: accountJSON.publicKey.publicKeyPem,
129 followersCount: followersCount,
130 followingCount: followingCount,
131 inboxUrl: accountJSON.inbox,
132 outboxUrl: accountJSON.outbox,
133 sharedInboxUrl: accountJSON.endpoints.sharedInbox,
134 followersUrl: accountJSON.followers,
135 followingUrl: accountJSON.following
138 const accountHost = url.parse(account.url).host
139 const serverOptions = {
147 const [ server ] = await db.Server.findOrCreate(serverOptions)
148 account.set('serverId', server.id)
150 return { account, server }
153 async function fetchRemoteVideoChannel (ownerAccount: AccountInstance, videoChannelUrl: string) {
155 uri: videoChannelUrl,
158 'Accept': ACTIVITY_PUB_ACCEPT_HEADER
162 logger.info('Fetching remote video channel %s.', videoChannelUrl)
166 requestResult = await doRequest(options)
168 logger.warn('Cannot fetch remote video channel %s.', videoChannelUrl, err)
172 const videoChannelJSON: VideoChannelObject = JSON.parse(requestResult.body)
173 if (isVideoChannelObjectValid(videoChannelJSON) === false) {
174 logger.debug('Remote video channel JSON is not valid.', { videoChannelJSON })
178 const videoChannelAttributes = videoChannelActivityObjectToDBAttributes(videoChannelJSON, ownerAccount)
179 const videoChannel = db.VideoChannel.build(videoChannelAttributes)
180 videoChannel.Account = ownerAccount
185 function fetchRemoteVideoPreview (video: VideoInstance) {
187 const host = video.VideoChannel.Account.Server.host
188 const path = join(STATIC_PATHS.PREVIEWS, video.getPreviewName())
190 return request.get(REMOTE_SCHEME.HTTP + '://' + host + path)
193 async function fetchRemoteVideoDescription (video: VideoInstance) {
198 const { body } = await doRequest(options)
199 return body.description ? body.description : ''
202 function activityPubContextify <T> (data: T) {
203 return Object.assign(data,{
205 'https://www.w3.org/ns/activitystreams',
206 'https://w3id.org/security/v1',
208 'Hashtag': 'as:Hashtag',
209 'uuid': 'http://schema.org/identifier',
210 'category': 'http://schema.org/category',
211 'licence': 'http://schema.org/license',
212 'nsfw': 'as:sensitive',
213 'language': 'http://schema.org/inLanguage',
214 'views': 'http://schema.org/Number',
215 'size': 'http://schema.org/Number',
216 'VideoChannel': 'https://peertu.be/ns/VideoChannel'
222 function activityPubCollectionPagination (url: string, page: number, result: ResultList<any>) {
223 const baseUrl = url.split('?').shift
228 totalItems: result.total,
230 id: baseUrl + '?page=' + page,
231 type: 'CollectionPage',
232 totalItems: result.total,
233 next: baseUrl + '?page=' + (page + 1),
239 return activityPubContextify(obj)
242 // ---------------------------------------------------------------------------
245 fetchRemoteAccountAndCreateServer,
246 activityPubContextify,
247 activityPubCollectionPagination,
249 generateThumbnailFromUrl,
251 fetchRemoteVideoPreview,
252 fetchRemoteVideoDescription,
253 shareVideoChannelByServer,
255 getOrCreateVideoChannel
258 // ---------------------------------------------------------------------------
260 async function fetchAccountCount (url: string) {
268 requestResult = await doRequest(options)
270 logger.warn('Cannot fetch remote account count %s.', url, err)
274 return requestResult.totalItems ? requestResult.totalItems : 0