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"
"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

View file

@ -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,
}

View file

@ -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]]
}