Fetch previews for files

This commit is contained in:
Bohdan Horbeshko 2022-03-14 17:57:03 -04:00
parent d6b3ed3aaa
commit 976abf68ce
2 changed files with 113 additions and 33 deletions

View file

@ -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 {
}
}
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 {

View file

@ -130,7 +130,7 @@ func TestFormatMessageMultilinePreview(t *testing.T) {
}
}
func TestFormatContent(t *testing.T) {
func TestFormatFile(t *testing.T) {
file := client.File{
Size: 23899,
Local: &client.LocalFile{
@ -146,12 +146,34 @@ func TestFormatContent(t *testing.T) {
},
}
content := c.formatFile(&file)
content := c.formatFile(&file, false)
if content != ". (23 kbytes) | " {
t.Errorf("Wrong file label: %v", content)
}
}
func TestFormatPreview(t *testing.T) {
file := client.File{
Size: 23899,
Local: &client.LocalFile{
Path: "c:/pron/smokovnica_vhsrip.mov",
},
Remote: &client.RemoteFile{
UniqueId: "aZaZaZ",
},
}
c := Client{
content: &config.TelegramContentConfig{
Link: "localhvost",
},
}
content := c.formatFile(&file, true)
if content != "" {
t.Errorf("Wrong preview label: %v", content)
}
}
func TestMessageToTextSticker(t *testing.T) {
sticker := client.Message{
Content: &client.MessageSticker{
@ -361,7 +383,7 @@ func TestMessageToPrefix1(t *testing.T) {
},
},
}
prefix := (&Client{Session: &persistence.Session{}}).messageToPrefix(&message, "")
prefix := (&Client{Session: &persistence.Session{}}).messageToPrefix(&message, "", "")
if prefix != "➡ 42 | fwd: ziz" {
t.Errorf("Wrong prefix: %v", prefix)
}
@ -376,8 +398,8 @@ func TestMessageToPrefix2(t *testing.T) {
},
},
}
prefix := (&Client{Session: &persistence.Session{}}).messageToPrefix(&message, "")
if prefix != "⬅ 56 | fwd: (zaz)" {
prefix := (&Client{Session: &persistence.Session{}}).messageToPrefix(&message, "y.jpg", "")
if prefix != "⬅ 56 | fwd: (zaz) | preview: y.jpg" {
t.Errorf("Wrong prefix: %v", prefix)
}
}
@ -386,11 +408,13 @@ func TestMessageToPrefix3(t *testing.T) {
message := client.Message{
Id: 56,
ForwardInfo: &client.MessageForwardInfo{
Origin: &client.MessageForwardOriginChannel{},
Origin: &client.MessageForwardOriginChannel{
AuthorSignature: "zuz",
},
},
}
prefix := (&Client{Session: &persistence.Session{AsciiArrows: true}}).messageToPrefix(&message, "a.jpg")
if prefix != "< 56 | fwd: | file: a.jpg" {
prefix := (&Client{Session: &persistence.Session{AsciiArrows: true}}).messageToPrefix(&message, "", "a.jpg")
if prefix != "< 56 | fwd: (zuz) | file: a.jpg" {
t.Errorf("Wrong prefix: %v", prefix)
}
}
@ -400,8 +424,23 @@ func TestMessageToPrefix4(t *testing.T) {
Id: 23,
IsOutgoing: true,
}
prefix := (&Client{Session: &persistence.Session{AsciiArrows: true}}).messageToPrefix(&message, "")
prefix := (&Client{Session: &persistence.Session{AsciiArrows: true}}).messageToPrefix(&message, "", "")
if prefix != "> 23" {
t.Errorf("Wrong prefix: %v", prefix)
}
}
func TestMessageToPrefix5(t *testing.T) {
message := client.Message{
Id: 560,
ForwardInfo: &client.MessageForwardInfo{
Origin: &client.MessageForwardOriginChat{
AuthorSignature: "zyz",
},
},
}
prefix := (&Client{Session: &persistence.Session{AsciiArrows: true}}).messageToPrefix(&message, "h.jpg", "a.jpg")
if prefix != "< 560 | fwd: (zyz) | preview: h.jpg | file: a.jpg" {
t.Errorf("Wrong prefix: %v", prefix)
}
}