aboutsummaryrefslogtreecommitdiff
path: root/ui/member-list.go
blob: eda9da4739587645eae79284cd68c39bf3b28703 (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
// 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 ui

import (
	"math"
	"sort"
	"strings"

	"github.com/mattn/go-runewidth"

	"maunium.net/go/mautrix/event"
	"maunium.net/go/mautrix/id"
	"maunium.net/go/mauview"
	"maunium.net/go/tcell"

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

type MemberList struct {
	list roomMemberList
}

func NewMemberList() *MemberList {
	return &MemberList{}
}

type memberListItem struct {
	rooms.Member
	PowerLevel int
	Sigil      rune
	UserID     id.UserID
	Color      tcell.Color
}

type roomMemberList []*memberListItem

func (rml roomMemberList) Len() int {
	return len(rml)
}

func (rml roomMemberList) Less(i, j int) bool {
	if rml[i].PowerLevel != rml[j].PowerLevel {
		return rml[i].PowerLevel > rml[j].PowerLevel
	}
	return strings.Compare(strings.ToLower(rml[i].Displayname), strings.ToLower(rml[j].Displayname)) < 0
}

func (rml roomMemberList) Swap(i, j int) {
	rml[i], rml[j] = rml[j], rml[i]
}

func (ml *MemberList) Update(data map[id.UserID]*rooms.Member, levels *event.PowerLevelsEventContent) *MemberList {
	ml.list = make(roomMemberList, len(data))
	i := 0
	highestLevel := math.MinInt32
	count := 0
	for _, level := range levels.Users {
		if level > highestLevel {
			highestLevel = level
			count = 1
		} else if level == highestLevel {
			count++
		}
	}
	for userID, member := range data {
		level := levels.GetUserLevel(userID)
		sigil := ' '
		if level == highestLevel && count == 1 {
			sigil = '~'
		} else if level > levels.StateDefault() {
			sigil = '&'
		} else if level >= levels.Ban() {
			sigil = '@'
		} else if level >= levels.Kick() || level >= levels.Redact() {
			sigil = '%'
		} else if level > levels.UsersDefault {
			sigil = '+'
		}
		ml.list[i] = &memberListItem{
			Member:     *member,
			UserID:     userID,
			PowerLevel: level,
			Sigil:      sigil,
			Color:      widget.GetHashColor(userID),
		}
		i++
	}
	sort.Sort(ml.list)
	return ml
}

func (ml *MemberList) Draw(screen mauview.Screen) {
	width, _ := screen.Size()
	sigilStyle := tcell.StyleDefault.Background(tcell.ColorGreen).Foreground(tcell.ColorWhite)
	for y, member := range ml.list {
		if member.Sigil != ' ' {
			screen.SetCell(0, y, sigilStyle, member.Sigil)
		}
		if member.Membership == "invite" {
			widget.WriteLineSimpleColor(screen, member.Displayname, 2, y, member.Color)
			screen.SetCell(1, y, tcell.StyleDefault, '(')
			if sw := runewidth.StringWidth(member.Displayname); sw+2 < width {
				screen.SetCell(sw+2, y, tcell.StyleDefault, ')')
			} else {
				screen.SetCell(width-1, y, tcell.StyleDefault, ')')
			}
		} else {
			widget.WriteLineSimpleColor(screen, member.Displayname, 1, y, member.Color)
		}
	}
}