Use JSON for sending messages

This commit is contained in:
Mark Veidemanis 2022-08-14 16:45:48 +01:00
parent 555bcb4c09
commit e7b7695efd
Signed by: m
GPG Key ID: 5ACFCEED46C0904F
4 changed files with 17 additions and 11 deletions

View File

@ -253,12 +253,12 @@ urlpatterns = [
name="threshold_irc_network_list",
),
path(
"manage/threshold/irc/msg/<str:net>/<int:num>/<str:channel>/",
"manage/threshold/irc/msg/<str:net>/<int:num>/",
ThresholdIRCSendMessage.as_view(),
name="threshold_irc_msg",
),
path(
"manage/threshold/irc/msg/<str:net>/<int:num>/<str:channel>/<str:nick>/",
"manage/threshold/irc/msg/<str:net>/<int:num>/<str:nick>/",
ThresholdIRCSendMessage.as_view(),
name="threshold_irc_msg",
),

View File

@ -1,5 +1,3 @@
import urllib.parse
from django.conf import settings
from core.lib.opensearch import client, run_main_query
@ -194,9 +192,8 @@ def get_irc_alerts(user):
def send_irc_message(net, num, channel, msg, nick=None):
channel = urllib.parse.quote(channel, safe="")
url = f"irc/msg/{net}/{num}/{channel}"
payload = {"msg": msg}
url = f"irc/msg/{net}/{num}"
payload = {"msg": msg, "channel": channel}
if nick:
payload["nick"] = nick
messaged = threshold_request(url, payload, method="PUT")

View File

@ -80,7 +80,8 @@
id="search"
class="button is-info is-fullwidth"
hx-headers='{"X-CSRFToken": "{{ csrf_token }}"}'
hx-put="{% url 'threshold_irc_msg' net num channel nick %}"
hx-put="{% url 'threshold_irc_msg' net num %}"
hx-vals='{"channel": "{{ channel }}", "nick": "{{ nick }}"}'
hx-trigger="click"
hx-target="#context-input"
hx-swap="outerHTML">

View File

@ -657,7 +657,7 @@ class ThresholdIRCSendMessage(SuperUserRequiredMixin, APIView):
parser_classes = [FormParser]
template_name = "partials/context-input.html"
def put(self, request, net, num, channel, nick=None):
def put(self, request, net, num, nick=None):
"""
Send a message
"""
@ -669,13 +669,21 @@ class ThresholdIRCSendMessage(SuperUserRequiredMixin, APIView):
self.template_name,
{"message": message, "class": message_class},
)
if "channel" not in request.data:
message = "No channel"
message_class = "danger"
return render(
request,
self.template_name,
{"message": message, "class": message_class},
)
if nick:
messaged = threshold.send_irc_message(
net, num, channel, request.data["msg"], nick=nick
net, num, request.data["channel"], request.data["msg"], nick=nick
)
else:
messaged = threshold.send_irc_message(
net, num, channel, request.data["msg"]
net, num, request.data["channel"], request.data["msg"]
)
if not messaged:
message = "Failed to send message"