Skip to content

Commit

Permalink
feat: add message class abstracting message handling (#23)
Browse files Browse the repository at this point in the history
* refactor: add comments to clarify flow

Signed-off-by: Gerard Hickey <[email protected]>

* feat: add messages class to front end

Update the handling of messages on the front end code to better control
the update of messages and coordinateion with the rest of the UI.

Signed-off-by: Gerard Hickey <[email protected]>

* add more config support

Signed-off-by: Gerard Hickey <[email protected]>

* feat: message ID can be specified in send_message API

Signed-off-by: Gerard Hickey <[email protected]>

* add md5 module to front-end

Signed-off-by: Gerard Hickey <[email protected]>

* integrate message class into front-end code

Signed-off-by: Gerard Hickey <[email protected]>

---------

Signed-off-by: Gerard Hickey <[email protected]>
  • Loading branch information
hickey committed Feb 5, 2024
1 parent b70bd0a commit 5e9c8f6
Show file tree
Hide file tree
Showing 6 changed files with 671 additions and 300 deletions.
12 changes: 10 additions & 2 deletions meshchat
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ function send_message()
print("\r")

local message = query.message:gsub("\n", "\\n"):gsub('"', '\\"'):gsub("\t", " ")

local id = query.id or hash();
local epoch = os.time()
if query.epoch and tonumber(query.epoch) > epoch then
epoch = query.epoch
Expand All @@ -106,7 +106,7 @@ function send_message()
release_lock()
die("Cannot send message")
end
f:write(hash() .. "\t" .. epoch .. "\t" .. message .. "\t" .. query.call_sign .. "\t" .. node_name() .. "\t" .. platform .. "\t" .. query.channel .. "\n")
f:write(id .. "\t" .. epoch .. "\t" .. message .. "\t" .. query.call_sign .. "\t" .. node_name() .. "\t" .. platform .. "\t" .. query.channel .. "\n")
f:close()

sort_and_trim_db()
Expand All @@ -133,6 +133,7 @@ function messages()

local node = node_name()

-- read in message DB and parse the contents
local messages = {}
for line in io.lines(messages_db_file)
do
Expand All @@ -152,6 +153,8 @@ function messages()

if tonumber(query.epoch) and query.call_sign then
local users = {}

-- read the users status file
if nixio.fs.stat(local_users_status_file) then
for line in io.lines(local_users_status_file)
do
Expand All @@ -162,11 +165,14 @@ function messages()
end
end

-- set the timestamp
local epoch = os.time()
if tonumber(query.epoch) > epoch then
epoch = query.epoch
end

-- rewrite user status file updating the timestamp for requesting call sign
-- query.id is the meshchat_id
local f = io.open(local_users_status_file, "w")
if f then
local found_user = false
Expand All @@ -188,6 +194,7 @@ function messages()

release_lock()

-- order messages according to time
table.sort(messages, function(a, b) return a.epoch > b.epoch end)

output:write(luci.jsonc.stringify(messages))
Expand Down Expand Up @@ -487,6 +494,7 @@ function messages_version_ui()
epoch = query.epoch
end

-- TODO refactor here and messages function into a single code block
local f = io.open(local_users_status_file, "w")
if f then
local found_user = false
Expand Down
Loading

0 comments on commit 5e9c8f6

Please sign in to comment.