Enable sending IRC messages from context modal

This commit is contained in:
Mark Veidemanis 2022-08-03 07:20:30 +01:00
parent c012792c42
commit c9a17a6fa4
Signed by: m
GPG Key ID: 5ACFCEED46C0904F
8 changed files with 84 additions and 26 deletions

View File

@ -41,6 +41,7 @@ from core.views.manage.threshold.irc import (
ThresholdIRCNetworkRelayStatus, ThresholdIRCNetworkRelayStatus,
ThresholdIRCNetworks, ThresholdIRCNetworks,
ThresholdIRCOverviewAlerts, ThresholdIRCOverviewAlerts,
ThresholdIRCSendMessage,
ThresholdIRCStats, ThresholdIRCStats,
) )
@ -214,6 +215,11 @@ urlpatterns = [
ThresholdIRCNetworkActionsList.as_view(), ThresholdIRCNetworkActionsList.as_view(),
name="threshold_irc_network_actions_list", 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/chans/", ThresholdChans.as_view(), name="chans"),
path("api/users/", ThresholdUsers.as_view(), name="users"), path("api/users/", ThresholdUsers.as_view(), name="users"),

View File

@ -42,4 +42,5 @@ def construct_query(index, net, channel, src, num, size):
"fields": fields, "fields": fields,
"_source": False, "_source": False,
} }
print("QUERY", query)
return query return query

View File

@ -193,3 +193,11 @@ def get_irc_alerts(user):
element["time"] = time element["time"] = time
results_parsed.append(element) results_parsed.append(element)
return results_parsed 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

View File

@ -65,31 +65,32 @@
<div class="is-active" data-content="1"> <div class="is-active" data-content="1">
<h4 class="subtitle is-4">Scrollback of {{ channel }} on {{ net }}{{ num }}</h4> <h4 class="subtitle is-4">Scrollback of {{ channel }} on {{ net }}{{ num }}</h4>
{% include 'modals/context_table.html' %} {% include 'modals/context_table.html' %}
<div class="field has-addons"> {% if user.is_superuser and src == 'irc' %}
<div class="control is-expanded has-icons-left"> <form method="PUT">
<input <article class="field has-addons">
hx-post="{% url 'search' %}" <article class="control is-expanded has-icons-left">
hx-trigger="keyup changed delay:200ms" <input id="context-input" name="msg" class="input" type="text" placeholder="Type your message here">
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">
<span class="icon is-small is-left"> <span class="icon is-small is-left">
<i class="fas fa-magnifying-glass"></i> <i class="fas fa-magnifying-glass"></i>
</span> </span>
</div> </article>
<div class="control"> <article class="control">
<div class="field"> <article class="field">
<button <button
id="search" id="search"
class="button is-info is-fullwidth" 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-trigger="click"
hx-target="#results" hx-target="#context-input"
hx-swap="innerHTML"> hx-swap="outerHTML">
Search Send
</button> </button>
</div> </article>
</div> </article>
</div> </article>
</form>
{% endif %}
</div> </div>
<div data-content="2"> <div data-content="2">

View File

@ -0,0 +1 @@
<input id="context-input" name="msg" class="input is-{{ class }}" type="text" placeholder="Type your message here">

View File

@ -0,0 +1,5 @@
{% if message is not None %}
<main class="notification is-{{ class }}" hx-ext="remove-me" remove-me="3s">
{{ message }}
</main>
{% endif %}

View File

@ -442,3 +442,39 @@ class ThresholdIRCNetworkActionsRelay(SuperUserRequiredMixin, APIView):
"class": message_class, "class": message_class,
} }
return render(request, self.template_name, context) 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)