Mercurial Hosting > chat
changeset 33:e2b7f6393dab
add online
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Sun, 10 Nov 2024 19:57:14 -0700 |
parents | 0f40501b0b56 |
children | 62d04ca486dd |
files | src/chat.css src/chat.js src/delete_user.js.luan src/get_chat.js.luan src/heartbeat.js.luan src/index.html.luan src/lib/Chat.luan src/lib/Shared.luan src/lib/User.luan |
diffstat | 9 files changed, 86 insertions(+), 38 deletions(-) [+] |
line wrap: on
line diff
diff -r 0f40501b0b56 -r e2b7f6393dab src/chat.css --- a/src/chat.css Sat Nov 09 21:41:11 2024 -0700 +++ b/src/chat.css Sun Nov 10 19:57:14 2024 -0700 @@ -102,6 +102,16 @@ max-width: 80vw; } +span[online] { + display: inline-block; + aspect-ratio: 1; + background-color: grey; + height: 1em; + border-radius: 50%; + margin-left: 4px; + margin-bottom: -2px; +} + @media (min-width: 700px) { div[content] {
diff -r 0f40501b0b56 -r e2b7f6393dab src/chat.js --- a/src/chat.js Sat Nov 09 21:41:11 2024 -0700 +++ b/src/chat.js Sun Nov 10 19:57:14 2024 -0700 @@ -173,6 +173,7 @@ } window.onfocus = function() { + // console.log('onfocus'); document.title = title; hasUnseen = false; }; @@ -207,8 +208,9 @@ } setInterval(function(){ + showOnline(); ajax(`heartbeat.js?last_update=${lastUpdate}`); -}, 60000 ); +}, 10000 ); function resync(updated) { lastUpdate = updated; @@ -223,3 +225,30 @@ sound.play(); } +let online = {}; + +function setOnline(userId) { + online[userId] = Date.now(); +} + +function showOnline() { + let old = Date.now() - 20000; + for( let id of Object.keys(online) ) { + if( online[id] < old ) + delete online[id]; + } + let a = []; + for( let id in online ) { + a.push( `span[online="${id}"]` ); + } + let style = document.querySelector('style[online]'); + if( a.length === 0 ) { + style.innerHTML = ''; + } else { + style.innerHTML = ` + ${a.join(', ')} { + background-color: green; + } +` ; + } +}
diff -r 0f40501b0b56 -r e2b7f6393dab src/delete_user.js.luan --- a/src/delete_user.js.luan Sat Nov 09 21:41:11 2024 -0700 +++ b/src/delete_user.js.luan Sun Nov 10 19:57:14 2024 -0700 @@ -1,16 +1,9 @@ local Luan = require "luan:Luan.luan" local error = Luan.error -local ipairs = Luan.ipairs or error() -local stringify = Luan.stringify or error() local Io = require "luan:Io.luan" local Http = require "luan:http/Http.luan" local User = require "site:/lib/User.luan" local current_user = User.current or error() -local Chat = require "site:/lib/Chat.luan" -local chat_search = Chat.search or error() -local Utils = require "site:/lib/Utils.luan" -local list_to_set = Utils.list_to_set or error() -local set_to_list = Utils.set_to_list or error() local Shared = require "site:/lib/Shared.luan" local http_push_to_users = Shared.http_push_to_users or error() local Logging = require "luan:logging/Logging.luan" @@ -20,14 +13,7 @@ return function() local user = current_user() if user ~= nil then - local user_ids = list_to_set{} - local chats = chat_search( "chat_user_ids:"..user.id ) - for _, chat in ipairs(chats) do - for _, user_id in ipairs(chat.user_ids) do - user_ids[user_id] = true - end - end - user_ids = set_to_list(user_ids) + local user_ids = user.chatting_with_ids() local js = "getChats(null)" user.delete() http_push_to_users( user_ids, js )
diff -r 0f40501b0b56 -r e2b7f6393dab src/get_chat.js.luan --- a/src/get_chat.js.luan Sat Nov 09 21:41:11 2024 -0700 +++ b/src/get_chat.js.luan Sun Nov 10 19:57:14 2024 -0700 @@ -13,6 +13,7 @@ local post_search = Post.search or error() local Shared = require "site:/lib/Shared.luan" local post_html = Shared.post_html or error() +local chat_other_users_html = Shared.chat_other_users_html or error() local Logging = require "luan:logging/Logging.luan" local logger = Logging.logger "get_chat.js" @@ -26,7 +27,7 @@ <div top> <h3> <img back onclick="back()" src="/images/arrow_back.svg"> - <span><%= chat.other_users_email(user) %></span> + <span><% chat_other_users_html(chat,user) %></span> </h3> <button onclick='deleteChat()'>delete</button> </div>
diff -r 0f40501b0b56 -r e2b7f6393dab src/heartbeat.js.luan --- a/src/heartbeat.js.luan Sat Nov 09 21:41:11 2024 -0700 +++ b/src/heartbeat.js.luan Sun Nov 10 19:57:14 2024 -0700 @@ -8,18 +8,24 @@ local Http = require "luan:http/Http.luan" local User = require "site:/lib/User.luan" local current_user = User.current or error() +local Shared = require "site:/lib/Shared.luan" +local http_push_to_users = Shared.http_push_to_users or error() local Logging = require "luan:logging/Logging.luan" local logger = Logging.logger "heartbeat.js" return function() + local user = current_user() or error() + local user_ids = user.chatting_with_ids() + local js = "setOnline("..user.id..")" + http_push_to_users( user_ids, js ) + local last_update = Http.request.parameters.last_update or error() last_update = to_number(last_update) or error() - local user = current_user() or error() local user_last_update = user.last_update() local now = time_now() if now - user_last_update < 10000 or last_update >= user_last_update then - logger.info "ok" + -- logger.info "ok" return end logger.info "update"
diff -r 0f40501b0b56 -r e2b7f6393dab src/index.html.luan --- a/src/index.html.luan Sat Nov 09 21:41:11 2024 -0700 +++ b/src/index.html.luan Sun Nov 10 19:57:14 2024 -0700 @@ -94,6 +94,7 @@ <style> @import "chat.css?s=<%=started%>"; </style> + <style online></style> <script src="chat.js?s=<%=started%>"></script> </head> <body>
diff -r 0f40501b0b56 -r e2b7f6393dab src/lib/Chat.luan --- a/src/lib/Chat.luan Sat Nov 09 21:41:11 2024 -0700 +++ b/src/lib/Chat.luan Sun Nov 10 19:57:14 2024 -0700 @@ -1,9 +1,6 @@ local Luan = require "luan:Luan.luan" local error = Luan.error local ipairs = Luan.ipairs or error() -local Table = require "luan:Table.luan" -local concat = Table.concat or error() -local is_empty = Table.is_empty or error() local Time = require "luan:Time.luan" local time_now = Time.now or error() local Http = require "luan:http/Http.luan" @@ -50,21 +47,6 @@ end ) end - function chat.other_users_email(user) - local User = require "site:/lib/User.luan" - local get_user_by_id = User.get_by_id or error() - - local my_id = user.id - local t = {} - for _, user_id in ipairs(chat.user_ids) do - if user_id ~= my_id then - local other_user = get_user_by_id(user_id) or error() - t[#t+1] = other_user.email - end - end - return concat( t, ", " ) - end - function chat.http_push(message) local url = base_url().."/chat/"..chat.id Http.push(url,message)
diff -r 0f40501b0b56 -r e2b7f6393dab src/lib/Shared.luan --- a/src/lib/Shared.luan Sat Nov 09 21:41:11 2024 -0700 +++ b/src/lib/Shared.luan Sun Nov 10 19:57:14 2024 -0700 @@ -117,12 +117,29 @@ <% end +local function chat_other_users_html(chat,user) + local my_id = user.id + local is_first = true + for _, user_id in ipairs(chat.user_ids) do + if user_id ~= my_id then + local other_user = get_user_by_id(user_id) or error() + if is_first then + is_first = false + else + %>, <% + end + %><%= other_user.email %><span online="<%= other_user.id %>"></span><% + end + end +end +Shared.chat_other_users_html = chat_other_users_html + function Shared.chats_html() local user = current_user() or error() local chats = chat_search( "chat_user_ids:"..user.id, "chat_updated desc" ) for _, chat in ipairs(chats) do %> - <div chat="<%=chat.id%>" onclick="selectChat(this)"><%= chat.other_users_email(user) %></div> + <div chat="<%=chat.id%>" onclick="selectChat(this)"><% chat_other_users_html(chat,user) %></div> <% end end
diff -r 0f40501b0b56 -r e2b7f6393dab src/lib/User.luan --- a/src/lib/User.luan Sat Nov 09 21:41:11 2024 -0700 +++ b/src/lib/User.luan Sun Nov 10 19:57:14 2024 -0700 @@ -18,6 +18,9 @@ local run_in_transaction = Db.run_in_transaction or error() local Chat = require "site:/lib/Chat.luan" local chat_search = Chat.search or error() +local Utils = require "site:/lib/Utils.luan" +local list_to_set = Utils.list_to_set or error() +local set_to_list = Utils.set_to_list or error() local Logging = require "luan:logging/Logging.luan" local logger = Logging.logger "User" @@ -79,6 +82,19 @@ else error() end end + function user.chatting_with_ids() + local my_id = user.id + local user_ids = list_to_set{} + local chats = chat_search( "chat_user_ids:"..my_id ) + for _, chat in ipairs(chats) do + for _, user_id in ipairs(chat.user_ids) do + user_ids[user_id] = true + end + end + user_ids[my_id] = false + return set_to_list(user_ids) + end + return user end