Refactoring, disabled ability to take same nicks twice.

This commit is contained in:
Marat Fayzullin 2023-11-21 16:26:31 -05:00
parent 75eae8ed0e
commit 0c2854252c
9 changed files with 73 additions and 69 deletions

View File

@ -314,7 +314,7 @@ input[type=range]:disabled {
width: 100%;
}
#openwebrx-messages .nickname {
#openwebrx-messages .chatname {
color: #bbb;
font-weight: bold;
}

View File

@ -136,13 +136,3 @@ body.has-theme #openwebrx-digimode-content-container .gradient {
box-shadow: 0px 10px 10px inset var(--theme-color2);
background: initial;
}
/* these need more work */
body.has-theme input[type=range] {
/* --track-background: #001727; */
}
body.has-theme input[type=range]::-webkit-slider-runnable-track,
body.has-theme input[type=range]:focus::-webkit-slider-runnable-track {
/* background: #001727; */
}

View File

@ -178,9 +178,9 @@
</div>
<div id="openwebrx-chat-inputs">
<div class="input-group" style="display: flex; justify-content: space-between;">
<input id="chatNickname" style="width:15%; margin-right: 0.2rem" placeholder="Callsign" />
<input id="chatMessage" style="width:99%; margin-right: 0.2rem" onkeydown="UI.chatKeyPress(event);" />
<div class="openwebrx-button" onclick="UI.chatSend();">Send</div>
<input id="openwebrx-chat-name" style="width:15%; margin-right: 0.2rem" placeholder="Name" />
<input id="openwebrx-chat-message" style="width:99%; margin-right: 0.2rem" placeholder="Message" onkeydown="Chat.keyPress(event);" />
<div class="openwebrx-button" onclick="Chat.send();">Send</div>
</div>
</div>
</div>

57
htdocs/lib/Chat.js Normal file
View File

@ -0,0 +1,57 @@
//
// Built-in Chat
//
function Chat() {}
// We start with these values
Chat.nickname = '';
// Load chat settings from local storage.
Chat.loadSettings = function() {
this.setNickname(LS.has('chatname')? LS.loadStr('chatname') : '');
};
// Set chat nickname.
Chat.setNickname = function(nickname) {
if (this.nickname !== nickname) {
this.nickname = nickname;
LS.save('chatname', nickname);
$('#openwebrx-chat-name').val(nickname);
}
};
Chat.recvMessage = function(nickname, text, color = 'white') {
// Show chat panel
toggle_panel('openwebrx-panel-log', true);
divlog(
'[<span class="chatname" style="color:' + color + ';">'
+ Utils.htmlEscape(nickname) + '</span>]:&nbsp;'
+ '<span class="chatmessage">' + Utils.htmlEscape(text)
+ '</span>'
);
};
Chat.sendMessage = function(text, nickname = '') {
ws.send(JSON.stringify({
'type': 'sendmessage', 'name': nickname, 'text': text
}));
};
// Collect nick and message from controls and send message.
Chat.send = function() {
this.setNickname($('#openwebrx-chat-name').val().trim());
var msg = $('#openwebrx-chat-message').val().trim();
if (msg.length > 0) this.sendMessage(msg, this.nickname);
$('#openwebrx-chat-message').val('');
};
// Attach events to chat controls.
Chat.keyPress = function(event) {
if (event.key === 'Enter') {
event.preventDefault();
this.send();
}
};

View File

@ -14,7 +14,6 @@ UI.nrThreshold = 0;
UI.nrEnabled = false;
UI.wheelSwap = false;
UI.spectrum = false;
UI.nickname = '';
// Foldable UI sections and their initial states
UI.sections = {
@ -28,7 +27,6 @@ UI.sections = {
UI.loadSettings = function() {
this.setTheme(LS.has('ui_theme')? LS.loadStr('ui_theme') : 'default');
this.setOpacity(LS.has('ui_opacity')? LS.loadInt('ui_opacity') : 100);
this.setNickname(LS.has('nickname')? LS.loadStr('nickname') : '');
this.toggleFrame(LS.has('ui_frame')? LS.loadBool('ui_frame') : false);
this.toggleWheelSwap(LS.has('ui_wheel')? LS.loadBool('ui_wheel') : false);
this.toggleSpectrum(LS.has('ui_spectrum')? LS.loadBool('ui_spectrum') : false);
@ -239,51 +237,3 @@ UI.setTheme = function(theme) {
$('body').addClass('has-theme');
}
};
//
// Chat
//
// Set chat nickname.
UI.setNickname = function(nickname) {
if (this.nickname !== nickname) {
this.nickname = nickname;
LS.save('nickname', nickname);
$('#chatNickname').val(nickname);
}
};
UI.recvChatMessage = function(nickname, text, color = 'white') {
// Show chat panel
toggle_panel('openwebrx-panel-log', true);
divlog(
'[<span class="nickname" style="color:' + color + ';">'
+ Utils.htmlEscape(nickname) + '</span>]:&nbsp;'
+ '<span class="chatmessage">' + Utils.htmlEscape(text)
+ '</span>'
);
};
UI.sendChatMessage = function(text, nickname = '') {
ws.send(JSON.stringify({
'type': 'sendmessage', 'name': nickname, 'text': text
}));
};
// Collect nick and message from controls and send message.
UI.chatSend = function() {
this.setNickname($('#chatNickname').val().trim());
var msg = $('#chatMessage').val().trim();
if (msg.length > 0) this.sendChatMessage(msg, this.nickname);
$('#chatMessage').val('');
};
// Attach events to chat controls.
UI.chatKeyPress = function(event) {
if (event.key === 'Enter') {
event.preventDefault();
this.chatSend();
}
};

View File

@ -1168,7 +1168,7 @@ function on_ws_recv(evt) {
divlog(json['value'], true);
break;
case 'chat_message':
UI.recvChatMessage(json['sender'], json['text'], json['color']);
Chat.recvMessage(json['name'], json['text'], json['color']);
break;
case 'backoff':
divlog("Server is currently busy: " + json['reason'], true);
@ -1326,6 +1326,7 @@ function onAudioStart(apiType){
// Load user interface settings from local storage
UI.loadSettings();
Chat.loadSettings();
}
var reconnect_timeout = false;

View File

@ -71,9 +71,15 @@ class ClientRegistry(object):
# Broadcast chat message to all connected clients.
def broadcastChatMessage(self, client, text: str, name: str = None):
# Names can only include alphanumerics
if name is not None:
# Names can only include alphanumerics
name = re.sub("\W+", "", name)
# Cannot have duplicate names
if client not in self.chat or name != self.chat[client]["name"]:
for c in self.chat:
if name == self.chat[c]["name"]:
name = None
break
# If we have seen this client chatting before...
if client in self.chat:
# Rename existing client as needed, keep color

View File

@ -491,11 +491,10 @@ class OpenWebRxReceiverClient(OpenWebRxClient, SdrSourceEventClient):
def write_backoff_message(self, reason):
self.send({"type": "backoff", "reason": reason})
def write_chat_message(self, sender, text, color = "white"):
logger.debug("Sending {0}".format({"type": "chat_message", "sender": sender, "text": text}))
def write_chat_message(self, name, text, color = "white"):
self.send({
"type": "chat_message",
"sender": sender,
"name": name,
"text": text,
"color": color
})

View File

@ -141,6 +141,7 @@ class CompiledAssetsController(GzipMixin, ModificationAwareController):
"lib/Scanner.js",
"lib/Utils.js",
"lib/Clock.js",
"lib/Chat.js",
"lib/UI.js",
],
"map-google.js": [