diff options
author | Bohdan Horbeshko <bodqhrohro@gmail.com> | 2022-03-15 00:57:03 +0300 |
---|---|---|
committer | Bohdan Horbeshko <bodqhrohro@gmail.com> | 2022-03-15 00:57:03 +0300 |
commit | 976abf68ce101a4fe6508effde72234951efeea5 (patch) | |
tree | b9395e677cbee28e8af97ed66301ceba165beaa0 /telegram/utils.go | |
parent | d6b3ed3aaa518c5f80f7eceddede507af592a45c (diff) |
Fetch previews for files
Diffstat (limited to 'telegram/utils.go')
-rw-r--r-- | telegram/utils.go | 89 |
1 files changed, 65 insertions, 24 deletions
diff --git a/telegram/utils.go b/telegram/utils.go index 3511de0..39e270e 100644 --- a/telegram/utils.go +++ b/telegram/utils.go @@ -355,7 +355,7 @@ func (c *Client) formatForward(fwd *client.MessageForwardInfo) string { return "Unknown forward type" } -func (c *Client) formatFile(file *client.File) string { +func (c *Client) formatFile(file *client.File, compact bool) string { if file == nil || file.Local == nil || file.Remote == nil { return "" } @@ -404,7 +404,11 @@ func (c *Client) formatFile(file *client.File) string { } } - return fmt.Sprintf("%s (%v kbytes) | %s", filepath.Base(src), file.Size/1024, link) + if compact { + return link + } else { + return fmt.Sprintf("%s (%v kbytes) | %s", filepath.Base(src), file.Size/1024, link) + } } func (c *Client) formatBantime(hours int64) int32 { @@ -608,47 +612,71 @@ func (c *Client) messageToText(message *client.Message, preview bool) string { return fmt.Sprintf("unknown message (%s)", message.Content.MessageContentType()) } -func (c *Client) contentToFile(content client.MessageContent) *client.File { +func (c *Client) contentToFile(content client.MessageContent) (*client.File, *client.File) { if content == nil { - return nil + return nil, nil } switch content.MessageContentType() { case client.TypeMessageSticker: sticker, _ := content.(*client.MessageSticker) - return sticker.Sticker.Sticker + file := sticker.Sticker.Sticker + if sticker.Sticker.IsAnimated && sticker.Sticker.Thumbnail != nil && sticker.Sticker.Thumbnail.File != nil { + file = sticker.Sticker.Thumbnail.File + } + return file, nil case client.TypeMessageVoiceNote: voice, _ := content.(*client.MessageVoiceNote) - return voice.VoiceNote.Voice + return voice.VoiceNote.Voice, nil case client.TypeMessageVideoNote: video, _ := content.(*client.MessageVideoNote) - return video.VideoNote.Video + var preview *client.File + if video.VideoNote.Thumbnail != nil { + preview = video.VideoNote.Thumbnail.File + } + return video.VideoNote.Video, preview case client.TypeMessageAnimation: animation, _ := content.(*client.MessageAnimation) - return animation.Animation.Animation + var preview *client.File + if animation.Animation.Thumbnail != nil { + preview = animation.Animation.Thumbnail.File + } + return animation.Animation.Animation, preview case client.TypeMessagePhoto: photo, _ := content.(*client.MessagePhoto) sizes := photo.Photo.Sizes if len(sizes) >= 1 { file := sizes[len(sizes)-1].Photo - return file + return file, nil } - return nil + return nil, nil case client.TypeMessageAudio: audio, _ := content.(*client.MessageAudio) - return audio.Audio.Audio + var preview *client.File + if audio.Audio.AlbumCoverThumbnail != nil { + preview = audio.Audio.AlbumCoverThumbnail.File + } + return audio.Audio.Audio, preview case client.TypeMessageVideo: video, _ := content.(*client.MessageVideo) - return video.Video.Video + var preview *client.File + if video.Video.Thumbnail != nil { + preview = video.Video.Thumbnail.File + } + return video.Video.Video, preview case client.TypeMessageDocument: document, _ := content.(*client.MessageDocument) - return document.Document.Document + var preview *client.File + if document.Document.Thumbnail != nil { + preview = document.Document.Thumbnail.File + } + return document.Document.Document, preview } - return nil + return nil, nil } -func (c *Client) messageToPrefix(message *client.Message, fileString string) string { +func (c *Client) messageToPrefix(message *client.Message, previewString string, fileString string) string { prefix := []string{} // message direction var directionChar string @@ -686,6 +714,10 @@ func (c *Client) messageToPrefix(message *client.Message, fileString string) str if message.ForwardInfo != nil { prefix = append(prefix, "fwd: "+c.formatForward(message.ForwardInfo)) } + // preview + if previewString != "" { + prefix = append(prefix, "preview: "+previewString) + } // file if fileString != "" { prefix = append(prefix, "file: "+fileString) @@ -694,6 +726,17 @@ func (c *Client) messageToPrefix(message *client.Message, fileString string) str return strings.Join(prefix, " | ") } +func (c *Client) ensureDownloadFile(file *client.File) *client.File { + if file != nil { + newFile, err := c.DownloadFile(file.Id, 1, true) + if err == nil { + return newFile + } + } + + return 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 @@ -709,19 +752,17 @@ func (c *Client) ProcessIncomingMessage(chatId int64, message *client.Message) { } } else { text = c.messageToText(message, false) - file := c.contentToFile(content) - // download file (if one) - if file != nil { - newFile, err := c.DownloadFile(file.Id, 1, true) - if err == nil { - file = newFile - } - } // OTR support (I do not know why would you need it, seriously) if !(strings.HasPrefix(text, "?OTR") || c.Session.RawMessages) { + file, preview := c.contentToFile(content) + + // download file and preview (if present) + file = c.ensureDownloadFile(file) + preview = c.ensureDownloadFile(preview) + var prefix strings.Builder - prefix.WriteString(c.messageToPrefix(message, c.formatFile(file))) + prefix.WriteString(c.messageToPrefix(message, c.formatFile(preview, true), c.formatFile(file, false))) if text != "" { // \n if it is groupchat and message is not empty if chatId < 0 { |