Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

History visibility convenience functions #318

Merged
merged 3 commits into from
Jun 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 31 additions & 2 deletions eventcontent.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,18 +223,47 @@ const (
)

// Value returns the history visibility. If an unknown value is set then
// it will return "joined".
// it will return "shared".
func (h HistoryVisibility) Value() HistoryVisibility {
switch h {
case HistoryVisibilityWorldReadable, HistoryVisibilityShared:
return h
case HistoryVisibilityInvited, HistoryVisibilityJoined:
return h
default:
return HistoryVisibilityJoined
return HistoryVisibilityShared
}
}

var hisVisStringToIntMapping = map[HistoryVisibility]uint8{
HistoryVisibilityWorldReadable: 1, // Starting at 1, to avoid confusions with Go default values
HistoryVisibilityShared: 2,
HistoryVisibilityInvited: 3,
HistoryVisibilityJoined: 4,
}

var hisVisIntToStringMapping = map[uint8]HistoryVisibility{
1: HistoryVisibilityWorldReadable, // Starting at 1, to avoid confusions with Go default values
2: HistoryVisibilityShared,
3: HistoryVisibilityInvited,
4: HistoryVisibilityJoined,
}

// NumericValue returns the numeric value of the HistoryVisibility.
func (h HistoryVisibility) NumericValue() uint8 {
return hisVisStringToIntMapping[h.Value()]
}

// HistoryVisibilityFromInt gets the HistoryVisibility given an uint8.
// If there is no match, it returns "shared".
func HistoryVisibilityFromInt(n uint8) HistoryVisibility {
vis, ok := hisVisIntToStringMapping[n]
if !ok {
return HistoryVisibilityShared
}
return vis
}

// JoinRuleContent is the JSON content of a m.room.join_rules event needed for auth checks.
// See https://matrix.org/docs/spec/client_server/r0.2.0.html#m-room-join-rules for descriptions of the fields.
type JoinRuleContent struct {
Expand Down
57 changes: 57 additions & 0 deletions eventcontent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,60 @@ func TestStrictPowerLevelContent(t *testing.T) {
t.Fatal("bad content should have errored but didn't")
}
}

func TestHistoryVisibilityFromInt(t *testing.T) {
tests := []struct {
name string
args uint8
want HistoryVisibility
}{
{
name: "unknown value returns shared visibility",
args: 100,
want: HistoryVisibilityShared,
},
{
name: "known value returns correct visibility",
args: 1,
want: HistoryVisibilityWorldReadable,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := HistoryVisibilityFromInt(tt.args); got != tt.want {
t.Errorf("HistoryVisibilityFromInt() = %v, want %v", got, tt.want)
}
})
}
}

func TestHistoryVisibility_NumericValue(t *testing.T) {
tests := []struct {
name string
h HistoryVisibility
want uint8
}{
{
name: "unknown history visibility defaults to shared",
h: HistoryVisibility("doesNotExist"),
want: 2,
},
{
name: "history visibility returns correct value",
h: HistoryVisibilityJoined,
want: 4,
},
{
name: "history visibility returns correct value 2",
h: HistoryVisibilityShared,
want: 2,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.h.NumericValue(); got != tt.want {
t.Errorf("NumericValue() = %v, want %v", got, tt.want)
}
})
}
}
3 changes: 2 additions & 1 deletion headeredevent.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import (
// about the room version. All header fields will be added into the event
// when marshalling into JSON and will be separated out when unmarshalling.
type HeaderedEvent struct {
RoomVersion RoomVersion `json:"-"`
RoomVersion RoomVersion `json:"-"`
Visibility HistoryVisibility `json:"-"`
*Event
}

Expand Down