annotate src/get_chat.js.luan @ 89:3053a4fc10be

spy
author Franklin Schmidt <fschmidt@gmail.com>
date Thu, 13 Mar 2025 17:56:39 -0600
parents a47036fd0158
children
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()
63
afd5ab5b02a2 file upload
Franklin Schmidt <fschmidt@gmail.com>
parents: 53
diff changeset
4 local String = require "luan:String.luan"
afd5ab5b02a2 file upload
Franklin Schmidt <fschmidt@gmail.com>
parents: 53
diff changeset
5 local digest_message = String.digest_message or error()
7
41d35b72c774 chat page
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
6 local Parsers = require "luan:Parsers.luan"
41d35b72c774 chat page
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
7 local json_string = Parsers.json_string or error()
65
6cfef9850520 people popup
Franklin Schmidt <fschmidt@gmail.com>
parents: 63
diff changeset
8 local Html = require "luan:Html.luan"
6cfef9850520 people popup
Franklin Schmidt <fschmidt@gmail.com>
parents: 63
diff changeset
9 local html_encode = Html.encode or error()
7
41d35b72c774 chat page
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
10 local Io = require "luan:Io.luan"
41d35b72c774 chat page
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
11 local Http = require "luan:http/Http.luan"
41d35b72c774 chat page
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
12 local User = require "site:/lib/User.luan"
41d35b72c774 chat page
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
13 local current_user = User.current or error()
46
42b741a1d5c6 add username
Franklin Schmidt <fschmidt@gmail.com>
parents: 41
diff changeset
14 local get_user_by_id = User.get_by_id or error()
7
41d35b72c774 chat page
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
15 local Chat = require "site:/lib/Chat.luan"
41d35b72c774 chat page
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
16 local get_chat_by_id = Chat.get_by_id or error()
10
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents: 7
diff changeset
17 local Post = require "site:/lib/Post.luan"
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents: 7
diff changeset
18 local post_search = Post.search or error()
12
9f45d32670ae server push
Franklin Schmidt <fschmidt@gmail.com>
parents: 11
diff changeset
19 local Shared = require "site:/lib/Shared.luan"
9f45d32670ae server push
Franklin Schmidt <fschmidt@gmail.com>
parents: 11
diff changeset
20 local post_html = Shared.post_html or error()
83
a47036fd0158 group chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 79
diff changeset
21 local group_name = Shared.group_name or error()
27
809193524522 responsive
Franklin Schmidt <fschmidt@gmail.com>
parents: 12
diff changeset
22 local Logging = require "luan:logging/Logging.luan"
809193524522 responsive
Franklin Schmidt <fschmidt@gmail.com>
parents: 12
diff changeset
23 local logger = Logging.logger "get_chat.js"
7
41d35b72c774 chat page
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
24
10
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents: 7
diff changeset
25
63
afd5ab5b02a2 file upload
Franklin Schmidt <fschmidt@gmail.com>
parents: 53
diff changeset
26 local function get_html(user,chat)
89
Franklin Schmidt <fschmidt@gmail.com>
parents: 83
diff changeset
27 local spy = Http.request.cookies.spy ~= nil
Franklin Schmidt <fschmidt@gmail.com>
parents: 83
diff changeset
28 if not spy then
Franklin Schmidt <fschmidt@gmail.com>
parents: 83
diff changeset
29 chat.read(user)
Franklin Schmidt <fschmidt@gmail.com>
parents: 83
diff changeset
30 end
10
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents: 7
diff changeset
31 local posts = post_search( "post_chat_id:"..chat.id, "id" )
7
41d35b72c774 chat page
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
32 %>
10
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents: 7
diff changeset
33 <div top>
27
809193524522 responsive
Franklin Schmidt <fschmidt@gmail.com>
parents: 12
diff changeset
34 <h3>
809193524522 responsive
Franklin Schmidt <fschmidt@gmail.com>
parents: 12
diff changeset
35 <img back onclick="back()" src="/images/arrow_back.svg">
83
a47036fd0158 group chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 79
diff changeset
36 <span>
a47036fd0158 group chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 79
diff changeset
37 <%
a47036fd0158 group chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 79
diff changeset
38 local user_ids = chat.other_user_ids(user.id)
a47036fd0158 group chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 79
diff changeset
39 if #user_ids > 1 then
a47036fd0158 group chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 79
diff changeset
40 %>
a47036fd0158 group chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 79
diff changeset
41 <%= group_name(user_ids) %>
a47036fd0158 group chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 79
diff changeset
42 <%
a47036fd0158 group chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 79
diff changeset
43 else
a47036fd0158 group chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 79
diff changeset
44 local other_user = get_user_by_id(user_ids[1]) or error()
a47036fd0158 group chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 79
diff changeset
45 %>
a47036fd0158 group chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 79
diff changeset
46 <%= other_user.name_html() %><span online="<%= other_user.id %>"></span>
a47036fd0158 group chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 79
diff changeset
47 <%
a47036fd0158 group chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 79
diff changeset
48 local voice_url = other_user.voice_url
a47036fd0158 group chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 79
diff changeset
49 if voice_url ~= nil then
a47036fd0158 group chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 79
diff changeset
50 %>
a47036fd0158 group chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 79
diff changeset
51 <a href="<%=voice_url%>" title="Call" target="voice"><img phone src="/images/call.svg"></a>
a47036fd0158 group chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 79
diff changeset
52 <%
a47036fd0158 group chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 79
diff changeset
53 end
a47036fd0158 group chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 79
diff changeset
54 end
a47036fd0158 group chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 79
diff changeset
55 %>
a47036fd0158 group chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 79
diff changeset
56 </span>
27
809193524522 responsive
Franklin Schmidt <fschmidt@gmail.com>
parents: 12
diff changeset
57 </h3>
68
Franklin Schmidt <fschmidt@gmail.com>
parents: 65
diff changeset
58 <span pulldown>
Franklin Schmidt <fschmidt@gmail.com>
parents: 65
diff changeset
59 <img onclick="clickMenu(this)" src="/images/menu.svg">
Franklin Schmidt <fschmidt@gmail.com>
parents: 65
diff changeset
60 <div>
Franklin Schmidt <fschmidt@gmail.com>
parents: 65
diff changeset
61 <span onclick="openPeople()">People in Chat</span>
83
a47036fd0158 group chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 79
diff changeset
62 <span onclick="openAddToChat()">Add Someone to Chat</span>
68
Franklin Schmidt <fschmidt@gmail.com>
parents: 65
diff changeset
63 <span onclick="deleteChat()">Delete Chat</span>
Franklin Schmidt <fschmidt@gmail.com>
parents: 65
diff changeset
64 </div>
Franklin Schmidt <fschmidt@gmail.com>
parents: 65
diff changeset
65 </span>
10
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents: 7
diff changeset
66 </div>
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents: 7
diff changeset
67 <div main>
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents: 7
diff changeset
68 <%
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents: 7
diff changeset
69 for _, post in ipairs(posts) do
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents: 7
diff changeset
70 post_html(post)
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents: 7
diff changeset
71 end
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents: 7
diff changeset
72 %>
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents: 7
diff changeset
73 <div input>
79
Franklin Schmidt <fschmidt@gmail.com>
parents: 73
diff changeset
74 <span textarea>
Franklin Schmidt <fschmidt@gmail.com>
parents: 73
diff changeset
75 <div reply hidden>
Franklin Schmidt <fschmidt@gmail.com>
parents: 73
diff changeset
76 <div reply_top>
Franklin Schmidt <fschmidt@gmail.com>
parents: 73
diff changeset
77 <span>Reply to</span>
Franklin Schmidt <fschmidt@gmail.com>
parents: 73
diff changeset
78 <img src="/images/close.svg" onclick="closeReply()">
Franklin Schmidt <fschmidt@gmail.com>
parents: 73
diff changeset
79 </div>
Franklin Schmidt <fschmidt@gmail.com>
parents: 73
diff changeset
80 <div text></div>
Franklin Schmidt <fschmidt@gmail.com>
parents: 73
diff changeset
81 <div><a when></a></div>
Franklin Schmidt <fschmidt@gmail.com>
parents: 73
diff changeset
82 </div>
Franklin Schmidt <fschmidt@gmail.com>
parents: 73
diff changeset
83 <textarea oninput="fixTextarea(event)" onkeydown="textareaKey(event)"></textarea>
Franklin Schmidt <fschmidt@gmail.com>
parents: 73
diff changeset
84 </span>
63
afd5ab5b02a2 file upload
Franklin Schmidt <fschmidt@gmail.com>
parents: 53
diff changeset
85 <button onclick="uploadFile()" title="Add file"><img src="/images/upload_file.svg"></button>
10
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents: 7
diff changeset
86 <button onclick="addPost()" title="Send"><img src="/images/send.svg"></button>
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents: 7
diff changeset
87 </div>
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents: 7
diff changeset
88 </div>
7
41d35b72c774 chat page
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
89 <%
41d35b72c774 chat page
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
90 end
41d35b72c774 chat page
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
91
65
6cfef9850520 people popup
Franklin Schmidt <fschmidt@gmail.com>
parents: 63
diff changeset
92 local function people(chat)
6cfef9850520 people popup
Franklin Schmidt <fschmidt@gmail.com>
parents: 63
diff changeset
93 for _, user_id in ipairs(chat.user_ids) do
6cfef9850520 people popup
Franklin Schmidt <fschmidt@gmail.com>
parents: 63
diff changeset
94 local user = get_user_by_id(user_id)
6cfef9850520 people popup
Franklin Schmidt <fschmidt@gmail.com>
parents: 63
diff changeset
95 local name = user.name
6cfef9850520 people popup
Franklin Schmidt <fschmidt@gmail.com>
parents: 63
diff changeset
96 local email = user.email
6cfef9850520 people popup
Franklin Schmidt <fschmidt@gmail.com>
parents: 63
diff changeset
97 local voice_url = user.voice_url
73
a63faf49e1d7 last seen
Franklin Schmidt <fschmidt@gmail.com>
parents: 68
diff changeset
98 local voice = voice_url and `%> <a href="<%=voice_url%>" title="Call" target="voice"><img phone src="/images/call.svg"></a><%` or ""
65
6cfef9850520 people popup
Franklin Schmidt <fschmidt@gmail.com>
parents: 63
diff changeset
99 %>
6cfef9850520 people popup
Franklin Schmidt <fschmidt@gmail.com>
parents: 63
diff changeset
100 <p>
6cfef9850520 people popup
Franklin Schmidt <fschmidt@gmail.com>
parents: 63
diff changeset
101 <%
6cfef9850520 people popup
Franklin Schmidt <fschmidt@gmail.com>
parents: 63
diff changeset
102 if name == nil then
6cfef9850520 people popup
Franklin Schmidt <fschmidt@gmail.com>
parents: 63
diff changeset
103 %>
73
a63faf49e1d7 last seen
Franklin Schmidt <fschmidt@gmail.com>
parents: 68
diff changeset
104 <b><%=html_encode(email)%></b><%=voice%><br>
65
6cfef9850520 people popup
Franklin Schmidt <fschmidt@gmail.com>
parents: 63
diff changeset
105 <%
6cfef9850520 people popup
Franklin Schmidt <fschmidt@gmail.com>
parents: 63
diff changeset
106 else
6cfef9850520 people popup
Franklin Schmidt <fschmidt@gmail.com>
parents: 63
diff changeset
107 %>
6cfef9850520 people popup
Franklin Schmidt <fschmidt@gmail.com>
parents: 63
diff changeset
108 <b><%=html_encode(name)%></b><%=voice%><br>
73
a63faf49e1d7 last seen
Franklin Schmidt <fschmidt@gmail.com>
parents: 68
diff changeset
109 <%=html_encode(email)%><br>
65
6cfef9850520 people popup
Franklin Schmidt <fschmidt@gmail.com>
parents: 63
diff changeset
110 <%
6cfef9850520 people popup
Franklin Schmidt <fschmidt@gmail.com>
parents: 63
diff changeset
111 end
6cfef9850520 people popup
Franklin Schmidt <fschmidt@gmail.com>
parents: 63
diff changeset
112 %>
73
a63faf49e1d7 last seen
Franklin Schmidt <fschmidt@gmail.com>
parents: 68
diff changeset
113 <span last_seen="<%=user_id%>"></span>
65
6cfef9850520 people popup
Franklin Schmidt <fschmidt@gmail.com>
parents: 63
diff changeset
114 </p>
6cfef9850520 people popup
Franklin Schmidt <fschmidt@gmail.com>
parents: 63
diff changeset
115 <%
6cfef9850520 people popup
Franklin Schmidt <fschmidt@gmail.com>
parents: 63
diff changeset
116 end
6cfef9850520 people popup
Franklin Schmidt <fschmidt@gmail.com>
parents: 63
diff changeset
117 end
6cfef9850520 people popup
Franklin Schmidt <fschmidt@gmail.com>
parents: 63
diff changeset
118
7
41d35b72c774 chat page
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
119 return function()
63
afd5ab5b02a2 file upload
Franklin Schmidt <fschmidt@gmail.com>
parents: 53
diff changeset
120 local user = current_user() or error()
afd5ab5b02a2 file upload
Franklin Schmidt <fschmidt@gmail.com>
parents: 53
diff changeset
121 local chat = Http.request.parameters.chat or error()
afd5ab5b02a2 file upload
Franklin Schmidt <fschmidt@gmail.com>
parents: 53
diff changeset
122 chat = get_chat_by_id(chat) or error()
afd5ab5b02a2 file upload
Franklin Schmidt <fschmidt@gmail.com>
parents: 53
diff changeset
123 local html = `get_html(user,chat)`
afd5ab5b02a2 file upload
Franklin Schmidt <fschmidt@gmail.com>
parents: 53
diff changeset
124 local digest = digest_message("MD5",user.password..chat.id)
7
41d35b72c774 chat page
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
125 Io.stdout = Http.response.text_writer()
41d35b72c774 chat page
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
126 %>
63
afd5ab5b02a2 file upload
Franklin Schmidt <fschmidt@gmail.com>
parents: 53
diff changeset
127 gotChat(<%=json_string(html)%>);
afd5ab5b02a2 file upload
Franklin Schmidt <fschmidt@gmail.com>
parents: 53
diff changeset
128 filebinUrl = 'https://filebin.net/<%=digest%>/';
65
6cfef9850520 people popup
Franklin Schmidt <fschmidt@gmail.com>
parents: 63
diff changeset
129 document.querySelector('dialog[people] div[people]').innerHTML = <%=json_string(`people(chat)`)%>;
7
41d35b72c774 chat page
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
130 <%
41d35b72c774 chat page
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
131 end