Added ability for admin to broadcast messages.

This commit is contained in:
Marat Fayzullin 2023-11-25 20:48:22 -05:00
parent 0d22dbe9a3
commit efcddf8765
5 changed files with 37 additions and 0 deletions

View File

@ -23,4 +23,18 @@ $.fn.clientList = function() {
return false;
});
});
$('#broadcast-send').on('click', function(e) {
var text = $('#broadcast-text').val();
if (text.length > 0) {
$.ajax("/broadcast", {
data: JSON.stringify({ text: text }),
contentType: 'application/json',
method: 'POST'
}).done(function() {
$('#broadcast-text').val('');
});
}
return false;
});
}

View File

@ -40,6 +40,11 @@ ${header}
<div class="row">
<h1 class="col-12">Clients</h1>
</div>
<div class="row">
Broadcast message to all users
<input class="form-control form-control-sm" id="broadcast-text" placeholder="Message to broadcast" type="text" />
<button type="button" class="btn btn-sm btn-danger" style="align:right;" id="broadcast-send">send</button>
</div>
<div class="row client-list">
${clients}
</div>

View File

@ -103,6 +103,11 @@ class ClientRegistry(object):
for c in self.clients:
c.write_chat_message(name, text, color)
# Broadcast administrative message to all connected clients.
def broadcastAdminMessage(self, text: str):
for c in self.clients:
c.write_log_message(text)
# Get client IP address from the handler.
def getIp(self, handler):
ip = handler.client_address[0]

View File

@ -97,3 +97,15 @@ class ClientController(AuthorizationMixin, WebpageController):
except Exception as e:
logger.debug("unban(): " + str(e))
self.send_response("{}", content_type="application/json", code=400)
def broadcast(self):
try:
data = json.loads(self.get_body().decode("utf-8"))
text = data["text"].strip() if "text" in data else ""
if len(text) > 0:
logger.info("Broadcasting '{0}' to all clients".format(text))
ClientRegistry.getSharedInstance().broadcastAdminMessage(text)
self.send_response("{}", content_type="application/json", code=200)
except Exception as e:
logger.debug("broadcast(): " + str(e))
self.send_response("{}", content_type="application/json", code=400)

View File

@ -163,6 +163,7 @@ class Router(object):
StaticRoute("/clients", ClientController),
StaticRoute("/ban", ClientController, method="POST", options={"action": "ban"}),
StaticRoute("/unban", ClientController, method="POST", options={"action": "unban"}),
StaticRoute("/broadcast", ClientController, method="POST", options={"action": "broadcast"}),
StaticRoute("/login", SessionController, options={"action": "loginAction"}),
StaticRoute("/login", SessionController, method="POST", options={"action": "processLoginAction"}),
StaticRoute("/logout", SessionController, options={"action": "logoutAction"}),