annotate src/get_chat.js.luan @ 10:f9e6a4cc4f7d

add Post
author Franklin Schmidt <fschmidt@gmail.com>
date Wed, 30 Oct 2024 23:18:45 -0600
parents src/chat.js.luan@41d35b72c774
children 563a5358f2ee
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
7
41d35b72c774 chat page
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1 local Luan = require "luan:Luan.luan"
41d35b72c774 chat page
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
2 local error = Luan.error
10
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents: 7
diff changeset
3 local ipairs = Luan.ipairs or error()
7
41d35b72c774 chat page
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
4 local Parsers = require "luan:Parsers.luan"
41d35b72c774 chat page
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
5 local json_string = Parsers.json_string or error()
10
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents: 7
diff changeset
6 local Html = require "luan:Html.luan"
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents: 7
diff changeset
7 local html_encode = Html.encode or error()
7
41d35b72c774 chat page
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
8 local Io = require "luan:Io.luan"
41d35b72c774 chat page
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
9 local Http = require "luan:http/Http.luan"
41d35b72c774 chat page
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
10 local User = require "site:/lib/User.luan"
10
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents: 7
diff changeset
11 local get_user_by_id = User.get_by_id or error()
7
41d35b72c774 chat page
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
12 local current_user = User.current or error()
41d35b72c774 chat page
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
13 local Chat = require "site:/lib/Chat.luan"
41d35b72c774 chat page
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
14 local get_chat_by_id = Chat.get_by_id or error()
10
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents: 7
diff changeset
15 local Post = require "site:/lib/Post.luan"
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents: 7
diff changeset
16 local post_search = Post.search or error()
7
41d35b72c774 chat page
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
17
41d35b72c774 chat page
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
18
10
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents: 7
diff changeset
19 local function post_html(post)
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents: 7
diff changeset
20 local author = get_user_by_id(post.author_id)
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents: 7
diff changeset
21 local id = post.id
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents: 7
diff changeset
22 %>
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents: 7
diff changeset
23 <div post="<%=id%>">
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents: 7
diff changeset
24 <div who>
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents: 7
diff changeset
25 <span author><%=author.email%></span>
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents: 7
diff changeset
26 <span when fix><%=post.date%></span>
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents: 7
diff changeset
27 </div>
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents: 7
diff changeset
28 <div text><%= html_encode(post.content) %></div>
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents: 7
diff changeset
29 </div>
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents: 7
diff changeset
30 <%
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents: 7
diff changeset
31 end
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents: 7
diff changeset
32
7
41d35b72c774 chat page
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
33 local function html()
41d35b72c774 chat page
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
34 local user = current_user() or error()
41d35b72c774 chat page
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
35 local chat = Http.request.parameters.chat or error()
41d35b72c774 chat page
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
36 chat = get_chat_by_id(chat) or error()
10
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents: 7
diff changeset
37 local posts = post_search( "post_chat_id:"..chat.id, "id" )
7
41d35b72c774 chat page
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
38 %>
10
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents: 7
diff changeset
39 <div top>
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents: 7
diff changeset
40 <h3><%= chat.other_users_email(user) %></h3>
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents: 7
diff changeset
41 <button>delete</button>
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents: 7
diff changeset
42 </div>
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents: 7
diff changeset
43 <div main>
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents: 7
diff changeset
44 <%
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents: 7
diff changeset
45 for _, post in ipairs(posts) do
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents: 7
diff changeset
46 post_html(post)
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents: 7
diff changeset
47 end
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents: 7
diff changeset
48 %>
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents: 7
diff changeset
49 <div input>
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents: 7
diff changeset
50 <textarea oninput="fixTextarea(event)" onkeydown="textareaKey(event)"></textarea>
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents: 7
diff changeset
51 <button onclick="addPost()" title="Send"><img src="/images/send.svg"></button>
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents: 7
diff changeset
52 </div>
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents: 7
diff changeset
53 </div>
7
41d35b72c774 chat page
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
54 <%
41d35b72c774 chat page
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
55 end
41d35b72c774 chat page
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
56
41d35b72c774 chat page
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
57 return function()
41d35b72c774 chat page
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
58 Io.stdout = Http.response.text_writer()
41d35b72c774 chat page
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
59 %>
41d35b72c774 chat page
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
60 document.querySelector('div[posts]').innerHTML = <%=json_string(`html()`)%>;
10
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents: 7
diff changeset
61 fixDates();
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents: 7
diff changeset
62 document.querySelector('div[input] textarea').focus();
7
41d35b72c774 chat page
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
63 <%
41d35b72c774 chat page
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
64 end