migrate follow requests to separate pinia store

This commit is contained in:
Henry Jameson 2026-08-31 19:05:26 +03:00
commit 8a743c648c
17 changed files with 121 additions and 99 deletions

View file

@ -0,0 +1,44 @@
import { ref } from 'vue'
import { useFollowRequestsStore } from 'src/stores/follow_requests.js'
import { useUsersStore } from 'src/stores/users.js'
import { fetchFollowRequests } from 'src/api/user.js'
import { promiseInterval } from 'src/services/promise_interval/promise_interval.js'
const followRequestFetcher = ({ credentials }) => {
const interval = ref(null)
const fetchAndUpdate = () => {
return fetchFollowRequests({ credentials })
.then((result) => {
const { data: requests } = result
useFollowRequestsStore().setFollowRequests(requests)
useUsersStore().addNewUsers(result)
})
.catch((e) => {
console.error(e)
})
}
const startFetching = () => {
if (interval.value) throw new Error('Interval already exists!')
fetchAndUpdate()
interval.value = promiseInterval(fetchAndUpdate, 10000)
}
const stopFetching = () => {
interval.value.stop()
interval.value = null
}
return {
fetchAndUpdate,
startFetching,
stopFetching,
}
}
export default followRequestFetcher

View file

@ -0,0 +1,37 @@
import { defineStore } from 'pinia'
import followRequestFetcher from 'src/stores/fetchers/follow_requests.js'
import { useOAuthStore } from 'src/stores/oauth.js'
export const useFollowRequestsStore = defineStore('followRequests', {
state: () => ({
fetcher: null,
requests: new Map(),
}),
getters: {
followRequestsCount(state) {
return state.requests.size
},
},
actions: {
startFetching() {
if (this.fetcher) throw 'Fetcher already exists!'
this.fetcher = followRequestFetcher({
credentials: useOAuthStore().token,
})
this.fetcher.startFetching()
},
stopFetching() {
if (!this.fetcher) throw "Fetcher doesn't exists!"
this.fetcher.stopFetching(), (this.fetcher = null)
},
setFollowRequests(requests) {
this.requests = new Map(requests.map((user) => [user.id, user]))
},
remove(id) {
this.requests.delete(id)
}
},
})

View file

@ -6,6 +6,7 @@ import { useAnnouncementsStore } from 'src/stores/announcements.js'
import { useBookmarkFoldersStore } from 'src/stores/bookmark_folders.js'
import { useChatsStore } from 'src/stores/chats.js'
import { useEmojiStore } from 'src/stores/emoji.js'
import { useFollowRequestsStore } from 'src/stores/follow_requests.js'
import { useInstanceStore } from 'src/stores/instance.js'
import { useInstanceCapabilitiesStore } from 'src/stores/instance_capabilities.js'
import { useInterfaceStore } from 'src/stores/interface.js'
@ -678,9 +679,10 @@ export const useUsersStore = defineStore('users', {
useListsStore().startFetching()
useBookmarkFoldersStore().startFetching()
if (user.locked) {
dispatch('startFetchingFollowRequests')
}
// if (user.locked) {
dispatch('startFetchingFollowRequests')
useFollowRequestsStore().startFetching()
// }
if (useMergedConfigStore().mergedConfig.useStreamingApi) {
useStreamingStore().initSocket(true)
@ -724,6 +726,9 @@ export const useUsersStore = defineStore('users', {
useListsStore().stopFetching()
useBookmarkFoldersStore().stopFetching()
useChatsStore().stopFetching()
// if (this.currentUser.locked) {
useFollowRequestsStore().stopFetching()
// }
store?.dispatch('stopFetchingFollowRequests')