From f0e5e0022f565034393a8d5f0afcbd21470284c3 Mon Sep 17 00:00:00 2001 From: smagnin Date: Tue, 7 Apr 2020 01:26:02 +0200 Subject: [PATCH 1/3] add text for video message --- ui/messages/parser.go | 3 ++ ui/messages/videomessage.go | 92 +++++++++++++++++++++++++++++++++++++ 2 files changed, 95 insertions(+) create mode 100644 ui/messages/videomessage.go diff --git a/ui/messages/parser.go b/ui/messages/parser.go index 75c564e..ce5827a 100644 --- a/ui/messages/parser.go +++ b/ui/messages/parser.go @@ -144,6 +144,9 @@ func ParseMessage(matrix ifc.MatrixContainer, room *rooms.Room, evt *event.Event debug.Printf("Failed to download %s: %v", evt.Content.URL, err) } return NewImageMessage(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 } diff --git a/ui/messages/videomessage.go b/ui/messages/videomessage.go new file mode 100644 index 0000000..d5b87f8 --- /dev/null +++ b/ui/messages/videomessage.go @@ -0,0 +1,92 @@ +// 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 . + +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) + } +} From 8b68140dc9e4bc6d51f49a4a38a09e1a0b0ffd67 Mon Sep 17 00:00:00 2001 From: Simon Magnin-Feysot Date: Tue, 7 Apr 2020 18:06:20 +0200 Subject: [PATCH 2/3] refactor to allow every file message to be downloaded --- ui/message-view.go | 2 +- .../{imagemessage.go => filemessage.go} | 70 +++++++------- ui/messages/parser.go | 7 +- ui/messages/videomessage.go | 92 ------------------- 4 files changed, 36 insertions(+), 135 deletions(-) rename ui/messages/{imagemessage.go => filemessage.go} (54%) delete mode 100644 ui/messages/videomessage.go diff --git a/ui/message-view.go b/ui/message-view.go index 6fa579a..eab7bf6 100644 --- a/ui/message-view.go +++ b/ui/message-view.go @@ -349,7 +349,7 @@ func (view *MessageView) SetSelected(message *messages.UIMessage) { } 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()) // No need to re-render return false diff --git a/ui/messages/imagemessage.go b/ui/messages/filemessage.go similarity index 54% rename from ui/messages/imagemessage.go rename to ui/messages/filemessage.go index 5e72c88..3878b56 100644 --- a/ui/messages/imagemessage.go +++ b/ui/messages/filemessage.go @@ -33,7 +33,7 @@ import ( "maunium.net/go/gomuks/ui/messages/tstring" ) -type ImageMessage struct { +type FileMessage struct { Body string Homeserver string FileID string @@ -43,30 +43,30 @@ type ImageMessage struct { matrix ifc.MatrixContainer } -// NewImageMessage creates a new ImageMessage 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 { - return newUIMessage(evt, displayname, &ImageMessage{ - Body: body, +// NewFileMessage creates a new FileMessage object with the provided values and the default state. +func NewFileMessage(matrix ifc.MatrixContainer, evt *event.Event, displayname string, body, homeserver, fileID string, data []byte) *UIMessage { + return newUIMessage(evt, displayname, &FileMessage{ + Body: body, Homeserver: homeserver, - FileID: fileID, - data: data, - matrix: matrix, + FileID: fileID, + data: data, + matrix: matrix, }) } -func (msg *ImageMessage) Clone() MessageRenderer { +func (msg *FileMessage) Clone() MessageRenderer { data := make([]byte, len(msg.data)) copy(data, msg.data) - return &ImageMessage{ - Body: msg.Body, - Homeserver: msg.Homeserver, - FileID: msg.FileID, - data: data, - matrix: msg.matrix, + return &FileMessage{ + Body: msg.Body, + Homeserver: msg.Homeserver, + FileID: msg.FileID, + data: data, + matrix: msg.matrix, } } -func (msg *ImageMessage) RegisterMatrix(matrix ifc.MatrixContainer) { +func (msg *FileMessage) RegisterMatrix(matrix ifc.MatrixContainer) { msg.matrix = matrix if len(msg.data) == 0 { @@ -74,72 +74,68 @@ func (msg *ImageMessage) RegisterMatrix(matrix ifc.MatrixContainer) { } } -func (msg *ImageMessage) NotificationContent() string { - return "Sent an image" +func (msg *FileMessage) NotificationContent() string { + 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)) } -func (msg *ImageMessage) String() string { - return fmt.Sprintf(`&messages.ImageMessage{Body="%s", Homeserver="%s", FileID="%s"}`, msg.Body, msg.Homeserver, msg.FileID) +func (msg *FileMessage) String() string { + 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() - 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)) 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 } - debug.Print("Image", msg.Homeserver, msg.FileID, "loaded.") + debug.Print("File", msg.Homeserver, msg.FileID, "loaded.") msg.data = data } -func (msg *ImageMessage) Path() string { +func (msg *FileMessage) Path() string { return msg.matrix.GetCachePath(msg.Homeserver, msg.FileID) } -// CalculateBuffer generates the internal buffer for this message that consists -// 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) { +func (msg *FileMessage) CalculateBuffer(prefs config.UserPreferences, width int, uiMsg *UIMessage) { if width < 2 { 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) return } img, _, err := image.DecodeConfig(bytes.NewReader(msg.data)) if err != nil { - debug.Print("Image could not be decoded:", err) + debug.Print("File could not be decoded:", err) } imgWidth := img.Width if img.Width > width { 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 { msg.buffer = []tstring.TString{tstring.NewColorTString("Failed to display image", tcell.ColorRed)} debug.Print("Failed to display image:", err) return } - msg.buffer = ansImage.Render() + msg.buffer = ansFile.Render() } -func (msg *ImageMessage) Height() int { +func (msg *FileMessage) Height() int { return len(msg.buffer) } -func (msg *ImageMessage) Draw(screen mauview.Screen) { +func (msg *FileMessage) Draw(screen mauview.Screen) { for y, line := range msg.buffer { line.Draw(screen, 0, y) } diff --git a/ui/messages/parser.go b/ui/messages/parser.go index ce5827a..1c145e5 100644 --- a/ui/messages/parser.go +++ b/ui/messages/parser.go @@ -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) 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) if err != nil { debug.Printf("Failed to download %s: %v", evt.Content.URL, err) } - return NewImageMessage(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 NewFileMessage(matrix, evt, displayname, evt.Content.Body, hs, id, data) } return nil } diff --git a/ui/messages/videomessage.go b/ui/messages/videomessage.go deleted file mode 100644 index d5b87f8..0000000 --- a/ui/messages/videomessage.go +++ /dev/null @@ -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 . - -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) - } -} From 41e7b018c1f36395dccdff586f2668a249a5bcc0 Mon Sep 17 00:00:00 2001 From: Simon Magnin-Feysot Date: Tue, 7 Apr 2020 19:06:10 +0200 Subject: [PATCH 3/3] command toggle downloads --- config/config.go | 1 + ui/commands.go | 7 +++++++ ui/messages/base.go | 2 +- ui/messages/expandedtextmessage.go | 2 +- ui/messages/filemessage.go | 4 ++-- ui/messages/htmlmessage.go | 2 +- ui/messages/redactedmessage.go | 2 +- ui/messages/textmessage.go | 2 +- 8 files changed, 15 insertions(+), 7 deletions(-) diff --git a/config/config.go b/config/config.go index b8ff74b..e8c7b1a 100644 --- a/config/config.go +++ b/config/config.go @@ -47,6 +47,7 @@ type UserPreferences struct { DisableEmojis bool `yaml:"disable_emojis"` DisableMarkdown bool `yaml:"disable_markdown"` DisableHTML bool `yaml:"disable_html"` + DisableDownloads bool `yaml:"disable_downloads"` } // Config contains the main config of gomuks. diff --git a/ui/commands.go b/ui/commands.go index 747be1e..a57a78d 100644 --- a/ui/commands.go +++ b/ui/commands.go @@ -570,6 +570,13 @@ func cmdToggle(cmd *Command) { } else { cmd.Reply("Enabled Markdown input") } + case "downloads": + cmd.Config.Preferences.DisableDownloads = !cmd.Config.Preferences.DisableDownloads + if cmd.Config.Preferences.DisableDownloads { + cmd.Reply("Disabled Downloads input") + } else { + cmd.Reply("Enabled Downloads input") + } default: cmd.Reply("Usage: /toggle ") return diff --git a/ui/messages/base.go b/ui/messages/base.go index e86b85c..58d9b6e 100644 --- a/ui/messages/base.go +++ b/ui/messages/base.go @@ -36,7 +36,7 @@ type MessageRenderer interface { NotificationContent() string PlainText() string CalculateBuffer(prefs config.UserPreferences, width int, msg *UIMessage) - RegisterMatrix(matrix ifc.MatrixContainer) + RegisterMatrix(matrix ifc.MatrixContainer, prefs config.UserPreferences) Height() int Clone() MessageRenderer String() string diff --git a/ui/messages/expandedtextmessage.go b/ui/messages/expandedtextmessage.go index 424db79..c666613 100644 --- a/ui/messages/expandedtextmessage.go +++ b/ui/messages/expandedtextmessage.go @@ -89,4 +89,4 @@ func (msg *ExpandedTextMessage) Draw(screen mauview.Screen) { } } -func (msg *ExpandedTextMessage) RegisterMatrix(matrix ifc.MatrixContainer) {} +func (msg *ExpandedTextMessage) RegisterMatrix(matrix ifc.MatrixContainer, prefs config.UserPreferences) {} diff --git a/ui/messages/filemessage.go b/ui/messages/filemessage.go index 3878b56..9fb3499 100644 --- a/ui/messages/filemessage.go +++ b/ui/messages/filemessage.go @@ -66,10 +66,10 @@ func (msg *FileMessage) Clone() MessageRenderer { } } -func (msg *FileMessage) RegisterMatrix(matrix ifc.MatrixContainer) { +func (msg *FileMessage) RegisterMatrix(matrix ifc.MatrixContainer, prefs config.UserPreferences) { msg.matrix = matrix - if len(msg.data) == 0 { + if len(msg.data) == 0 && !prefs.DisableDownloads { go msg.updateData() } } diff --git a/ui/messages/htmlmessage.go b/ui/messages/htmlmessage.go index e476bc9..ff49e57 100644 --- a/ui/messages/htmlmessage.go +++ b/ui/messages/htmlmessage.go @@ -39,7 +39,7 @@ func NewHTMLMessage(evt *event.Event, displayname string, root html.Entity) *UIM }) } -func (hw *HTMLMessage) RegisterMatrix(matrix ifc.MatrixContainer) {} +func (hw *HTMLMessage) RegisterMatrix(matrix ifc.MatrixContainer, prefs config.UserPreferences) {} func (hw *HTMLMessage) Clone() MessageRenderer { return &HTMLMessage{ diff --git a/ui/messages/redactedmessage.go b/ui/messages/redactedmessage.go index fc34682..56b4822 100644 --- a/ui/messages/redactedmessage.go +++ b/ui/messages/redactedmessage.go @@ -65,4 +65,4 @@ func (msg *RedactedMessage) Draw(screen mauview.Screen) { } } -func (msg *RedactedMessage) RegisterMatrix(matrix ifc.MatrixContainer) {} +func (msg *RedactedMessage) RegisterMatrix(matrix ifc.MatrixContainer, prefs config.UserPreferences) {} diff --git a/ui/messages/textmessage.go b/ui/messages/textmessage.go index 2e27e33..0bfa27b 100644 --- a/ui/messages/textmessage.go +++ b/ui/messages/textmessage.go @@ -102,4 +102,4 @@ func (msg *TextMessage) Draw(screen mauview.Screen) { } } -func (msg *TextMessage) RegisterMatrix(matrix ifc.MatrixContainer) {} +func (msg *TextMessage) RegisterMatrix(matrix ifc.MatrixContainer, prefs config.UserPreferences) {}