diff --git a/eventcontent.go b/eventcontent.go index 8bb7eddc..fede9293 100644 --- a/eventcontent.go +++ b/eventcontent.go @@ -223,7 +223,7 @@ 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: @@ -231,10 +231,39 @@ func (h HistoryVisibility) Value() HistoryVisibility { 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 { diff --git a/eventcontent_test.go b/eventcontent_test.go index 79ec42a5..cc8bc487 100644 --- a/eventcontent_test.go +++ b/eventcontent_test.go @@ -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) + } + }) + } +} diff --git a/headeredevent.go b/headeredevent.go index ef8ec492..449d60e6 100644 --- a/headeredevent.go +++ b/headeredevent.go @@ -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 }