2018-04-10 18:31:28 +02:00
|
|
|
// gomuks - A terminal Matrix client written in Go.
|
2019-01-17 13:13:25 +01:00
|
|
|
// Copyright (C) 2019 Tulir Asokan
|
2018-04-10 18:31:28 +02:00
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
2019-01-17 13:13:25 +01:00
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
2018-04-10 18:31:28 +02:00
|
|
|
// 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
|
2019-01-17 13:13:25 +01:00
|
|
|
// GNU Affero General Public License for more details.
|
2018-04-10 18:31:28 +02:00
|
|
|
//
|
2019-01-17 13:13:25 +01:00
|
|
|
// 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/>.
|
2018-04-10 18:31:28 +02:00
|
|
|
|
|
|
|
package messages
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2018-04-11 18:20:40 +02:00
|
|
|
"fmt"
|
2019-06-15 17:01:05 +02:00
|
|
|
"image"
|
2019-01-17 13:13:25 +01:00
|
|
|
"image/color"
|
2018-04-10 18:31:28 +02:00
|
|
|
|
2019-01-17 13:13:25 +01:00
|
|
|
"maunium.net/go/mautrix"
|
2019-06-15 00:11:51 +02:00
|
|
|
"maunium.net/go/mauview"
|
2019-01-17 13:13:25 +01:00
|
|
|
"maunium.net/go/tcell"
|
2018-04-10 18:31:28 +02:00
|
|
|
|
2018-06-01 23:44:21 +02:00
|
|
|
"maunium.net/go/gomuks/config"
|
2018-04-10 18:31:28 +02:00
|
|
|
"maunium.net/go/gomuks/debug"
|
|
|
|
"maunium.net/go/gomuks/interface"
|
2018-04-11 18:20:40 +02:00
|
|
|
"maunium.net/go/gomuks/lib/ansimage"
|
2018-04-11 16:57:15 +02:00
|
|
|
"maunium.net/go/gomuks/ui/messages/tstring"
|
2018-04-10 18:31:28 +02:00
|
|
|
)
|
|
|
|
|
2018-04-13 20:25:45 +02:00
|
|
|
type ImageMessage struct {
|
2018-06-01 23:44:21 +02:00
|
|
|
Body string
|
2018-04-11 18:20:40 +02:00
|
|
|
Homeserver string
|
|
|
|
FileID string
|
|
|
|
data []byte
|
2019-06-15 00:11:51 +02:00
|
|
|
buffer []tstring.TString
|
2018-04-11 18:20:40 +02:00
|
|
|
|
2018-04-18 17:35:24 +02:00
|
|
|
matrix ifc.MatrixContainer
|
2018-04-10 18:31:28 +02:00
|
|
|
}
|
|
|
|
|
2018-04-13 20:25:45 +02:00
|
|
|
// NewImageMessage creates a new ImageMessage object with the provided values and the default state.
|
2019-06-15 00:11:51 +02:00
|
|
|
func NewImageMessage(matrix ifc.MatrixContainer, event *mautrix.Event, displayname string, body, homeserver, fileID string, data []byte) *UIMessage {
|
|
|
|
return newUIMessage(event, displayname, &ImageMessage{
|
|
|
|
Body: body,
|
|
|
|
Homeserver: homeserver,
|
|
|
|
FileID: fileID,
|
|
|
|
data: data,
|
|
|
|
matrix: matrix,
|
|
|
|
})
|
2018-04-11 18:20:40 +02:00
|
|
|
}
|
|
|
|
|
2019-06-15 00:11:51 +02:00
|
|
|
func (msg *ImageMessage) Clone() MessageRenderer {
|
2019-04-10 20:06:19 +02:00
|
|
|
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,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-18 17:35:24 +02:00
|
|
|
func (msg *ImageMessage) RegisterMatrix(matrix ifc.MatrixContainer) {
|
|
|
|
msg.matrix = matrix
|
2018-04-11 18:20:40 +02:00
|
|
|
|
|
|
|
if len(msg.data) == 0 {
|
2019-06-16 14:19:42 +02:00
|
|
|
go msg.updateData()
|
2018-04-11 18:20:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-13 20:25:45 +02:00
|
|
|
func (msg *ImageMessage) NotificationContent() string {
|
|
|
|
return "Sent an image"
|
|
|
|
}
|
|
|
|
|
2018-05-22 21:06:48 +02:00
|
|
|
func (msg *ImageMessage) PlainText() string {
|
|
|
|
return fmt.Sprintf("%s: %s", msg.Body, msg.matrix.GetDownloadURL(msg.Homeserver, msg.FileID))
|
|
|
|
}
|
|
|
|
|
2019-06-15 00:11:51 +02:00
|
|
|
func (msg *ImageMessage) String() string {
|
|
|
|
return fmt.Sprintf(`&messages.ImageMessage{Body="%s", Homeserver="%s", FileID="%s"}`, msg.Body, msg.Homeserver, msg.FileID)
|
|
|
|
}
|
|
|
|
|
2018-04-13 20:25:45 +02:00
|
|
|
func (msg *ImageMessage) updateData() {
|
2018-04-30 11:01:24 +02:00
|
|
|
defer debug.Recover()
|
2018-04-11 18:20:40 +02:00
|
|
|
debug.Print("Loading image:", msg.Homeserver, msg.FileID)
|
2018-04-18 17:35:24 +02:00
|
|
|
data, _, _, err := msg.matrix.Download(fmt.Sprintf("mxc://%s/%s", msg.Homeserver, msg.FileID))
|
2018-04-11 18:20:40 +02:00
|
|
|
if err != nil {
|
2019-03-25 23:37:35 +01:00
|
|
|
debug.Printf("Failed to download image %s/%s: %v", msg.Homeserver, msg.FileID, err)
|
2018-04-11 18:20:40 +02:00
|
|
|
return
|
2018-04-10 18:31:28 +02:00
|
|
|
}
|
2018-04-30 11:01:24 +02:00
|
|
|
debug.Print("Image", msg.Homeserver, msg.FileID, "loaded.")
|
2018-04-11 18:20:40 +02:00
|
|
|
msg.data = data
|
|
|
|
}
|
|
|
|
|
2018-04-13 20:25:45 +02:00
|
|
|
func (msg *ImageMessage) Path() string {
|
2018-04-18 17:35:24 +02:00
|
|
|
return msg.matrix.GetCachePath(msg.Homeserver, msg.FileID)
|
2018-04-10 18:31:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
2019-06-15 17:01:05 +02:00
|
|
|
// parameter. If the message width is larger than the width of the buffer
|
|
|
|
// the message is scaled to one third the buffer width.
|
2019-06-15 00:11:51 +02:00
|
|
|
func (msg *ImageMessage) CalculateBuffer(prefs config.UserPreferences, width int, uiMsg *UIMessage) {
|
2018-04-10 18:31:28 +02:00
|
|
|
if width < 2 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-06-01 23:43:56 +02:00
|
|
|
if prefs.BareMessageView || prefs.DisableImages {
|
2019-06-15 00:11:51 +02:00
|
|
|
msg.buffer = calculateBufferWithText(prefs, tstring.NewTString(msg.PlainText()), width, uiMsg)
|
2018-05-22 21:06:48 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-06-15 17:01:05 +02:00
|
|
|
img, _, err := image.DecodeConfig(bytes.NewReader(msg.data))
|
|
|
|
if err != nil {
|
|
|
|
debug.Print("Image 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)
|
2018-04-10 18:31:28 +02:00
|
|
|
if err != nil {
|
2018-04-11 16:57:15 +02:00
|
|
|
msg.buffer = []tstring.TString{tstring.NewColorTString("Failed to display image", tcell.ColorRed)}
|
2018-04-10 18:31:28 +02:00
|
|
|
debug.Print("Failed to display image:", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-06-15 17:01:05 +02:00
|
|
|
msg.buffer = ansImage.Render()
|
2018-04-13 20:25:45 +02:00
|
|
|
}
|
2019-06-15 00:11:51 +02:00
|
|
|
|
|
|
|
func (msg *ImageMessage) Height() int {
|
|
|
|
return len(msg.buffer)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (msg *ImageMessage) Draw(screen mauview.Screen) {
|
|
|
|
for y, line := range msg.buffer {
|
|
|
|
line.Draw(screen, 0, y)
|
|
|
|
}
|
|
|
|
}
|