Cut fallback quotes out

This commit is contained in:
Bohdan Horbeshko 2023-03-08 07:51:19 -05:00
parent 4a5b83dff5
commit b1135b070b
3 changed files with 80 additions and 9 deletions

View file

@ -30,6 +30,7 @@ var spaceRegex = regexp.MustCompile(`\s+`)
var replyRegex = regexp.MustCompile("\\A>>? ?([0-9]+)\\n") var replyRegex = regexp.MustCompile("\\A>>? ?([0-9]+)\\n")
const newlineChar string = "\n" const newlineChar string = "\n"
const messageHeaderSeparator string = " | "
// GetContactByUsername resolves username to user id retrieves user and chat information // GetContactByUsername resolves username to user id retrieves user and chat information
func (c *Client) GetContactByUsername(username string) (*client.Chat, *client.User, error) { func (c *Client) GetContactByUsername(username string) (*client.Chat, *client.User, error) {
@ -710,7 +711,15 @@ func (c *Client) contentToFile(content client.MessageContent) (*client.File, *cl
return nil, nil return nil, nil
} }
func (c *Client) messageToPrefix(message *client.Message, previewString string, fileString string, replyMsg *client.Message) string { func (c *Client) countCharsInLines(lines *[]string) (count int) {
for _, line := range *lines {
count += len(line)
}
return
}
func (c *Client) messageToPrefix(message *client.Message, previewString string, fileString string, replyMsg *client.Message) (string, int, int) {
var replyStart, replyEnd int
prefix := []string{} prefix := []string{}
// message direction // message direction
var directionChar string var directionChar string
@ -734,7 +743,10 @@ func (c *Client) messageToPrefix(message *client.Message, previewString string,
} }
// reply to // reply to
if message.ReplyToMessageId != 0 { if message.ReplyToMessageId != 0 {
prefix = append(prefix, "reply: "+c.formatMessage(message.ChatId, message.ReplyToMessageId, true, replyMsg)) replyStart = c.countCharsInLines(&prefix) + (len(prefix) - 1) * len(messageHeaderSeparator)
replyLine := "reply: "+c.formatMessage(message.ChatId, message.ReplyToMessageId, true, replyMsg)
prefix = append(prefix, replyLine)
replyEnd = replyStart + len(replyLine) + len(messageHeaderSeparator)
} }
if message.ForwardInfo != nil { if message.ForwardInfo != nil {
prefix = append(prefix, "fwd: "+c.formatForward(message.ForwardInfo)) prefix = append(prefix, "fwd: "+c.formatForward(message.ForwardInfo))
@ -748,7 +760,7 @@ func (c *Client) messageToPrefix(message *client.Message, previewString string,
prefix = append(prefix, "file: "+fileString) prefix = append(prefix, "file: "+fileString)
} }
return strings.Join(prefix, " | ") return strings.Join(prefix, messageHeaderSeparator), replyStart, replyEnd
} }
func (c *Client) ensureDownloadFile(file *client.File) *client.File { func (c *Client) ensureDownloadFile(file *client.File) *client.File {
@ -805,19 +817,26 @@ func (c *Client) ProcessIncomingMessage(chatId int64, message *client.Message) {
} }
text = oob text = oob
} else if !c.Session.RawMessages { } else if !c.Session.RawMessages {
var prefix strings.Builder var newText strings.Builder
prefix.WriteString(c.messageToPrefix(message, previewName, fileName, replyMsg))
prefix, replyStart, replyEnd := c.messageToPrefix(message, previewName, fileName, replyMsg)
newText.WriteString(prefix)
if reply != nil {
reply.Start = uint64(replyStart)
reply.End = uint64(replyEnd)
}
if text != "" { if text != "" {
// \n if it is groupchat and message is not empty // \n if it is groupchat and message is not empty
if chatId < 0 { if chatId < 0 {
prefix.WriteString("\n") newText.WriteString("\n")
} else if chatId > 0 { } else if chatId > 0 {
prefix.WriteString(" | ") newText.WriteString(" | ")
} }
prefix.WriteString(text) newText.WriteString(text)
} }
text = prefix.String() text = newText.String()
} }
} }
} }

View file

@ -2,6 +2,7 @@ package extensions
import ( import (
"encoding/xml" "encoding/xml"
"strconv"
"gosrc.io/xmpp/stanza" "gosrc.io/xmpp/stanza"
) )
@ -118,6 +119,28 @@ type Reply struct {
Id string `xml:"id,attr"` Id string `xml:"id,attr"`
} }
// Fallback is from XEP-0428
type Fallback struct {
XMLName xml.Name `xml:"urn:xmpp:fallback:0 fallback"`
For string `xml:"for,attr"`
Body []FallbackBody
Subject []FallbackSubject
}
// FallbackBody is from XEP-0428
type FallbackBody struct {
XMLName xml.Name `xml:"body"`
Start string `xml:"start,attr"`
End string `xml:"end,attr"`
}
// FallbackSubject is from XEP-0428
type FallbackSubject struct {
XMLName xml.Name `xml:"subject"`
Start string `xml:"start,attr"`
End string `xml:"end,attr"`
}
// Namespace is a namespace! // Namespace is a namespace!
func (c PresenceNickExtension) Namespace() string { func (c PresenceNickExtension) Namespace() string {
return c.XMLName.Space return c.XMLName.Space
@ -143,6 +166,24 @@ func (c Reply) Namespace() string {
return c.XMLName.Space return c.XMLName.Space
} }
// Namespace is a namespace!
func (c Fallback) Namespace() string {
return c.XMLName.Space
}
// NewReplyFallback initializes a fallback range
func NewReplyFallback(start uint64, end uint64) Fallback {
return Fallback{
For: "urn:xmpp:reply:0",
Body: []FallbackBody{
FallbackBody{
Start: strconv.FormatUint(start, 10),
End: strconv.FormatUint(end, 10),
},
},
}
}
func init() { func init() {
// presence nick // presence nick
stanza.TypeRegistry.MapExtension(stanza.PKTPresence, xml.Name{ stanza.TypeRegistry.MapExtension(stanza.PKTPresence, xml.Name{
@ -167,4 +208,10 @@ func init() {
"urn:xmpp:reply:0", "urn:xmpp:reply:0",
"reply", "reply",
}, Reply{}) }, Reply{})
// fallback
stanza.TypeRegistry.MapExtension(stanza.PKTMessage, xml.Name{
"urn:xmpp:fallback:0",
"fallback",
}, Fallback{})
} }

View file

@ -16,6 +16,8 @@ import (
type Reply struct { type Reply struct {
Author string Author string
Id string Id string
Start uint64
End uint64
} }
const NSNick string = "http://jabber.org/protocol/nick" const NSNick string = "http://jabber.org/protocol/nick"
@ -89,6 +91,9 @@ func sendMessageWrapper(to string, from string, body string, id string, componen
To: reply.Author, To: reply.Author,
Id: reply.Id, Id: reply.Id,
}) })
if reply.End > 0 {
message.Extensions = append(message.Extensions, extensions.NewReplyFallback(reply.Start, reply.End))
}
} }
sendMessage(&message, component) sendMessage(&message, component)