Show unread status reload link

This commit is contained in:
Mouse Reeve 2021-03-23 12:52:38 -07:00
parent 28651bd804
commit b8cd1d5bce
7 changed files with 34 additions and 21 deletions

View file

@ -70,6 +70,10 @@ class ActivityStream(ABC):
.order_by("-published_date")
)
def get_unread_count(self, user):
""" get the unread status count for this user's feed """
return int(r.get(self.unread_id(user)))
def populate_stream(self, user):
""" go from zero to a timeline """
pipeline = r.pipeline()

View file

@ -61,9 +61,9 @@ function polling(el, delay) {
function updateCountElement(el, data) {
const currentCount = el.innerText;
const count = data[el.getAttribute('data-poll')];
const count = data.count;
if (count != currentCount) {
addRemoveClass(el, 'hidden', count < 1);
addRemoveClass(el.closest('[data-poll-wrapper]'), 'hidden', count < 1);
el.innerText = count;
}
}

View file

@ -26,6 +26,10 @@
</ul>
</div>
<a href="{{ request.path }}" class="hidden notification is-primary is-block" data-poll-wrapper>
{% blocktrans %}load <span data-poll="stream/{{ tab }}">0</span> unread status(es){% endblocktrans %}
</a>
{# announcements and system messages #}
{% if request.user.show_goal and not goal and tab == 'home' %}
{% now 'Y' as year %}

View file

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

View file

@ -37,7 +37,8 @@ urlpatterns = [
re_path(r"^api/v1/instance/?$", views.instance_info),
re_path(r"^api/v1/instance/peers/?$", views.peers),
# polling updates
re_path("^api/updates/notifications/?$", views.Updates.as_view()),
re_path("^api/updates/notifications/?$", views.get_notification_count),
re_path("^api/updates/stream/(?P<stream>[a-z]+)/?$", views.get_unread_status_count),
# authentication
re_path(r"^login/?$", views.Login.as_view()),
re_path(r"^register/?$", views.Register.as_view()),

View file

@ -33,6 +33,6 @@ from .shelf import shelve, unshelve
from .site import Site
from .status import CreateStatus, DeleteStatus
from .tag import Tag, AddTag, RemoveTag
from .updates import Updates
from .updates import get_notification_count, get_unread_status_count
from .user import User, EditUser, Followers, Following
from .wellknown import webfinger, nodeinfo_pointer, nodeinfo, instance_info, peers

View file

@ -1,20 +1,24 @@
""" 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 """
from bookwyrm import activitystreams
def get(self, request):
""" any notifications waiting? """
return JsonResponse(
{
"notifications": request.user.notification_set.filter(
read=False
).count(),
}
)
@login_required
def get_notification_count(request):
""" any notifications waiting? """
return JsonResponse({
"count": request.user.notification_set.filter(
read=False
).count(),
})
@login_required
def get_unread_status_count(request, stream):
""" any unread statuses for this feed? """
stream = activitystreams.streams.get(stream)
if not stream:
return JsonResponse({})
return JsonResponse({
"count": stream.get_unread_count(request.user)
})