]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/helpers/activitypub.ts
de20ba55d28989ce3e19a0b651c7a02cca3e7079
[github/Chocobozzz/PeerTube.git] / server / helpers / activitypub.ts
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'
13
14 function generateThumbnailFromUrl (video: VideoInstance, icon: ActivityIconObject) {
15 const thumbnailName = video.getThumbnailName()
16 const thumbnailPath = join(CONFIG.STORAGE.THUMBNAILS_DIR, thumbnailName)
17
18 const options = {
19 method: 'GET',
20 uri: icon.url
21 }
22 return doRequestAndSaveToFile(options, thumbnailPath)
23 }
24
25 function getActivityPubUrl (type: 'video' | 'videoChannel' | 'account' | 'videoAbuse', 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
29 else if (type === 'videoAbuse') return CONFIG.WEBSERVER.URL + '/admin/video-abuses/' + id
30
31 return ''
32 }
33
34 async function getOrCreateAccount (accountUrl: string) {
35 let account = await db.Account.loadByUrl(accountUrl)
36
37 // We don't have this account in our database, fetch it on remote
38 if (!account) {
39 const res = await fetchRemoteAccountAndCreateServer(accountUrl)
40 if (res === undefined) throw new Error('Cannot fetch remote account.')
41
42 // Save our new account in database
43 const account = res.account
44 await account.save()
45 }
46
47 return account
48 }
49
50 async function fetchRemoteAccountAndCreateServer (accountUrl: string) {
51 const options = {
52 uri: accountUrl,
53 method: 'GET',
54 headers: {
55 'Accept': ACTIVITY_PUB_ACCEPT_HEADER
56 }
57 }
58
59 logger.info('Fetching remote account %s.', accountUrl)
60
61 let requestResult
62 try {
63 requestResult = await doRequest(options)
64 } catch (err) {
65 logger.warn('Cannot fetch remote account %s.', accountUrl, err)
66 return undefined
67 }
68
69 const accountJSON: ActivityPubActor = JSON.parse(requestResult.body)
70 if (isRemoteAccountValid(accountJSON) === false) {
71 logger.debug('Remote account JSON is not valid.', { accountJSON })
72 return undefined
73 }
74
75 const followersCount = await fetchAccountCount(accountJSON.followers)
76 const followingCount = await fetchAccountCount(accountJSON.following)
77
78 const account = db.Account.build({
79 uuid: accountJSON.uuid,
80 name: accountJSON.preferredUsername,
81 url: accountJSON.url,
82 publicKey: accountJSON.publicKey.publicKeyPem,
83 privateKey: null,
84 followersCount: followersCount,
85 followingCount: followingCount,
86 inboxUrl: accountJSON.inbox,
87 outboxUrl: accountJSON.outbox,
88 sharedInboxUrl: accountJSON.endpoints.sharedInbox,
89 followersUrl: accountJSON.followers,
90 followingUrl: accountJSON.following
91 })
92
93 const accountHost = url.parse(account.url).host
94 const serverOptions = {
95 where: {
96 host: accountHost
97 },
98 defaults: {
99 host: accountHost
100 }
101 }
102 const [ server ] = await db.Server.findOrCreate(serverOptions)
103 account.set('serverId', server.id)
104
105 return { account, server }
106 }
107
108 function fetchRemoteVideoPreview (video: VideoInstance) {
109 // FIXME: use url
110 const host = video.VideoChannel.Account.Server.host
111 const path = join(STATIC_PATHS.PREVIEWS, video.getPreviewName())
112
113 return request.get(REMOTE_SCHEME.HTTP + '://' + host + path)
114 }
115
116 async function fetchRemoteVideoDescription (video: VideoInstance) {
117 const options = {
118 uri: video.url
119 }
120
121 const { body } = await doRequest(options)
122 return body.description ? body.description : ''
123 }
124
125 function activityPubContextify <T> (data: T) {
126 return Object.assign(data,{
127 '@context': [
128 'https://www.w3.org/ns/activitystreams',
129 'https://w3id.org/security/v1',
130 {
131 'Hashtag': 'as:Hashtag',
132 'uuid': 'http://schema.org/identifier',
133 'category': 'http://schema.org/category',
134 'licence': 'http://schema.org/license',
135 'nsfw': 'as:sensitive',
136 'language': 'http://schema.org/inLanguage',
137 'views': 'http://schema.org/Number',
138 'size': 'http://schema.org/Number',
139 'VideoChannel': 'https://peertu.be/ns/VideoChannel'
140 }
141 ]
142 })
143 }
144
145 function activityPubCollectionPagination (url: string, page: number, result: ResultList<any>) {
146 const baseUrl = url.split('?').shift
147
148 const obj = {
149 id: baseUrl,
150 type: 'Collection',
151 totalItems: result.total,
152 first: {
153 id: baseUrl + '?page=' + page,
154 type: 'CollectionPage',
155 totalItems: result.total,
156 next: baseUrl + '?page=' + (page + 1),
157 partOf: baseUrl,
158 items: result.data
159 }
160 }
161
162 return activityPubContextify(obj)
163 }
164
165 // ---------------------------------------------------------------------------
166
167 export {
168 fetchRemoteAccountAndCreateServer,
169 activityPubContextify,
170 activityPubCollectionPagination,
171 getActivityPubUrl,
172 generateThumbnailFromUrl,
173 getOrCreateAccount,
174 fetchRemoteVideoPreview,
175 fetchRemoteVideoDescription
176 }
177
178 // ---------------------------------------------------------------------------
179
180 async function fetchAccountCount (url: string) {
181 const options = {
182 uri: url,
183 method: 'GET'
184 }
185
186 let requestResult
187 try {
188 requestResult = await doRequest(options)
189 } catch (err) {
190 logger.warn('Cannot fetch remote account count %s.', url, err)
191 return undefined
192 }
193
194 return requestResult.totalItems ? requestResult.totalItems : 0
195 }