Move unread chat count to one request
This commit is contained in:
parent
a7f7aab444
commit
bddb00050a
5 changed files with 40 additions and 48 deletions
|
@ -1,6 +1,10 @@
|
|||
from cryptography.fernet import Fernet
|
||||
|
||||
from django.conf import settings
|
||||
from django.db.models import OuterRef, Count, Subquery, IntegerField
|
||||
from django.db.models.functions import Coalesce
|
||||
|
||||
from chat_box.models import Ignore, Message, UserRoom
|
||||
|
||||
secret_key = settings.CHAT_SECRET_KEY
|
||||
fernet = Fernet(secret_key)
|
||||
|
@ -18,3 +22,28 @@ def decrypt_url(message_encrypted):
|
|||
return int(creator_id), int(other_id)
|
||||
except Exception as e:
|
||||
return None, None
|
||||
|
||||
|
||||
def get_unread_boxes(profile):
|
||||
ignored_users = Ignore.get_ignored_users(profile)
|
||||
|
||||
mess = (
|
||||
Message.objects.filter(room=OuterRef("room"), time__gte=OuterRef("last_seen"))
|
||||
.exclude(author=profile)
|
||||
.exclude(author__in=ignored_users)
|
||||
.order_by()
|
||||
.values("room")
|
||||
.annotate(unread_count=Count("pk"))
|
||||
.values("unread_count")
|
||||
)
|
||||
|
||||
unread_boxes = (
|
||||
UserRoom.objects.filter(user=profile, room__isnull=False)
|
||||
.annotate(
|
||||
unread_count=Coalesce(Subquery(mess, output_field=IntegerField()), 0),
|
||||
)
|
||||
.filter(unread_count__gte=1)
|
||||
.count()
|
||||
)
|
||||
|
||||
return unread_boxes
|
||||
|
|
|
@ -502,32 +502,3 @@ def toggle_ignore(request, **kwargs):
|
|||
Ignore.toggle_ignore(request.profile, other_user)
|
||||
next_url = request.GET.get("next", "/")
|
||||
return HttpResponseRedirect(next_url)
|
||||
|
||||
|
||||
@login_required
|
||||
def get_unread_boxes(request):
|
||||
if request.method != "GET":
|
||||
return HttpResponseBadRequest()
|
||||
|
||||
ignored_users = Ignore.get_ignored_users(request.profile)
|
||||
|
||||
mess = (
|
||||
Message.objects.filter(room=OuterRef("room"), time__gte=OuterRef("last_seen"))
|
||||
.exclude(author=request.profile)
|
||||
.exclude(author__in=ignored_users)
|
||||
.order_by()
|
||||
.values("room")
|
||||
.annotate(unread_count=Count("pk"))
|
||||
.values("unread_count")
|
||||
)
|
||||
|
||||
unread_boxes = (
|
||||
UserRoom.objects.filter(user=request.profile, room__isnull=False)
|
||||
.annotate(
|
||||
unread_count=Coalesce(Subquery(mess, output_field=IntegerField()), 0),
|
||||
)
|
||||
.filter(unread_count__gte=1)
|
||||
.count()
|
||||
)
|
||||
|
||||
return JsonResponse({"unread_boxes": unread_boxes})
|
||||
|
|
|
@ -1036,11 +1036,6 @@ urlpatterns = [
|
|||
chat.toggle_ignore,
|
||||
name="toggle_ignore",
|
||||
),
|
||||
url(
|
||||
r"^get_unread_boxes$",
|
||||
chat.get_unread_boxes,
|
||||
name="get_unread_boxes",
|
||||
),
|
||||
]
|
||||
),
|
||||
),
|
||||
|
|
|
@ -16,6 +16,7 @@ from judge.models.choices import ACE_THEMES, MATH_ENGINES_CHOICES, TIMEZONE
|
|||
from judge.models.runtime import Language
|
||||
from judge.ratings import rating_class
|
||||
|
||||
|
||||
__all__ = ["Organization", "Profile", "OrganizationRequest", "Friend"]
|
||||
|
||||
|
||||
|
@ -242,6 +243,12 @@ class Profile(models.Model):
|
|||
}
|
||||
return self.notifications.filter(**query).count()
|
||||
|
||||
@cached_property
|
||||
def count_unread_chat_boxes(self):
|
||||
from chat_box.utils import get_unread_boxes
|
||||
|
||||
return get_unread_boxes(self)
|
||||
|
||||
_pp_table = [pow(settings.DMOJ_PP_STEP, i) for i in range(settings.DMOJ_PP_ENTRIES)]
|
||||
|
||||
def calculate_points(self, table=_pp_table):
|
||||
|
|
|
@ -158,20 +158,6 @@
|
|||
id: '{{ request.user.id|escapejs }}',
|
||||
name: '{{ request.user.username|escapejs }}'
|
||||
};
|
||||
$(function() {
|
||||
if ($('#chat-icon').length) {
|
||||
$.get("{{url('get_unread_boxes')}}")
|
||||
.done(function(data) {
|
||||
if (data.unread_boxes) {
|
||||
var html = `<sub class="unread_boxes">${data.unread_boxes}</sub>`;
|
||||
$('#chat-icon').append(html);
|
||||
}
|
||||
})
|
||||
.fail(function(data) {
|
||||
console.log('Fail to get unread boxes');
|
||||
});
|
||||
}
|
||||
});
|
||||
</script>
|
||||
{% else %}
|
||||
<script>window.user = {};</script>
|
||||
|
@ -230,6 +216,10 @@
|
|||
<span class="navbar-icons">
|
||||
<span title="{{_('Chat')}}">
|
||||
<a id="chat-icon" href="{{ url('chat', '') }}" class="icofont-wechat navbar-icon" aria-hidden="true">
|
||||
{% set unread_chat = request.profile.count_unread_chat_boxes %}
|
||||
{% if unread_chat %}
|
||||
<sub class="unread_boxes">{{unread_chat}}</sub>
|
||||
{% endif %}
|
||||
</a>
|
||||
</span>
|
||||
|
||||
|
|
Loading…
Reference in a new issue