aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/lib/activitypub/account.ts
diff options
context:
space:
mode:
Diffstat (limited to 'server/lib/activitypub/account.ts')
-rw-r--r--server/lib/activitypub/account.ts69
1 files changed, 48 insertions, 21 deletions
diff --git a/server/lib/activitypub/account.ts b/server/lib/activitypub/account.ts
index 704a92e13..906c8ff29 100644
--- a/server/lib/activitypub/account.ts
+++ b/server/lib/activitypub/account.ts
@@ -1,27 +1,65 @@
1import * as Bluebird from 'bluebird'
1import * as url from 'url' 2import * as url from 'url'
2import { ActivityPubActor } from '../../../shared/models/activitypub/activitypub-actor' 3import { ActivityPubActor } from '../../../shared/models/activitypub/activitypub-actor'
3import { isRemoteAccountValid } from '../../helpers/custom-validators/activitypub/account' 4import { isRemoteAccountValid } from '../../helpers/custom-validators/activitypub/account'
5import { retryTransactionWrapper } from '../../helpers/database-utils'
4import { logger } from '../../helpers/logger' 6import { logger } from '../../helpers/logger'
5import { doRequest } from '../../helpers/requests' 7import { doRequest } from '../../helpers/requests'
6import { ACTIVITY_PUB } from '../../initializers/constants' 8import { ACTIVITY_PUB } from '../../initializers/constants'
7import { database as db } from '../../initializers/database' 9import { database as db } from '../../initializers/database'
10import { AccountInstance } from '../../models/account/account-interface'
11import { Transaction } from 'sequelize'
8 12
9async function getOrCreateAccount (accountUrl: string) { 13async function getOrCreateAccountAndServer (accountUrl: string) {
10 let account = await db.Account.loadByUrl(accountUrl) 14 let account = await db.Account.loadByUrl(accountUrl)
11 15
12 // We don't have this account in our database, fetch it on remote 16 // We don't have this account in our database, fetch it on remote
13 if (!account) { 17 if (!account) {
14 const res = await fetchRemoteAccountAndCreateServer(accountUrl) 18 account = await fetchRemoteAccount(accountUrl)
15 if (res === undefined) throw new Error('Cannot fetch remote account.') 19 if (account === undefined) throw new Error('Cannot fetch remote account.')
16 20
17 // Save our new account in database 21 const options = {
18 account = await res.account.save() 22 arguments: [ account ],
23 errorMessage: 'Cannot save account and server with many retries.'
24 }
25 account = await retryTransactionWrapper(saveAccountAndServerIfNotExist, options)
19 } 26 }
20 27
21 return account 28 return account
22} 29}
23 30
24async function fetchRemoteAccountAndCreateServer (accountUrl: string) { 31function saveAccountAndServerIfNotExist (account: AccountInstance, t?: Transaction): Bluebird<AccountInstance> | Promise<AccountInstance> {
32 if (t !== undefined) {
33 return save(t)
34 } else {
35 return db.sequelize.transaction(t => {
36 return save(t)
37 })
38 }
39
40 async function save (t: Transaction) {
41 const accountHost = url.parse(account.url).host
42
43 const serverOptions = {
44 where: {
45 host: accountHost
46 },
47 defaults: {
48 host: accountHost
49 },
50 transaction: t
51 }
52 const [ server ] = await db.Server.findOrCreate(serverOptions)
53
54 // Save our new account in database
55 account.set('serverId', server.id)
56 account = await account.save({ transaction: t })
57
58 return account
59 }
60}
61
62async function fetchRemoteAccount (accountUrl: string) {
25 const options = { 63 const options = {
26 uri: accountUrl, 64 uri: accountUrl,
27 method: 'GET', 65 method: 'GET',
@@ -64,24 +102,13 @@ async function fetchRemoteAccountAndCreateServer (accountUrl: string) {
64 followingUrl: accountJSON.following 102 followingUrl: accountJSON.following
65 }) 103 })
66 104
67 const accountHost = url.parse(account.url).host 105 return account
68 const serverOptions = {
69 where: {
70 host: accountHost
71 },
72 defaults: {
73 host: accountHost
74 }
75 }
76 const [ server ] = await db.Server.findOrCreate(serverOptions)
77 account.set('serverId', server.id)
78
79 return { account, server }
80} 106}
81 107
82export { 108export {
83 getOrCreateAccount, 109 getOrCreateAccountAndServer,
84 fetchRemoteAccountAndCreateServer 110 fetchRemoteAccount,
111 saveAccountAndServerIfNotExist
85} 112}
86 113
87// --------------------------------------------------------------------------- 114// ---------------------------------------------------------------------------