~f4814n/frost

ref: ff15c3c08fea frost/view/memberlist/memberlist.go -rw-r--r-- 3.9 KiB
ff15c3c0Fabian Geiselhart Add screenshots 3 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
package memberlist

import (
	"image"
	_ "image/gif"
	_ "image/jpeg"
	_ "image/png"
	"net/url"
	"unicode"

	"gioui.org/layout"
	"gioui.org/widget/material"
	"git.sr.ht/~f4814n/frost"
	"git.sr.ht/~f4814n/frost/component/cache"
	fwidget "git.sr.ht/~f4814n/frost/widget"
	"git.sr.ht/~f4814n/matrix"
	"go.uber.org/zap"
	_ "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"
)

type (
	g = layout.Context
	d = layout.Dimensions
)

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

func (e addAvatarEvent) FrostEvent() {}

type view struct {
	rx, tx chan frost.Event

	matrixEvents chan matrix.Event

	userList *layout.List
	users    []matrix.Member

	avatars map[string]image.Image

	room matrix.Room

	theme *material.Theme

	logger *zap.Logger
}

func New(room matrix.Room, theme *material.Theme, logger *zap.Logger) frost.View {
	return &view{
		matrixEvents: make(chan matrix.Event, 100),
		avatars:      make(map[string]image.Image),
		userList:     &layout.List{Axis: layout.Vertical},
		room:         room,
		logger:       logger,
		theme:        theme,
	}
}

func (v *view) Run(rx, tx chan frost.Event) {
	v.rx, v.tx = rx, tx

	v.room.Notify(v.matrixEvents)

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

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

	v.users = append(v.users, user)
	v.logger.Debug("Adding user", zap.String("user", user.Displayname()))

	go v.updateAvatar(user)
}

func (v *view) updateAvatar(user matrix.Member) {
	event, ok := v.room.GetState("m.room.member", user.ID)
	if !ok {
		return
	}

	uriString, ok := event.Content["avatar_url"]
	if !ok {
		return
	}

	log := v.logger.With(
		zap.String("user", user.Displayname()),
		zap.Any("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.Debug("User has invalid avatar_url", zap.Error(err))
		return
	}

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

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

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

	thumb := <-thumbChan

	img, _, err := image.Decode(thumb.Content)
	if err != nil {
		log.Debug("Failed to decode image", zap.Error(err))
		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 *view) update(gtx g) {
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.logger.Debug("Adding avatar image", zap.String("user", event.User.ID))
				v.avatars[event.User.ID] = event.Image
			}
		default:
			break loop
		}
	}
}

func (v *view) Layout(gtx layout.Context) layout.Dimensions {
	v.update(gtx)

	return v.userList.Layout(gtx, len(v.users), v.listElem)
}

func (v *view) listElem(gtx g, index int) d {
	user := v.users[index]

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

	icon := fwidget.InitialSign{Initial: initial, Theme: v.theme}.Layout
	if img, ok := v.avatars[user.ID]; ok {
		icon = fwidget.RoundImage{Image: img}.Layout
	}

	elem := fwidget.ListElement{
		Before: icon,
		Lines:  [2]string{user.Displayname()},
	}
	return elem.Layout(gtx, v.theme)
}

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