mirror of
https://github.com/tooot-app/app
synced 2025-06-05 22:19:13 +02:00
Restructure the project
This commit is contained in:
10
src/utils/fetches/accountFetch.ts
Normal file
10
src/utils/fetches/accountFetch.ts
Normal file
@ -0,0 +1,10 @@
|
||||
import client from 'src/api/client'
|
||||
|
||||
export const accountFetch = async (key: string, { id }: { id: string }) => {
|
||||
const res = await client({
|
||||
method: 'get',
|
||||
instance: 'local',
|
||||
endpoint: `accounts/${id}`
|
||||
})
|
||||
return Promise.resolve(res.body)
|
||||
}
|
10
src/utils/fetches/emojisFetch.ts
Normal file
10
src/utils/fetches/emojisFetch.ts
Normal file
@ -0,0 +1,10 @@
|
||||
import client from 'src/api/client'
|
||||
|
||||
export const emojisFetch = async () => {
|
||||
const res = await client({
|
||||
method: 'get',
|
||||
instance: 'local',
|
||||
endpoint: 'custom_emojis'
|
||||
})
|
||||
return Promise.resolve(res.body)
|
||||
}
|
14
src/utils/fetches/instanceFetch.ts
Normal file
14
src/utils/fetches/instanceFetch.ts
Normal file
@ -0,0 +1,14 @@
|
||||
import client from 'src/api/client'
|
||||
|
||||
export const instanceFetch = async (
|
||||
key: string,
|
||||
{ instance }: { instance: string }
|
||||
) => {
|
||||
const res = await client({
|
||||
method: 'get',
|
||||
instance: 'remote',
|
||||
instanceUrl: instance,
|
||||
endpoint: `instance`
|
||||
})
|
||||
return Promise.resolve(res.body)
|
||||
}
|
23
src/utils/fetches/searchFetch.ts
Normal file
23
src/utils/fetches/searchFetch.ts
Normal file
@ -0,0 +1,23 @@
|
||||
import client from 'src/api/client'
|
||||
|
||||
export const searchFetch = async (
|
||||
{} = {},
|
||||
{
|
||||
type,
|
||||
term,
|
||||
limit = 20
|
||||
}: {
|
||||
type: 'accounts' | 'hashtags' | 'statuses'
|
||||
term: string
|
||||
limit?: number
|
||||
}
|
||||
) => {
|
||||
const res = await client({
|
||||
version: 'v2',
|
||||
method: 'get',
|
||||
instance: 'local',
|
||||
endpoint: 'search',
|
||||
query: { type, q: term, limit }
|
||||
})
|
||||
return Promise.resolve(res.body)
|
||||
}
|
173
src/utils/fetches/timelineFetch.ts
Normal file
173
src/utils/fetches/timelineFetch.ts
Normal file
@ -0,0 +1,173 @@
|
||||
import { uniqBy } from 'lodash'
|
||||
|
||||
import client from 'src/api/client'
|
||||
|
||||
export const timelineFetch = async (
|
||||
key: string,
|
||||
{
|
||||
page,
|
||||
query = {},
|
||||
account,
|
||||
hashtag,
|
||||
list,
|
||||
toot
|
||||
}: {
|
||||
page: string
|
||||
query?: {
|
||||
[key: string]: string | number | boolean
|
||||
}
|
||||
account?: string
|
||||
hashtag?: string
|
||||
list?: string
|
||||
toot?: string
|
||||
},
|
||||
pagination: {
|
||||
direction: 'prev' | 'next'
|
||||
id: string
|
||||
}
|
||||
) => {
|
||||
let res
|
||||
|
||||
if (pagination && pagination.id) {
|
||||
switch (pagination.direction) {
|
||||
case 'prev':
|
||||
query.min_id = pagination.id
|
||||
break
|
||||
case 'next':
|
||||
query.max_id = pagination.id
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
switch (page) {
|
||||
case 'Following':
|
||||
res = await client({
|
||||
method: 'get',
|
||||
instance: 'local',
|
||||
endpoint: 'timelines/home',
|
||||
query
|
||||
})
|
||||
return Promise.resolve({ toots: res.body })
|
||||
|
||||
case 'Local':
|
||||
query.local = 'true'
|
||||
res = await client({
|
||||
method: 'get',
|
||||
instance: 'local',
|
||||
endpoint: 'timelines/public',
|
||||
query
|
||||
})
|
||||
return Promise.resolve({ toots: res.body })
|
||||
|
||||
case 'LocalPublic':
|
||||
res = await client({
|
||||
method: 'get',
|
||||
instance: 'local',
|
||||
endpoint: 'timelines/public',
|
||||
query
|
||||
})
|
||||
return Promise.resolve({ toots: res.body })
|
||||
|
||||
case 'RemotePublic':
|
||||
res = await client({
|
||||
method: 'get',
|
||||
instance: 'remote',
|
||||
endpoint: 'timelines/public',
|
||||
query
|
||||
})
|
||||
return Promise.resolve({ toots: res.body })
|
||||
|
||||
case 'Notifications':
|
||||
res = await client({
|
||||
method: 'get',
|
||||
instance: 'local',
|
||||
endpoint: 'notifications',
|
||||
query
|
||||
})
|
||||
return Promise.resolve({ toots: res.body })
|
||||
|
||||
case 'Account_Default':
|
||||
res = await client({
|
||||
method: 'get',
|
||||
instance: 'local',
|
||||
endpoint: `accounts/${account}/statuses`,
|
||||
query: {
|
||||
pinned: 'true'
|
||||
}
|
||||
})
|
||||
let toots: Mastodon.Status[] = res.body
|
||||
res = await client({
|
||||
method: 'get',
|
||||
instance: 'local',
|
||||
endpoint: `accounts/${account}/statuses`,
|
||||
query: {
|
||||
exclude_replies: 'true'
|
||||
}
|
||||
})
|
||||
toots = uniqBy([...toots, ...res.body], 'id')
|
||||
return Promise.resolve({ toots: toots })
|
||||
|
||||
case 'Account_All':
|
||||
res = await client({
|
||||
method: 'get',
|
||||
instance: 'local',
|
||||
endpoint: `accounts/${account}/statuses`,
|
||||
query
|
||||
})
|
||||
return Promise.resolve({ toots: res.body })
|
||||
|
||||
case 'Account_Media':
|
||||
res = await client({
|
||||
method: 'get',
|
||||
instance: 'local',
|
||||
endpoint: `accounts/${account}/statuses`,
|
||||
query: {
|
||||
only_media: 'true'
|
||||
}
|
||||
})
|
||||
return Promise.resolve({ toots: res.body })
|
||||
|
||||
case 'Hashtag':
|
||||
res = await client({
|
||||
method: 'get',
|
||||
instance: 'local',
|
||||
endpoint: `timelines/tag/${hashtag}`,
|
||||
query
|
||||
})
|
||||
return Promise.resolve({ toots: res.body })
|
||||
|
||||
// case 'List':
|
||||
// res = await client({
|
||||
// method: 'get',
|
||||
// instance: 'local',
|
||||
// endpoint: `timelines/list/${list}`,
|
||||
// query
|
||||
// })
|
||||
// return {
|
||||
// toots: res.body
|
||||
// }
|
||||
|
||||
case 'Toot':
|
||||
const current = await client({
|
||||
method: 'get',
|
||||
instance: 'local',
|
||||
endpoint: `statuses/${toot}`
|
||||
})
|
||||
const context = await client({
|
||||
method: 'get',
|
||||
instance: 'local',
|
||||
endpoint: `statuses/${toot}/context`
|
||||
})
|
||||
return Promise.resolve({
|
||||
toots: [
|
||||
...context.body.ancestors,
|
||||
current.body,
|
||||
...context.body.descendants
|
||||
],
|
||||
pointer: context.body.ancestors.length
|
||||
})
|
||||
|
||||
default:
|
||||
console.error('First time fetching timeline error')
|
||||
}
|
||||
}
|
@ -1,26 +0,0 @@
|
||||
import AsyncStorage from '@react-native-async-storage/async-storage'
|
||||
|
||||
const getItem = async () => {
|
||||
try {
|
||||
const value = await AsyncStorage.getItem('@social.xmflsct.com')
|
||||
if (!value) {
|
||||
await AsyncStorage.setItem(
|
||||
'@social.xmflsct.com',
|
||||
'qjzJ0IjvZ1apsn0_wBkGcdjKgX7Dao9KEPhGwggPwAo'
|
||||
)
|
||||
}
|
||||
return value
|
||||
} catch (e) {
|
||||
console.error('Get token error')
|
||||
}
|
||||
}
|
||||
|
||||
const getAllKeys = async () => {
|
||||
try {
|
||||
return await AsyncStorage.getAllKeys()
|
||||
} catch (e) {
|
||||
console.error('Get all keys error')
|
||||
}
|
||||
}
|
||||
|
||||
export default { getItem, getAllKeys }
|
97
src/utils/slices/instancesSlice.ts
Normal file
97
src/utils/slices/instancesSlice.ts
Normal file
@ -0,0 +1,97 @@
|
||||
import { createAsyncThunk, createSlice } from '@reduxjs/toolkit'
|
||||
|
||||
import { RootState } from 'src/store'
|
||||
import client from 'src/api/client'
|
||||
|
||||
export type InstancesState = {
|
||||
local: {
|
||||
url: string | undefined
|
||||
token: string | undefined
|
||||
account: {
|
||||
id: Mastodon.Account['id'] | undefined
|
||||
preferences: Mastodon.Preferences
|
||||
}
|
||||
}
|
||||
remote: {
|
||||
url: string
|
||||
}
|
||||
}
|
||||
|
||||
export const updateLocal = createAsyncThunk(
|
||||
'instances/updateLocal',
|
||||
async ({
|
||||
url,
|
||||
token
|
||||
}: {
|
||||
url: InstancesState['local']['url']
|
||||
token: InstancesState['local']['token']
|
||||
}) => {
|
||||
const {
|
||||
body: { id }
|
||||
} = await client({
|
||||
method: 'get',
|
||||
instance: 'remote',
|
||||
instanceUrl: url,
|
||||
endpoint: `accounts/verify_credentials`,
|
||||
headers: { Authorization: `Bearer ${token}` }
|
||||
})
|
||||
|
||||
const { body: preferences } = await client({
|
||||
method: 'get',
|
||||
instance: 'remote',
|
||||
instanceUrl: url,
|
||||
endpoint: `preferences`,
|
||||
headers: { Authorization: `Bearer ${token}` }
|
||||
})
|
||||
|
||||
return {
|
||||
url,
|
||||
token,
|
||||
account: {
|
||||
id,
|
||||
preferences
|
||||
}
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
const instancesSlice = createSlice({
|
||||
name: 'instances',
|
||||
initialState: {
|
||||
local: {
|
||||
url: undefined,
|
||||
token: undefined,
|
||||
account: {
|
||||
id: undefined,
|
||||
preferences: {
|
||||
'posting:default:visibility': undefined,
|
||||
'posting:default:sensitive': undefined,
|
||||
'posting:default:language': undefined,
|
||||
'reading:expand:media': undefined,
|
||||
'reading:expand:spoilers': undefined
|
||||
}
|
||||
}
|
||||
},
|
||||
remote: {
|
||||
url: 'mastodon.social'
|
||||
}
|
||||
} as InstancesState,
|
||||
reducers: {},
|
||||
extraReducers: builder => {
|
||||
builder.addCase(updateLocal.fulfilled, (state, action) => {
|
||||
state.local = action.payload
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
export const getLocalUrl = (state: RootState) => state.instances.local.url
|
||||
export const getLocalAccountId = (state: RootState) =>
|
||||
state.instances.local.account.id
|
||||
export const getLocalAccountPreferences = (state: RootState) =>
|
||||
state.instances.local.account.preferences
|
||||
|
||||
// export const {
|
||||
// updateLocalInstance,
|
||||
// updateLocalAccount
|
||||
// } = instancesSlice.actions
|
||||
export default instancesSlice.reducer
|
Reference in New Issue
Block a user