From 99ef5472959a4e4d49bd9760fcb8b3872e21fa71 Mon Sep 17 00:00:00 2001 From: Neil Alexander Date: Thu, 7 Apr 2022 10:10:28 +0100 Subject: [PATCH] Simplify presence stringification (should help with vector-im/element-android#5712) --- clientapi/routing/presence.go | 1 - federationapi/routing/send.go | 1 - syncapi/streams/stream_presence.go | 13 ++++++--- syncapi/sync/requestpool.go | 2 -- syncapi/types/presence.go | 38 +++++++++++++++++++-------- syncapi/types/presence_string.go | 26 ------------------ syncapi/types/presence_test.go | 42 ------------------------------ 7 files changed, 36 insertions(+), 87 deletions(-) delete mode 100644 syncapi/types/presence_string.go delete mode 100644 syncapi/types/presence_test.go diff --git a/clientapi/routing/presence.go b/clientapi/routing/presence.go index 63fbb75ef..093a62464 100644 --- a/clientapi/routing/presence.go +++ b/clientapi/routing/presence.go @@ -70,7 +70,6 @@ func SetPresence( JSON: jsonerror.Unknown(fmt.Sprintf("Unknown presence '%s'.", presence.Presence)), } } - err := producer.SendPresence(req.Context(), userID, presenceStatus, presence.StatusMsg) if err != nil { log.WithError(err).Errorf("failed to update presence") diff --git a/federationapi/routing/send.go b/federationapi/routing/send.go index 1bba632b5..c3351388c 100644 --- a/federationapi/routing/send.go +++ b/federationapi/routing/send.go @@ -413,7 +413,6 @@ func (t *txnReq) processPresence(ctx context.Context, e gomatrixserverlib.EDU) e for _, content := range payload.Push { presence, ok := syncTypes.PresenceFromString(content.Presence) if !ok { - logrus.Warnf("invalid presence '%s', skipping.", content.Presence) continue } if err := t.producer.SendPresence(ctx, content.UserID, presence, content.StatusMsg, content.LastActiveAgo); err != nil { diff --git a/syncapi/streams/stream_presence.go b/syncapi/streams/stream_presence.go index 6d5ec54bb..9a6c5c130 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 cf667337d..6fc08bd92 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 40aa29cf5..30e025b9f 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 467b463b8..000000000 --- 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 dbc201c5c..000000000 --- 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) - } - }) - } -}