From bdd970ca681dce10dc5deb8f7bc63c5d3f08a21d Mon Sep 17 00:00:00 2001 From: Sol Fisher Romanoff Date: Sat, 5 Nov 2022 16:18:28 +0200 Subject: [PATCH] Refetch reports every 60 seconds --- src/boot/after_store.js | 1 + .../mod_modal/tabs/reports_tab/reports_tab.js | 3 --- src/modules/api.js | 14 +++++++++++++ src/modules/reports.js | 4 ---- .../backend_interactor_service.js | 5 +++++ .../reports_fetcher.service.js | 20 +++++++++++++++++++ 6 files changed, 40 insertions(+), 7 deletions(-) create mode 100644 src/services/reports_fetcher/reports_fetcher.service.js diff --git a/src/boot/after_store.js b/src/boot/after_store.js index 9f0ff4d3..403adc48 100644 --- a/src/boot/after_store.js +++ b/src/boot/after_store.js @@ -396,6 +396,7 @@ const afterStoreSetup = async ({ store, i18n }) => { // Start fetching things that don't need to block the UI store.dispatch('fetchMutes') store.dispatch('startFetchingAnnouncements') + store.dispatch('startFetchingReports') getTOS({ store }) getStickers({ store }) diff --git a/src/components/mod_modal/tabs/reports_tab/reports_tab.js b/src/components/mod_modal/tabs/reports_tab/reports_tab.js index 86ce1068..1babc7ca 100644 --- a/src/components/mod_modal/tabs/reports_tab/reports_tab.js +++ b/src/components/mod_modal/tabs/reports_tab/reports_tab.js @@ -9,9 +9,6 @@ const ReportsTab = { showClosed: false } }, - created () { - this.$store.dispatch('getReports') - }, components: { Checkbox, ReportCard diff --git a/src/modules/api.js b/src/modules/api.js index e2b3b37b..c54aa4fb 100644 --- a/src/modules/api.js +++ b/src/modules/api.js @@ -163,6 +163,7 @@ const api = { dispatch('startFetchingTimeline', { timeline: 'friends' }) dispatch('startFetchingNotifications') dispatch('startFetchingAnnouncements') + dispatch('startFetchingReports') dispatch('pushGlobalNotice', { level: 'error', messageKey: 'timeline.socket_broke', @@ -280,6 +281,19 @@ const api = { if (!fetcher) return store.commit('removeFetcher', { fetcherName: 'announcements', fetcher }) }, + + // Reports + startFetchingReports (store) { + if (store.state.fetchers['reports']) return + const fetcher = store.state.backendInteractor.startFetchingReports({ store }) + store.commit('addFetcher', { fetcherName: 'reports', fetcher }) + }, + stopFetchingReports (store) { + const fetcher = store.state.fetchers.reports + if (!fetcher) return + store.commit('removeFetcher', { fetcherName: 'reports', fetcher }) + }, + getSupportedTranslationlanguages (store) { store.state.backendInteractor.getSupportedTranslationlanguages({ store }) .then((data) => { diff --git a/src/modules/reports.js b/src/modules/reports.js index 6048d719..5130b989 100644 --- a/src/modules/reports.js +++ b/src/modules/reports.js @@ -70,10 +70,6 @@ const reports = { closeUserReportingModal ({ commit }) { commit('closeUserReportingModal') }, - getReports ({ rootState, commit }, params) { - return rootState.api.backendInteractor.getReports(params) - .then(reports => forEach(reports, report => commit('setReport', { report }))) - }, updateReportStates ({ rootState, commit }, { reports }) { commit('updateReportStates', { reports }) return rootState.api.backendInteractor.updateReportStates({ reports }) diff --git a/src/services/backend_interactor_service/backend_interactor_service.js b/src/services/backend_interactor_service/backend_interactor_service.js index 596151d8..4d6f80c2 100644 --- a/src/services/backend_interactor_service/backend_interactor_service.js +++ b/src/services/backend_interactor_service/backend_interactor_service.js @@ -5,6 +5,7 @@ import followRequestFetcher from '../../services/follow_request_fetcher/follow_r import listsFetcher from '../../services/lists_fetcher/lists_fetcher.service.js' import announcementsFetcher from '../../services/announcements_fetcher/announcements_fetcher.service.js' import configFetcher from '../config_fetcher/config_fetcher.service.js' +import reportsFetcher from '../reports_fetcher/reports_fetcher.service.js' const backendInteractorService = credentials => ({ startFetchingTimeline ({ timeline, store, userId = false, listId = false, tag }) { @@ -39,6 +40,10 @@ const backendInteractorService = credentials => ({ return announcementsFetcher.startFetching({ store, credentials }) }, + startFetchingReports ({ store, state, limit, page, pageSize }) { + return reportsFetcher.startFetching({ store, credentials, state, limit, page, pageSize }) + }, + startUserSocket ({ store }) { const serv = store.rootState.instance.server.replace('http', 'ws') const url = serv + getMastodonSocketURI({ credentials, stream: 'user' }) diff --git a/src/services/reports_fetcher/reports_fetcher.service.js b/src/services/reports_fetcher/reports_fetcher.service.js new file mode 100644 index 00000000..f0bb9dcf --- /dev/null +++ b/src/services/reports_fetcher/reports_fetcher.service.js @@ -0,0 +1,20 @@ +import apiService from '../api/api.service.js' +import { promiseInterval } from '../promise_interval/promise_interval.js' +import { forEach } from 'lodash' + +const fetchAndUpdate = ({ store, credentials, state, limit, page, pageSize }) => { + return apiService.getReports({ credentials, state, limit, page, pageSize }) + .then(reports => forEach(reports, report => store.commit('setReport', { report }))) +} + +const startFetching = ({ store, credentials, state, limit, page, pageSize }) => { + const boundFetchAndUpdate = () => fetchAndUpdate({ store, credentials, state, limit, page, pageSize }) + boundFetchAndUpdate() + return promiseInterval(boundFetchAndUpdate, 60000) +} + +const reportsFetcher = { + startFetching +} + +export default reportsFetcher