~f4814n/frost

ref: abcd6e72b355822384a3244234ae055345acab60 frost/view/roomhistory/event.go -rw-r--r-- 5.2 KiB
abcd6e72Fabian Geiselhart Bump matrix version 5 months ago
                                                                                
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
package roomhistory

import (
	"fmt"
	"time"

	fwidget "git.sr.ht/~f4814n/frost/widget"

	"git.sr.ht/~f4814n/frost/util"

	"gioui.org/layout"
	"gioui.org/unit"
	"gioui.org/widget/material"
	"git.sr.ht/~f4814n/matrix"
)

type statusEvent struct {
	Text    string
	Invalid bool
}

func newStatusEvent(ev matrix.StateEvent) statusEvent {
	senderName := ev.Sender.Displayname()

	var (
		text    string
		invalid bool
	)

	switch ev.Type {
	case "m.room.member":
		text = fmt.Sprintf("%s changed their profile", ev.StateKey)
	case "m.room.create":
		text = fmt.Sprintf("%s created the room", senderName)
	case "m.room.power_levels":
		text = fmt.Sprintf("Room power levels were changed by %s", senderName)
	case "m.room.join_rules":
		text = fmt.Sprintf("Join rules were changed by %s", senderName)
	case "m.room.name":
		text = fmt.Sprintf("%s changed the room name to \"%s\"", senderName, ev.Room.Displayname())
	case "m.room.topic":
		var str string
		if topic, ok := ev.Content["topic"]; ok {
			str = fmt.Sprintf("%s changed the room topic to \"%s\"", senderName, topic)
		} else {
			str = fmt.Sprintf("%s removed the room topic", senderName)
		}
		text = str
	case "m.room.guest_access":
		var str string
		if access, ok := ev.Content["guest_access"]; ok && access == "can_join" || access == "forbidden" {
			if access == "can_join" {
				str = fmt.Sprintf("%s allowed guest to join", senderName)
			} else {
				str = fmt.Sprintf("%s disallowed guests to join", senderName)
			}
		} else {
			str = "[Unformattable Event]"
			invalid = true
		}
		text = str
	case "m.room.history_visibility":
		var str string
		if access, ok := ev.Content["history_visibility"]; ok {
			str = fmt.Sprintf("%s set the history visibility to \"%s\"", senderName, access)
		} else {
			str = "[Unformattable Event]"
			invalid = true
		}
		text = str
	case "m.room.canonical_alias":
		var str string
		if alias, ok := ev.Content["canonical_alias"]; ok {
			str = fmt.Sprintf("%s changed the canonical alias to \"%s\"", senderName, alias)
		} else {
			str = fmt.Sprintf("%s set no or removed the canonical alias", senderName)
		}
		text = str
	default:
		invalid = true
		text = "[Unformattable Event]"
	}

	return statusEvent{
		Text:    text,
		Invalid: invalid,
	}
}

func (e statusEvent) Layout(gtx g, theme *material.Theme) d {
	align := layout.Center
	bgcol := util.NRGB(0xeeeeee)

	return align.Layout(gtx, func(gtx g) d {
		bg := fwidget.Background{
			Color: bgcol,
			Inset: layout.Inset{
				Top:    unit.Dp(8),
				Bottom: unit.Dp(8),
				Left:   unit.Dp(12),
				Right:  unit.Dp(12),
			},
			Radius: unit.Dp(10),
		}

		return bg.Layout(gtx, func(gtx g) d {
			lbl := material.Body1(theme, e.Text)
			if e.Invalid {
				lbl.Color = util.NRGB(0xff0000)
			} else {
				lbl.Color = theme.Palette.ContrastBg
			}
			return lbl.Layout(gtx)
		})
	})
}

type messageEvent struct {
	Time    time.Time
	Text    string
	Sender  matrix.Member
	Own     bool
	Invalid bool
}

func newMessageEvent(own bool, matrixEvent matrix.RoomEvent) messageEvent {
	event := messageEvent{
		Time:   matrixEvent.OriginServerTS,
		Sender: matrixEvent.Sender,
		Own:    own,
	}

	if matrixEvent.Type == "m.room.encrypted" {
		var err error
		matrixEvent, err = matrixEvent.Decrypt()
		if err != nil {
			event.Text = err.Error()
			event.Invalid = true
		}
	}

	if body, ok := matrixEvent.Content["body"]; matrixEvent.Type == "m.room.message" && ok {
		event.Text = body.(string)
	}

	return event
}

func (e messageEvent) Layout(gtx g, theme *material.Theme) d {
	align := layout.W
	bgcol := theme.Palette.ContrastBg
	if e.Own {
		align = layout.E
		bgcol = util.NRGB(0xeeeeee)
	}

	return align.Layout(gtx, func(gtx g) d {
		bg := fwidget.Background{
			Color: bgcol,
			Inset: layout.Inset{
				Top:    unit.Dp(8),
				Bottom: unit.Dp(8),
				Left:   unit.Dp(12),
				Right:  unit.Dp(12),
			},
			Radius: unit.Dp(10),
		}

		var msgWidth int
		timecol := util.NRGBA(0xaaaaaaaa)

		return bg.Layout(gtx, func(gtx g) d {
			return layout.Flex{Axis: layout.Vertical}.Layout(gtx,
				layout.Rigid(func(gtx g) d {
					senderName := e.Sender.Displayname()
					lbl := material.Body2(theme, senderName)
					// TODO
					// lbl.Color = initialColor(senderName[0])
					return lbl.Layout(gtx)
				}),

				layout.Rigid(func(gtx g) d {
					return e.layoutRoomEventContent(gtx, theme, &msgWidth)
				}),

				layout.Rigid(func(gtx g) d {
					gtx.Constraints.Min.X = msgWidth
					f := layout.Flex{
						Axis:      layout.Horizontal,
						Spacing:   layout.SpaceBetween,
						Alignment: layout.Middle,
					}

					var children []layout.FlexChild
					child := layout.Rigid(func(gtx g) d {
						time := util.FormatTime(e.Time)
						lbl := material.Caption(theme, time)
						lbl.Color = timecol
						return lbl.Layout(gtx)
					})
					children = append(children, child)

					return f.Layout(gtx, children...)
				}),
			)
		})
	})
}

func (e messageEvent) layoutRoomEventContent(gtx g, theme *material.Theme, msgWidth *int) d {
	lbl := material.Body2(theme, e.Text)
	if e.Own {
		lbl.Color = util.NRGB(0x000000)
	} else {
		lbl.Color = util.NRGB(0xffffff)
	}

	if e.Invalid {
		lbl.Color = util.NRGB(0xff0000)
	}

	dims := lbl.Layout(gtx)
	dims.Size.Y += gtx.Px(unit.Dp(4))
	*msgWidth = dims.Size.X
	return dims
}