~f4814n/frost

ref: 6da9fac5317c679e3007224f7bb2ac4c73cde289 frost/cmd/frost/users_view.go -rw-r--r-- 4.6 KiB
6da9fac5Fabian Geiselhart UsersView: Use BackEvent 1 year, 14 days 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
package main

import (
	"gioui.org/layout"
	"gioui.org/unit"
	"gioui.org/widget"
	"gioui.org/widget/material"
	"git.sr.ht/~f4814n/matrix"
	"github.com/sirupsen/logrus"
	"golang.org/x/exp/shiny/materialdesign/icons"
	_ "golang.org/x/image/bmp"
	"golang.org/x/image/draw"
	_ "golang.org/x/image/tiff"
	_ "golang.org/x/image/vp8"
	_ "golang.org/x/image/vp8l"
	_ "golang.org/x/image/webp"
	"image"
	_ "image/gif"
	_ "image/jpeg"
	_ "image/png"
	"net/url"
	"unicode"
)

type AddAvatarEvent struct {
	Image image.Image
	User  matrix.User
}

type UsersView struct {
	rx, tx chan Event

	matrixEvents chan matrix.Event

	backButton *widget.Clickable

	userList *layout.List
	users    []matrix.Member

	avatars map[string]image.Image

	room matrix.Room

	log *logrus.Entry
}

func NewUsersView(room matrix.Room) *UsersView {
	return &UsersView{
		matrixEvents: make(chan matrix.Event, 100),
		avatars:      make(map[string]image.Image),
		backButton:   new(widget.Clickable),
		userList:     &layout.List{Axis: layout.Vertical},
		room:         room,
		log:          logrus.WithField("component", "users_view"),
	}

}

func (v *UsersView) Start(rx, tx chan Event) {
	v.rx, v.tx = rx, tx

	v.room.Notify(v.matrixEvents)

	for _, m := range v.room.Members() {
		v.addUser(m)
	}
}

func (v *UsersView) addUser(user matrix.Member) {
	for _, v := range v.users {
		if v == user {
			return
		}
	}

	v.users = append(v.users, user)
	v.log.WithField("user", user.Displayname()).Debug("Adding user")

	go v.updateAvatar(user)
}

func (v *UsersView) updateAvatar(user matrix.Member) {
	uriString := v.room.GetState("m.room.member", user.ID).Content["avatar_url"]

	log := v.log.WithFields(logrus.Fields{
		"user": user.Displayname(),
		"url":  uriString,
	})

	if _, ok := uriString.(string); !ok {
		log.Debug("avatar_url is not a string. Empty?")
		return
	}

	uri, err := url.Parse(uriString.(string))
	if err != nil {
		log.WithField("error", err).Debug("User has invalid avatar_url")
		return
	}

	errChan := make(chan error, 1)
	thumbChan := make(chan matrix.Thumbnail, 1)

	v.tx <- CacheFetchThumbnailEvent{
		URI:       uri,
		Method:    matrix.Scale,
		Height:    64,
		Width:     64,
		Thumbnail: thumbChan,
		Error:     errChan,
	}

	err = <-errChan
	if err != nil {
		log.WithField("error", err).Debug("Failed to fetch thumbnail")
		return
	}

	thumb := <-thumbChan

	img, _, err := image.Decode(thumb.Content)
	if err != nil {
		log.WithField("error", err).Debug("Failed to decode image")
		return
	}

	dst := image.NewRGBA(image.Rect(0, 0, 46, 46))
	draw.CatmullRom.Scale(dst, dst.Bounds(), img, img.Bounds(), draw.Over, nil)

	v.rx <- AddAvatarEvent{
		Image: dst,
		User:  user.User,
	}
}

func (v *UsersView) update(gtx Gtx) {
loop:
	for {
		select {
		case event := <-v.matrixEvents:
			if event, ok := event.(matrix.StateEvent); ok {
				if event.Type == "m.room.member" {
					if member := v.room.Member(event.StateKey); member != nil {
						v.addUser(*member)
					}
				}
			}
		case event := <-v.rx:
			switch event := event.(type) {
			case AddAvatarEvent:
				v.log.WithField("user", event.User.ID).Debug("Adding avatar image")
				v.avatars[event.User.ID] = event.Image
			}
		default:
			break loop
		}
	}

	if v.backButton.Clicked() {
		v.tx <- BackEvent{}
	}
}

func (v *UsersView) Layout(gtx Gtx) Dims {
	v.update(gtx)

	return layout.Flex{Axis: layout.Vertical}.Layout(gtx,
		layout.Rigid(v.topbar),
		layout.Flexed(1, func(gtx Gtx) Dims {
			return v.userList.Layout(gtx, len(v.users), v.listElem)
		}),
	)
}

func (v *UsersView) listElem(gtx Gtx, index int) Dims {
	user := v.users[index]

	var initial string
	for _, c := range user.Displayname() {
		initial = string(unicode.ToUpper(c))
		break
	}

	icon := InitialSign{Initial: initial}.Layout
	if img, ok := v.avatars[user.ID]; ok {
		icon = func(gtx Gtx) Dims {
			gtx.Constraints = layout.Exact(image.Point{
				X: gtx.Px(unit.Dp(46)),
				Y: gtx.Px(unit.Dp(46)),
			})
			return RoundImage{Image: img}.Layout(gtx)
		}
	}

	elem := IconListElement{
		Icon:    icon,
		Content: material.Body1(theme, user.Displayname()).Layout,
	}
	return elem.Layout(gtx)
}

func (v *UsersView) Stop() {
	v.room.Stop(v.matrixEvents)
}

func (v *UsersView) topbar(gtx Gtx) Dims {
	return layout.Stack{Alignment: layout.SW}.Layout(gtx,
		layout.Expanded(func(gtx Gtx) Dims {
			gtx.Constraints.Min.X = gtx.Constraints.Max.X
			return fill{theme.Color.Primary}.Layout(gtx)
		}),
		layout.Stacked(func(gtx Gtx) Dims {
			return layout.Flex{Alignment: layout.Middle}.Layout(gtx,
				layout.Rigid(func(gtx Gtx) Dims {
					btn := material.IconButton(theme, v.backButton, mustIcon(icons.NavigationArrowBack))
					return btn.Layout(gtx)

				}),
			)
		}),
	)
}