aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/helpers/activitypub.ts
diff options
context:
space:
mode:
Diffstat (limited to 'server/helpers/activitypub.ts')
-rw-r--r--server/helpers/activitypub.ts29
1 files changed, 19 insertions, 10 deletions
diff --git a/server/helpers/activitypub.ts b/server/helpers/activitypub.ts
index a1493e5c1..b91490a0b 100644
--- a/server/helpers/activitypub.ts
+++ b/server/helpers/activitypub.ts
@@ -5,7 +5,7 @@ import { ActivityIconObject } from '../../shared/index'
5import { ActivityPubActor } from '../../shared/models/activitypub/activitypub-actor' 5import { ActivityPubActor } from '../../shared/models/activitypub/activitypub-actor'
6import { ResultList } from '../../shared/models/result-list.model' 6import { ResultList } from '../../shared/models/result-list.model'
7import { database as db, REMOTE_SCHEME } from '../initializers' 7import { database as db, REMOTE_SCHEME } from '../initializers'
8import { CONFIG, STATIC_PATHS } from '../initializers/constants' 8import { ACTIVITY_PUB_ACCEPT_HEADER, CONFIG, STATIC_PATHS } from '../initializers/constants'
9import { VideoInstance } from '../models/video/video-interface' 9import { VideoInstance } from '../models/video/video-interface'
10import { isRemoteAccountValid } from './custom-validators' 10import { isRemoteAccountValid } from './custom-validators'
11import { logger } from './logger' 11import { logger } from './logger'
@@ -35,11 +35,11 @@ async function getOrCreateAccount (accountUrl: string) {
35 35
36 // We don't have this account in our database, fetch it on remote 36 // We don't have this account in our database, fetch it on remote
37 if (!account) { 37 if (!account) {
38 const { account } = await fetchRemoteAccountAndCreatePod(accountUrl) 38 const res = await fetchRemoteAccountAndCreatePod(accountUrl)
39 39 if (res === undefined) throw new Error('Cannot fetch remote account.')
40 if (!account) throw new Error('Cannot fetch remote account.')
41 40
42 // Save our new account in database 41 // Save our new account in database
42 const account = res.account
43 await account.save() 43 await account.save()
44 } 44 }
45 45
@@ -49,19 +49,27 @@ async function getOrCreateAccount (accountUrl: string) {
49async function fetchRemoteAccountAndCreatePod (accountUrl: string) { 49async function fetchRemoteAccountAndCreatePod (accountUrl: string) {
50 const options = { 50 const options = {
51 uri: accountUrl, 51 uri: accountUrl,
52 method: 'GET' 52 method: 'GET',
53 headers: {
54 'Accept': ACTIVITY_PUB_ACCEPT_HEADER
55 }
53 } 56 }
54 57
58 logger.info('Fetching remote account %s.', accountUrl)
59
55 let requestResult 60 let requestResult
56 try { 61 try {
57 requestResult = await doRequest(options) 62 requestResult = await doRequest(options)
58 } catch (err) { 63 } catch (err) {
59 logger.warning('Cannot fetch remote account %s.', accountUrl, err) 64 logger.warn('Cannot fetch remote account %s.', accountUrl, err)
60 return undefined 65 return undefined
61 } 66 }
62 67
63 const accountJSON: ActivityPubActor = requestResult.body 68 const accountJSON: ActivityPubActor = JSON.parse(requestResult.body)
64 if (isRemoteAccountValid(accountJSON) === false) return undefined 69 if (isRemoteAccountValid(accountJSON) === false) {
70 logger.debug('Remote account JSON is not valid.', { accountJSON })
71 return undefined
72 }
65 73
66 const followersCount = await fetchAccountCount(accountJSON.followers) 74 const followersCount = await fetchAccountCount(accountJSON.followers)
67 const followingCount = await fetchAccountCount(accountJSON.following) 75 const followingCount = await fetchAccountCount(accountJSON.following)
@@ -90,7 +98,8 @@ async function fetchRemoteAccountAndCreatePod (accountUrl: string) {
90 host: accountHost 98 host: accountHost
91 } 99 }
92 } 100 }
93 const pod = await db.Pod.findOrCreate(podOptions) 101 const [ pod ] = await db.Pod.findOrCreate(podOptions)
102 account.set('podId', pod.id)
94 103
95 return { account, pod } 104 return { account, pod }
96} 105}
@@ -176,7 +185,7 @@ async function fetchAccountCount (url: string) {
176 try { 185 try {
177 requestResult = await doRequest(options) 186 requestResult = await doRequest(options)
178 } catch (err) { 187 } catch (err) {
179 logger.warning('Cannot fetch remote account count %s.', url, err) 188 logger.warn('Cannot fetch remote account count %s.', url, err)
180 return undefined 189 return undefined
181 } 190 }
182 191