Debian Codegraph @

event/reply.go

raw
1 // Copyright (c) 2020 Tulir Asokan
2 //
3 // This Source Code Form is subject to the terms of the Mozilla Public
4 // License, v. 2.0. If a copy of the MPL was not distributed with this
5 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
6
7 package event
8
9 import (
10 "fmt"
11 "regexp"
12 "strings"
13
14 "golang.org/x/net/html"
15
16 "maunium.net/go/mautrix/id"
17 )
18
19 var HTMLReplyFallbackRegex = regexp.MustCompile(`^<mx-reply>[\s\S]+?</mx-reply>`)
20
21 func TrimReplyFallbackHTML(html string) string {
22 return HTMLReplyFallbackRegex.ReplaceAllString(html, "")
23 }
24
25 func TrimReplyFallbackText(text string) string {
26 if !strings.HasPrefix(text, "> ") || !strings.Contains(text, "\n") {
27 return text
28 }
29
30 lines := strings.Split(text, "\n")
31 for len(lines) > 0 && strings.HasPrefix(lines[0], "> ") {
32 lines = lines[1:]
33 }
34 return strings.TrimSpace(strings.Join(lines, "\n"))
35 }
36
37 func (content *MessageEventContent) RemoveReplyFallback() {
38 if len(content.GetReplyTo()) > 0 && !content.replyFallbackRemoved {
39 if content.Format == FormatHTML {
40 content.FormattedBody = TrimReplyFallbackHTML(content.FormattedBody)
41 }
42 content.Body = TrimReplyFallbackText(content.Body)
43 content.replyFallbackRemoved = true
44 }
45 }
46
47 func (content *MessageEventContent) GetReplyTo() id.EventID {
48 if content.RelatesTo != nil && content.RelatesTo.Type == RelReply {
49 return content.RelatesTo.EventID
50 }
51 return ""
52 }
53
54 const ReplyFormat = `<mx-reply><blockquote><a href="https://matrix.to/#/%s/%s">In reply to</a> <a href="https://matrix.to/#/%s">%s</a><br>%s</blockquote></mx-reply>`
55
56 func (evt *Event) GenerateReplyFallbackHTML() string {
57 parsedContent, ok := evt.Content.Parsed.(*MessageEventContent)
58 if !ok {
59 return ""
60 }
61 parsedContent.RemoveReplyFallback()
62 body := parsedContent.FormattedBody
63 if len(body) == 0 {
64 body = strings.ReplaceAll(html.EscapeString(parsedContent.Body), "\n", "<br/>")
65 }
66
67 senderDisplayName := evt.Sender
68
69 return fmt.Sprintf(ReplyFormat, evt.RoomID, evt.ID, evt.Sender, senderDisplayName, body)
70 }
71
72 func (evt *Event) GenerateReplyFallbackText() string {
73 parsedContent, ok := evt.Content.Parsed.(*MessageEventContent)
74 if !ok {
75 return ""
76 }
77 parsedContent.RemoveReplyFallback()
78 body := parsedContent.Body
79 lines := strings.Split(strings.TrimSpace(body), "\n")
80 firstLine, lines := lines[0], lines[1:]
81
82 senderDisplayName := evt.Sender
83
84 var fallbackText strings.Builder
85 _, _ = fmt.Fprintf(&fallbackText, "> <%s> %s", senderDisplayName, firstLine)
86 for _, line := range lines {
87 _, _ = fmt.Fprintf(&fallbackText, "\n> %s", line)
88 }
89 fallbackText.WriteString("\n\n")
90 return fallbackText.String()
91 }
92
93 func (content *MessageEventContent) SetReply(inReplyTo *Event) {
94 content.RelatesTo = &RelatesTo{
95 EventID: inReplyTo.ID,
96 Type: RelReply,
97 }
98
99 if content.MsgType == MsgText || content.MsgType == MsgNotice {
100 content.EnsureHasHTML()
101 content.FormattedBody = inReplyTo.GenerateReplyFallbackHTML() + content.FormattedBody
102 content.Body = inReplyTo.GenerateReplyFallbackText() + content.Body
103 content.replyFallbackRemoved = false
104 }
105 }