refactor to allow every file message to be downloaded

This commit is contained in:
Simon Magnin-Feysot 2020-04-07 18:06:20 +02:00
parent f0e5e0022f
commit 8b68140dc9
4 changed files with 36 additions and 135 deletions

View File

@ -349,7 +349,7 @@ func (view *MessageView) SetSelected(message *messages.UIMessage) {
} }
func (view *MessageView) handleMessageClick(message *messages.UIMessage, mod tcell.ModMask) bool { func (view *MessageView) handleMessageClick(message *messages.UIMessage, mod tcell.ModMask) bool {
if msg, ok := message.Renderer.(*messages.ImageMessage); ok && mod > 0 { if msg, ok := message.Renderer.(*messages.FileMessage); ok && mod > 0 {
open.Open(msg.Path()) open.Open(msg.Path())
// No need to re-render // No need to re-render
return false return false

View File

@ -33,7 +33,7 @@ import (
"maunium.net/go/gomuks/ui/messages/tstring" "maunium.net/go/gomuks/ui/messages/tstring"
) )
type ImageMessage struct { type FileMessage struct {
Body string Body string
Homeserver string Homeserver string
FileID string FileID string
@ -43,9 +43,9 @@ type ImageMessage struct {
matrix ifc.MatrixContainer matrix ifc.MatrixContainer
} }
// NewImageMessage creates a new ImageMessage object with the provided values and the default state. // NewFileMessage creates a new FileMessage object with the provided values and the default state.
func NewImageMessage(matrix ifc.MatrixContainer, evt *event.Event, displayname string, body, homeserver, fileID string, data []byte) *UIMessage { func NewFileMessage(matrix ifc.MatrixContainer, evt *event.Event, displayname string, body, homeserver, fileID string, data []byte) *UIMessage {
return newUIMessage(evt, displayname, &ImageMessage{ return newUIMessage(evt, displayname, &FileMessage{
Body: body, Body: body,
Homeserver: homeserver, Homeserver: homeserver,
FileID: fileID, FileID: fileID,
@ -54,10 +54,10 @@ func NewImageMessage(matrix ifc.MatrixContainer, evt *event.Event, displayname s
}) })
} }
func (msg *ImageMessage) Clone() MessageRenderer { func (msg *FileMessage) Clone() MessageRenderer {
data := make([]byte, len(msg.data)) data := make([]byte, len(msg.data))
copy(data, msg.data) copy(data, msg.data)
return &ImageMessage{ return &FileMessage{
Body: msg.Body, Body: msg.Body,
Homeserver: msg.Homeserver, Homeserver: msg.Homeserver,
FileID: msg.FileID, FileID: msg.FileID,
@ -66,7 +66,7 @@ func (msg *ImageMessage) Clone() MessageRenderer {
} }
} }
func (msg *ImageMessage) RegisterMatrix(matrix ifc.MatrixContainer) { func (msg *FileMessage) RegisterMatrix(matrix ifc.MatrixContainer) {
msg.matrix = matrix msg.matrix = matrix
if len(msg.data) == 0 { if len(msg.data) == 0 {
@ -74,72 +74,68 @@ func (msg *ImageMessage) RegisterMatrix(matrix ifc.MatrixContainer) {
} }
} }
func (msg *ImageMessage) NotificationContent() string { func (msg *FileMessage) NotificationContent() string {
return "Sent an image" return "Sent a file"
} }
func (msg *ImageMessage) PlainText() string { func (msg *FileMessage) PlainText() string {
return fmt.Sprintf("%s: %s", msg.Body, msg.matrix.GetDownloadURL(msg.Homeserver, msg.FileID)) return fmt.Sprintf("%s: %s", msg.Body, msg.matrix.GetDownloadURL(msg.Homeserver, msg.FileID))
} }
func (msg *ImageMessage) String() string { func (msg *FileMessage) String() string {
return fmt.Sprintf(`&messages.ImageMessage{Body="%s", Homeserver="%s", FileID="%s"}`, msg.Body, msg.Homeserver, msg.FileID) return fmt.Sprintf(`&messages.FileMessage{Body="%s", Homeserver="%s", FileID="%s"}`, msg.Body, msg.Homeserver, msg.FileID)
} }
func (msg *ImageMessage) updateData() { func (msg *FileMessage) updateData() {
defer debug.Recover() defer debug.Recover()
debug.Print("Loading image:", msg.Homeserver, msg.FileID) debug.Print("Loading file:", msg.Homeserver, msg.FileID)
data, _, _, err := msg.matrix.Download(fmt.Sprintf("mxc://%s/%s", msg.Homeserver, msg.FileID)) data, _, _, err := msg.matrix.Download(fmt.Sprintf("mxc://%s/%s", msg.Homeserver, msg.FileID))
if err != nil { if err != nil {
debug.Printf("Failed to download image %s/%s: %v", msg.Homeserver, msg.FileID, err) debug.Printf("Failed to download file %s/%s: %v", msg.Homeserver, msg.FileID, err)
return return
} }
debug.Print("Image", msg.Homeserver, msg.FileID, "loaded.") debug.Print("File", msg.Homeserver, msg.FileID, "loaded.")
msg.data = data msg.data = data
} }
func (msg *ImageMessage) Path() string { func (msg *FileMessage) Path() string {
return msg.matrix.GetCachePath(msg.Homeserver, msg.FileID) return msg.matrix.GetCachePath(msg.Homeserver, msg.FileID)
} }
// CalculateBuffer generates the internal buffer for this message that consists func (msg *FileMessage) CalculateBuffer(prefs config.UserPreferences, width int, uiMsg *UIMessage) {
// of the text of this message split into lines at most as wide as the width
// parameter. If the message width is larger than the width of the buffer
// the message is scaled to one third the buffer width.
func (msg *ImageMessage) CalculateBuffer(prefs config.UserPreferences, width int, uiMsg *UIMessage) {
if width < 2 { if width < 2 {
return return
} }
if prefs.BareMessageView || prefs.DisableImages { if prefs.BareMessageView || prefs.DisableImages || uiMsg.Type != "m.image" {
msg.buffer = calculateBufferWithText(prefs, tstring.NewTString(msg.PlainText()), width, uiMsg) msg.buffer = calculateBufferWithText(prefs, tstring.NewTString(msg.PlainText()), width, uiMsg)
return return
} }
img, _, err := image.DecodeConfig(bytes.NewReader(msg.data)) img, _, err := image.DecodeConfig(bytes.NewReader(msg.data))
if err != nil { if err != nil {
debug.Print("Image could not be decoded:", err) debug.Print("File could not be decoded:", err)
} }
imgWidth := img.Width imgWidth := img.Width
if img.Width > width { if img.Width > width {
imgWidth = width / 3 imgWidth = width / 3
} }
ansImage, err := ansimage.NewScaledFromReader(bytes.NewReader(msg.data), 0, imgWidth, color.Black) ansFile, err := ansimage.NewScaledFromReader(bytes.NewReader(msg.data), 0, imgWidth, color.Black)
if err != nil { if err != nil {
msg.buffer = []tstring.TString{tstring.NewColorTString("Failed to display image", tcell.ColorRed)} msg.buffer = []tstring.TString{tstring.NewColorTString("Failed to display image", tcell.ColorRed)}
debug.Print("Failed to display image:", err) debug.Print("Failed to display image:", err)
return return
} }
msg.buffer = ansImage.Render() msg.buffer = ansFile.Render()
} }
func (msg *ImageMessage) Height() int { func (msg *FileMessage) Height() int {
return len(msg.buffer) return len(msg.buffer)
} }
func (msg *ImageMessage) Draw(screen mauview.Screen) { func (msg *FileMessage) Draw(screen mauview.Screen) {
for y, line := range msg.buffer { for y, line := range msg.buffer {
line.Draw(screen, 0, y) line.Draw(screen, 0, y)
} }

View File

@ -138,15 +138,12 @@ func ParseMessage(matrix ifc.MatrixContainer, room *rooms.Room, evt *event.Event
} }
evt.Content.Body = strings.Replace(evt.Content.Body, "\t", " ", -1) evt.Content.Body = strings.Replace(evt.Content.Body, "\t", " ", -1)
return NewTextMessage(evt, displayname, evt.Content.Body) return NewTextMessage(evt, displayname, evt.Content.Body)
case "m.image": case "m.file", "m.video", "m.audio", "m.image":
data, hs, id, err := matrix.Download(evt.Content.URL) data, hs, id, err := matrix.Download(evt.Content.URL)
if err != nil { if err != nil {
debug.Printf("Failed to download %s: %v", evt.Content.URL, err) debug.Printf("Failed to download %s: %v", evt.Content.URL, err)
} }
return NewImageMessage(matrix, evt, displayname, evt.Content.Body, hs, id, data) return NewFileMessage(matrix, evt, displayname, evt.Content.Body, hs, id, data)
case "m.video":
_, hs, id, _ := matrix.Download(evt.Content.URL)
return NewVideoMessage(matrix, evt, displayname, evt.Content.Body, hs, id)
} }
return nil return nil
} }

View File

@ -1,92 +0,0 @@
// gomuks - A terminal Matrix client written in Go.
// Copyright (C) 2019 Tulir Asokan
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package messages
import (
"fmt"
"maunium.net/go/gomuks/matrix/event"
"maunium.net/go/mauview"
"maunium.net/go/gomuks/config"
"maunium.net/go/gomuks/interface"
"maunium.net/go/gomuks/ui/messages/tstring"
)
type VideoMessage struct {
Body string
Homeserver string
FileID string
buffer []tstring.TString
matrix ifc.MatrixContainer
}
// NewVideoMessage creates a new VideoMessage object with the provided values and the default state.
func NewVideoMessage(matrix ifc.MatrixContainer, evt *event.Event, displayname string, body, homeserver, fileID string) *UIMessage {
return newUIMessage(evt, displayname, &VideoMessage{
Body: body,
Homeserver: homeserver,
FileID: fileID,
matrix: matrix,
})
}
func (msg *VideoMessage) Clone() MessageRenderer {
return &VideoMessage{
Body: msg.Body,
Homeserver: msg.Homeserver,
FileID: msg.FileID,
matrix: msg.matrix,
}
}
func (msg *VideoMessage) NotificationContent() string {
return "Sent a video"
}
func (msg *VideoMessage) PlainText() string {
return fmt.Sprintf("%s: %s", msg.Body, msg.matrix.GetDownloadURL(msg.Homeserver, msg.FileID))
}
func (msg *VideoMessage) String() string {
return fmt.Sprintf(`&messages.VideoMessage{Body="%s", Homeserver="%s", FileID="%s"}`, msg.Body, msg.Homeserver, msg.FileID)
}
func (msg *VideoMessage) Path() string {
return msg.matrix.GetCachePath(msg.Homeserver, msg.FileID)
}
func (msg *VideoMessage) RegisterMatrix(matrix ifc.MatrixContainer) {
msg.matrix = matrix
}
// Print only Plain Text
func (msg *VideoMessage) CalculateBuffer(prefs config.UserPreferences, width int, uiMsg *UIMessage) {
msg.buffer = calculateBufferWithText(prefs, tstring.NewTString(msg.PlainText()), width, uiMsg)
return
}
func (msg *VideoMessage) Height() int {
return len(msg.buffer)
}
func (msg *VideoMessage) Draw(screen mauview.Screen) {
for y, line := range msg.buffer {
line.Draw(screen, 0, y)
}
}