aboutsummaryrefslogtreecommitdiff
path: root/telegram
diff options
context:
space:
mode:
authorBohdan Horbeshko <bodqhrohro@gmail.com>2023-01-16 04:35:13 +0300
committerBohdan Horbeshko <bodqhrohro@gmail.com>2023-01-16 04:35:13 +0300
commitb3b53b6145c4b8e31d134942507c3ce0226182a4 (patch)
tree4f0753727317859504c26dd5bd4f338991db9082 /telegram
parent68e3581724f04366c114f267b3264075bc635061 (diff)
OOB
Diffstat (limited to 'telegram')
-rw-r--r--telegram/utils.go25
-rw-r--r--telegram/utils_test.go10
2 files changed, 24 insertions, 11 deletions
diff --git a/telegram/utils.go b/telegram/utils.go
index 17e9861..1545939 100644
--- a/telegram/utils.go
+++ b/telegram/utils.go
@@ -350,10 +350,10 @@ func (c *Client) formatForward(fwd *client.MessageForwardInfo) string {
return "Unknown forward type"
}
-func (c *Client) formatFile(file *client.File, compact bool) string {
+func (c *Client) formatFile(file *client.File, compact bool) (string, string) {
log.Debugf("file: %#v", file)
if file == nil || file.Local == nil || file.Remote == nil {
- return ""
+ return "", ""
}
gateway.StorageLock.Lock()
@@ -367,7 +367,7 @@ func (c *Client) formatFile(file *client.File, compact bool) string {
_, err := os.Stat(src)
if err != nil {
log.Errorf("Cannot access source file: %v", err)
- return ""
+ return "", ""
}
size64 := uint64(file.Size)
@@ -385,7 +385,7 @@ func (c *Client) formatFile(file *client.File, compact bool) string {
log.Warn(err.Error())
} else {
log.Errorf("File moving error: %v", err)
- return "<ERROR>"
+ return "<ERROR>", ""
}
}
gateway.CachedStorageSize += size64
@@ -410,9 +410,9 @@ func (c *Client) formatFile(file *client.File, compact bool) string {
}
if compact {
- return link
+ return link, link
} else {
- return fmt.Sprintf("%s (%v kbytes) | %s", filepath.Base(src), file.Size/1024, link)
+ return fmt.Sprintf("%s (%v kbytes) | %s", filepath.Base(src), file.Size/1024, link), link
}
}
@@ -749,7 +749,7 @@ func (c *Client) ensureDownloadFile(file *client.File) *client.File {
// ProcessIncomingMessage transfers a message to XMPP side and marks it as read on Telegram side
func (c *Client) ProcessIncomingMessage(chatId int64, message *client.Message) {
- var text string
+ var text, oob string
content := message.Content
if content != nil && content.MessageContentType() == client.TypeMessageChatChangePhoto {
chat, err := c.client.GetChat(&client.GetChatRequest{
@@ -772,7 +772,10 @@ func (c *Client) ProcessIncomingMessage(chatId int64, message *client.Message) {
preview = c.ensureDownloadFile(preview)
var prefix strings.Builder
- prefix.WriteString(c.messageToPrefix(message, c.formatFile(preview, true), c.formatFile(file, false)))
+ previewName, _ := c.formatFile(preview, true)
+ fileName, link := c.formatFile(file, false)
+ prefix.WriteString(c.messageToPrefix(message, previewName, fileName))
+ oob = link
if text != "" {
// \n if it is groupchat and message is not empty
if chatId < 0 {
@@ -786,6 +789,10 @@ func (c *Client) ProcessIncomingMessage(chatId int64, message *client.Message) {
text = prefix.String()
}
+
+ if c.Session.OOBMode && oob != "" {
+ text = oob
+ }
}
// mark message as read
@@ -795,7 +802,7 @@ func (c *Client) ProcessIncomingMessage(chatId int64, message *client.Message) {
ForceRead: true,
})
// forward message to XMPP
- gateway.SendMessage(c.jid, strconv.FormatInt(chatId, 10), text, c.xmpp)
+ gateway.SendMessageWithOOB(c.jid, strconv.FormatInt(chatId, 10), text, c.xmpp, oob)
}
// ProcessOutgoingMessage executes commands or sends messages to mapped chats
diff --git a/telegram/utils_test.go b/telegram/utils_test.go
index f0140ae..bfd6c49 100644
--- a/telegram/utils_test.go
+++ b/telegram/utils_test.go
@@ -146,10 +146,13 @@ func TestFormatFile(t *testing.T) {
},
}
- content := c.formatFile(&file, false)
+ content, link := c.formatFile(&file, false)
if content != ". (23 kbytes) | " {
t.Errorf("Wrong file label: %v", content)
}
+ if link != "" {
+ t.Errorf("Wrong file link: %v", link)
+ }
}
func TestFormatPreview(t *testing.T) {
@@ -168,10 +171,13 @@ func TestFormatPreview(t *testing.T) {
},
}
- content := c.formatFile(&file, true)
+ content, link := c.formatFile(&file, true)
if content != "" {
t.Errorf("Wrong preview label: %v", content)
}
+ if link != "" {
+ t.Errorf("Wrong preview link: %v", link)
+ }
}
func TestMessageToTextSticker(t *testing.T) {