mirror of
https://github.com/tooot-app/app
synced 2025-03-17 12:00:17 +01:00
134 lines
3.4 KiB
TypeScript
134 lines
3.4 KiB
TypeScript
import apiGeneral from '@api/general'
|
|
import { createAsyncThunk } from '@reduxjs/toolkit'
|
|
import { RootState } from '@root/store'
|
|
import { InstanceLatest } from '@utils/migrations/instances/migration'
|
|
|
|
const addInstance = createAsyncThunk(
|
|
'instances/add',
|
|
async ({
|
|
domain,
|
|
token,
|
|
instance,
|
|
appData
|
|
}: {
|
|
domain: InstanceLatest['url']
|
|
token: InstanceLatest['token']
|
|
instance: Mastodon.Instance
|
|
appData: InstanceLatest['appData']
|
|
}): Promise<{ type: 'add' | 'overwrite'; data: InstanceLatest }> => {
|
|
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}` }
|
|
}).catch(error => {
|
|
if (error?.status === 404) {
|
|
return Promise.resolve({ body: {} })
|
|
} else {
|
|
return Promise.reject()
|
|
}
|
|
})
|
|
|
|
const { body: filters } = await apiGeneral<Mastodon.Filter<any>[]>({
|
|
method: 'get',
|
|
domain,
|
|
url: `api/v1/filters`,
|
|
headers: { Authorization: `Bearer ${token}` }
|
|
})
|
|
|
|
return Promise.resolve({
|
|
type,
|
|
data: {
|
|
active: true,
|
|
appData,
|
|
url: domain,
|
|
token,
|
|
uri: instance.uri.replace(/^https?:\/\//, ''), // Pleroma includes schema
|
|
urls: instance.urls,
|
|
account: {
|
|
id,
|
|
acct,
|
|
avatarStatic: avatar_static,
|
|
preferences
|
|
},
|
|
version: instance.version,
|
|
...(instance.configuration && {
|
|
configuration: instance.configuration
|
|
}),
|
|
filters,
|
|
notifications_filter: {
|
|
follow: true,
|
|
follow_request: true,
|
|
favourite: true,
|
|
reblog: true,
|
|
mention: true,
|
|
poll: true,
|
|
status: true,
|
|
update: true,
|
|
'admin.sign_up': true,
|
|
'admin.report': true
|
|
},
|
|
push: {
|
|
global: false,
|
|
decode: false,
|
|
alerts: {
|
|
follow: true,
|
|
follow_request: true,
|
|
favourite: true,
|
|
reblog: true,
|
|
mention: true,
|
|
poll: true,
|
|
status: true,
|
|
update: true,
|
|
'admin.sign_up': false,
|
|
'admin.report': false
|
|
},
|
|
keys: { auth: undefined, public: undefined, private: undefined }
|
|
},
|
|
followingPage: {
|
|
showBoosts: true,
|
|
showReplies: true
|
|
},
|
|
mePage: {
|
|
followedTags: { shown: false },
|
|
lists: { shown: false },
|
|
announcements: { shown: false, unread: 0 }
|
|
},
|
|
drafts: [],
|
|
frequentEmojis: []
|
|
}
|
|
})
|
|
}
|
|
)
|
|
|
|
export default addInstance
|