view src/heartbeat.js.luan @ 92:c41e200c3e76

try to fix lost posts
author Franklin Schmidt <fschmidt@gmail.com>
date Mon, 31 Mar 2025 15:43:14 -0600
parents 3053a4fc10be
children
line wrap: on
line source

local Luan = require "luan:Luan.luan"
local error = Luan.error
local ipairs = Luan.ipairs or 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 get_user_by_id = User.get_by_id 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 Chat = require "site:/lib/Chat.luan"
local get_chat_by_id = Chat.get_by_id or error()
local Logging = require "luan:logging/Logging.luan"
local logger = Logging.logger "heartbeat.js"


return function()
	local user = current_user() or error()
	local spy = Http.request.cookies.spy ~= nil
	if not spy then
		set_online(user)
	end
	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 > 10000 and last_update < user_last_update then
		logger.info "update"
%>
		location = '/';
<%
		return
	end

	local online = get_online(user)
%>
	online = <%=json_string(online,compressed)%>;
	showOnline();
<%
	local chat = Http.request.parameters.chat
	if chat ~= nil then
		chat = get_chat_by_id(chat)
		if chat == nil then
%>
			location = '/';
<%
			return
		end
		local my_user_id = user.id
		for _, user_id in ipairs(chat.user_ids) do
			if user_id == my_user_id then continue end
			local user = get_user_by_id(user_id)
%>
			readUpTo(<%=user_id%>,<%=json_string(user.name_html())%>,<%=chat.unread(user)%>);
<%
		end
	end
end