From 4c015bb25d023b79acac6a4d5130f913354542e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcell=20F=C3=BCl=C3=B6p?= Date: Sun, 19 Jun 2022 13:51:40 +0000 Subject: [PATCH] :sparkles: Add limit option for NextcloudNotifications widget Limit displayed notifications either by count or by age. An integer value is interpeted as count limit, a number suffixed with 'm', 'h' or 'd' is converted to minutes, hours or days, respectively, and older notifications are not shown. --- .../Widgets/NextcloudNotifications.vue | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/components/Widgets/NextcloudNotifications.vue b/src/components/Widgets/NextcloudNotifications.vue index d68ad1b8..f8b90bb7 100644 --- a/src/components/Widgets/NextcloudNotifications.vue +++ b/src/components/Widgets/NextcloudNotifications.vue @@ -54,6 +54,23 @@ export default { notifications: [], }; }, + computed: { + /* Parse the limit user option to either an integer or to an integer + 'm', 'h' or 'd' */ + limit() { + const lim = this.options.limit; + const defaultLimit = [0, false]; + if (typeof lim === 'string') { + const k = { m: 60, h: 60 * 60, d: 60 * 60 * 24 }; + const m = lim.match(/(\d+)([hmd])/); + if (m.length !== 3) return defaultLimit; + return [false, m[1] * k[m[2]] * 1000]; + } + if (typeof lim === 'number') { + return [parseInt(this.options.limit, 10) || 0, false]; + } + return defaultLimit; + }, + }, methods: { allowedStatuscodes() { return [100, 200]; @@ -72,8 +89,15 @@ export default { }, processNotifications(response) { const notifications = this.validateResponse(response); + const [limitCount, limitTime] = this.limit; this.notifications = []; notifications.forEach((notification) => { + if (limitCount && this.notifications.length === limitCount) return; // count limit + const notiDate = Date.parse(notification.datetime); + const now = new Date().getTime(); + if (limitTime && notiDate && now - notiDate > limitTime) { // time limit + return; + } this.notifications.push(notification); }); },