Merge remote-tracking branch 'pike4000/video'
This commit is contained in:
commit
80564b2887
@ -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.
|
||||
|
@ -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 <rooms/users/baremessages/images/typingnotif/emojis>")
|
||||
return
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
@ -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) {}
|
||||
|
@ -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,9 +43,9 @@ 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{
|
||||
// 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,
|
||||
@ -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))
|
||||
copy(data, msg.data)
|
||||
return &ImageMessage{
|
||||
return &FileMessage{
|
||||
Body: msg.Body,
|
||||
Homeserver: msg.Homeserver,
|
||||
FileID: msg.FileID,
|
||||
@ -66,80 +66,76 @@ func (msg *ImageMessage) Clone() MessageRenderer {
|
||||
}
|
||||
}
|
||||
|
||||
func (msg *ImageMessage) 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()
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
@ -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{
|
||||
|
@ -138,12 +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)
|
||||
return NewFileMessage(matrix, evt, displayname, evt.Content.Body, hs, id, data)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
@ -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) {}
|
||||
|
@ -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) {}
|
||||
|
Loading…
Reference in New Issue
Block a user