Enable sending IRC messages from context modal
This commit is contained in:
parent
c012792c42
commit
c9a17a6fa4
|
@ -41,6 +41,7 @@ from core.views.manage.threshold.irc import (
|
|||
ThresholdIRCNetworkRelayStatus,
|
||||
ThresholdIRCNetworks,
|
||||
ThresholdIRCOverviewAlerts,
|
||||
ThresholdIRCSendMessage,
|
||||
ThresholdIRCStats,
|
||||
)
|
||||
|
||||
|
@ -214,6 +215,11 @@ urlpatterns = [
|
|||
ThresholdIRCNetworkActionsList.as_view(),
|
||||
name="threshold_irc_network_actions_list",
|
||||
),
|
||||
path(
|
||||
"manage/threshold/irc/msg/<str:net>/<int:num>/<str:channel>/",
|
||||
ThresholdIRCSendMessage.as_view(),
|
||||
name="threshold_irc_msg",
|
||||
),
|
||||
##
|
||||
path("api/chans/", ThresholdChans.as_view(), name="chans"),
|
||||
path("api/users/", ThresholdUsers.as_view(), name="users"),
|
||||
|
|
|
@ -42,4 +42,5 @@ def construct_query(index, net, channel, src, num, size):
|
|||
"fields": fields,
|
||||
"_source": False,
|
||||
}
|
||||
print("QUERY", query)
|
||||
return query
|
||||
|
|
|
@ -193,3 +193,11 @@ def get_irc_alerts(user):
|
|||
element["time"] = time
|
||||
results_parsed.append(element)
|
||||
return results_parsed
|
||||
|
||||
|
||||
def send_irc_message(net, num, channel, msg):
|
||||
channel = urllib.parse.quote(channel, safe="")
|
||||
url = f"irc/msg/{net}/{num}/{channel}"
|
||||
payload = {"msg": msg}
|
||||
messaged = threshold_request(url, payload, method="PUT")
|
||||
return messaged
|
||||
|
|
|
@ -65,31 +65,32 @@
|
|||
<div class="is-active" data-content="1">
|
||||
<h4 class="subtitle is-4">Scrollback of {{ channel }} on {{ net }}{{ num }}</h4>
|
||||
{% include 'modals/context_table.html' %}
|
||||
<div class="field has-addons">
|
||||
<div class="control is-expanded has-icons-left">
|
||||
<input
|
||||
hx-post="{% url 'search' %}"
|
||||
hx-trigger="keyup changed delay:200ms"
|
||||
hx-target="#results"
|
||||
hx-swap="innerHTML" id="query" name="query" value="{{ params.query }}" class="input" type="text" placeholder="msg: science AND nick: BillNye AND channel: #science">
|
||||
{% if user.is_superuser and src == 'irc' %}
|
||||
<form method="PUT">
|
||||
<article class="field has-addons">
|
||||
<article class="control is-expanded has-icons-left">
|
||||
<input id="context-input" name="msg" class="input" type="text" placeholder="Type your message here">
|
||||
<span class="icon is-small is-left">
|
||||
<i class="fas fa-magnifying-glass"></i>
|
||||
</span>
|
||||
</div>
|
||||
<div class="control">
|
||||
<div class="field">
|
||||
</article>
|
||||
<article class="control">
|
||||
<article class="field">
|
||||
<button
|
||||
id="search"
|
||||
class="button is-info is-fullwidth"
|
||||
hx-post="{% url 'search' %}"
|
||||
hx-headers='{"X-CSRFToken": "{{ csrf_token }}"}'
|
||||
hx-put="{% url 'threshold_irc_msg' net num channel %}"
|
||||
hx-trigger="click"
|
||||
hx-target="#results"
|
||||
hx-swap="innerHTML">
|
||||
Search
|
||||
hx-target="#context-input"
|
||||
hx-swap="outerHTML">
|
||||
Send
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</article>
|
||||
</article>
|
||||
</article>
|
||||
</form>
|
||||
{% endif %}
|
||||
|
||||
</div>
|
||||
<div data-content="2">
|
||||
|
|
|
@ -0,0 +1 @@
|
|||
<input id="context-input" name="msg" class="input is-{{ class }}" type="text" placeholder="Type your message here">
|
|
@ -0,0 +1,5 @@
|
|||
{% if message is not None %}
|
||||
<main class="notification is-{{ class }}" hx-ext="remove-me" remove-me="3s">
|
||||
{{ message }}
|
||||
</main>
|
||||
{% endif %}
|
|
@ -442,3 +442,39 @@ class ThresholdIRCNetworkActionsRelay(SuperUserRequiredMixin, APIView):
|
|||
"class": message_class,
|
||||
}
|
||||
return render(request, self.template_name, context)
|
||||
|
||||
|
||||
class ThresholdIRCSendMessage(SuperUserRequiredMixin, APIView):
|
||||
parser_classes = [FormParser]
|
||||
template_name = "partials/context-input.html"
|
||||
|
||||
def put(self, request, net, num, channel):
|
||||
"""
|
||||
Send a message
|
||||
"""
|
||||
if "msg" not in request.data:
|
||||
message = "No message to send"
|
||||
message_class = "danger"
|
||||
return render(
|
||||
request,
|
||||
self.template_name,
|
||||
{"message": message, "class": message_class},
|
||||
)
|
||||
|
||||
messaged = threshold.send_irc_message(net, num, channel, request.data["msg"])
|
||||
if not messaged:
|
||||
message = "Failed to send message"
|
||||
message_class = "danger"
|
||||
elif messaged["success"]:
|
||||
message = "Send message"
|
||||
message_class = "success"
|
||||
else:
|
||||
message = messaged["reason"]
|
||||
message_class = "danger"
|
||||
|
||||
context = {
|
||||
"net": net,
|
||||
"message": message,
|
||||
"class": message_class,
|
||||
}
|
||||
return render(request, self.template_name, context)
|
||||
|
|
Loading…
Reference in New Issue