From 4efb3477fb430f4b733e9189f856151b0a8044d8 Mon Sep 17 00:00:00 2001 From: Till Faelligen Date: Wed, 19 Jan 2022 17:32:50 +0100 Subject: [PATCH] Remove ToPresenceStatus --- clientapi/routing/presence.go | 20 +++++++++++--------- userapi/types/presence.go | 20 +++++--------------- userapi/types/presence_string.go | 15 +++++++-------- 3 files changed, 23 insertions(+), 32 deletions(-) diff --git a/clientapi/routing/presence.go b/clientapi/routing/presence.go index e9049aaff..ea1a6a21f 100644 --- a/clientapi/routing/presence.go +++ b/clientapi/routing/presence.go @@ -19,6 +19,7 @@ import ( "fmt" "io/ioutil" "net/http" + "strings" "time" "github.com/matrix-org/dendrite/clientapi/jsonerror" @@ -70,19 +71,20 @@ func SetPresence(req *http.Request, } } - p := types.ToPresenceStatus(r.Presence) + p := strings.TrimSpace(strings.ToLower(r.Presence)) // requested new presence is not allowed by the spec - if p == types.Unknown { + presence, ok := types.KnownPresence[p] + if !ok { return util.JSONResponse{ Code: http.StatusBadRequest, JSON: jsonerror.BadJSON( - fmt.Sprintf("The sent presence value '%s' is not allowed.", r.Presence), + fmt.Sprintf("The sent presence value '%s' is not allowed.", p), ), } } lastActive := gomatrixserverlib.AsTimestamp(time.Now()) - if err := api.SetPresence(req.Context(), eduAPI, userAPI, userID, r.StatusMsg, p, lastActive); err != nil { + if err := api.SetPresence(req.Context(), eduAPI, userAPI, userID, r.StatusMsg, presence, lastActive); err != nil { logrus.WithError(err).Error("failed to send presence to eduserver") return util.ErrorResponse(err) } @@ -94,10 +96,10 @@ func SetPresence(req *http.Request, } type presenceResponse struct { - Presence string `json:"presence"` - StatusMsg string `json:"status_msg,omitempty"` - LastActiveAgo int64 `json:"last_active_ago,omitempty"` - CurrentlyActive bool `json:"currently_active,omitempty"` + Presence string `json:"presence"` + StatusMsg *string `json:"status_msg,omitempty"` + LastActiveAgo int64 `json:"last_active_ago,omitempty"` + CurrentlyActive bool `json:"currently_active,omitempty"` } // GetPresence returns the presence status of a given user. @@ -145,7 +147,7 @@ func GetPresence(req *http.Request, resp := presenceResponse{} lastActive := time.Since(presence.LastActiveTS.Time()) resp.LastActiveAgo = lastActive.Milliseconds() - resp.StatusMsg = *presence.StatusMsg + resp.StatusMsg = presence.StatusMsg resp.CurrentlyActive = lastActive <= time.Minute*5 if !resp.CurrentlyActive { presence.PresenceStatus = types.Unavailable diff --git a/userapi/types/presence.go b/userapi/types/presence.go index 6dd0fb64b..ff8813348 100644 --- a/userapi/types/presence.go +++ b/userapi/types/presence.go @@ -14,27 +14,17 @@ package types -import "strings" - type PresenceStatus int //go:generate stringer -type=PresenceStatus -output=presence_string.go -linecomment const ( - Unknown PresenceStatus = iota - 1 // unknown - Online // online + Online PresenceStatus = iota + 1 // online Offline // offline Unavailable // unavailable ) -// ToPresenceStatus tries to convert the given string to a PresenceStatus -func ToPresenceStatus(v string) PresenceStatus { - switch strings.ToLower(v) { - case "online": - return Online - case "offline": - return Offline - case "unavailable": - return Unavailable - } - return Unknown +var KnownPresence = map[string]PresenceStatus{ + "online": Online, + "offline": Offline, + "unavailable": Unavailable, } diff --git a/userapi/types/presence_string.go b/userapi/types/presence_string.go index a80302bab..16cbabab0 100644 --- a/userapi/types/presence_string.go +++ b/userapi/types/presence_string.go @@ -8,20 +8,19 @@ 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[Unknown - -1] - _ = x[Online-0] - _ = x[Offline-1] - _ = x[Unavailable-2] + _ = x[Online-1] + _ = x[Offline-2] + _ = x[Unavailable-3] } -const _PresenceStatus_name = "unknownonlineofflineunavailable" +const _PresenceStatus_name = "onlineofflineunavailable" -var _PresenceStatus_index = [...]uint8{0, 7, 13, 20, 31} +var _PresenceStatus_index = [...]uint8{0, 6, 13, 24} func (i PresenceStatus) String() string { - i -= -1 + i -= 1 if i < 0 || i >= PresenceStatus(len(_PresenceStatus_index)-1) { - return "PresenceStatus(" + strconv.FormatInt(int64(i+-1), 10) + ")" + return "PresenceStatus(" + strconv.FormatInt(int64(i+1), 10) + ")" } return _PresenceStatus_name[_PresenceStatus_index[i]:_PresenceStatus_index[i+1]] }