2021-02-20 19:12:44 +01:00
|
|
|
import apiGeneral from '@api/general'
|
|
|
|
import { createAsyncThunk } from '@reduxjs/toolkit'
|
|
|
|
import { RootState } from '@root/store'
|
2022-05-28 19:24:08 +02:00
|
|
|
import { InstanceLatest } from '@utils/migrations/instances/migration'
|
2021-02-20 19:12:44 +01:00
|
|
|
|
|
|
|
const addInstance = createAsyncThunk(
|
|
|
|
'instances/add',
|
|
|
|
async ({
|
|
|
|
domain,
|
|
|
|
token,
|
|
|
|
instance,
|
|
|
|
appData
|
|
|
|
}: {
|
2022-05-28 19:24:08 +02:00
|
|
|
domain: InstanceLatest['url']
|
|
|
|
token: InstanceLatest['token']
|
2021-02-20 19:12:44 +01:00
|
|
|
instance: Mastodon.Instance
|
2022-05-28 19:24:08 +02:00
|
|
|
appData: InstanceLatest['appData']
|
|
|
|
}): Promise<{ type: 'add' | 'overwrite'; data: InstanceLatest }> => {
|
2021-02-20 19:12:44 +01:00
|
|
|
const { store } = require('@root/store')
|
|
|
|
const instances = (store.getState() as RootState).instances.instances
|
|
|
|
|
|
|
|
const {
|
|
|
|
body: { id, acct, avatar_static }
|
|
|
|
} = await apiGeneral<Mastodon.Account>({
|
|
|
|
method: 'get',
|
|
|
|
domain,
|
|
|
|
url: `api/v1/accounts/verify_credentials`,
|
|
|
|
headers: { Authorization: `Bearer ${token}` }
|
|
|
|
})
|
|
|
|
|
|
|
|
let type: 'add' | 'overwrite'
|
|
|
|
type = 'add'
|
|
|
|
if (
|
|
|
|
instances.length &&
|
|
|
|
instances.filter(instance => {
|
|
|
|
if (instance.url === domain && instance.account.id === id) {
|
|
|
|
return true
|
|
|
|
} else {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}).length
|
|
|
|
) {
|
|
|
|
type = 'overwrite'
|
|
|
|
} else {
|
|
|
|
type = 'add'
|
|
|
|
}
|
|
|
|
|
|
|
|
const { body: preferences } = await apiGeneral<Mastodon.Preferences>({
|
|
|
|
method: 'get',
|
|
|
|
domain,
|
|
|
|
url: `api/v1/preferences`,
|
|
|
|
headers: { Authorization: `Bearer ${token}` }
|
2022-12-20 00:45:53 +01:00
|
|
|
}).catch(error => {
|
|
|
|
if (error?.status === 404) {
|
|
|
|
return Promise.resolve({ body: {} })
|
|
|
|
} else {
|
|
|
|
return Promise.reject()
|
|
|
|
}
|
2021-02-20 19:12:44 +01:00
|
|
|
})
|
|
|
|
|
2022-12-18 17:25:18 +01:00
|
|
|
const { body: filters } = await apiGeneral<Mastodon.Filter<any>[]>({
|
2021-08-29 15:25:38 +02:00
|
|
|
method: 'get',
|
|
|
|
domain,
|
|
|
|
url: `api/v1/filters`,
|
|
|
|
headers: { Authorization: `Bearer ${token}` }
|
|
|
|
})
|
|
|
|
|
2021-02-20 19:12:44 +01:00
|
|
|
return Promise.resolve({
|
|
|
|
type,
|
|
|
|
data: {
|
|
|
|
active: true,
|
|
|
|
appData,
|
|
|
|
url: domain,
|
|
|
|
token,
|
2022-12-20 10:04:21 +01:00
|
|
|
uri: instance.uri.replace(/^https?:\/\//, ''), // Pleroma includes schema
|
2021-02-20 19:12:44 +01:00
|
|
|
urls: instance.urls,
|
|
|
|
account: {
|
|
|
|
id,
|
|
|
|
acct,
|
|
|
|
avatarStatic: avatar_static,
|
|
|
|
preferences
|
|
|
|
},
|
2022-04-28 22:52:47 +02:00
|
|
|
version: instance.version,
|
2021-11-15 22:34:43 +01:00
|
|
|
...(instance.configuration && {
|
|
|
|
configuration: instance.configuration
|
|
|
|
}),
|
2021-08-29 15:25:38 +02:00
|
|
|
filters,
|
2021-03-17 15:30:28 +01:00
|
|
|
notifications_filter: {
|
|
|
|
follow: true,
|
2022-05-28 19:24:08 +02:00
|
|
|
follow_request: true,
|
2021-03-17 15:30:28 +01:00
|
|
|
favourite: true,
|
|
|
|
reblog: true,
|
|
|
|
mention: true,
|
|
|
|
poll: true,
|
2022-05-28 19:24:08 +02:00
|
|
|
status: true,
|
2022-12-10 22:43:37 +01:00
|
|
|
update: true,
|
|
|
|
'admin.sign_up': true,
|
|
|
|
'admin.report': true
|
2021-03-17 15:30:28 +01:00
|
|
|
},
|
2021-02-20 19:12:44 +01:00
|
|
|
push: {
|
2022-12-09 21:09:00 +01:00
|
|
|
global: false,
|
|
|
|
decode: false,
|
2021-02-27 16:33:54 +01:00
|
|
|
alerts: {
|
2022-12-09 21:09:00 +01:00
|
|
|
follow: true,
|
|
|
|
follow_request: true,
|
|
|
|
favourite: true,
|
|
|
|
reblog: true,
|
|
|
|
mention: true,
|
|
|
|
poll: true,
|
|
|
|
status: true,
|
2022-12-15 19:31:20 +01:00
|
|
|
update: true,
|
2022-12-09 21:09:00 +01:00
|
|
|
'admin.sign_up': false,
|
|
|
|
'admin.report': false
|
2021-02-27 16:33:54 +01:00
|
|
|
},
|
2021-12-18 19:59:38 +01:00
|
|
|
keys: { auth: undefined, public: undefined, private: undefined }
|
2021-02-20 19:12:44 +01:00
|
|
|
},
|
2022-12-14 23:37:41 +01:00
|
|
|
followingPage: {
|
|
|
|
showBoosts: true,
|
|
|
|
showReplies: true
|
|
|
|
},
|
2022-01-16 23:26:05 +01:00
|
|
|
mePage: {
|
2022-12-10 20:19:18 +01:00
|
|
|
followedTags: { shown: false },
|
2022-01-16 23:26:05 +01:00
|
|
|
lists: { shown: false },
|
|
|
|
announcements: { shown: false, unread: 0 }
|
|
|
|
},
|
2022-02-14 23:43:43 +01:00
|
|
|
drafts: [],
|
|
|
|
frequentEmojis: []
|
2021-02-20 19:12:44 +01:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
export default addInstance
|