| 1 |
// gomuks - A terminal Matrix client written in Go. |
| 2 |
// Copyright (C) 2020 Tulir Asokan |
| 3 |
// |
| 4 |
// This program is free software: you can redistribute it and/or modify |
| 5 |
// it under the terms of the GNU Affero General Public License as published by |
| 6 |
// the Free Software Foundation, either version 3 of the License, or |
| 7 |
// (at your option) any later version. |
| 8 |
// |
| 9 |
// This program is distributed in the hope that it will be useful, |
| 10 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
// GNU Affero General Public License for more details. |
| 13 |
// |
| 14 |
// You should have received a copy of the GNU Affero General Public License |
| 15 |
// along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 16 |
|
| 17 |
package messages |
| 18 |
|
| 19 |
import ( |
| 20 |
"bytes" |
| 21 |
"fmt" |
| 22 |
"image" |
| 23 |
"image/color" |
| 24 |
|
| 25 |
"maunium.net/go/mautrix/crypto/attachment" |
| 26 |
"maunium.net/go/mautrix/event" |
| 27 |
"maunium.net/go/mautrix/id" |
| 28 |
|
| 29 |
"go.mau.fi/mauview" |
| •30 |
"github.com/gdamore/tcell/v2" |
| 31 |
|
| 32 |
"maunium.net/go/gomuks/config" |
| 33 |
"maunium.net/go/gomuks/debug" |
| 34 |
ifc "maunium.net/go/gomuks/interface" |
| 35 |
"maunium.net/go/gomuks/lib/ansimage" |
| 36 |
"maunium.net/go/gomuks/matrix/muksevt" |
| 37 |
"maunium.net/go/gomuks/ui/messages/tstring" |
| 38 |
) |
| 39 |
|
| 40 |
type FileMessage struct { |
| 41 |
Type event.MessageType |
| 42 |
Body string |
| 43 |
|
| 44 |
URL id.ContentURI |
| 45 |
File *attachment.EncryptedFile |
| 46 |
Thumbnail id.ContentURI |
| 47 |
ThumbnailFile *attachment.EncryptedFile |
| 48 |
|
| 49 |
eventID id.EventID |
| 50 |
|
| 51 |
imageData []byte |
| 52 |
buffer []tstring.TString |
| 53 |
|
| 54 |
matrix ifc.MatrixContainer |
| 55 |
} |
| 56 |
|
| 57 |
// NewFileMessage creates a new FileMessage object with the provided values and the default state. |
| 58 |
func NewFileMessage(matrix ifc.MatrixContainer, evt *muksevt.Event, displayname string) *UIMessage { |
| 59 |
content := evt.Content.AsMessage() |
| 60 |
var file, thumbnailFile *attachment.EncryptedFile |
| 61 |
if content.File != nil { |
| 62 |
file = &content.File.EncryptedFile |
| 63 |
content.URL = content.File.URL |
| 64 |
} |
| 65 |
if content.GetInfo().ThumbnailFile != nil { |
| 66 |
thumbnailFile = &content.Info.ThumbnailFile.EncryptedFile |
| 67 |
content.Info.ThumbnailURL = content.Info.ThumbnailFile.URL |
| 68 |
} |
| 69 |
return newUIMessage(evt, displayname, &FileMessage{ |
| 70 |
Type: content.MsgType, |
| 71 |
Body: content.Body, |
| 72 |
URL: content.URL.ParseOrIgnore(), |
| 73 |
File: file, |
| 74 |
Thumbnail: content.GetInfo().ThumbnailURL.ParseOrIgnore(), |
| 75 |
ThumbnailFile: thumbnailFile, |
| 76 |
eventID: evt.ID, |
| 77 |
matrix: matrix, |
| 78 |
}) |
| 79 |
} |
| 80 |
|
| 81 |
func (msg *FileMessage) Clone() MessageRenderer { |
| 82 |
data := make([]byte, len(msg.imageData)) |
| 83 |
copy(data, msg.imageData) |
| 84 |
return &FileMessage{ |
| 85 |
Body: msg.Body, |
| 86 |
URL: msg.URL, |
| 87 |
Thumbnail: msg.Thumbnail, |
| 88 |
imageData: data, |
| 89 |
matrix: msg.matrix, |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
func (msg *FileMessage) NotificationContent() string { |
| 94 |
switch msg.Type { |
| 95 |
case event.MsgImage: |
| 96 |
return "Sent an image" |
| 97 |
case event.MsgAudio: |
| 98 |
return "Sent an audio file" |
| 99 |
case event.MsgVideo: |
| 100 |
return "Sent a video" |
| 101 |
case event.MsgFile: |
| 102 |
fallthrough |
| 103 |
default: |
| 104 |
return "Sent a file" |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
func (msg *FileMessage) PlainText() string { |
| 109 |
return fmt.Sprintf("%s: %s", msg.Body, msg.matrix.GetDownloadURL(msg.URL, msg.File)) |
| 110 |
} |
| 111 |
|
| 112 |
func (msg *FileMessage) String() string { |
| 113 |
return fmt.Sprintf(`&messages.FileMessage{Body="%s", URL="%s", Thumbnail="%s"}`, msg.Body, msg.URL, msg.Thumbnail) |
| 114 |
} |
| 115 |
|
| 116 |
func (msg *FileMessage) DownloadPreview() { |
| 117 |
var url id.ContentURI |
| 118 |
var file *attachment.EncryptedFile |
| 119 |
if !msg.Thumbnail.IsEmpty() { |
| 120 |
url = msg.Thumbnail |
| 121 |
file = msg.ThumbnailFile |
| 122 |
} else if msg.Type == event.MsgImage && !msg.URL.IsEmpty() { |
| 123 |
msg.Thumbnail = msg.URL |
| 124 |
url = msg.URL |
| 125 |
file = msg.File |
| 126 |
} else { |
| 127 |
return |
| 128 |
} |
| 129 |
debug.Print("Loading file:", url) |
| 130 |
data, err := msg.matrix.Download(url, file) |
| 131 |
if err != nil { |
| 132 |
debug.Printf("Failed to download file %s: %v", url, err) |
| 133 |
return |
| 134 |
} |
| 135 |
debug.Print("File", url, "loaded.") |
| 136 |
msg.imageData = data |
| 137 |
} |
| 138 |
|
| 139 |
func (msg *FileMessage) ThumbnailPath() string { |
| 140 |
return msg.matrix.GetCachePath(msg.Thumbnail) |
| 141 |
} |
| 142 |
|
| 143 |
func (msg *FileMessage) CalculateBuffer(prefs config.UserPreferences, width int, uiMsg *UIMessage) { |
| 144 |
if width < 2 { |
| 145 |
return |
| 146 |
} |
| 147 |
|
| 148 |
if prefs.BareMessageView || prefs.DisableImages || len(msg.imageData) == 0 { |
| 149 |
url := msg.matrix.GetDownloadURL(msg.URL, msg.File) |
| 150 |
var urlTString tstring.TString |
| 151 |
if prefs.EnableInlineURLs() { |
| 152 |
urlTString = tstring.NewStyleTString(url, tcell.StyleDefault.Url(url).UrlId(msg.eventID.String())) |
| 153 |
} else { |
| 154 |
urlTString = tstring.NewTString(url) |
| 155 |
} |
| 156 |
text := tstring.NewTString(msg.Body). |
| 157 |
Append(": "). |
| 158 |
AppendTString(urlTString) |
| 159 |
msg.buffer = calculateBufferWithText(prefs, text, width, uiMsg) |
| 160 |
return |
| 161 |
} |
| 162 |
|
| 163 |
img, _, err := image.DecodeConfig(bytes.NewReader(msg.imageData)) |
| 164 |
if err != nil { |
| 165 |
debug.Print("File could not be decoded:", err) |
| 166 |
} |
| 167 |
imgWidth := img.Width |
| 168 |
if img.Width > width { |
| 169 |
imgWidth = width / 3 |
| 170 |
} |
| 171 |
|
| 172 |
ansFile, err := ansimage.NewScaledFromReader(bytes.NewReader(msg.imageData), 0, imgWidth, color.Black) |
| 173 |
if err != nil { |
| 174 |
msg.buffer = []tstring.TString{tstring.NewColorTString("Failed to display image", tcell.ColorRed)} |
| 175 |
debug.Print("Failed to display image:", err) |
| 176 |
return |
| 177 |
} |
| 178 |
|
| 179 |
msg.buffer = ansFile.Render() |
| 180 |
} |
| 181 |
|
| 182 |
func (msg *FileMessage) Height() int { |
| 183 |
return len(msg.buffer) |
| 184 |
} |
| 185 |
|
| 186 |
func (msg *FileMessage) Draw(screen mauview.Screen, _ *UIMessage) { |
| 187 |
for y, line := range msg.buffer { |
| 188 |
line.Draw(screen, 0, y) |
| 189 |
} |
| 190 |
} |