diff options
Diffstat (limited to 'syncapi')
-rw-r--r-- | syncapi/streams/stream_presence.go | 13 | ||||
-rw-r--r-- | syncapi/sync/requestpool.go | 2 | ||||
-rw-r--r-- | syncapi/types/presence.go | 38 | ||||
-rw-r--r-- | syncapi/types/presence_string.go | 26 | ||||
-rw-r--r-- | syncapi/types/presence_test.go | 42 |
5 files changed, 36 insertions, 85 deletions
diff --git a/syncapi/streams/stream_presence.go b/syncapi/streams/stream_presence.go index 6d5ec54b..9a6c5c13 100644 --- a/syncapi/streams/stream_presence.go +++ b/syncapi/streams/stream_presence.go @@ -111,10 +111,15 @@ func (p *PresenceStreamProvider) IncrementalSync( continue } } - presence.ClientFields.LastActiveAgo = presence.LastActiveAgo() - if presence.ClientFields.Presence == "online" { - currentlyActive := presence.CurrentlyActive() - presence.ClientFields.CurrentlyActive = ¤tlyActive + + if _, known := types.PresenceFromString(presence.ClientFields.Presence); known { + presence.ClientFields.LastActiveAgo = presence.LastActiveAgo() + if presence.ClientFields.Presence == "online" { + currentlyActive := presence.CurrentlyActive() + presence.ClientFields.CurrentlyActive = ¤tlyActive + } + } else { + presence.ClientFields.Presence = "offline" } content, err := json.Marshal(presence.ClientFields) diff --git a/syncapi/sync/requestpool.go b/syncapi/sync/requestpool.go index cf667337..6fc08bd9 100644 --- a/syncapi/sync/requestpool.go +++ b/syncapi/sync/requestpool.go @@ -122,10 +122,8 @@ func (rp *RequestPool) updatePresence(db storage.Presence, presence string, user presenceID, ok := types.PresenceFromString(presence) if !ok { // this should almost never happen - logrus.Errorf("unknown presence '%s'", presence) return } - newPresence := types.PresenceInternal{ ClientFields: types.PresenceClientResponse{ Presence: presenceID.String(), diff --git a/syncapi/types/presence.go b/syncapi/types/presence.go index 40aa29cf..30e025b9 100644 --- a/syncapi/types/presence.go +++ b/syncapi/types/presence.go @@ -21,25 +21,41 @@ import ( "github.com/matrix-org/gomatrixserverlib" ) -//go:generate stringer -type=Presence -linecomment type Presence uint8 const ( - PresenceUnavailable Presence = iota + 1 // unavailable - PresenceOnline // online - PresenceOffline // offline + PresenceUnknown Presence = iota + PresenceUnavailable // unavailable + PresenceOnline // online + PresenceOffline // offline ) +func (p Presence) String() string { + switch p { + case PresenceUnavailable: + return "unavailable" + case PresenceOnline: + return "online" + case PresenceOffline: + return "offline" + default: + return "unknown" + } +} + // PresenceFromString returns the integer representation of the given input presence. // Returns false for ok, if input is not a valid presence value. -func PresenceFromString(input string) (p Presence, ok bool) { - for i := 0; i < len(_Presence_index)-1; i++ { - l, r := _Presence_index[i], _Presence_index[i+1] - if strings.EqualFold(input, _Presence_name[l:r]) { - return Presence(i + 1), true - } +func PresenceFromString(input string) (Presence, bool) { + switch strings.ToLower(input) { + case "unavailable": + return PresenceUnavailable, true + case "online": + return PresenceOnline, true + case "offline": + return PresenceOffline, true + default: + return PresenceUnknown, false } - return 0, false } type PresenceInternal struct { diff --git a/syncapi/types/presence_string.go b/syncapi/types/presence_string.go deleted file mode 100644 index 467b463b..00000000 --- a/syncapi/types/presence_string.go +++ /dev/null @@ -1,26 +0,0 @@ -// Code generated by "stringer -type=Presence -linecomment"; DO NOT EDIT. - -package types - -import "strconv" - -func _() { - // An "invalid array index" compiler error signifies that the constant values have changed. - // Re-run the stringer command to generate them again. - var x [1]struct{} - _ = x[PresenceUnavailable-1] - _ = x[PresenceOnline-2] - _ = x[PresenceOffline-3] -} - -const _Presence_name = "unavailableonlineoffline" - -var _Presence_index = [...]uint8{0, 11, 17, 24} - -func (i Presence) String() string { - i -= 1 - if i >= Presence(len(_Presence_index)-1) { - return "Presence(" + strconv.FormatInt(int64(i+1), 10) + ")" - } - return _Presence_name[_Presence_index[i]:_Presence_index[i+1]] -} diff --git a/syncapi/types/presence_test.go b/syncapi/types/presence_test.go deleted file mode 100644 index dbc201c5..00000000 --- a/syncapi/types/presence_test.go +++ /dev/null @@ -1,42 +0,0 @@ -package types - -import "testing" - -func TestPresenceFromString(t *testing.T) { - tests := []struct { - name string - input string - wantStatus Presence - wantOk bool - }{ - { - name: "presence unavailable", - input: "unavailable", - wantStatus: PresenceUnavailable, - wantOk: true, - }, - { - name: "presence online", - input: "OnLINE", - wantStatus: PresenceOnline, - wantOk: true, - }, - { - name: "unknown presence", - input: "unknown", - wantStatus: 0, - wantOk: false, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - got, got1 := PresenceFromString(tt.input) - if got != tt.wantStatus { - t.Errorf("PresenceFromString() got = %v, want %v", got, tt.wantStatus) - } - if got1 != tt.wantOk { - t.Errorf("PresenceFromString() got1 = %v, want %v", got1, tt.wantOk) - } - }) - } -} |