Skip to content

Commit

Permalink
Alert creator on ticket reply
Browse files Browse the repository at this point in the history
  • Loading branch information
hieplpvip committed Dec 12, 2024
1 parent 7802db4 commit d86acb2
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 0 deletions.
1 change: 1 addition & 0 deletions dmoj/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -680,6 +680,7 @@
EVENT_DAEMON_AMQP_EXCHANGE = 'dmoj-events'
EVENT_DAEMON_SUBMISSION_KEY = '6Sdmkx^%pk@GsifDfXcwX*Y7LRF%RGT8vmFpSxFBT$fwS7trc8raWfN#CSfQuKApx&$B#Gh2L7p%W!Ww'
EVENT_DAEMON_CONTEST_KEY = '&w7hB-.9WnY2Jj^Qm+|?o6a<!}_2Wiw+?(_Yccqq{uR;:kWQP+3R<r(ICc|4^dDeEuJE{*D;Gg@K(4K>'
EVENT_DAEMON_TICKET_KEY = '@R3DjH&egtm0HNhok6ERIMK!zlTzq2hrSGG2Se8SujCoO(2NX!DkbzcgQtm90FHDvpFM3gJ&D7acS$ta'

# Internationalization
# https://docs.djangoproject.com/en/3.2/topics/i18n/
Expand Down
11 changes: 11 additions & 0 deletions judge/models/profile.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import base64
import hashlib
import hmac
import json
import secrets
Expand Down Expand Up @@ -26,6 +27,7 @@
from judge.ratings import rating_class
from judge.utils.float_compare import float_compare_equal
from judge.utils.two_factor import webauthn_decode
from judge.utils.unicode import utf8bytes

__all__ = ['Organization', 'OrganizationMonthlyUsage', 'Profile', 'OrganizationRequest', 'WebAuthnCredential']

Expand Down Expand Up @@ -232,6 +234,15 @@ class Profile(models.Model):
# For ICPC only
group = models.TextField(verbose_name=_('uni group'), null=True, blank=True)

@classmethod
def get_ticket_secret(cls, profile_id):
return (hmac.new(utf8bytes(settings.EVENT_DAEMON_TICKET_KEY), b'%d' % profile_id, hashlib.sha512)
.hexdigest()[:16] + '%08x' % profile_id)

@cached_property
def ticket_secret(self):
return self.get_ticket_secret(self.id)

@cached_property
def organization(self):
# We do this to take advantage of prefetch_related
Expand Down
5 changes: 5 additions & 0 deletions judge/views/ticket.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,11 @@ def form_valid(self, form):
event.post('ticket-%d' % self.object.id, {
'type': 'ticket-message', 'message': message.id,
})
if self.request.profile != self.object.user:
event.post(f'tickets_{self.object.user.ticket_secret}', {
'title': self.object.title,
'body': message.body,
})
on_new_ticket_message.delay(message.pk, message.ticket.pk, message.body)
return HttpResponseRedirect('%s#message-%d' % (reverse('ticket', args=[self.object.id]), message.id))

Expand Down
8 changes: 8 additions & 0 deletions templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,14 @@
'{{ EVENT_DAEMON_POLL_LOCATION }}',
{{ EVENT_LAST_MSG }}
);
{% if request.user.is_authenticated %}
$(function () {
event_dispatcher.auto_reconnect = true;
event_dispatcher.on('tickets_{{ request.profile.ticket_secret }}', function (data) {
alert({{ _('You have a new reply on ticket ') }} + data.title + '\n\n' + data.body);
});
})
{% endif %}
</script>
{% endif %}
{% if request.in_contest %}
Expand Down

0 comments on commit d86acb2

Please sign in to comment.