This repository has been archived by the owner on May 30, 2023. It is now read-only.
/
room-member.go
84 lines (73 loc) · 2.27 KB
/
room-member.go
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
// Copyright 2017 Michael Telatynski <7t3chguy@gmail.com>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package mxclient
type PowerLevel int
// TODO don't bother with this and have a map similar to react-sdk "Roles.js"
// String is the Stringer implementation for PowerLevel
func (powerLevel PowerLevel) String() string {
switch int(powerLevel) {
case 100:
return "Admin"
case 50:
return "Moderator"
case 0:
return "User"
case -1:
return "Muted"
default:
return "Custom"
}
}
// Int allows a quick denature of PowerLevel to an int
func (powerLevel PowerLevel) Int() int {
return int(powerLevel)
}
// implements sort.Interface
type MemberList []*MemberInfo
func (ml MemberList) Len() int { return len(ml) }
func (ml MemberList) Less(i, j int) bool {
a, b := ml[i], ml[j]
plA, plB := a.PowerLevel.Int(), b.PowerLevel.Int()
if plA == plB {
// Secondary sort is Low->High Lexicographically on GetName()
return a.GetName() < b.GetName()
}
// Primary Sort is High->Low on PowerLevel
return plA > plB
}
func (ml MemberList) Swap(i, j int) { ml[i], ml[j] = ml[j], ml[i] }
type MemberInfo struct {
MXID string
Membership string
DisplayName string
AvatarURL MXCURL
PowerLevel PowerLevel
}
// NewMemberInfo returns a new MemberInfo with defaults (membership=leave) applied.
func NewMemberInfo(mxid string) *MemberInfo {
return &MemberInfo{
MXID: mxid,
Membership: "leave",
}
}
// GetName returns either the user's DisplayName, or if empty, their MXID.
// TODO make this disambiguate users if their DisplayName is not unique,
// implementation tips say to make a map of DisplayNames (or Set?) -> MXID?
func (memberInfo MemberInfo) GetName() string {
if memberInfo.DisplayName != "" {
return memberInfo.DisplayName
} else {
return memberInfo.MXID
}
}