]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/helpers/activitypub.ts
Continue activitypub
[github/Chocobozzz/PeerTube.git] / server / helpers / activitypub.ts
1 import * as url from 'url'
2
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'
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', 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
28
29 return ''
30 }
31
32 async function getOrCreateAccount (accountUrl: string) {
33 let account = await db.Account.loadByUrl(accountUrl)
34
35 // We don't have this account in our database, fetch it on remote
36 if (!account) {
37 const { account } = await fetchRemoteAccountAndCreatePod(accountUrl)
38
39 if (!account) throw new Error('Cannot fetch remote account.')
40
41 // Save our new account in database
42 await account.save()
43 }
44
45 return account
46 }
47
48 async function fetchRemoteAccountAndCreatePod (accountUrl: string) {
49 const options = {
50 uri: accountUrl,
51 method: 'GET'
52 }
53
54 let requestResult
55 try {
56 requestResult = await doRequest(options)
57 } catch (err) {
58 logger.warning('Cannot fetch remote account %s.', accountUrl, err)
59 return undefined
60 }
61
62 const accountJSON: ActivityPubActor = requestResult.body
63 if (isRemoteAccountValid(accountJSON) === false) return undefined
64
65 const followersCount = await fetchAccountCount(accountJSON.followers)
66 const followingCount = await fetchAccountCount(accountJSON.following)
67
68 const account = db.Account.build({
69 uuid: accountJSON.uuid,
70 name: accountJSON.preferredUsername,
71 url: accountJSON.url,
72 publicKey: accountJSON.publicKey.publicKeyPem,
73 privateKey: null,
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
81 })
82
83 const accountHost = url.parse(account.url).host
84 const podOptions = {
85 where: {
86 host: accountHost
87 },
88 defaults: {
89 host: accountHost
90 }
91 }
92 const pod = await db.Pod.findOrCreate(podOptions)
93
94 return { account, pod }
95 }
96
97 function activityPubContextify (data: object) {
98 return Object.assign(data,{
99 '@context': [
100 'https://www.w3.org/ns/activitystreams',
101 'https://w3id.org/security/v1',
102 {
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'
111 }
112 ]
113 })
114 }
115
116 function activityPubCollectionPagination (url: string, page: number, result: ResultList<any>) {
117 const baseUrl = url.split('?').shift
118
119 const obj = {
120 id: baseUrl,
121 type: 'Collection',
122 totalItems: result.total,
123 first: {
124 id: baseUrl + '?page=' + page,
125 type: 'CollectionPage',
126 totalItems: result.total,
127 next: baseUrl + '?page=' + (page + 1),
128 partOf: baseUrl,
129 items: result.data
130 }
131 }
132
133 return activityPubContextify(obj)
134 }
135
136 // ---------------------------------------------------------------------------
137
138 export {
139 fetchRemoteAccountAndCreatePod,
140 activityPubContextify,
141 activityPubCollectionPagination,
142 getActivityPubUrl,
143 generateThumbnailFromUrl,
144 getOrCreateAccount
145 }
146
147 // ---------------------------------------------------------------------------
148
149 async function fetchAccountCount (url: string) {
150 const options = {
151 uri: url,
152 method: 'GET'
153 }
154
155 let requestResult
156 try {
157 requestResult = await doRequest(options)
158 } catch (err) {
159 logger.warning('Cannot fetch remote account count %s.', url, err)
160 return undefined
161 }
162
163 return requestResult.totalItems ? requestResult.totalItems : 0
164 }