2018-04-13 20:25:45 +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-13 20:25:45 +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-13 20:25:45 +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-13 20:25:45 +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-13 20:25:45 +02:00
|
|
|
|
|
|
|
package messages
|
|
|
|
|
|
|
|
import (
|
2019-04-09 17:45:41 +02:00
|
|
|
"encoding/json"
|
2019-04-12 23:51:58 +02:00
|
|
|
"fmt"
|
2018-04-13 20:25:45 +02:00
|
|
|
"time"
|
|
|
|
|
2019-04-10 01:19:38 +02:00
|
|
|
"maunium.net/go/gomuks/config"
|
2019-01-17 13:13:25 +01:00
|
|
|
"maunium.net/go/mautrix"
|
2019-04-07 17:21:38 +02:00
|
|
|
"maunium.net/go/mauview"
|
2019-01-17 13:13:25 +01:00
|
|
|
"maunium.net/go/tcell"
|
|
|
|
|
2018-04-13 20:25:45 +02:00
|
|
|
"maunium.net/go/gomuks/interface"
|
|
|
|
"maunium.net/go/gomuks/ui/widget"
|
|
|
|
)
|
|
|
|
|
2019-06-15 00:11:51 +02:00
|
|
|
type MessageRenderer interface {
|
|
|
|
Draw(screen mauview.Screen)
|
|
|
|
NotificationContent() string
|
|
|
|
PlainText() string
|
|
|
|
CalculateBuffer(prefs config.UserPreferences, width int, msg *UIMessage)
|
|
|
|
RegisterMatrix(matrix ifc.MatrixContainer)
|
|
|
|
Height() int
|
|
|
|
Clone() MessageRenderer
|
|
|
|
String() string
|
|
|
|
}
|
|
|
|
|
|
|
|
type UIMessage struct {
|
|
|
|
EventID string
|
|
|
|
TxnID string
|
|
|
|
Relation mautrix.RelatesTo
|
|
|
|
Type mautrix.MessageType
|
|
|
|
SenderID string
|
|
|
|
SenderName string
|
|
|
|
DefaultSenderColor tcell.Color
|
|
|
|
Timestamp time.Time
|
|
|
|
State mautrix.OutgoingEventState
|
|
|
|
IsHighlight bool
|
|
|
|
IsService bool
|
|
|
|
Source json.RawMessage
|
|
|
|
ReplyTo *UIMessage
|
|
|
|
Renderer MessageRenderer
|
|
|
|
}
|
|
|
|
|
|
|
|
const DateFormat = "January _2, 2006"
|
|
|
|
const TimeFormat = "15:04:05"
|
|
|
|
|
|
|
|
func newUIMessage(event *mautrix.Event, displayname string, renderer MessageRenderer) *UIMessage {
|
2019-04-09 17:45:41 +02:00
|
|
|
msgtype := event.Content.MsgType
|
|
|
|
if len(msgtype) == 0 {
|
|
|
|
msgtype = mautrix.MessageType(event.Type.String())
|
|
|
|
}
|
|
|
|
|
2019-06-15 00:11:51 +02:00
|
|
|
return &UIMessage{
|
|
|
|
SenderID: event.Sender,
|
|
|
|
SenderName: displayname,
|
|
|
|
Timestamp: unixToTime(event.Timestamp),
|
|
|
|
DefaultSenderColor: widget.GetHashColor(event.Sender),
|
|
|
|
Type: msgtype,
|
|
|
|
EventID: event.ID,
|
|
|
|
TxnID: event.Unsigned.TransactionID,
|
|
|
|
Relation: *event.Content.GetRelatesTo(),
|
|
|
|
State: event.Unsigned.OutgoingState,
|
|
|
|
IsHighlight: false,
|
|
|
|
IsService: false,
|
|
|
|
Source: event.Content.VeryRaw,
|
|
|
|
Renderer: renderer,
|
2018-04-13 20:25:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-09 17:45:41 +02:00
|
|
|
func unixToTime(unix int64) time.Time {
|
|
|
|
timestamp := time.Now()
|
|
|
|
if unix != 0 {
|
|
|
|
timestamp = time.Unix(unix/1000, unix%1000*1000)
|
|
|
|
}
|
|
|
|
return timestamp
|
|
|
|
}
|
|
|
|
|
2018-04-13 20:25:45 +02:00
|
|
|
// Sender gets the string that should be displayed as the sender of this message.
|
|
|
|
//
|
|
|
|
// If the message is being sent, the sender is "Sending...".
|
|
|
|
// If sending has failed, the sender is "Error".
|
|
|
|
// If the message is an emote, the sender is blank.
|
|
|
|
// In any other case, the sender is the display name of the user who sent the message.
|
2019-06-15 00:11:51 +02:00
|
|
|
func (msg *UIMessage) Sender() string {
|
|
|
|
switch msg.State {
|
2019-04-09 17:45:41 +02:00
|
|
|
case mautrix.EventStateLocalEcho:
|
2018-04-13 20:25:45 +02:00
|
|
|
return "Sending..."
|
2019-04-09 17:45:41 +02:00
|
|
|
case mautrix.EventStateSendFail:
|
2018-04-13 20:25:45 +02:00
|
|
|
return "Error"
|
|
|
|
}
|
2019-06-15 00:11:51 +02:00
|
|
|
switch msg.Type {
|
2018-04-13 20:25:45 +02:00
|
|
|
case "m.emote":
|
|
|
|
// Emotes don't show a separate sender, it's included in the buffer.
|
|
|
|
return ""
|
|
|
|
default:
|
2019-06-15 00:11:51 +02:00
|
|
|
return msg.SenderName
|
2018-04-13 20:25:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-15 00:11:51 +02:00
|
|
|
func (msg *UIMessage) NotificationSenderName() string {
|
|
|
|
return msg.SenderName
|
2018-04-18 12:38:33 +02:00
|
|
|
}
|
|
|
|
|
2019-06-15 00:11:51 +02:00
|
|
|
func (msg *UIMessage) NotificationContent() string {
|
|
|
|
return msg.Renderer.NotificationContent()
|
2018-04-13 20:25:45 +02:00
|
|
|
}
|
|
|
|
|
2019-06-15 00:11:51 +02:00
|
|
|
func (msg *UIMessage) getStateSpecificColor() tcell.Color {
|
|
|
|
switch msg.State {
|
2019-04-09 17:45:41 +02:00
|
|
|
case mautrix.EventStateLocalEcho:
|
2018-04-13 20:25:45 +02:00
|
|
|
return tcell.ColorGray
|
2019-04-09 17:45:41 +02:00
|
|
|
case mautrix.EventStateSendFail:
|
2018-04-13 20:25:45 +02:00
|
|
|
return tcell.ColorRed
|
2019-04-09 17:45:41 +02:00
|
|
|
case mautrix.EventStateDefault:
|
2018-04-13 20:25:45 +02:00
|
|
|
fallthrough
|
|
|
|
default:
|
|
|
|
return tcell.ColorDefault
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// SenderColor returns the color the name of the sender should be shown in.
|
|
|
|
//
|
|
|
|
// If the message is being sent, the color is gray.
|
|
|
|
// If sending has failed, the color is red.
|
|
|
|
//
|
|
|
|
// In any other case, the color is whatever is specified in the Message struct.
|
|
|
|
// Usually that means it is the hash-based color of the sender (see ui/widget/color.go)
|
2019-06-15 00:11:51 +02:00
|
|
|
func (msg *UIMessage) SenderColor() tcell.Color {
|
2018-04-13 20:25:45 +02:00
|
|
|
stateColor := msg.getStateSpecificColor()
|
|
|
|
switch {
|
|
|
|
case stateColor != tcell.ColorDefault:
|
|
|
|
return stateColor
|
2019-06-15 00:11:51 +02:00
|
|
|
case msg.Type == "m.room.member":
|
|
|
|
return widget.GetHashColor(msg.SenderName)
|
|
|
|
case msg.IsService:
|
2018-04-13 20:25:45 +02:00
|
|
|
return tcell.ColorGray
|
|
|
|
default:
|
2019-06-15 00:11:51 +02:00
|
|
|
return msg.DefaultSenderColor
|
2018-04-13 20:25:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TextColor returns the color the actual content of the message should be shown in.
|
2019-06-15 00:11:51 +02:00
|
|
|
func (msg *UIMessage) TextColor() tcell.Color {
|
2018-04-13 20:25:45 +02:00
|
|
|
stateColor := msg.getStateSpecificColor()
|
|
|
|
switch {
|
|
|
|
case stateColor != tcell.ColorDefault:
|
|
|
|
return stateColor
|
2019-06-15 00:11:51 +02:00
|
|
|
case msg.IsService, msg.Type == "m.notice":
|
2018-04-13 20:25:45 +02:00
|
|
|
return tcell.ColorGray
|
2019-06-15 00:11:51 +02:00
|
|
|
case msg.IsHighlight:
|
2018-04-13 20:25:45 +02:00
|
|
|
return tcell.ColorYellow
|
2019-06-15 00:11:51 +02:00
|
|
|
case msg.Type == "m.room.member":
|
2018-04-13 20:25:45 +02:00
|
|
|
return tcell.ColorGreen
|
|
|
|
default:
|
|
|
|
return tcell.ColorDefault
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TimestampColor returns the color the timestamp should be shown in.
|
|
|
|
//
|
|
|
|
// As with SenderColor(), messages being sent and messages that failed to be sent are
|
|
|
|
// gray and red respectively.
|
|
|
|
//
|
|
|
|
// However, other messages are the default color instead of a color stored in the struct.
|
2019-06-15 00:11:51 +02:00
|
|
|
func (msg *UIMessage) TimestampColor() tcell.Color {
|
|
|
|
if msg.IsService {
|
2019-04-10 16:08:39 +02:00
|
|
|
return tcell.ColorGray
|
|
|
|
}
|
2018-04-13 20:25:45 +02:00
|
|
|
return msg.getStateSpecificColor()
|
|
|
|
}
|
|
|
|
|
2019-06-15 00:11:51 +02:00
|
|
|
func (msg *UIMessage) ReplyHeight() int {
|
2019-04-10 01:19:38 +02:00
|
|
|
if msg.ReplyTo != nil {
|
2019-04-13 16:04:52 +02:00
|
|
|
return 1 + msg.ReplyTo.Height()
|
2019-04-10 01:19:38 +02:00
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2018-04-13 20:25:45 +02:00
|
|
|
// Height returns the number of rows in the computed buffer (see Buffer()).
|
2019-06-15 00:11:51 +02:00
|
|
|
func (msg *UIMessage) Height() int {
|
|
|
|
return msg.ReplyHeight() + msg.Renderer.Height()
|
2018-04-13 20:25:45 +02:00
|
|
|
}
|
|
|
|
|
2019-06-15 00:11:51 +02:00
|
|
|
func (msg *UIMessage) Time() time.Time {
|
|
|
|
return msg.Timestamp
|
2018-04-13 20:25:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// FormatTime returns the formatted time when the message was sent.
|
2019-06-15 00:11:51 +02:00
|
|
|
func (msg *UIMessage) FormatTime() string {
|
|
|
|
return msg.Timestamp.Format(TimeFormat)
|
2018-04-13 20:25:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// FormatDate returns the formatted date when the message was sent.
|
2019-06-15 00:11:51 +02:00
|
|
|
func (msg *UIMessage) FormatDate() string {
|
|
|
|
return msg.Timestamp.Format(DateFormat)
|
2018-04-13 20:25:45 +02:00
|
|
|
}
|
|
|
|
|
2019-06-15 00:11:51 +02:00
|
|
|
func (msg *UIMessage) SameDate(message *UIMessage) bool {
|
|
|
|
year1, month1, day1 := msg.Timestamp.Date()
|
|
|
|
year2, month2, day2 := message.Timestamp.Date()
|
2019-04-10 00:42:27 +02:00
|
|
|
return day1 == day2 && month1 == month2 && year1 == year2
|
|
|
|
}
|
|
|
|
|
2019-06-15 00:11:51 +02:00
|
|
|
func (msg *UIMessage) ID() string {
|
|
|
|
if len(msg.EventID) == 0 {
|
|
|
|
return msg.TxnID
|
2019-04-10 00:04:39 +02:00
|
|
|
}
|
2019-06-15 00:11:51 +02:00
|
|
|
return msg.EventID
|
2018-04-13 20:25:45 +02:00
|
|
|
}
|
|
|
|
|
2019-06-15 00:11:51 +02:00
|
|
|
func (msg *UIMessage) SetID(id string) {
|
|
|
|
msg.EventID = id
|
2019-04-09 17:45:41 +02:00
|
|
|
}
|
|
|
|
|
2019-06-15 00:11:51 +02:00
|
|
|
func (msg *UIMessage) SetIsHighlight(isHighlight bool) {
|
|
|
|
// TODO Textmessage cache needs to be cleared
|
|
|
|
msg.IsHighlight = isHighlight
|
2019-04-10 01:19:38 +02:00
|
|
|
}
|
|
|
|
|
2019-06-15 00:11:51 +02:00
|
|
|
func (msg *UIMessage) Draw(screen mauview.Screen) {
|
2019-04-10 01:19:38 +02:00
|
|
|
screen = msg.DrawReply(screen)
|
2019-06-15 00:11:51 +02:00
|
|
|
msg.Renderer.Draw(screen)
|
2019-04-07 17:21:38 +02:00
|
|
|
}
|
2019-04-10 01:19:38 +02:00
|
|
|
|
2019-06-15 00:11:51 +02:00
|
|
|
func (msg *UIMessage) Clone() *UIMessage {
|
2019-04-10 20:06:19 +02:00
|
|
|
clone := *msg
|
2019-06-16 13:29:03 +02:00
|
|
|
clone.ReplyTo = nil
|
2019-06-15 00:11:51 +02:00
|
|
|
clone.Renderer = clone.Renderer.Clone()
|
|
|
|
return &clone
|
2019-04-10 20:06:19 +02:00
|
|
|
}
|
|
|
|
|
2019-06-15 00:11:51 +02:00
|
|
|
func (msg *UIMessage) CalculateReplyBuffer(preferences config.UserPreferences, width int) {
|
2019-04-10 01:19:38 +02:00
|
|
|
if msg.ReplyTo == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
msg.ReplyTo.CalculateBuffer(preferences, width-1)
|
|
|
|
}
|
|
|
|
|
2019-06-15 00:11:51 +02:00
|
|
|
func (msg *UIMessage) CalculateBuffer(preferences config.UserPreferences, width int) {
|
2019-06-16 13:29:03 +02:00
|
|
|
msg.Renderer.CalculateBuffer(preferences, width, msg)
|
|
|
|
msg.CalculateReplyBuffer(preferences, width)
|
2019-06-15 00:11:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (msg *UIMessage) DrawReply(screen mauview.Screen) mauview.Screen {
|
2019-04-10 01:19:38 +02:00
|
|
|
if msg.ReplyTo == nil {
|
|
|
|
return screen
|
|
|
|
}
|
|
|
|
width, height := screen.Size()
|
|
|
|
replyHeight := msg.ReplyTo.Height()
|
2019-04-13 16:04:52 +02:00
|
|
|
widget.WriteLineSimpleColor(screen, "In reply to", 1, 0, tcell.ColorGreen)
|
2019-06-15 00:11:51 +02:00
|
|
|
widget.WriteLineSimpleColor(screen, msg.ReplyTo.SenderName, 13, 0, msg.ReplyTo.SenderColor())
|
2019-04-13 16:04:52 +02:00
|
|
|
for y := 0; y < 1+replyHeight; y++ {
|
|
|
|
screen.SetCell(0, y, tcell.StyleDefault, '▊')
|
2019-04-10 01:19:38 +02:00
|
|
|
}
|
|
|
|
replyScreen := mauview.NewProxyScreen(screen, 1, 1, width-1, replyHeight)
|
|
|
|
msg.ReplyTo.Draw(replyScreen)
|
2019-04-13 16:04:52 +02:00
|
|
|
return mauview.NewProxyScreen(screen, 0, replyHeight+1, width, height-replyHeight-1)
|
2019-04-10 01:19:38 +02:00
|
|
|
}
|
2019-04-12 23:51:58 +02:00
|
|
|
|
2019-06-15 00:11:51 +02:00
|
|
|
func (msg *UIMessage) String() string {
|
|
|
|
return fmt.Sprintf(`&messages.UIMessage{
|
2019-04-12 23:51:58 +02:00
|
|
|
ID="%s", TxnID="%s",
|
|
|
|
Type="%s", Timestamp=%s,
|
|
|
|
Sender={ID="%s", Name="%s", Color=#%X},
|
|
|
|
IsService=%t, IsHighlight=%t,
|
2019-06-15 00:11:51 +02:00
|
|
|
Renderer=%s,
|
2019-04-12 23:51:58 +02:00
|
|
|
}`,
|
2019-06-15 00:11:51 +02:00
|
|
|
msg.EventID, msg.TxnID,
|
|
|
|
msg.Type, msg.Timestamp.String(),
|
|
|
|
msg.SenderID, msg.SenderName, msg.DefaultSenderColor.Hex(),
|
|
|
|
msg.IsService, msg.IsHighlight, msg.Renderer.String(),
|
2019-04-12 23:51:58 +02:00
|
|
|
)
|
|
|
|
}
|
2019-06-15 00:11:51 +02:00
|
|
|
|
|
|
|
func (msg *UIMessage) PlainText() string {
|
|
|
|
return msg.Renderer.PlainText()
|
|
|
|
}
|