view src/heartbeat.js.luan @ 66:f067de76084c

minor
author Franklin Schmidt <fschmidt@gmail.com>
date Thu, 06 Mar 2025 03:09:24 -0700
parents 42b741a1d5c6
children a63faf49e1d7
line wrap: on
line source

local Luan = require "luan:Luan.luan"
local error = Luan.error
local String = require "luan:String.luan"
local to_number = String.to_number or error()
local Time = require "luan:Time.luan"
local time_now = Time.now 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 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(last_update)
	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"
		return
	end
	logger.info "update"
	Io.stdout = Http.response.text_writer()
%>
	location = '/';
<%
end