From 1e03f26cfa518eb59e4eb2a8dce659596b9490e9 Mon Sep 17 00:00:00 2001 From: Son NK <> Date: Sat, 23 May 2020 22:51:00 +0200 Subject: [PATCH 1/2] Return whether there's more notification in GET /api/notifications --- README.md | 27 ++++++++++++++++++++++----- app/api/views/notification.py | 25 ++++++++++++++----------- tests/api/test_notification.py | 8 +++++--- 3 files changed, 41 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index a9cc3f78..6d03926d 100644 --- a/README.md +++ b/README.md @@ -1229,11 +1229,28 @@ Input: - page in url: the page number, starts at 0 Output: -List of notification, each notification has: -- id -- message: the message in html -- read: whether the user has read the notification -- created_at: when the notification is created +- more: whether there's more notifications +- notifications: list of notification, each notification has: + - id + - message: the message in html + - read: whether the user has read the notification + - created_at: when the notification is created + +For example + +```json +{ + "more": false, + "notifications": [ + { + "created_at": "2 minutes ago", + "id": 1, + "message": "Hey!", + "read": false + } + ] +} +``` #### POST /api/notifications/:notification_id diff --git a/app/api/views/notification.py b/app/api/views/notification.py index dc386b89..c56847e8 100644 --- a/app/api/views/notification.py +++ b/app/api/views/notification.py @@ -1,5 +1,3 @@ -from time import sleep - from flask import g from flask import jsonify from flask import request @@ -21,11 +19,13 @@ def get_notifications(): Input: - page: in url. Starts at 0 - Output: list of notifications. Each notification has the following field: - - id - - message - - read - - created_at + Output: + - more: boolean. Whether there's more notification to load + - notifications: list of notifications. + - id + - message + - read + - created_at """ user = g.user try: @@ -36,22 +36,25 @@ def get_notifications(): notifications = ( Notification.query.filter_by(user_id=user.id) .order_by(Notification.read, Notification.created_at.desc()) - .limit(PAGE_LIMIT) + .limit(PAGE_LIMIT + 1) # load a record more to know whether there's more .offset(page * PAGE_LIMIT) .all() ) + have_more = len(notifications) > PAGE_LIMIT + return ( jsonify( - [ + more=have_more, + notifications=[ { "id": notification.id, "message": notification.message, "read": notification.read, "created_at": notification.created_at.humanize(), } - for notification in notifications - ] + for notification in notifications[:PAGE_LIMIT] + ], ), 200, ) diff --git a/tests/api/test_notification.py b/tests/api/test_notification.py index 00a27167..d7b81672 100644 --- a/tests/api/test_notification.py +++ b/tests/api/test_notification.py @@ -25,8 +25,9 @@ def test_get_notifications(flask_client): ) assert r.status_code == 200 - assert len(r.json) == 2 - for n in r.json: + assert r.json["more"] is False + assert len(r.json["notifications"]) == 2 + for n in r.json["notifications"]: assert n["id"] > 0 assert n["message"] assert n["read"] is False @@ -37,7 +38,8 @@ def test_get_notifications(flask_client): url_for("api.get_notifications", page=1), headers={"Authentication": api_key.code}, ) - assert len(r.json) == 0 + assert r.json["more"] is False + assert len(r.json["notifications"]) == 0 def test_mark_notification_as_read(flask_client): From 3c6d137bf12d7a997921246954d2a20418048e46 Mon Sep 17 00:00:00 2001 From: Son NK <> Date: Sat, 23 May 2020 22:52:35 +0200 Subject: [PATCH 2/2] =?UTF-8?q?Only=20show=20notification=20icon=20when=20?= =?UTF-8?q?there's=20at=20least=201=20notification.=20Only=20show=20"load?= =?UTF-8?q?=20more"=20when=20there's=20more=20=F0=9F=99=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- templates/footer.html | 12 ++++++++---- templates/header.html | 3 ++- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/templates/footer.html b/templates/footer.html index fdc987ce..ed994c53 100644 --- a/templates/footer.html +++ b/templates/footer.html @@ -57,7 +57,8 @@ notifications: [], page: 0, loading: true, - canLoadMore: true + canLoadMore: false, + showNotification: false, }, computed: { has_non_read_notification: function () { @@ -94,8 +95,8 @@ }); if (res.ok) { let json = await res.json(); - if (json.length == 0) that.canLoadMore = false; - that.notifications = that.notifications.concat(json); + that.canLoadMore = json.more; + that.notifications = that.notifications.concat(json.notifications); } } @@ -110,8 +111,11 @@ }); if (res.ok) { let json = await res.json(); - that.notifications = json; + that.notifications = json.notifications; that.loading = false; + that.canLoadMore = json.more + if (that.notifications.length > 0) + that.showNotification = true; } } }) diff --git a/templates/header.html b/templates/header.html index 84e18f32..667ab045 100644 --- a/templates/header.html +++ b/templates/header.html @@ -15,7 +15,8 @@ {% endif %} -