diff options
| author | kaniini <nenolod@gmail.com> | 2018-08-24 23:04:36 +0000 |
|---|---|---|
| committer | kaniini <nenolod@gmail.com> | 2018-08-24 23:04:36 +0000 |
| commit | 673f0fca3f7898641cdc488e6e6cc9033ca51777 (patch) | |
| tree | a92128b2779b3971d15699b4410c37687446d480 /src/services/notifications_fetcher/notifications_fetcher.service.js | |
| parent | 55650ff7ea9867cdb8adf7077b36bbb8c7bfcb75 (diff) | |
| parent | fe906cc3f0c8388bcb2a33be9c72ca5365bde0a5 (diff) | |
Merge branch 'notifications' into 'develop'
Support qvitter api notifications
Closes #129
See merge request pleroma/pleroma-fe!306
Diffstat (limited to 'src/services/notifications_fetcher/notifications_fetcher.service.js')
| -rw-r--r-- | src/services/notifications_fetcher/notifications_fetcher.service.js | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/src/services/notifications_fetcher/notifications_fetcher.service.js b/src/services/notifications_fetcher/notifications_fetcher.service.js new file mode 100644 index 00000000..1480cded --- /dev/null +++ b/src/services/notifications_fetcher/notifications_fetcher.service.js @@ -0,0 +1,46 @@ +import apiService from '../api/api.service.js' + +const update = ({store, notifications, older}) => { + store.dispatch('setNotificationsError', { value: false }) + + store.dispatch('addNewNotifications', { notifications, older }) +} + +const fetchAndUpdate = ({store, credentials, older = false}) => { + const args = { credentials } + const rootState = store.rootState || store.state + const timelineData = rootState.statuses.notifications + + if (older) { + if (timelineData.minId !== Number.POSITIVE_INFINITY) { + args['until'] = timelineData.minId + } + } else { + args['since'] = timelineData.maxId + } + + args['timeline'] = 'notifications' + + return apiService.fetchTimeline(args) + .then((notifications) => { + update({store, notifications, older}) + }, () => store.dispatch('setNotificationsError', { value: true })) + .catch(() => store.dispatch('setNotificationsError', { value: true })) +} + +const startFetching = ({credentials, store}) => { + fetchAndUpdate({ credentials, store }) + const boundFetchAndUpdate = () => fetchAndUpdate({ credentials, store }) + // Initially there's set flag to silence all desktop notifications so + // that there won't spam of them when user just opened up the FE we + // reset that flag after a while to show new notifications once again. + setTimeout(() => store.dispatch('setNotificationsSilence', false), 10000) + return setInterval(boundFetchAndUpdate, 10000) +} + +const notificationsFetcher = { + fetchAndUpdate, + startFetching +} + +export default notificationsFetcher |
