Poll for notifications

This commit is contained in:
Mouse Reeve 2021-01-18 16:32:02 -08:00
parent dda4698a84
commit 394666357f
5 changed files with 45 additions and 5 deletions

View file

@ -31,8 +31,29 @@ window.onload = function() {
// hidden submit button in a form
document.querySelectorAll('.hidden-form input')
.forEach(t => t.onchange = revealForm);
// polling
document.querySelectorAll('[data-poll]')
.forEach(t => setInterval(function () { polling(t) }, 10000));
};
function polling(el) {
// poll the endpoint
fetch('/api/updates/' + el.getAttribute('data-poll'), {
method : "GET",
}).then(response => response.json())
.then(data => updateCountElement(el, data));
}
function updateCountElement(el, data) {
const count = data[el.getAttribute('data-poll')];
if (count) {
removeClass(el, 'hidden');
el.innerHTML = count;
}
}
function revealForm(e) {
var hidden = e.currentTarget.closest('.hidden-form').getElementsByClassName('hidden')[0];
if (hidden) {

View file

@ -113,9 +113,9 @@
<span class="is-sr-only">Notifications</span>
</span>
</span>
{% if request.user|notification_count %}
<span class="tag is-danger is-medium">{{ request.user | notification_count }}</span>
{% endif %}
<span class="{% if not request.user|notification_count %}hidden {% endif %}tag is-danger is-medium" data-poll="notifications">
{{ request.user | notification_count }}
</span>
</div>
</a>
</div>

View file

@ -31,14 +31,15 @@ urlpatterns = [
re_path(r'^inbox/?$', incoming.shared_inbox),
re_path(r'%s/inbox/?$' % local_user_path, incoming.inbox),
re_path(r'%s/outbox/?$' % local_user_path, views.Outbox.as_view()),
# .well-known endpoints
re_path(r'^.well-known/webfinger/?$', wellknown.webfinger),
re_path(r'^.well-known/nodeinfo/?$', wellknown.nodeinfo_pointer),
re_path(r'^nodeinfo/2\.0/?$', wellknown.nodeinfo),
re_path(r'^api/v1/instance/?$', wellknown.instance_info),
re_path(r'^api/v1/instance/peers/?$', wellknown.peers),
# polling updates
re_path('^api/updates/notifications/?$', views.Updates.as_view()),
# authentication
re_path(r'^login/?$', views.Login.as_view()),
re_path(r'^register/?$', views.Register.as_view()),

View file

@ -23,4 +23,5 @@ from .shelf import Shelf
from .shelf import user_shelves_page, create_shelf, delete_shelf
from .shelf import shelve, unshelve
from .status import Status, Replies, CreateStatus, DeleteStatus
from .updates import Updates
from .user import User, EditUser, Followers, Following

17
bookwyrm/views/updates.py Normal file
View file

@ -0,0 +1,17 @@
''' endpoints for getting updates about activity '''
from django.contrib.auth.decorators import login_required
from django.http import JsonResponse
from django.utils.decorators import method_decorator
from django.views import View
# pylint: disable= no-self-use
@method_decorator(login_required, name='dispatch')
class Updates(View):
''' so the app can poll '''
def get(self, request):
''' any notifications waiting? '''
return JsonResponse({
'notifications': request.user.notification_set.filter(
read=False
).count(),
})