Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/nextcloud/notifications.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'src/services/notificationsService.js')
-rw-r--r--src/services/notificationsService.js78
1 files changed, 78 insertions, 0 deletions
diff --git a/src/services/notificationsService.js b/src/services/notificationsService.js
new file mode 100644
index 0000000..51778ba
--- /dev/null
+++ b/src/services/notificationsService.js
@@ -0,0 +1,78 @@
+/**
+ * @copyright Copyright (c) 2020 Joas Schilling <coding@schilljs.com>
+ *
+ * @license GNU AGPL version 3 or any later version
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+import moment from '@nextcloud/moment'
+import axios from '@nextcloud/axios'
+import { generateOcsUrl } from '@nextcloud/router'
+import BrowserStorage from './BrowserStorage'
+
+const getNotificationsData = async(tabId, lastETag, forceRefresh) => {
+ const lastUpdated = parseInt(BrowserStorage.getItem('lastUpdated'), 10)
+ const lastTab = BrowserStorage.getItem('tabId')
+ const now = moment().format('X')
+
+ if (forceRefresh
+ // Allow the same tab to refresh with less than the timeout,
+ || (lastTab === tabId && lastUpdated + 25 < now)
+ // and at the same time give it some more time against other tabs.
+ || lastUpdated + 35 < now) {
+ BrowserStorage.setItem('tabId', tabId)
+ BrowserStorage.setItem('lastUpdated', now)
+ // console.debug('Refetching data in ' + tabId + ' (prev: ' + lastTab + ' age: ' + (now - lastUpdated) + ')')
+ await refreshData(lastETag)
+ // } else {
+ // console.debug('Reusing data in ' + tabId + ' (prev: ' + lastTab + ' age: ' + (now - lastUpdated) + ')')
+ }
+
+ return {
+ status: parseInt(BrowserStorage.getItem('status'), 10),
+ headers: JSON.parse(BrowserStorage.getItem('headers') || '[]'),
+ data: JSON.parse(BrowserStorage.getItem('data') || '[]'),
+ tabId: BrowserStorage.getItem('tabId'),
+ lastUpdated: parseInt(BrowserStorage.getItem('lastUpdated'), 10),
+ }
+}
+
+const refreshData = async(lastETag) => {
+ let requestConfig = {}
+ if (lastETag) {
+ requestConfig = {
+ headers: {
+ 'If-None-Match': lastETag,
+ },
+ }
+ }
+
+ try {
+ const response = await axios.get(generateOcsUrl('apps/notifications/api/v2', 2) + 'notifications', requestConfig)
+
+ BrowserStorage.setItem('status', '' + response.status)
+ if (response.status !== 204) {
+ BrowserStorage.setItem('headers', JSON.stringify(response.headers))
+ BrowserStorage.setItem('data', JSON.stringify(response.data.ocs.data))
+ }
+ } catch (error) {
+ BrowserStorage.setItem('status', '' + error.response.status)
+ }
+}
+
+export {
+ getNotificationsData,
+}