Mercurial Hosting > chat
annotate src/lib/Shared.luan @ 118:04933d5ba05a
handle markdown
| author | Franklin Schmidt <fschmidt@gmail.com> |
|---|---|
| date | Thu, 04 Dec 2025 22:57:00 -0700 |
| parents | e2deb5236f26 |
| children |
| rev | line source |
|---|---|
| 0 | 1 local Luan = require "luan:Luan.luan" |
| 2 local error = Luan.error | |
| 3 local ipairs = Luan.ipairs or error() | |
| 1 | 4 local parse = Luan.parse or error() |
| 117 | 5 local String = require "luan:String.luan" |
| 6 local contains = String.contains or error() | |
| 83 | 7 local Table = require "luan:Table.luan" |
| 8 local concat = Table.concat or error() | |
| 3 | 9 local Time = require "luan:Time.luan" |
| 10 local Thread = require "luan:Thread.luan" | |
| 11 local thread_run = Thread.run or error() | |
| 12 | 12 local Html = require "luan:Html.luan" |
| 13 local html_encode = Html.encode or error() | |
| 117 | 14 local url_encode = Html.url_encode or error() |
| 16 | 15 local Http = require "luan:http/Http.luan" |
| 3 | 16 local Mail = require "luan:mail/Mail.luan" |
| 2 | 17 local User = require "site:/lib/User.luan" |
| 18 local current_user = User.current or error() | |
| 12 | 19 local get_user_by_id = User.get_by_id or error() |
| 15 | 20 local Chat = require "site:/lib/Chat.luan" |
| 21 local chat_search = Chat.search or error() | |
| 16 | 22 local Utils = require "site:/lib/Utils.luan" |
| 23 local base_url = Utils.base_url or error() | |
| 117 | 24 local to_list = Utils.to_list or error() |
| 69 | 25 local Db = require "site:/lib/Db.luan" |
| 79 | 26 local Post = require "site:/lib/Post.luan" |
| 27 local get_post_by_id = Post.get_by_id or error() | |
| 101 | 28 local Config = require "site:/private/Config.luan" |
| 45 | 29 local Logging = require "luan:logging/Logging.luan" |
| 30 local logger = Logging.logger "Shared" | |
| 0 | 31 |
| 32 | |
| 33 local Shared = {} | |
| 34 | |
| 3 | 35 local started = Time.now() |
| 10 | 36 Shared.started = started |
| 3 | 37 |
| 45 | 38 local title |
| 39 local domain = Http.domain | |
| 40 if domain == "chat.luan.software" then | |
| 51 | 41 title = "Luan Chat" |
| 45 | 42 elseif domain == "test.chat.luan.software" then |
| 51 | 43 title = "Luan Chat test" |
| 45 | 44 elseif domain == nil then |
| 51 | 45 title = "Luan Chat local" |
| 45 | 46 else |
| 47 error(domain) | |
| 48 end | |
| 49 Shared.title = title | |
| 50 | |
| 0 | 51 function Shared.head() |
| 52 %> | |
| 53 <meta name="viewport" content="width=device-width, initial-scale=1"> | |
| 86 | 54 <link rel="manifest" href="/manifest.json" /> |
| 45 | 55 <title><%=title%></title> |
| 0 | 56 <style> |
| 3 | 57 @import "/site.css?s=<%=started%>"; |
| 0 | 58 </style> |
| 3 | 59 <script src="/site.js?s=<%=started%>"></script> |
| 0 | 60 <% |
| 61 end | |
| 62 | |
| 63 local function header(crumbs) | |
| 2 | 64 local user = current_user() |
| 0 | 65 %> |
| 66 <div header> | |
| 2 | 67 <span> |
| 51 | 68 <a href="/">Luan Chat</a> |
| 0 | 69 <% for _, crumb in ipairs(crumbs or {}) do %> |
| 2 | 70 / <%=crumb%> |
| 0 | 71 <% end %> |
| 2 | 72 </span> |
| 41 | 73 <span right> |
| 2 | 74 <% if user == nil then %> |
| 75 <a href="/login.html">Login / Register</a> | |
| 41 | 76 <% else |
| 77 local voice_url = user.voice_url | |
| 78 if voice_url ~= nil then | |
| 79 %> | |
| 80 <a href="<%=voice_url%>" title="Call" target="voice"><img phone src="/images/call.svg"></a> | |
| 81 <% | |
| 82 end | |
| 83 %> | |
| 2 | 84 <a href="/account.html"><%= user.email %></a> |
| 85 <% end %> | |
| 86 </span> | |
| 0 | 87 </div> |
| 88 <% | |
| 89 end | |
| 90 Shared.header = header | |
| 91 | |
| 92 function Shared.private_header() | |
| 93 header{ | |
| 94 [[<a href="/private/">private</a>]] | |
| 95 [[<a href="/private/tools/">tools</a>]] | |
| 96 } | |
| 97 end | |
| 98 | |
| 45 | 99 local default_from = title.." <chat@luan.software>" |
| 101 | 100 local send_mail0 = Mail.sender(Config.mail_server).send |
| 45 | 101 function Shared.send_mail(mail) |
| 102 mail.From = mail.From or default_from | |
| 103 send_mail0(mail) | |
| 104 end | |
| 3 | 105 |
| 106 function Shared.send_mail_async(mail) | |
| 45 | 107 mail.From = mail.From or default_from |
| 3 | 108 thread_run( function() |
| 45 | 109 send_mail0(mail) |
| 3 | 110 end ) |
| 111 end | |
| 112 | |
| 12 | 113 function Shared.post_html(post) |
| 22 | 114 local author_id = post.author_id |
| 115 local user = current_user() or error() | |
| 116 local author = get_user_by_id(author_id) | |
| 12 | 117 local id = post.id |
|
106
15cf43dd144c
handle reply to deleted
Franklin Schmidt <fschmidt@gmail.com>
parents:
101
diff
changeset
|
118 local reply_id = post.reply |
|
15cf43dd144c
handle reply to deleted
Franklin Schmidt <fschmidt@gmail.com>
parents:
101
diff
changeset
|
119 local reply = reply_id and get_post_by_id(reply_id) |
| 12 | 120 %> |
| 79 | 121 <div post="<%=id%>" author="<%=author.id%>" id="p<%=id%>" fix> |
| 35 | 122 <div who> |
| 46 | 123 <span author><%=author.name_html()%></span> |
| 34 | 124 <span right> |
| 35 | 125 <span when><%=post.date%></span> |
| 126 <span pulldown></span> | |
| 34 | 127 </span> |
| 12 | 128 </div> |
| 79 | 129 <% if reply ~= nil then %> |
| 130 <div quote> | |
| 131 <blockquote><%= html_encode(reply.content) %></blockquote> | |
| 132 <div><a when href="#p<%=reply.id%>"><%=reply.date%></a></div> | |
| 133 </div> | |
|
106
15cf43dd144c
handle reply to deleted
Franklin Schmidt <fschmidt@gmail.com>
parents:
101
diff
changeset
|
134 <% elseif reply_id ~= nil then %> |
|
15cf43dd144c
handle reply to deleted
Franklin Schmidt <fschmidt@gmail.com>
parents:
101
diff
changeset
|
135 <div quote> |
|
15cf43dd144c
handle reply to deleted
Franklin Schmidt <fschmidt@gmail.com>
parents:
101
diff
changeset
|
136 <span deleted>Message deleted</span> |
|
15cf43dd144c
handle reply to deleted
Franklin Schmidt <fschmidt@gmail.com>
parents:
101
diff
changeset
|
137 </div> |
| 79 | 138 <% end %> |
| 35 | 139 <div text><%= html_encode(post.content) %></div> |
| 12 | 140 </div> |
| 141 <% | |
| 142 end | |
| 143 | |
| 83 | 144 local function group_name(user_ids) |
| 145 local t = {nil} | |
| 146 for _, user_id in ipairs(user_ids) do | |
| 147 t[#t+1] = get_user_by_id(user_id).name_html() | |
| 148 end | |
| 149 return concat(t,", ") | |
| 150 end | |
| 151 Shared.group_name = group_name | |
| 152 | |
| 15 | 153 function Shared.chats_html() |
| 154 local user = current_user() or error() | |
| 155 local chats = chat_search( "chat_user_ids:"..user.id, "chat_updated desc" ) | |
| 156 for _, chat in ipairs(chats) do | |
| 59 | 157 local chat_id = chat.id |
| 53 | 158 local unread = chat.unread(user) |
| 15 | 159 %> |
| 59 | 160 <div chat="<%=chat_id%>" onclick="selectChat('<%=chat_id%>')"> |
| 83 | 161 <% |
| 162 local user_ids = chat.other_user_ids(user.id) | |
| 163 if #user_ids > 1 then | |
| 164 %> | |
| 165 <%= group_name(user_ids) %> | |
| 166 <% | |
| 167 else | |
| 168 local other_user = get_user_by_id(user_ids[1]) or error() | |
| 169 %> | |
| 53 | 170 <%= other_user.name_html() %> |
| 171 <span online="<%= other_user.id %>"></span> | |
| 83 | 172 <% |
| 173 end | |
| 174 %> | |
| 53 | 175 <span unread="<%=unread%>"><%=unread%></span> |
| 176 </div> | |
| 15 | 177 <% |
| 178 end | |
| 179 end | |
| 180 | |
| 16 | 181 function Shared.http_push_to_users(user_ids,message) |
| 182 local base = base_url().."/user/" | |
| 183 for _, user_id in ipairs(user_ids) do | |
| 184 local url = base..user_id | |
| 185 Http.push(url,message) | |
| 186 end | |
| 187 end | |
| 188 | |
| 73 | 189 Shared.compressed = {compressed=true} |
| 190 | |
| 117 | 191 function Shared.add_with(path) |
| 192 local with = Http.request.parameters.with | |
| 193 if with ~= nil then | |
| 194 with = to_list(with) | |
| 195 local t = {} | |
| 196 for _, email in ipairs(with) do | |
| 197 t[#t+1] = "with="..url_encode(email) | |
| 198 end | |
| 199 path = path..(contains(path,"?") and "&" or "?")..concat(t,"&") | |
| 200 end | |
| 201 return path | |
| 202 end | |
| 203 | |
| 204 function Shared.hidden_with() | |
| 205 local with = Http.request.parameters.with | |
| 206 with = to_list(with) | |
| 207 for _, email in ipairs(with) do %> | |
| 208 <input type=hidden name=with value="<%=html_encode(email)%>"> | |
| 209 <% end | |
| 210 end | |
| 211 | |
| 0 | 212 return Shared |
