allow saving display name to config

This commit is contained in:
liamcottle
2024-04-29 23:16:54 +12:00
parent f5b5970eec
commit 1a7baffe28
2 changed files with 101 additions and 4 deletions

View File

@@ -93,14 +93,22 @@
<!-- my identity -->
<div v-if="config" class="border rounded-xl bg-white shadow">
<div class="flex border-b border-gray-300 text-gray-700 p-2">
<div class="mr-2">
<div class="my-auto mr-2">
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="w-6 h-6">
<path stroke-linecap="round" stroke-linejoin="round" d="M17.982 18.725A7.488 7.488 0 0 0 12 15.75a7.488 7.488 0 0 0-5.982 2.975m11.963 0a9 9 0 1 0-11.963 0m11.963 0A8.966 8.966 0 0 1 12 21a8.966 8.966 0 0 1-5.982-2.275M15 9.75a3 3 0 1 1-6 0 3 3 0 0 1 6 0Z" />
</svg>
</div>
<div>My Identity</div>
<div class="my-auto">My Identity</div>
<div class="ml-auto">
<button @click="saveDisplayName" type="button" class="my-auto inline-flex items-center gap-x-1 rounded-md bg-gray-500 px-2 py-1 text-sm font-semibold text-white shadow-sm hover:bg-gray-400 focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-gray-500">
Save
</button>
</div>
</div>
<div class="divide-y text-gray-900">
<div class="p-1">
<input v-model="displayName" type="text" class="bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block w-full p-2.5">
</div>
<div class="p-1">
<div>Identity Hash</div>
<div class="text-sm text-gray-700">{{ config.identity_hash }}</div>
@@ -246,6 +254,7 @@
isSendingMessage: false,
autoScrollOnNewMessage: true,
displayName: "Anonymous Peer",
config: null,
peers: {},
@@ -282,6 +291,7 @@
switch(json.type){
case 'config': {
this.config = json.config;
this.displayName = json.config.display_name;
break;
}
case 'announce': {
@@ -396,6 +406,28 @@
this.scrollMessagesToBottom();
},
async updateConfig(config) {
// do nothing if not connected to websocket
if(!this.isWebsocketConnected){
return;
}
try {
this.ws.send(JSON.stringify({
"type": "config.set",
"config": config,
}));
} catch(e) {
console.error(e);
}
},
async saveDisplayName() {
await this.updateConfig({
"display_name": this.displayName,
});
},
addNewLine: function() {
this.newMessageText += "\n";
},

69
web.py
View File

@@ -17,6 +17,13 @@ class ReticulumWebChat:
def __init__(self, identity: RNS.Identity):
# default values before loading config
self.display_name = "Anonymous Peer"
# load config
self.config_file = "storage/config.json"
self.load_config()
# init reticulum
self.reticulum = RNS.Reticulum(None)
self.identity = identity
@@ -25,7 +32,7 @@ class ReticulumWebChat:
self.message_router = LXMF.LXMRouter(identity=self.identity, storagepath="storage/lxmf")
# register lxmf identity
self.local_lxmf_destination = self.message_router.register_delivery_identity(self.identity, display_name="ReticulumWebChat")
self.local_lxmf_destination = self.message_router.register_delivery_identity(self.identity, display_name=self.display_name)
# set a callback for when an lxmf message is received
self.message_router.register_delivery_callback(self.on_lxmf_delivery)
@@ -36,6 +43,46 @@ class ReticulumWebChat:
# remember websocket clients
self.websocket_clients = []
def load_config(self):
# default config
config = {
}
# attempt to load config and override default values
try:
with open(self.config_file, 'r') as f:
custom_config = json.load(f)
config |= custom_config
# config is broken, fallback to defaults
except:
print("failed to load config, defaults will be used")
# update display name from config
if "display_name" in config:
self.display_name = config["display_name"]
# return loaded config
return config
def save_config(self):
# build config
config = {
"display_name": self.display_name,
}
# attempt to save config
try:
with open(self.config_file, 'w') as f:
json.dump(config, f, indent=4)
# config is broken, fallback to defaults
except:
print("failed to save config")
async def run(self, host, port):
# start websocket server
@@ -103,8 +150,25 @@ class ReticulumWebChat:
# get type from client data
_type = data["type"]
# handle updating config
if _type == "config.set":
# send lxmf message to destination
config = data["config"]
# update display name in state
if "display_name" in config and config["display_name"] != "":
self.display_name = config["display_name"]
print("updated display name to: " + self.display_name)
# save config
self.save_config()
# send config to websocket clients
self.send_config_to_websocket_clients()
# handle sending an lxmf message
if _type == "lxmf.delivery":
elif _type == "lxmf.delivery":
# send lxmf message to destination
destination_hash = data["destination_hash"]
@@ -136,6 +200,7 @@ class ReticulumWebChat:
self.websocket_broadcast(json.dumps({
"type": "config",
"config": {
"display_name": self.display_name,
"identity_hash": self.identity.hexhash,
"lxmf_address_hash": self.local_lxmf_destination.hexhash,
},