aboutsummaryrefslogtreecommitdiff
path: root/ui/messages/base.go
blob: d3bf251ffa9562740b07f6347f3475e0d1eb1e64 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
// gomuks - A terminal Matrix client written in Go.
// Copyright (C) 2020 Tulir Asokan
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// 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
// GNU Affero General Public License for more details.
//
// 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/>.

package messages

import (
	"fmt"
	"sort"
	"time"

	"maunium.net/go/gomuks/config"
	"maunium.net/go/gomuks/matrix/muksevt"
	"maunium.net/go/mautrix/event"
	"maunium.net/go/mautrix/id"
	"maunium.net/go/mauview"
	"maunium.net/go/tcell"

	"maunium.net/go/gomuks/ui/widget"
)

type MessageRenderer interface {
	Draw(screen mauview.Screen)
	NotificationContent() string
	PlainText() string
	CalculateBuffer(prefs config.UserPreferences, width int, msg *UIMessage)
	Height() int
	Clone() MessageRenderer
	String() string
}

type ReactionItem struct {
	Key   string
	Count int
}

func (ri ReactionItem) String() string {
	return fmt.Sprintf("%d×%s", ri.Count, ri.Key)
}

type ReactionSlice []ReactionItem

func (rs ReactionSlice) Len() int {
	return len(rs)
}

func (rs ReactionSlice) Less(i, j int) bool {
	return rs[i].Key < rs[j].Key
}

func (rs ReactionSlice) Swap(i, j int) {
	rs[i], rs[j] = rs[j], rs[i]
}

type UIMessage struct {
	EventID            id.EventID
	TxnID              string
	Relation           event.RelatesTo
	Type               event.MessageType
	SenderID           id.UserID
	SenderName         string
	DefaultSenderColor tcell.Color
	Timestamp          time.Time
	State              muksevt.OutgoingState
	IsHighlight        bool
	IsService          bool
	IsSelected         bool
	Edited             bool
	Event              *muksevt.Event
	ReplyTo            *UIMessage
	Reactions          ReactionSlice
	Renderer           MessageRenderer
}

func (msg *UIMessage) GetEvent() *muksevt.Event {
	if msg == nil {
		return nil
	}
	return msg.Event
}

const DateFormat = "January _2, 2006"
const TimeFormat = "15:04:05"

func newUIMessage(evt *muksevt.Event, displayname string, renderer MessageRenderer) *UIMessage {
	msgContent := evt.Content.AsMessage()
	msgtype := msgContent.MsgType
	if len(msgtype) == 0 {
		msgtype = event.MessageType(evt.Type.String())
	}

	reactions := make(ReactionSlice, 0, len(evt.Unsigned.Relations.Annotations.Map))
	for key, count := range evt.Unsigned.Relations.Annotations.Map {
		reactions = append(reactions, ReactionItem{
			Key:   key,
			Count: count,
		})
	}
	sort.Sort(reactions)

	return &UIMessage{
		SenderID:           evt.Sender,
		SenderName:         displayname,
		Timestamp:          unixToTime(evt.Timestamp),
		DefaultSenderColor: widget.GetHashColor(evt.Sender),
		Type:               msgtype,
		EventID:            evt.ID,
		TxnID:              evt.Unsigned.TransactionID,
		Relation:           *msgContent.GetRelatesTo(),
		State:              evt.Gomuks.OutgoingState,
		IsHighlight:        false,
		IsService:          false,
		Edited:             len(evt.Gomuks.Edits) > 0,
		Reactions:          reactions,
		Event:              evt,
		Renderer:           renderer,
	}
}

func (msg *UIMessage) AddReaction(key string) {
	found := false
	for _, rs := range msg.Reactions {
		if rs.Key == key {
			rs.Count++
			found = true
			break
		}
	}
	if !found {
		msg.Reactions = append(msg.Reactions, ReactionItem{
			Key:   key,
			Count: 1,
		})
	}
	sort.Sort(msg.Reactions)
}

func unixToTime(unix int64) time.Time {
	timestamp := time.Now()
	if unix != 0 {
		timestamp = time.Unix(unix/1000, unix%1000*1000)
	}
	return timestamp
}

// 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.
func (msg *UIMessage) Sender() string {
	switch msg.State {
	case muksevt.StateLocalEcho:
		return "Sending..."
	case muksevt.StateSendFail:
		return "Error"
	}
	switch msg.Type {
	case "m.emote":
		// Emotes don't show a separate sender, it's included in the buffer.
		return ""
	default:
		return msg.SenderName
	}
}

func (msg *UIMessage) NotificationSenderName() string {
	return msg.SenderName
}

func (msg *UIMessage) NotificationContent() string {
	return msg.Renderer.NotificationContent()
}

func (msg *UIMessage) getStateSpecificColor() tcell.Color {
	switch msg.State {
	case muksevt.StateLocalEcho:
		return tcell.ColorGray
	case muksevt.StateSendFail:
		return tcell.ColorRed
	case muksevt.StateDefault:
		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)
func (msg *UIMessage) SenderColor() tcell.Color {
	stateColor := msg.getStateSpecificColor()
	switch {
	case stateColor != tcell.ColorDefault:
		return stateColor
	case msg.Type == "m.room.member":
		return widget.GetHashColor(msg.SenderName)
	case msg.IsService:
		return tcell.ColorGray
	default:
		return msg.DefaultSenderColor
	}
}

// TextColor returns the color the actual content of the message should be shown in.
func (msg *UIMessage) TextColor() tcell.Color {
	stateColor := msg.getStateSpecificColor()
	switch {
	case stateColor != tcell.ColorDefault:
		return stateColor
	case msg.IsService, msg.Type == "m.notice":
		return tcell.ColorGray
	case msg.IsHighlight:
		return tcell.ColorYellow
	case msg.Type == "m.room.member":
		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.
func (msg *UIMessage) TimestampColor() tcell.Color {
	if msg.IsService {
		return tcell.ColorGray
	}
	return msg.getStateSpecificColor()
}

func (msg *UIMessage) ReplyHeight() int {
	if msg.ReplyTo != nil {
		return 1 + msg.ReplyTo.Height()
	}
	return 0
}

func (msg *UIMessage) ReactionHeight() int {
	if len(msg.Reactions) > 0 {
		return 1
	}
	return 0
}

// Height returns the number of rows in the computed buffer (see Buffer()).
func (msg *UIMessage) Height() int {
	return msg.ReplyHeight() + msg.Renderer.Height() + msg.ReactionHeight()
}

func (msg *UIMessage) Time() time.Time {
	return msg.Timestamp
}

// FormatTime returns the formatted time when the message was sent.
func (msg *UIMessage) FormatTime() string {
	return msg.Timestamp.Format(TimeFormat)
}

// FormatDate returns the formatted date when the message was sent.
func (msg *UIMessage) FormatDate() string {
	return msg.Timestamp.Format(DateFormat)
}

func (msg *UIMessage) SameDate(message *UIMessage) bool {
	year1, month1, day1 := msg.Timestamp.Date()
	year2, month2, day2 := message.Timestamp.Date()
	return day1 == day2 && month1 == month2 && year1 == year2
}

func (msg *UIMessage) ID() id.EventID {
	if len(msg.EventID) == 0 {
		return id.EventID(msg.TxnID)
	}
	return msg.EventID
}

func (msg *UIMessage) SetID(id id.EventID) {
	msg.EventID = id
}

func (msg *UIMessage) SetIsHighlight(isHighlight bool) {
	msg.IsHighlight = isHighlight
}

func (msg *UIMessage) DrawReactions(screen mauview.Screen) {
	if len(msg.Reactions) == 0 {
		return
	}
	width, height := screen.Size()
	screen = mauview.NewProxyScreen(screen, 0, height-1, width, 1)

	x := 0
	for _, reaction := range msg.Reactions {
		_, drawn := mauview.PrintWithStyle(screen, reaction.String(), x, 0, width-x, mauview.AlignLeft, tcell.StyleDefault.Foreground(mauview.Styles.PrimaryTextColor).Background(tcell.ColorDarkGreen))
		x += drawn + 1
		if x >= width {
			break
		}
	}
}

func (msg *UIMessage) Draw(screen mauview.Screen) {
	proxyScreen := msg.DrawReply(screen)
	msg.Renderer.Draw(proxyScreen)
	msg.DrawReactions(proxyScreen)
	if msg.IsSelected {
		w, h := screen.Size()
		for x := 0; x < w; x++ {
			for y := 0; y < h; y++ {
				mainc, combc, style, _ := screen.GetContent(x, y)
				_, bg, _ := style.Decompose()
				if bg == tcell.ColorDefault {
					screen.SetContent(x, y, mainc, combc, style.Background(tcell.ColorDarkGreen))
				}
			}
		}
	}
}

func (msg *UIMessage) Clone() *UIMessage {
	clone := *msg
	clone.ReplyTo = nil
	clone.Reactions = nil
	clone.Renderer = clone.Renderer.Clone()
	return &clone
}

func (msg *UIMessage) CalculateReplyBuffer(preferences config.UserPreferences, width int) {
	if msg.ReplyTo == nil {
		return
	}
	msg.ReplyTo.CalculateBuffer(preferences, width-1)
}

func (msg *UIMessage) CalculateBuffer(preferences config.UserPreferences, width int) {
	msg.Renderer.CalculateBuffer(preferences, width, msg)
	msg.CalculateReplyBuffer(preferences, width)
}

func (msg *UIMessage) DrawReply(screen mauview.Screen) mauview.Screen {
	if msg.ReplyTo == nil {
		return screen
	}
	width, height := screen.Size()
	replyHeight := msg.ReplyTo.Height()
	widget.WriteLineSimpleColor(screen, "In reply to", 1, 0, tcell.ColorGreen)
	widget.WriteLineSimpleColor(screen, msg.ReplyTo.SenderName, 13, 0, msg.ReplyTo.SenderColor())
	for y := 0; y < 1+replyHeight; y++ {
		screen.SetCell(0, y, tcell.StyleDefault, '▊')
	}
	replyScreen := mauview.NewProxyScreen(screen, 1, 1, width-1, replyHeight)
	msg.ReplyTo.Draw(replyScreen)
	return mauview.NewProxyScreen(screen, 0, replyHeight+1, width, height-replyHeight-1)
}

func (msg *UIMessage) String() string {
	return fmt.Sprintf(`&messages.UIMessage{
    ID="%s", TxnID="%s",
    Type="%s", Timestamp=%s,
    Sender={ID="%s", Name="%s", Color=#%X},
    IsService=%t, IsHighlight=%t,
    Renderer=%s,
}`,
		msg.EventID, msg.TxnID,
		msg.Type, msg.Timestamp.String(),
		msg.SenderID, msg.SenderName, msg.DefaultSenderColor.Hex(),
		msg.IsService, msg.IsHighlight, msg.Renderer.String())
}

func (msg *UIMessage) PlainText() string {
	return msg.Renderer.PlainText()
}