Remove ToPresenceStatus

This commit is contained in:
Till Faelligen 2022-01-19 17:32:50 +01:00
parent 66a68a3594
commit 4efb3477fb
3 changed files with 23 additions and 32 deletions

View file

@ -19,6 +19,7 @@ import (
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"strings"
"time" "time"
"github.com/matrix-org/dendrite/clientapi/jsonerror" "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 // requested new presence is not allowed by the spec
if p == types.Unknown { presence, ok := types.KnownPresence[p]
if !ok {
return util.JSONResponse{ return util.JSONResponse{
Code: http.StatusBadRequest, Code: http.StatusBadRequest,
JSON: jsonerror.BadJSON( 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()) 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") logrus.WithError(err).Error("failed to send presence to eduserver")
return util.ErrorResponse(err) return util.ErrorResponse(err)
} }
@ -94,10 +96,10 @@ func SetPresence(req *http.Request,
} }
type presenceResponse struct { type presenceResponse struct {
Presence string `json:"presence"` Presence string `json:"presence"`
StatusMsg string `json:"status_msg,omitempty"` StatusMsg *string `json:"status_msg,omitempty"`
LastActiveAgo int64 `json:"last_active_ago,omitempty"` LastActiveAgo int64 `json:"last_active_ago,omitempty"`
CurrentlyActive bool `json:"currently_active,omitempty"` CurrentlyActive bool `json:"currently_active,omitempty"`
} }
// GetPresence returns the presence status of a given user. // GetPresence returns the presence status of a given user.
@ -145,7 +147,7 @@ func GetPresence(req *http.Request,
resp := presenceResponse{} resp := presenceResponse{}
lastActive := time.Since(presence.LastActiveTS.Time()) lastActive := time.Since(presence.LastActiveTS.Time())
resp.LastActiveAgo = lastActive.Milliseconds() resp.LastActiveAgo = lastActive.Milliseconds()
resp.StatusMsg = *presence.StatusMsg resp.StatusMsg = presence.StatusMsg
resp.CurrentlyActive = lastActive <= time.Minute*5 resp.CurrentlyActive = lastActive <= time.Minute*5
if !resp.CurrentlyActive { if !resp.CurrentlyActive {
presence.PresenceStatus = types.Unavailable presence.PresenceStatus = types.Unavailable

View file

@ -14,27 +14,17 @@
package types package types
import "strings"
type PresenceStatus int type PresenceStatus int
//go:generate stringer -type=PresenceStatus -output=presence_string.go -linecomment //go:generate stringer -type=PresenceStatus -output=presence_string.go -linecomment
const ( const (
Unknown PresenceStatus = iota - 1 // unknown Online PresenceStatus = iota + 1 // online
Online // online
Offline // offline Offline // offline
Unavailable // unavailable Unavailable // unavailable
) )
// ToPresenceStatus tries to convert the given string to a PresenceStatus var KnownPresence = map[string]PresenceStatus{
func ToPresenceStatus(v string) PresenceStatus { "online": Online,
switch strings.ToLower(v) { "offline": Offline,
case "online": "unavailable": Unavailable,
return Online
case "offline":
return Offline
case "unavailable":
return Unavailable
}
return Unknown
} }

View file

@ -8,20 +8,19 @@ func _() {
// An "invalid array index" compiler error signifies that the constant values have changed. // An "invalid array index" compiler error signifies that the constant values have changed.
// Re-run the stringer command to generate them again. // Re-run the stringer command to generate them again.
var x [1]struct{} var x [1]struct{}
_ = x[Unknown - -1] _ = x[Online-1]
_ = x[Online-0] _ = x[Offline-2]
_ = x[Offline-1] _ = x[Unavailable-3]
_ = x[Unavailable-2]
} }
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 { func (i PresenceStatus) String() string {
i -= -1 i -= 1
if i < 0 || i >= PresenceStatus(len(_PresenceStatus_index)-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]] return _PresenceStatus_name[_PresenceStatus_index[i]:_PresenceStatus_index[i+1]]
} }