Disable link previews for Matrix replies
This commit is contained in:
parent
aa54485f5d
commit
a53f61cc81
|
|
@ -5,6 +5,7 @@ import (
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"html"
|
||||||
"log"
|
"log"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
@ -194,16 +195,15 @@ func (b *Bot) onMessage(ctx context.Context, evt *event.Event) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *Bot) sendReply(ctx context.Context, evt *event.Event, response string) error {
|
func (b *Bot) sendReply(ctx context.Context, evt *event.Event, response string) error {
|
||||||
|
body, formatted := formatNoPreview(response)
|
||||||
content := &event.MessageEventContent{
|
content := &event.MessageEventContent{
|
||||||
MsgType: event.MsgText,
|
MsgType: event.MsgText,
|
||||||
Body: response,
|
Body: body,
|
||||||
|
BeeperLinkPreviews: []*event.BeeperLinkPreview{},
|
||||||
}
|
}
|
||||||
|
if formatted != "" {
|
||||||
original := evt.Content.AsMessage()
|
content.Format = event.FormatHTML
|
||||||
if original != nil && original.RelatesTo != nil && original.RelatesTo.GetThreadParent() != "" {
|
content.FormattedBody = formatted
|
||||||
content.SetThread(evt)
|
|
||||||
} else {
|
|
||||||
content.SetReply(evt)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err := b.client.SendMessageEvent(ctx, evt.RoomID, event.EventMessage, content)
|
_, err := b.client.SendMessageEvent(ctx, evt.RoomID, event.EventMessage, content)
|
||||||
|
|
@ -345,6 +345,52 @@ func roomList(rooms map[id.RoomID]struct{}) []string {
|
||||||
return list
|
return list
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func formatNoPreview(response string) (string, string) {
|
||||||
|
lines := strings.Split(response, "\n")
|
||||||
|
bodyLines := make([]string, 0, len(lines))
|
||||||
|
formattedLines := make([]string, 0, len(lines))
|
||||||
|
|
||||||
|
for _, line := range lines {
|
||||||
|
trimmed := strings.TrimSpace(line)
|
||||||
|
if trimmed == "" {
|
||||||
|
bodyLines = append(bodyLines, "")
|
||||||
|
formattedLines = append(formattedLines, "")
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
label, url, ok := splitLinkLine(trimmed)
|
||||||
|
if ok {
|
||||||
|
bodyLines = append(bodyLines, fmt.Sprintf("%s: %s", label, obfuscateURL(url)))
|
||||||
|
formattedLines = append(formattedLines, fmt.Sprintf("%s: <a href=\"%s\">%s</a>", html.EscapeString(label), html.EscapeString(url), html.EscapeString(url)))
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
bodyLines = append(bodyLines, line)
|
||||||
|
formattedLines = append(formattedLines, html.EscapeString(line))
|
||||||
|
}
|
||||||
|
|
||||||
|
body := strings.Join(bodyLines, "\n")
|
||||||
|
formatted := strings.Join(formattedLines, "<br/>")
|
||||||
|
return body, formatted
|
||||||
|
}
|
||||||
|
|
||||||
|
func splitLinkLine(line string) (string, string, bool) {
|
||||||
|
parts := strings.SplitN(line, ": ", 2)
|
||||||
|
if len(parts) != 2 {
|
||||||
|
return "", "", false
|
||||||
|
}
|
||||||
|
label := parts[0]
|
||||||
|
url := strings.TrimSpace(parts[1])
|
||||||
|
if !strings.HasPrefix(url, "http://") && !strings.HasPrefix(url, "https://") {
|
||||||
|
return "", "", false
|
||||||
|
}
|
||||||
|
return label, url, true
|
||||||
|
}
|
||||||
|
|
||||||
|
func obfuscateURL(url string) string {
|
||||||
|
return url
|
||||||
|
}
|
||||||
|
|
||||||
func isEncryptedInvite(evt *event.Event) bool {
|
func isEncryptedInvite(evt *event.Event) bool {
|
||||||
if evt == nil || evt.Unsigned.InviteRoomState == nil {
|
if evt == nil || evt.Unsigned.InviteRoomState == nil {
|
||||||
return false
|
return false
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue