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