xonecas.io
8058
Ir

irvc / commit 83a6252

Merge pull request #7 from sacenox/jm/feat

typing indicators and link previews

Jason Moiron2026-10-20 83a6252e00527a66763a85d4693424be5f1192ac parent f25f378parent 5204197

19 files changed

This diff is too big to show in full. The first 1000 lines are below, and every file is listed with its counts.

channels.go+8−2
@@ -323,7 +323,7 @@ func (s *Server) handleMessages(c *gin.Context) {
323323 return
324324 }
325325 rows, err := s.db.Query(
326- `SELECT m.id, m.channel_id, m.user_id, u.username, m.body, m.created_at, COALESCE(m.edited_at, 0)
326+ `SELECT m.id, m.channel_id, m.user_id, u.username, m.body, m.created_at, COALESCE(m.edited_at, 0), m.preview
327327 FROM messages m JOIN users u ON u.id = m.user_id
328328 WHERE m.channel_id = ? ORDER BY m.id DESC LIMIT 200`,
329329 channelID,
@@ -336,10 +336,12 @@ func (s *Server) handleMessages(c *gin.Context) {
336336 messages := []Message{}
337337 for rows.Next() {
338338 var m Message
339- if err := rows.Scan(&m.ID, &m.ChannelID, &m.UserID, &m.Username, &m.Body, &m.CreatedAt, &m.EditedAt); err != nil {
339+ var preview sql.NullString
340+ if err := rows.Scan(&m.ID, &m.ChannelID, &m.UserID, &m.Username, &m.Body, &m.CreatedAt, &m.EditedAt, &preview); err != nil {
340341 c.JSON(http.StatusInternalServerError, gin.H{"error": "db"})
341342 return
342343 }
344+ m.Preview = decodePreview(preview.String)
343345 messages = append(messages, m)
344346 }
345347 // reverse to chronological order
@@ -451,6 +453,7 @@ func (s *Server) handleSendMessage(c *gin.Context) {
451453 id, _ := res.LastInsertId()
452454 msg := Message{ID: id, ChannelID: channelID, UserID: user.ID, Username: user.Username, Body: body, CreatedAt: now}
453455 s.notifyChannel(chType, chName, event{Type: "message", Message: &msg})
456+ go s.refreshPreview(id)
454457 c.JSON(http.StatusOK, msg)
455458 }
456459
@@ -488,9 +491,11 @@ func (s *Server) handleEditMessage(c *gin.Context) {
488491 }
489492 m.Username = user.Username
490493 m.Reactions = s.messageReactions(m.ID)
494+ m.Preview = s.messagePreview(m.ID)
491495 var chType, chName string
492496 s.db.QueryRow(`SELECT type, name FROM channels WHERE id = ?`, m.ChannelID).Scan(&chType, &chName)
493497 s.notifyChannel(chType, chName, event{Type: "message_edit", Message: &m})
498+ go s.refreshPreview(m.ID)
494499 c.JSON(http.StatusOK, m)
495500 }
496501
@@ -553,6 +558,7 @@ func (s *Server) handleToggleReaction(c *gin.Context) {
553558 }
554559 }
555560 m.Reactions = s.messageReactions(m.ID)
561+ m.Preview = s.messagePreview(m.ID)
556562 s.notifyChannel(chType, chName, event{Type: "message_edit", Message: &m})
557563 c.JSON(http.StatusOK, m)
558564 }
db.go+2−0
@@ -33,6 +33,7 @@ type Message struct {
3333 CreatedAt int64 `json:"createdAt"`
3434 EditedAt int64 `json:"editedAt,omitempty"`
3535 Reactions map[string][]string `json:"reactions,omitempty"`
36+ Preview *Preview `json:"preview,omitempty"`
3637 }
3738
3839 func OpenDB(path string) *sql.DB {
@@ -106,6 +107,7 @@ func migrate(db *sql.DB) {
106107 log.Fatalf("migrate: %v", err)
107108 }
108109 db.Exec(`ALTER TABLE messages ADD COLUMN edited_at INTEGER`)
110+ db.Exec(`ALTER TABLE messages ADD COLUMN preview TEXT`)
109111 db.Exec(`ALTER TABLE channels ADD COLUMN permanent INTEGER NOT NULL DEFAULT 0`)
110112 db.Exec(`UPDATE channels SET permanent = 1 WHERE name = 'general' AND type IN ('text', 'voice')`)
111113 var n int
go.mod+1−1
@@ -6,6 +6,7 @@ require (
66 github.com/gin-gonic/gin v1.12.0
77 github.com/gorilla/websocket v1.5.3
88 golang.org/x/crypto v0.58.0
9+ golang.org/x/net v0.60.0
910 modernc.org/sqlite v1.60.1
1011 )
1112
@@ -38,7 +39,6 @@ require (
3839 github.com/ugorji/go/codec v1.3.1 // indirect
3940 go.mongodb.org/mongo-driver/v2 v2.5.0 // indirect
4041 golang.org/x/arch v0.22.0 // indirect
41- golang.org/x/net v0.60.0 // indirect
4242 golang.org/x/sys v0.49.0 // indirect
4343 golang.org/x/text v0.43.0 // indirect
4444 google.golang.org/protobuf v1.36.10 // indirect
main.go+6−4
@@ -15,9 +15,10 @@ import (
1515 )
1616
1717 type Server struct {
18- cfg *Config
19- db *sql.DB
20- hub *hub
18+ cfg *Config
19+ db *sql.DB
20+ hub *hub
21+ previews *previewer
2122 }
2223
2324 // usage prints out human readable usage for fs
@@ -116,7 +117,7 @@ func runServer(cfg *Config, db *sql.DB) {
116117 if err := os.MkdirAll(cfg.MediaDir, 0o755); err != nil {
117118 log.Fatalf("media dir: %v", err)
118119 }
119- s := &Server{cfg: cfg, db: db, hub: newHub(db)}
120+ s := &Server{cfg: cfg, db: db, hub: newHub(db), previews: newPreviewer(cfg.MediaDir, false)}
120121
121122 r := gin.Default()
122123 loginLimiter := newRateLimiter(10, time.Minute)
@@ -140,6 +141,7 @@ func runServer(cfg *Config, db *sql.DB) {
140141 authed.POST("/channels/:id/read", s.handleMarkRead)
141142 authed.PATCH("/messages/:id", s.handleEditMessage)
142143 authed.DELETE("/messages/:id", s.handleDeleteMessage)
144+ authed.DELETE("/messages/:id/preview", s.handleDeletePreview)
143145 authed.POST("/messages/:id/reactions", s.handleToggleReaction)
144146 authed.POST("/channels/:id/voice", s.handleVoiceToken)
145147 authed.POST("/channels/:id/ring", s.handleRing(true))
preview.go+468−0
@@ -0,0 +1,468 @@
1+package main
2+
3+import (
4+ "context"
5+ "database/sql"
6+ "encoding/json"
7+ "errors"
8+ "io"
9+ "mime"
10+ "net"
11+ "net/http"
12+ "net/url"
13+ "os"
14+ "path/filepath"
15+ "regexp"
16+ "strconv"
17+ "strings"
18+ "syscall"
19+ "time"
20+ "unicode"
21+ "unicode/utf8"
22+
23+ "github.com/gin-gonic/gin"
24+ "golang.org/x/net/html"
25+ "golang.org/x/net/html/charset"
26+)
27+
28+// Link previews ("cards"): the first link in a message is fetched once, by the
29+// server, and the Open Graph data found in its <head> is stored with the
30+// message. Clients never contact the linked site, and the thumbnail is copied
31+// into our own media store so viewers do not leak their address to it either.
32+
33+const (
34+ // maxPreviewBytes bounds how much of a page we will read looking for the
35+ // <head>. Pages are read lazily and abandoned at </head>, so a link to a
36+ // huge file costs at most this much, and usually nothing: non-HTML content
37+ // types are rejected before the body is touched.
38+ maxPreviewBytes = 4 << 20
39+ maxPreviewImage = 2 << 20
40+ previewTimeout = 10 * time.Second
41+ maxRedirects = 3
42+)
43+
44+// Preview kinds. Only "card" is rendered; the rest are recorded so that we do
45+// not fetch the same link again.
46+const (
47+ kindCard = "card"
48+ kindImage = "image" // the link is an image
49+ kindOther = "other" // not HTML (a download, a PDF, ...) or unreadable
50+ kindNone = "none" // HTML, but without Open Graph data
51+)
52+
53+type Preview struct {
54+ URL string `json:"url"`
55+ Kind string `json:"kind"`
56+ Title string `json:"title,omitempty"`
57+ Description string `json:"description,omitempty"`
58+ Image string `json:"image,omitempty"`
59+ Site string `json:"site,omitempty"`
60+}
61+
62+var (
63+ imageMarkdown = regexp.MustCompile(`!\[[^\]]*\]\([^)]*\)`)
64+ linkPattern = regexp.MustCompile(`https?://[^\s<>]+`)
65+)
66+
67+// firstLink returns the first http(s) URL in a message body, or "". Markdown
68+// image embeds (uploads, gifs) are skipped: they are already shown inline.
69+func firstLink(body string) string {
70+ loc := linkPattern.FindString(imageMarkdown.ReplaceAllString(body, " "))
71+ // Trailing punctuation belongs to the sentence, not the URL. A closing
72+ // paren is kept only when the URL has a matching opening one.
73+ for loc != "" {
74+ last := loc[len(loc)-1]
75+ switch {
76+ case strings.IndexByte(".,;:!?'\"]}*_", last) >= 0:
77+ case last == ')' && strings.Count(loc, ")") > strings.Count(loc, "("):
78+ default:
79+ u, err := url.Parse(loc)
80+ if err != nil || u.Host == "" {
81+ return ""
82+ }
83+ return loc
84+ }
85+ loc = loc[:len(loc)-1]
86+ }
87+ return ""
88+}
89+
90+var errBlockedAddr = errors.New("address not allowed")
91+
92+// Ranges that are not covered by the net.IP predicates but are not the public
93+// internet either.
94+var extraBlocked = func() []*net.IPNet {
95+ var out []*net.IPNet
96+ for _, c := range []string{"100.64.0.0/10", "198.18.0.0/15", "192.0.0.0/24", "240.0.0.0/4", "0.0.0.0/8"} {
97+ _, n, _ := net.ParseCIDR(c)
98+ out = append(out, n)
99+ }
100+ return out
101+}()
102+
103+// publicIP reports whether ip is somewhere a user-supplied link may send us.
104+func publicIP(ip net.IP) bool {
105+ if ip == nil || ip.IsLoopback() || ip.IsPrivate() || ip.IsUnspecified() ||
106+ ip.IsLinkLocalUnicast() || ip.IsLinkLocalMulticast() || ip.IsMulticast() {
107+ return false
108+ }
109+ for _, n := range extraBlocked {
110+ if n.Contains(ip) {
111+ return false
112+ }
113+ }
114+ return true
115+}
116+
117+type previewer struct {
118+ client *http.Client
119+ mediaDir string
120+ sem chan struct{}
121+}
122+
123+// newPreviewer builds the fetcher. The address check runs inside the dialer,
124+// on the IP actually being connected to, so it covers redirects and DNS tricks
125+// (rebinding) that checking the URL up front would miss. allowPrivate exists
126+// for tests that talk to a local server.
127+func newPreviewer(mediaDir string, allowPrivate bool) *previewer {
128+ dialer := &net.Dialer{
129+ Timeout: 5 * time.Second,
130+ Control: func(_, address string, _ syscall.RawConn) error {
131+ host, _, err := net.SplitHostPort(address)
132+ if err != nil || (!allowPrivate && !publicIP(net.ParseIP(host))) {
133+ return errBlockedAddr
134+ }
135+ return nil
136+ },
137+ }
138+ return &previewer{
139+ mediaDir: mediaDir,
140+ sem: make(chan struct{}, 4),
141+ client: &http.Client{
142+ Timeout: previewTimeout,
143+ Transport: &http.Transport{
144+ Proxy: nil, // a proxy would bypass the dial check
145+ DialContext: dialer.DialContext,
146+ TLSHandshakeTimeout: 5 * time.Second,
147+ ResponseHeaderTimeout: 5 * time.Second,
148+ MaxResponseHeaderBytes: 64 << 10,
149+ DisableKeepAlives: true,
150+ },
151+ CheckRedirect: func(req *http.Request, via []*http.Request) error {
152+ if len(via) > maxRedirects {
153+ return errors.New("too many redirects")
154+ }
155+ if req.URL.Scheme != "http" && req.URL.Scheme != "https" {
156+ return errors.New("bad redirect scheme")
157+ }
158+ return nil
159+ },
160+ },
161+ }
162+}
163+
164+func (p *previewer) get(ctx context.Context, rawURL, accept string) (*http.Response, error) {
165+ req, err := http.NewRequestWithContext(ctx, http.MethodGet, rawURL, nil)
166+ if err != nil {
167+ return nil, err
168+ }
169+ req.Header.Set("User-Agent", "Mozilla/5.0 (compatible; irvc-link-preview)")
170+ req.Header.Set("Accept", accept)
171+ req.Header.Set("Accept-Language", "en")
172+ resp, err := p.client.Do(req)
173+ if err != nil {
174+ return nil, err
175+ }
176+ if resp.StatusCode < 200 || resp.StatusCode > 299 {
177+ resp.Body.Close()
178+ return nil, errors.New("status " + resp.Status)
179+ }
180+ return resp, nil
181+}
182+
183+// fetch builds the preview for link. It returns nil only when the link could
184+// not be fetched at all, so a transient failure is retried on the next edit.
185+func (p *previewer) fetch(ctx context.Context, link string) *Preview {
186+ resp, err := p.get(ctx, link, "text/html,application/xhtml+xml;q=0.9,*/*;q=0.1")
187+ if err != nil {
188+ return nil
189+ }
190+ defer resp.Body.Close()
191+ out := &Preview{URL: link, Kind: kindOther}
192+
193+ ctype := resp.Header.Get("Content-Type")
194+ media, _, _ := mime.ParseMediaType(ctype)
195+ switch {
196+ case strings.HasPrefix(media, "image/"):
197+ out.Kind = kindImage
198+ return out
199+ case media != "text/html" && media != "application/xhtml+xml":
200+ return out
201+ }
202+
203+ og := parseOpenGraph(io.LimitReader(resp.Body, maxPreviewBytes), ctype)
204+ if og["title"] == "" && og["description"] == "" && og["image"] == "" {
205+ out.Kind = kindNone
206+ return out
207+ }
208+ out.Kind = kindCard
209+ out.Title = clip(og["title"], 200)
210+ out.Description = clip(og["description"], 300)
211+ out.Site = clip(og["site_name"], 80)
212+ if img := og["image"]; img != "" && resp.Request != nil {
213+ if ref, err := resp.Request.URL.Parse(img); err == nil && (ref.Scheme == "http" || ref.Scheme == "https") {
214+ out.Image = p.cacheImage(ctx, ref.String())
215+ }
216+ }
217+ return out
218+}
219+
220+// parseOpenGraph reads og:* meta tags from the document head, stopping at
221+// </head> or <body> (or the end of r) rather than parsing the whole page.
222+func parseOpenGraph(r io.Reader, contentType string) map[string]string {
223+ og := map[string]string{}
224+ if cr, err := charset.NewReader(r, contentType); err == nil {
225+ r = cr
226+ }
227+ z := html.NewTokenizer(r)
228+ for {
229+ switch z.Next() {
230+ case html.ErrorToken:
231+ return og
232+ case html.EndTagToken:
233+ if name, _ := z.TagName(); string(name) == "head" {
234+ return og
235+ }
236+ case html.StartTagToken, html.SelfClosingTagToken:
237+ name, hasAttr := z.TagName()
238+ switch string(name) {
239+ case "body":
240+ return og
241+ case "meta":
242+ if !hasAttr {
243+ continue
244+ }
245+ var prop, content string
246+ for {
247+ k, v, more := z.TagAttr()
248+ switch string(k) {
249+ case "property", "name":
250+ if prop == "" {
251+ prop = strings.ToLower(string(v))
252+ }
253+ case "content":
254+ content = string(v)
255+ }
256+ if !more {
257+ break
258+ }
259+ }
260+ if key, ok := strings.CutPrefix(prop, "og:"); ok && og[key] == "" {
261+ og[key] = cleanText(content)
262+ }
263+ }
264+ }
265+ }
266+}
267+
268+var entity = regexp.MustCompile(`&(#[0-9]+|#[xX][0-9a-fA-F]+|[A-Za-z][A-Za-z0-9]*);`)
269+
270+// cleanText turns a meta tag value into plain text for display. The tokenizer
271+// has already decoded the attribute once (so &amp; is & and &#8217; is ’), but
272+// plenty of sites encode twice (&amp;amp;), which would show up as a literal
273+// "&amp;" in the card. So: decode once more if entities remain, drop control
274+// characters and collapse whitespace. The result is plain text, never HTML;
275+// the client renders it as a text node, so a title containing "<b>" displays as
276+// those characters rather than being interpreted.
277+func cleanText(s string) string {
278+ s = strings.ToValidUTF8(s, "")
279+ if entity.MatchString(s) {
280+ s = html.UnescapeString(s)
281+ }
282+ s = strings.Map(func(r rune) rune {
283+ if unicode.IsControl(r) && !unicode.IsSpace(r) {
284+ return -1
285+ }
286+ return r
287+ }, s)
288+ return strings.Join(strings.Fields(s), " ")
289+}
290+
291+func clip(s string, n int) string {
292+ if utf8.RuneCountInString(s) <= n {
293+ return s
294+ }
295+ r := []rune(s)
296+ return strings.TrimSpace(string(r[:n-1])) + "…"
297+}
298+
299+// cacheImage downloads a preview thumbnail into the media store and returns
300+// its local URL, or "" if it is not a small raster image.
301+func (p *previewer) cacheImage(ctx context.Context, src string) string {
302+ resp, err := p.get(ctx, src, "image/*")
303+ if err != nil {
304+ return ""
305+ }
306+ defer resp.Body.Close()
307+ data, err := io.ReadAll(io.LimitReader(resp.Body, maxPreviewImage+1))
308+ if err != nil || len(data) > maxPreviewImage || len(data) == 0 {
309+ return ""
310+ }
311+ ext, ok := imageExt[http.DetectContentType(data)]
312+ if !ok {
313+ return ""
314+ }
315+ name := randToken() + ext
316+ if err := os.WriteFile(filepath.Join(p.mediaDir, name), data, 0o644); err != nil {
317+ return ""
318+ }
319+ return "/api/media/" + name
320+}
321+
322+func (s *Server) messagePreview(msgID int64) *Preview {
323+ var raw sql.NullString
324+ if s.db.QueryRow(`SELECT preview FROM messages WHERE id = ?`, msgID).Scan(&raw) != nil {
325+ return nil
326+ }
327+ return decodePreview(raw.String)
328+}
329+
330+func decodePreview(raw string) *Preview {
331+ if raw == "" {
332+ return nil
333+ }
334+ var p Preview
335+ if json.Unmarshal([]byte(raw), &p) != nil {
336+ return nil
337+ }
338+ return &p
339+}
340+
341+// refreshPreview brings a message's stored preview in line with its current
342+// body. It runs in the background after a message is sent or edited, and tells
343+// clients (as an edit event) only when a card appears or goes away.
344+func (s *Server) refreshPreview(msgID int64) {
345+ s.previews.sem <- struct{}{}
346+ defer func() { <-s.previews.sem }()
347+
348+ var body string
349+ var raw sql.NullString
350+ var channelID int64
351+ if s.db.QueryRow(`SELECT body, preview, channel_id FROM messages WHERE id = ?`, msgID).Scan(&body, &raw, &channelID) != nil {
352+ return
353+ }
354+ old := decodePreview(raw.String)
355+ link := firstLink(body)
356+
357+ var next *Preview
358+ if link != "" {
359+ if old != nil && old.URL == link {
360+ return
361+ }
362+ ctx, cancel := context.WithTimeout(context.Background(), previewTimeout)
363+ defer cancel()
364+ if next = s.previews.fetch(ctx, link); next == nil {
365+ return
366+ }
367+ }
368+
369+ stored := ""
370+ if next != nil {
371+ b, _ := json.Marshal(next)
372+ stored = string(b)
373+ }
374+ // Only write if the preview is still what we started from: a removal (or
375+ // another refresh) that landed while we were fetching wins.
376+ res, err := s.db.Exec(
377+ `UPDATE messages SET preview = NULLIF(?, '') WHERE id = ? AND COALESCE(preview, '') = ?`,
378+ stored, msgID, raw.String,
379+ )
380+ if err != nil {
381+ return
382+ }
383+ if n, _ := res.RowsAffected(); n == 0 {
384+ return
385+ }
386+ if (old != nil && old.Kind == kindCard) || (next != nil && next.Kind == kindCard) {
387+ s.broadcastMessage(msgID, channelID)
388+ }
389+}
390+
391+// broadcastMessage re-reads a message and sends it to the channel's audience as
392+// an edit, so clients replace their copy.
393+func (s *Server) broadcastMessage(msgID, channelID int64) {
394+ if m, chType, chName, ok := s.loadMessage(msgID, channelID); ok {
395+ s.notifyChannel(chType, chName, event{Type: "message_edit", Message: &m})
396+ }
397+}
398+
399+// loadMessage reads a message as clients see it, plus its channel's type and
400+// name for routing.
401+func (s *Server) loadMessage(msgID, channelID int64) (m Message, chType, chName string, ok bool) {
402+ var raw sql.NullString
403+ err := s.db.QueryRow(
404+ `SELECT m.id, m.channel_id, m.user_id, u.username, m.body, m.created_at, COALESCE(m.edited_at, 0), m.preview
405+ FROM messages m JOIN users u ON u.id = m.user_id WHERE m.id = ?`, msgID,
406+ ).Scan(&m.ID, &m.ChannelID, &m.UserID, &m.Username, &m.Body, &m.CreatedAt, &m.EditedAt, &raw)
407+ if err != nil {
408+ return m, "", "", false
409+ }
410+ m.Preview = decodePreview(raw.String)
411+ m.Reactions = s.messageReactions(m.ID)
412+ s.db.QueryRow(`SELECT type, name FROM channels WHERE id = ?`, channelID).Scan(&chType, &chName)
413+ return m, chType, chName, true
414+}
415+
416+var errNoPreview = errors.New("no preview")
417+
418+// removePreview deletes the card from a message its author owns. The link is
419+// remembered as "none" so the same link is not fetched again on a later edit,
420+// and the cached thumbnail is deleted along with it. It returns the message's
421+// channel for notification.
422+func (s *Server) removePreview(msgID, userID int64) (channelID int64, err error) {
423+ var raw sql.NullString
424+ if err := s.db.QueryRow(
425+ `SELECT channel_id, preview FROM messages WHERE id = ? AND user_id = ?`, msgID, userID,
426+ ).Scan(&channelID, &raw); err != nil {
427+ return 0, err
428+ }
429+ old := decodePreview(raw.String)
430+ if old == nil || old.Kind != kindCard {
431+ return channelID, errNoPreview
432+ }
433+ b, _ := json.Marshal(Preview{URL: old.URL, Kind: kindNone})
434+ if _, err := s.db.Exec(`UPDATE messages SET preview = ? WHERE id = ?`, string(b), msgID); err != nil {
435+ return 0, err
436+ }
437+ if name, ok := strings.CutPrefix(old.Image, "/api/media/"); ok && mediaName.MatchString(name) {
438+ os.Remove(filepath.Join(s.cfg.MediaDir, name))
439+ }
440+ return channelID, nil
441+}
442+
443+func (s *Server) handleDeletePreview(c *gin.Context) {
444+ msgID, err := strconv.ParseInt(c.Param("id"), 10, 64)
445+ if err != nil {
446+ c.JSON(http.StatusBadRequest, gin.H{"error": "bad message"})
447+ return
448+ }
449+ user := c.MustGet("user").(User)
450+ channelID, err := s.removePreview(msgID, user.ID)
451+ switch {
452+ case errors.Is(err, sql.ErrNoRows):
453+ c.JSON(http.StatusNotFound, gin.H{"error": "message not found"})
454+ return
455+ case errors.Is(err, errNoPreview):
456+ // Already gone: nothing to do, and the caller's goal is met.
457+ case err != nil:
458+ c.JSON(http.StatusInternalServerError, gin.H{"error": "db"})
459+ return
460+ }
461+ m, chType, chName, ok := s.loadMessage(msgID, channelID)
462+ if !ok {
463+ c.JSON(http.StatusInternalServerError, gin.H{"error": "db"})
464+ return
465+ }
466+ s.notifyChannel(chType, chName, event{Type: "message_edit", Message: &m})
467+ c.JSON(http.StatusOK, m)
468+}
preview_test.go+132−0
@@ -0,0 +1,132 @@
1+package main
2+
3+import (
4+ "context"
5+ "net"
6+ "net/http"
7+ "net/http/httptest"
8+ "os"
9+ "strings"
10+ "testing"
11+ "time"
12+)
13+
14+func TestFirstLink(t *testing.T) {
15+ cases := map[string]string{
16+ "look https://example.com/a?b=1.": "https://example.com/a?b=1",
17+ "(see https://example.com/x)": "https://example.com/x",
18+ "https://en.wikipedia.org/wiki/Go_(language) neat": "https://en.wikipedia.org/wiki/Go_(language)",
19+ "![gif](https://media.giphy.com/a.gif)\nno link": "",
20+ "![gif](https://x.test/a.gif) then http://b.test/ok": "http://b.test/ok",
21+ "first https://one.test second https://two.test": "https://one.test",
22+ "just text, ftp://nope.test": "",
23+ }
24+ for in, want := range cases {
25+ if got := firstLink(in); got != want {
26+ t.Errorf("firstLink(%q) = %q, want %q", in, got, want)
27+ }
28+ }
29+}
30+
31+func TestPublicIP(t *testing.T) {
32+ for ip, want := range map[string]bool{
33+ "8.8.8.8": true, "2606:4700::1111": true,
34+ "127.0.0.1": false, "::1": false, "10.1.2.3": false, "192.168.0.9": false,
35+ "172.16.5.5": false, "169.254.169.254": false, "100.64.0.1": false,
36+ "0.0.0.0": false, "fe80::1": false, "fd00::1": false, "::ffff:127.0.0.1": false,
37+ } {
38+ if got := publicIP(net.ParseIP(ip)); got != want {
39+ t.Errorf("publicIP(%s) = %v, want %v", ip, got, want)
40+ }
41+ }
42+}
43+
44+func TestPreviewFetch(t *testing.T) {
45+ png := []byte("\x89PNG\r\n\x1a\n" + strings.Repeat("x", 64))
46+ mux := http.NewServeMux()
47+ mux.HandleFunc("/page", func(w http.ResponseWriter, r *http.Request) {
48+ w.Header().Set("Content-Type", "text/html; charset=utf-8")
49+ w.Write([]byte(`<html><head><title>ignored</title>
50+ <meta property="og:title" content=" Hello World ">
51+ <meta property="og:description" content="A &amp; B">
52+ <meta name="og:site_name" content="Example">
53+ <meta property="og:image" content="/pic.png">
54+ </head><body><meta property="og:title" content="late"></body></html>`))
55+ })
56+ mux.HandleFunc("/pic.png", func(w http.ResponseWriter, r *http.Request) {
57+ w.Header().Set("Content-Type", "image/png")
58+ w.Write(png)
59+ })
60+ mux.HandleFunc("/plain", func(w http.ResponseWriter, r *http.Request) {
61+ w.Header().Set("Content-Type", "text/html")
62+ w.Write([]byte(`<html><head><title>no og</title></head><body>hi</body></html>`))
63+ })
64+ mux.HandleFunc("/file", func(w http.ResponseWriter, r *http.Request) {
65+ w.Header().Set("Content-Type", "application/zip")
66+ w.Write([]byte("PK"))
67+ })
68+ mux.HandleFunc("/huge", func(w http.ResponseWriter, r *http.Request) {
69+ w.Header().Set("Content-Type", "text/html")
70+ w.Write([]byte("<html><head>"))
71+ // An endless head with no og tags: reading must stop at the cap.
72+ junk := []byte(strings.Repeat("<!-- x -->", 1000))
73+ for {
74+ if _, err := w.Write(junk); err != nil {
75+ return
76+ }
77+ }
78+ })
79+ srv := httptest.NewServer(mux)
80+ defer srv.Close()
81+
82+ dir := t.TempDir()
83+ p := newPreviewer(dir, true)
84+ ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
85+ defer cancel()
86+
87+ card := p.fetch(ctx, srv.URL+"/page")
88+ if card == nil || card.Kind != kindCard || card.Title != "Hello World" ||
89+ card.Description != "A & B" || card.Site != "Example" {
90+ t.Fatalf("bad card: %+v", card)
91+ }
92+ if !strings.HasPrefix(card.Image, "/api/media/") || !strings.HasSuffix(card.Image, ".png") {
93+ t.Fatalf("thumbnail should be cached locally, got %q", card.Image)
94+ }
95+ if _, err := os.Stat(dir + "/" + strings.TrimPrefix(card.Image, "/api/media/")); err != nil {
96+ t.Fatalf("cached image missing: %v", err)
97+ }
98+
99+ for path, kind := range map[string]string{"/pic.png": kindImage, "/file": kindOther, "/plain": kindNone, "/huge": kindNone} {
100+ if got := p.fetch(ctx, srv.URL+path); got == nil || got.Kind != kind {
101+ t.Errorf("%s: got %+v, want kind %s", path, got, kind)
102+ }
103+ }
104+
105+ if got := newPreviewer(dir, false).fetch(ctx, srv.URL+"/page"); got != nil {
106+ t.Errorf("loopback must be blocked by default, got %+v", got)
107+ }
108+}
109+
110+// Meta values are decoded to plain text: single and double encoded entities
111+// both come out as the characters they stand for, and markup stays literal.
112+func TestParseOpenGraphEntities(t *testing.T) {
113+ doc := `<head>
114+ <meta property="og:title" content="Tom &amp;amp; Jerry &#8217;s &quot;best&quot;">
115+ <meta property="og:description" content="1 &lt; 2 &amp;&amp; &lt;b&gt;bold&lt;/b&gt;">
116+ <meta property="og:site_name" content="Plain &amp; simple">
117+ <meta property="og:image" content="a&#0;b&#7;c">
118+ </head>`
119+ og := parseOpenGraph(strings.NewReader(doc), "text/html; charset=utf-8")
120+ for key, want := range map[string]string{
121+ "title": `Tom & Jerry ’s "best"`,
122+ "description": "1 < 2 && <b>bold</b>",
123+ "site_name": "Plain & simple",
124+ } {
125+ if og[key] != want {
126+ t.Errorf("og:%s = %q, want %q", key, og[key], want)
127+ }
128+ }
129+ if strings.ContainsAny(og["image"], "\x00\x07") {
130+ t.Errorf("control characters survived: %q", og["image"])
131+ }
132+}
refresh_test.go+92−0
@@ -0,0 +1,92 @@
1+package main
2+
3+import (
4+ "net/http"
5+ "net/http/httptest"
6+ "os"
7+ "path/filepath"
8+ "strings"
9+ "testing"
10+ "time"
11+)
12+
13+// A card that appears after a message was sent must reach connected clients
14+// as an edit event carrying the preview.
15+func TestRefreshPreviewBroadcasts(t *testing.T) {
16+ page := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
17+ w.Header().Set("Content-Type", "text/html")
18+ w.Write([]byte(`<head><meta property="og:title" content="Hi"></head>`))
19+ }))
20+ defer page.Close()
21+
22+ dir := t.TempDir()
23+ db := OpenDB(filepath.Join(dir, "t.db"))
24+ s := &Server{db: db, hub: newHub(db), previews: newPreviewer(dir, true)}
25+ db.Exec(`INSERT INTO users (id, username, pass_hash, created_at) VALUES (1, 'a', 'x', 0)`)
26+ var ch int64
27+ db.QueryRow(`SELECT id FROM channels WHERE type = 'text' LIMIT 1`).Scan(&ch)
28+ res, _ := db.Exec(`INSERT INTO messages (channel_id, user_id, body, created_at) VALUES (?, 1, ?, 0)`, ch, "see "+page.URL)
29+ id, _ := res.LastInsertId()
30+ viewer := testClient(s.hub, 9, "viewer")
31+
32+ s.refreshPreview(id)
33+
34+ select {
35+ case data := <-viewer.send:
36+ if ev := string(data); !strings.Contains(ev, `"message_edit"`) || !strings.Contains(ev, `"kind":"card"`) {
37+ t.Fatalf("unexpected event %s", ev)
38+ }
39+ case <-time.After(time.Second):
40+ t.Fatal("no event broadcast for the new card")
41+ }
42+}
43+
44+// Only the author can remove a card; removal sticks (the same link is not
45+// fetched again), deletes the cached thumbnail, and a slow fetch that finishes
46+// afterwards cannot bring the card back.
47+func TestRemovePreview(t *testing.T) {
48+ hits := 0
49+ page := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
50+ hits++
51+ w.Header().Set("Content-Type", "text/html")
52+ w.Write([]byte(`<head><meta property="og:title" content="Hi"></head>`))
53+ }))
54+ defer page.Close()
55+
56+ dir := t.TempDir()
57+ db := OpenDB(filepath.Join(dir, "t.db"))
58+ s := &Server{cfg: &Config{MediaDir: dir}, db: db, hub: newHub(db), previews: newPreviewer(dir, true)}
59+ db.Exec(`INSERT INTO users (id, username, pass_hash, created_at) VALUES (1, 'a', 'x', 0), (2, 'b', 'x', 0)`)
60+ var ch int64
61+ db.QueryRow(`SELECT id FROM channels WHERE type = 'text' LIMIT 1`).Scan(&ch)
62+ res, _ := db.Exec(`INSERT INTO messages (channel_id, user_id, body, created_at) VALUES (?, 1, ?, 0)`, ch, "see "+page.URL)
63+ id, _ := res.LastInsertId()
64+
65+ s.refreshPreview(id)
66+ if p := s.messagePreview(id); p == nil || p.Kind != kindCard {
67+ t.Fatalf("expected a card, got %+v", p)
68+ }
69+
70+ thumb := "thumb.png"
71+ os.WriteFile(filepath.Join(dir, thumb), []byte("x"), 0o644)
72+ db.Exec(`UPDATE messages SET preview = ? WHERE id = ?`, `{"url":"`+page.URL+`","kind":"card","title":"Hi","image":"/api/media/`+thumb+`"}`, id)
73+
74+ if _, err := s.removePreview(id, 2); err == nil {
75+ t.Fatal("another user must not be able to remove the card")
76+ }
77+ if _, err := s.removePreview(id, 1); err != nil {
78+ t.Fatal(err)
79+ }
80+ if p := s.messagePreview(id); p == nil || p.Kind != kindNone || p.URL != page.URL {
81+ t.Fatalf("removed card should be remembered as none, got %+v", p)
82+ }
83+ if _, err := os.Stat(filepath.Join(dir, thumb)); err == nil {
84+ t.Fatal("cached thumbnail should be deleted")
85+ }
86+
87+ before := hits
88+ s.refreshPreview(id)
89+ if hits != before || s.messagePreview(id).Kind != kindNone {
90+ t.Fatal("the removed link must not be fetched or restored")
91+ }
92+}
typing_test.go+66−0
@@ -0,0 +1,66 @@
1+package main
2+
3+import (
4+ "path/filepath"
5+ "strings"
6+ "testing"
7+)
8+
9+func testClient(h *hub, id int64, name string) *client {
10+ c := &client{hub: h, userID: id, username: name, send: make(chan []byte, 8)}
11+ h.clients[id] = map[*client]bool{c: true}
12+ return c
13+}
14+
15+func got(c *client) bool { return len(c.send) > 0 }
16+
17+func drain(cs ...*client) {
18+ for _, c := range cs {
19+ for len(c.send) > 0 {
20+ <-c.send
21+ }
22+ }
23+}
24+
25+// Typing is relayed to everyone for a text channel, only to the two
26+// participants for a DM, and never faster than typingEvery per connection.
27+func TestTypingRelay(t *testing.T) {
28+ db := OpenDB(filepath.Join(t.TempDir(), "t.db"))
29+ h := newHub(db)
30+ a, b, outsider := testClient(h, 1, "a"), testClient(h, 2, "b"), testClient(h, 3, "c")
31+
32+ var text, dm int64
33+ db.QueryRow(`SELECT id FROM channels WHERE type = 'text' LIMIT 1`).Scan(&text)
34+ res, err := db.Exec(`INSERT INTO channels (type, name, position, created_at) VALUES ('dm', 'u:1:2', 0, 0)`)
35+ if err != nil {
36+ t.Fatal(err)
37+ }
38+ dm, _ = res.LastInsertId()
39+
40+ h.typing(a, text)
41+ if !got(a) || !got(b) || !got(outsider) {
42+ t.Fatal("text channel typing should reach everyone")
43+ }
44+ if ev := string(<-b.send); !strings.Contains(ev, `"username":"a"`) {
45+ t.Fatalf("unexpected event %s", ev)
46+ }
47+
48+ h.typing(a, text) // within typingEvery
49+ if got(b) {
50+ t.Fatal("typing events should be rate limited per connection")
51+ }
52+
53+ drain(a, b, outsider)
54+ a.typedAt = a.typedAt.Add(-2 * typingEvery)
55+ h.typing(a, dm)
56+ if !got(b) || got(outsider) {
57+ t.Fatal("DM typing should reach only its participants")
58+ }
59+
60+ outsider.typedAt = outsider.typedAt.Add(-2 * typingEvery)
61+ drain(a, b, outsider)
62+ h.typing(outsider, dm)
63+ if got(b) || got(a) {
64+ t.Fatal("a non-participant must not be able to type into a DM")
65+ }
66+}
web/src/components/Composer.tsx+6−3
@@ -20,12 +20,14 @@ export default function Composer({
2020 channel,
2121 users,
2222 onEditLast,
23- onCollapseImages,
23+ onCollapse,
24+ onTyping,
2425 }: {
2526 channel: Channel;
2627 users: UserSummary[];
2728 onEditLast: () => void;
28- onCollapseImages: () => void;
29+ onCollapse: () => void;
30+ onTyping: () => void;
2931 }) {
3032 const [body, setBody] = useState("");
3133 const [mention, setMention] = useState<{ query: string; start: number; index: number } | null>(
@@ -108,7 +110,7 @@ export default function Composer({
108110 if (!text) return;
109111 setBody("");
110112 setMention(null);
111- if (text === "/collapse") return onCollapseImages();
113+ if (text === "/collapse") return onCollapse();
112114 const gif = /^\/gif(?:\s+(.*))?$/.exec(text);
113115 if (gif) return void openGifs(gif[1]?.trim() ?? "");
114116 try {
@@ -238,6 +240,7 @@ export default function Composer({
238240 value={body}
239241 onChange={(e) => {
240242 setBody(e.target.value);
243+ if (e.target.value.trim()) onTyping();
241244 detectMention(e.target.value, e.target.selectionStart ?? e.target.value.length);
242245 }}
243246 onKeyDown={onKeyDown}
web/src/components/LinkPreview.tsx+103−0
@@ -0,0 +1,103 @@
1+import { ChevronDown, ChevronRight, X } from "lucide-react";
2+import { cn } from "cn";
3+import { IconButton } from "@/ui";
4+import { userColor } from "../lib/scheme";
5+import type { Preview } from "../lib/types";
6+
7+const host = (url: string) => {
8+ try {
9+ return new URL(url).hostname.replace(/^www\./, "");
10+ } catch {
11+ return url;
12+ }
13+};
14+
15+// LinkPreview is the Open Graph card for a message's first link. The server
16+// fetched it and cached the thumbnail, so nothing here contacts the linked site
17+// until the user clicks through. The header toggles it, like the image header.
18+export default function LinkPreview({
19+ preview,
20+ collapsed,
21+ onToggle,
22+ onRemove,
23+ pinned,
24+}: {
25+ preview: Preview;
26+ collapsed: boolean;
27+ onToggle: () => void;
28+ // Given only to the message's author: removes the card for everyone.
29+ onRemove?: () => void;
30+ // The message is selected: keep the remove button showing without a hover.
31+ pinned?: boolean;
32+}) {
33+ const site = preview.site || host(preview.url);
34+ return (
35+ <div className="pl-5 md:pl-8">
36+ <button
37+ type="button"
38+ onClick={onToggle}
39+ title={collapsed ? "show preview" : "hide preview"}
40+ className="flex max-w-full items-center gap-1 text-xs leading-6 text-faint hover:text-muted"
41+ >
42+ {collapsed ? (
43+ <ChevronRight className="size-3 shrink-0" />
44+ ) : (
45+ <ChevronDown className="size-3 shrink-0" />
46+ )}
47+ <span className="truncate">preview{collapsed && ` · ${site}`}</span>
48+ </button>
49+ {!collapsed && (
50+ <div className="mt-0.5 mb-1 flex items-start gap-1">
51+ <a
52+ href={preview.url}
53+ target="_blank"
54+ rel="noopener noreferrer nofollow"
55+ className="block min-w-0 max-w-md flex-1 overflow-hidden rounded-lg border border-line bg-surface transition-colors hover:border-accent/60"
56+ >
57+ {preview.image && (
58+ <img
59+ src={preview.image}
60+ alt=""
61+ loading="lazy"
62+ className="max-h-48 w-full object-cover"
63+ />
64+ )}
65+ <div className="space-y-0.5 px-3 py-2">
66+ <div className="truncate text-[0.7rem] text-faint">{site}</div>
67+ {preview.title && (
68+ // Skittles color when enabled (plain text color otherwise). Keyed on
69+ // the URL, so it varies from card to card but is stable for a given link.
70+ <div
71+ className="line-clamp-2 text-sm font-medium text-fg"
72+ style={{ color: userColor(preview.url, "var(--fg)") }}
73+ >
74+ {preview.title}
75+ </div>
76+ )}
77+ {preview.description && (
78+ <div className="line-clamp-3 text-xs text-muted">{preview.description}</div>
79+ )}
80+ </div>
81+ </a>
82+ {onRemove && (
83+ <IconButton
84+ label="remove preview"
85+ size="sm"
86+ className={cn(
87+ "text-danger hover:bg-danger/10 hover:text-danger",
88+ // Shown while the row is hovered, selected or has keyboard focus;
89+ // otherwise every card would carry an X.
Cut short: this commit's diff is over 1000 lines.
web/src/components/MessageActions.tsx+8−1
Cut short: this commit's diff is over 1000 lines.
web/src/components/MessageList.tsx+31−1
Cut short: this commit's diff is over 1000 lines.
web/src/lib/settings.ts+6−0
Cut short: this commit's diff is over 1000 lines.
web/src/lib/theme.ts+35−12
Cut short: this commit's diff is over 1000 lines.
web/src/lib/types.ts+13−1
Cut short: this commit's diff is over 1000 lines.
web/src/ui/Toggle.tsx+4−1
Cut short: this commit's diff is over 1000 lines.
web/src/views/Chat.tsx+126−6
Cut short: this commit's diff is over 1000 lines.
web/src/views/Settings.tsx+62−50
Cut short: this commit's diff is over 1000 lines.
ws.go+35−0
Cut short: this commit's diff is over 1000 lines.