mirror of
https://github.com/simple-login/app.git
synced 2024-09-30 05:31:30 +02:00
Merge pull request #208 from simple-login/notif-improve
Improve the notification
This commit is contained in:
commit
2d45b324e8
19
README.md
19
README.md
@ -1229,12 +1229,29 @@ Input:
|
|||||||
- page in url: the page number, starts at 0
|
- page in url: the page number, starts at 0
|
||||||
|
|
||||||
Output:
|
Output:
|
||||||
List of notification, each notification has:
|
- more: whether there's more notifications
|
||||||
|
- notifications: list of notification, each notification has:
|
||||||
- id
|
- id
|
||||||
- message: the message in html
|
- message: the message in html
|
||||||
- read: whether the user has read the notification
|
- read: whether the user has read the notification
|
||||||
- created_at: when the notification is created
|
- 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
|
#### POST /api/notifications/:notification_id
|
||||||
|
|
||||||
Mark a notification as read
|
Mark a notification as read
|
||||||
|
@ -1,5 +1,3 @@
|
|||||||
from time import sleep
|
|
||||||
|
|
||||||
from flask import g
|
from flask import g
|
||||||
from flask import jsonify
|
from flask import jsonify
|
||||||
from flask import request
|
from flask import request
|
||||||
@ -21,7 +19,9 @@ def get_notifications():
|
|||||||
Input:
|
Input:
|
||||||
- page: in url. Starts at 0
|
- page: in url. Starts at 0
|
||||||
|
|
||||||
Output: list of notifications. Each notification has the following field:
|
Output:
|
||||||
|
- more: boolean. Whether there's more notification to load
|
||||||
|
- notifications: list of notifications.
|
||||||
- id
|
- id
|
||||||
- message
|
- message
|
||||||
- read
|
- read
|
||||||
@ -36,22 +36,25 @@ def get_notifications():
|
|||||||
notifications = (
|
notifications = (
|
||||||
Notification.query.filter_by(user_id=user.id)
|
Notification.query.filter_by(user_id=user.id)
|
||||||
.order_by(Notification.read, Notification.created_at.desc())
|
.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)
|
.offset(page * PAGE_LIMIT)
|
||||||
.all()
|
.all()
|
||||||
)
|
)
|
||||||
|
|
||||||
|
have_more = len(notifications) > PAGE_LIMIT
|
||||||
|
|
||||||
return (
|
return (
|
||||||
jsonify(
|
jsonify(
|
||||||
[
|
more=have_more,
|
||||||
|
notifications=[
|
||||||
{
|
{
|
||||||
"id": notification.id,
|
"id": notification.id,
|
||||||
"message": notification.message,
|
"message": notification.message,
|
||||||
"read": notification.read,
|
"read": notification.read,
|
||||||
"created_at": notification.created_at.humanize(),
|
"created_at": notification.created_at.humanize(),
|
||||||
}
|
}
|
||||||
for notification in notifications
|
for notification in notifications[:PAGE_LIMIT]
|
||||||
]
|
],
|
||||||
),
|
),
|
||||||
200,
|
200,
|
||||||
)
|
)
|
||||||
|
@ -57,7 +57,8 @@
|
|||||||
notifications: [],
|
notifications: [],
|
||||||
page: 0,
|
page: 0,
|
||||||
loading: true,
|
loading: true,
|
||||||
canLoadMore: true
|
canLoadMore: false,
|
||||||
|
showNotification: false,
|
||||||
},
|
},
|
||||||
computed: {
|
computed: {
|
||||||
has_non_read_notification: function () {
|
has_non_read_notification: function () {
|
||||||
@ -94,8 +95,8 @@
|
|||||||
});
|
});
|
||||||
if (res.ok) {
|
if (res.ok) {
|
||||||
let json = await res.json();
|
let json = await res.json();
|
||||||
if (json.length == 0) that.canLoadMore = false;
|
that.canLoadMore = json.more;
|
||||||
that.notifications = that.notifications.concat(json);
|
that.notifications = that.notifications.concat(json.notifications);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -110,8 +111,11 @@
|
|||||||
});
|
});
|
||||||
if (res.ok) {
|
if (res.ok) {
|
||||||
let json = await res.json();
|
let json = await res.json();
|
||||||
that.notifications = json;
|
that.notifications = json.notifications;
|
||||||
that.loading = false;
|
that.loading = false;
|
||||||
|
that.canLoadMore = json.more
|
||||||
|
if (that.notifications.length > 0)
|
||||||
|
that.showNotification = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
@ -15,7 +15,8 @@
|
|||||||
</div>
|
</div>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
|
||||||
<div id="notification-app" class="dropdown d-none d-md-flex">
|
|
||||||
|
<div id="notification-app" class="dropdown d-none d-md-flex" v-if="showNotification">
|
||||||
<a class="nav-link icon" data-toggle="collapse" href="#notifications" style="height: 100%">
|
<a class="nav-link icon" data-toggle="collapse" href="#notifications" style="height: 100%">
|
||||||
<i class="fe fe-bell"></i>
|
<i class="fe fe-bell"></i>
|
||||||
<span v-if="has_non_read_notification" class="nav-unread"></span>
|
<span v-if="has_non_read_notification" class="nav-unread"></span>
|
||||||
|
@ -25,8 +25,9 @@ def test_get_notifications(flask_client):
|
|||||||
)
|
)
|
||||||
|
|
||||||
assert r.status_code == 200
|
assert r.status_code == 200
|
||||||
assert len(r.json) == 2
|
assert r.json["more"] is False
|
||||||
for n in r.json:
|
assert len(r.json["notifications"]) == 2
|
||||||
|
for n in r.json["notifications"]:
|
||||||
assert n["id"] > 0
|
assert n["id"] > 0
|
||||||
assert n["message"]
|
assert n["message"]
|
||||||
assert n["read"] is False
|
assert n["read"] is False
|
||||||
@ -37,7 +38,8 @@ def test_get_notifications(flask_client):
|
|||||||
url_for("api.get_notifications", page=1),
|
url_for("api.get_notifications", page=1),
|
||||||
headers={"Authentication": api_key.code},
|
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):
|
def test_mark_notification_as_read(flask_client):
|
||||||
|
Loading…
Reference in New Issue
Block a user