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") .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): def populate_stream(self, user):
""" go from zero to a timeline """ """ go from zero to a timeline """
pipeline = r.pipeline() pipeline = r.pipeline()

View file

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

View file

@ -26,6 +26,10 @@
</ul> </ul>
</div> </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 #} {# announcements and system messages #}
{% if request.user.show_goal and not goal and tab == 'home' %} {% if request.user.show_goal and not goal and tab == 'home' %}
{% now 'Y' as year %} {% now 'Y' as year %}

View file

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

View file

@ -37,7 +37,8 @@ urlpatterns = [
re_path(r"^api/v1/instance/?$", views.instance_info), re_path(r"^api/v1/instance/?$", views.instance_info),
re_path(r"^api/v1/instance/peers/?$", views.peers), re_path(r"^api/v1/instance/peers/?$", views.peers),
# polling updates # 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 # authentication
re_path(r"^login/?$", views.Login.as_view()), re_path(r"^login/?$", views.Login.as_view()),
re_path(r"^register/?$", views.Register.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 .site import Site
from .status import CreateStatus, DeleteStatus from .status import CreateStatus, DeleteStatus
from .tag import Tag, AddTag, RemoveTag 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 .user import User, EditUser, Followers, Following
from .wellknown import webfinger, nodeinfo_pointer, nodeinfo, instance_info, peers from .wellknown import webfinger, nodeinfo_pointer, nodeinfo, instance_info, peers

View file

@ -1,20 +1,24 @@
""" endpoints for getting updates about activity """ """ endpoints for getting updates about activity """
from django.contrib.auth.decorators import login_required from django.contrib.auth.decorators import login_required
from django.http import JsonResponse from django.http import JsonResponse
from django.utils.decorators import method_decorator
from django.views import View
# pylint: disable= no-self-use from bookwyrm import activitystreams
@method_decorator(login_required, name="dispatch")
class Updates(View):
""" so the app can poll """
def get(self, request): @login_required
def get_notification_count(request):
""" any notifications waiting? """ """ any notifications waiting? """
return JsonResponse( return JsonResponse({
{ "count": request.user.notification_set.filter(
"notifications": request.user.notification_set.filter(
read=False read=False
).count(), ).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)
})