view src/heartbeat.js.luan @ 73:a63faf49e1d7

last seen
author Franklin Schmidt <fschmidt@gmail.com>
date Sat, 08 Mar 2025 20:26:19 -0700
parents 42b741a1d5c6
children 377bdda60f0b
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 Parsers = require "luan:Parsers.luan"
local json_string = Parsers.json_string 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 compressed = Shared.compressed or error()
local Online = require "site:/lib/Online.luan"
local set_online = Online.set or error()
local get_online = Online.get or error()
local Logging = require "luan:logging/Logging.luan"
local logger = Logging.logger "heartbeat.js"


return function()
	local user = current_user() or error()
	set_online(user)
	Io.stdout = Http.response.text_writer()

	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()
	if time_now() - user_last_update > 70000 and last_update < user_last_update then
		logger.info "update"
%>
		location = '/';
<%
		return
	end

	local online = get_online(user)
%>
	online = <%=json_string(online,compressed)%>;
	showOnline();
<%
end