2018-04-10 18:31:28 +02:00
|
|
|
// gomuks - A terminal Matrix client written in Go.
|
2020-04-19 17:10:14 +02:00
|
|
|
// Copyright (C) 2020 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
|
|
|
|
2020-04-16 18:27:35 +02:00
|
|
|
"maunium.net/go/gomuks/matrix/muksevt"
|
|
|
|
"maunium.net/go/mautrix/event"
|
|
|
|
"maunium.net/go/mautrix/id"
|
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
|
|
|
)
|
|
|
|
|
2020-04-07 18:06:20 +02:00
|
|
|
type FileMessage struct {
|
2020-04-16 18:27:35 +02:00
|
|
|
Type event.MessageType
|
2020-04-08 14:30:29 +02:00
|
|
|
Body string
|
2020-04-16 18:27:35 +02:00
|
|
|
URL id.ContentURI
|
|
|
|
Thumbnail id.ContentURI
|
2020-04-08 14:30:29 +02:00
|
|
|
imageData []byte
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-04-07 18:06:20 +02:00
|
|
|
// NewFileMessage creates a new FileMessage object with the provided values and the default state.
|
2020-04-16 18:27:35 +02:00
|
|
|
func NewFileMessage(matrix ifc.MatrixContainer, evt *muksevt.Event, displayname string) *UIMessage {
|
2020-04-19 14:00:49 +02:00
|
|
|
content := evt.Content.AsMessage()
|
|
|
|
url, _ := content.URL.Parse()
|
|
|
|
thumbnail, _ := content.GetInfo().ThumbnailURL.Parse()
|
2020-04-07 18:06:20 +02:00
|
|
|
return newUIMessage(evt, displayname, &FileMessage{
|
2020-04-19 14:00:49 +02:00
|
|
|
Type: content.MsgType,
|
|
|
|
Body: content.Body,
|
2020-04-08 14:30:29 +02:00
|
|
|
URL: url,
|
|
|
|
Thumbnail: thumbnail,
|
|
|
|
matrix: matrix,
|
2019-06-15 00:11:51 +02:00
|
|
|
})
|
2018-04-11 18:20:40 +02:00
|
|
|
}
|
|
|
|
|
2020-04-07 18:06:20 +02:00
|
|
|
func (msg *FileMessage) Clone() MessageRenderer {
|
2020-04-08 14:30:29 +02:00
|
|
|
data := make([]byte, len(msg.imageData))
|
|
|
|
copy(data, msg.imageData)
|
2020-04-07 18:06:20 +02:00
|
|
|
return &FileMessage{
|
2020-04-08 14:30:29 +02:00
|
|
|
Body: msg.Body,
|
|
|
|
URL: msg.URL,
|
|
|
|
Thumbnail: msg.Thumbnail,
|
|
|
|
imageData: data,
|
|
|
|
matrix: msg.matrix,
|
2018-04-11 18:20:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-07 18:06:20 +02:00
|
|
|
func (msg *FileMessage) NotificationContent() string {
|
2020-04-08 14:30:29 +02:00
|
|
|
switch msg.Type {
|
2020-04-16 18:27:35 +02:00
|
|
|
case event.MsgImage:
|
2020-04-08 14:30:29 +02:00
|
|
|
return "Sent an image"
|
2020-04-16 18:27:35 +02:00
|
|
|
case event.MsgAudio:
|
2020-04-08 14:30:29 +02:00
|
|
|
return "Sent an audio file"
|
2020-04-16 18:27:35 +02:00
|
|
|
case event.MsgVideo:
|
2020-04-08 14:30:29 +02:00
|
|
|
return "Sent a video"
|
2020-04-16 18:27:35 +02:00
|
|
|
case event.MsgFile:
|
2020-04-08 14:30:29 +02:00
|
|
|
fallthrough
|
|
|
|
default:
|
|
|
|
return "Sent a file"
|
|
|
|
}
|
2018-04-13 20:25:45 +02:00
|
|
|
}
|
|
|
|
|
2020-04-07 18:06:20 +02:00
|
|
|
func (msg *FileMessage) PlainText() string {
|
2020-04-08 14:30:29 +02:00
|
|
|
return fmt.Sprintf("%s: %s", msg.Body, msg.matrix.GetDownloadURL(msg.URL))
|
2018-05-22 21:06:48 +02:00
|
|
|
}
|
|
|
|
|
2020-04-07 18:06:20 +02:00
|
|
|
func (msg *FileMessage) String() string {
|
2020-04-08 14:30:29 +02:00
|
|
|
return fmt.Sprintf(`&messages.FileMessage{Body="%s", URL="%s", Thumbnail="%s"}`, msg.Body, msg.URL, msg.Thumbnail)
|
2019-06-15 00:11:51 +02:00
|
|
|
}
|
|
|
|
|
2020-04-08 14:30:29 +02:00
|
|
|
func (msg *FileMessage) DownloadPreview() {
|
|
|
|
url := msg.Thumbnail
|
|
|
|
if url.IsEmpty() {
|
2020-04-16 18:27:35 +02:00
|
|
|
if msg.Type == event.MsgImage && !msg.URL.IsEmpty() {
|
2020-04-08 14:30:29 +02:00
|
|
|
msg.Thumbnail = msg.URL
|
|
|
|
url = msg.Thumbnail
|
|
|
|
} else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
debug.Print("Loading file:", url)
|
|
|
|
data, err := msg.matrix.Download(url)
|
2018-04-11 18:20:40 +02:00
|
|
|
if err != nil {
|
2020-04-08 14:30:29 +02:00
|
|
|
debug.Printf("Failed to download file %s: %v", url, err)
|
2018-04-11 18:20:40 +02:00
|
|
|
return
|
2018-04-10 18:31:28 +02:00
|
|
|
}
|
2020-04-08 14:30:29 +02:00
|
|
|
debug.Print("File", url, "loaded.")
|
|
|
|
msg.imageData = data
|
2018-04-11 18:20:40 +02:00
|
|
|
}
|
|
|
|
|
2020-04-08 14:30:29 +02:00
|
|
|
func (msg *FileMessage) ThumbnailPath() string {
|
|
|
|
return msg.matrix.GetCachePath(msg.Thumbnail)
|
2018-04-10 18:31:28 +02:00
|
|
|
}
|
|
|
|
|
2020-04-07 18:06:20 +02:00
|
|
|
func (msg *FileMessage) CalculateBuffer(prefs config.UserPreferences, width int, uiMsg *UIMessage) {
|
2018-04-10 18:31:28 +02:00
|
|
|
if width < 2 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-04-08 14:30:29 +02:00
|
|
|
if prefs.BareMessageView || prefs.DisableImages || len(msg.imageData) == 0 {
|
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
|
|
|
|
}
|
|
|
|
|
2020-04-08 14:30:29 +02:00
|
|
|
img, _, err := image.DecodeConfig(bytes.NewReader(msg.imageData))
|
2019-06-15 17:01:05 +02:00
|
|
|
if err != nil {
|
2020-04-07 18:06:20 +02:00
|
|
|
debug.Print("File could not be decoded:", err)
|
2019-06-15 17:01:05 +02:00
|
|
|
}
|
|
|
|
imgWidth := img.Width
|
|
|
|
if img.Width > width {
|
|
|
|
imgWidth = width / 3
|
|
|
|
}
|
|
|
|
|
2020-04-08 14:30:29 +02:00
|
|
|
ansFile, err := ansimage.NewScaledFromReader(bytes.NewReader(msg.imageData), 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
|
|
|
|
}
|
|
|
|
|
2020-04-07 18:06:20 +02:00
|
|
|
msg.buffer = ansFile.Render()
|
2018-04-13 20:25:45 +02:00
|
|
|
}
|
2019-06-15 00:11:51 +02:00
|
|
|
|
2020-04-07 18:06:20 +02:00
|
|
|
func (msg *FileMessage) Height() int {
|
2019-06-15 00:11:51 +02:00
|
|
|
return len(msg.buffer)
|
|
|
|
}
|
|
|
|
|
2020-04-07 18:06:20 +02:00
|
|
|
func (msg *FileMessage) Draw(screen mauview.Screen) {
|
2019-06-15 00:11:51 +02:00
|
|
|
for y, line := range msg.buffer {
|
|
|
|
line.Draw(screen, 0, y)
|
|
|
|
}
|
|
|
|
}
|