view src/get_chat.js.luan @ 53:9298b04607ae

add unread
author Franklin Schmidt <fschmidt@gmail.com>
date Mon, 03 Mar 2025 19:39:30 -0700
parents 42b741a1d5c6
children afd5ab5b02a2
line wrap: on
line source

local Luan = require "luan:Luan.luan"
local error = Luan.error
local ipairs = Luan.ipairs or error()
local Parsers = require "luan:Parsers.luan"
local json_string = Parsers.json_string 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 Chat = require "site:/lib/Chat.luan"
local get_chat_by_id = Chat.get_by_id or error()
local Post = require "site:/lib/Post.luan"
local post_search = Post.search or error()
local Shared = require "site:/lib/Shared.luan"
local post_html = Shared.post_html or error()
local Logging = require "luan:logging/Logging.luan"
local logger = Logging.logger "get_chat.js"


local function html()
	local user = current_user() or error()
	local chat = Http.request.parameters.chat or error()
	chat = get_chat_by_id(chat) or error()
	chat.read(user)
	local posts = post_search( "post_chat_id:"..chat.id, "id" )
%>
	<div top>
		<h3>
			<img back onclick="back()" src="/images/arrow_back.svg">
			<span><%
				local user_id = chat.other_user_id(user.id)
				local other_user = get_user_by_id(user_id) or error(user_id)
				%><%= other_user.name_html() %><span online="<%= other_user.id %>"></span><%
				local voice_url = other_user.voice_url
				if voice_url ~= nil then
					%> <a href="<%=voice_url%>" title="Call" target="voice"><img phone src="/images/call.svg"></a><%
				end
			%></span>
		</h3>
		<button onclick='deleteChat()'>Delete</button>
	</div>
	<div main>
<%
	for _, post in ipairs(posts) do
		post_html(post)
	end
%>
		<div input>
			<textarea oninput="fixTextarea(event)" onkeydown="textareaKey(event)"></textarea>
			<button onclick="addPost()" title="Send"><img src="/images/send.svg"></button>
		</div>
	</div>
<%
end

return function()
	Io.stdout = Http.response.text_writer()
%>
	gotChat(<%=json_string(`html()`)%>);
<%
end