Simplify presence stringification (should help with vector-im/element-android#5712)

This commit is contained in:
Neil Alexander 2022-04-07 10:10:28 +01:00
parent 99f2007eb6
commit 99ef547295
No known key found for this signature in database
GPG Key ID: A02A2019A2BB0944
7 changed files with 36 additions and 87 deletions

View File

@ -70,7 +70,6 @@ func SetPresence(
JSON: jsonerror.Unknown(fmt.Sprintf("Unknown presence '%s'.", presence.Presence)), JSON: jsonerror.Unknown(fmt.Sprintf("Unknown presence '%s'.", presence.Presence)),
} }
} }
err := producer.SendPresence(req.Context(), userID, presenceStatus, presence.StatusMsg) err := producer.SendPresence(req.Context(), userID, presenceStatus, presence.StatusMsg)
if err != nil { if err != nil {
log.WithError(err).Errorf("failed to update presence") log.WithError(err).Errorf("failed to update presence")

View File

@ -413,7 +413,6 @@ func (t *txnReq) processPresence(ctx context.Context, e gomatrixserverlib.EDU) e
for _, content := range payload.Push { for _, content := range payload.Push {
presence, ok := syncTypes.PresenceFromString(content.Presence) presence, ok := syncTypes.PresenceFromString(content.Presence)
if !ok { if !ok {
logrus.Warnf("invalid presence '%s', skipping.", content.Presence)
continue continue
} }
if err := t.producer.SendPresence(ctx, content.UserID, presence, content.StatusMsg, content.LastActiveAgo); err != nil { if err := t.producer.SendPresence(ctx, content.UserID, presence, content.StatusMsg, content.LastActiveAgo); err != nil {

View File

@ -111,11 +111,16 @@ func (p *PresenceStreamProvider) IncrementalSync(
continue continue
} }
} }
if _, known := types.PresenceFromString(presence.ClientFields.Presence); known {
presence.ClientFields.LastActiveAgo = presence.LastActiveAgo() presence.ClientFields.LastActiveAgo = presence.LastActiveAgo()
if presence.ClientFields.Presence == "online" { if presence.ClientFields.Presence == "online" {
currentlyActive := presence.CurrentlyActive() currentlyActive := presence.CurrentlyActive()
presence.ClientFields.CurrentlyActive = &currentlyActive presence.ClientFields.CurrentlyActive = &currentlyActive
} }
} else {
presence.ClientFields.Presence = "offline"
}
content, err := json.Marshal(presence.ClientFields) content, err := json.Marshal(presence.ClientFields)
if err != nil { if err != nil {

View File

@ -122,10 +122,8 @@ func (rp *RequestPool) updatePresence(db storage.Presence, presence string, user
presenceID, ok := types.PresenceFromString(presence) presenceID, ok := types.PresenceFromString(presence)
if !ok { // this should almost never happen if !ok { // this should almost never happen
logrus.Errorf("unknown presence '%s'", presence)
return return
} }
newPresence := types.PresenceInternal{ newPresence := types.PresenceInternal{
ClientFields: types.PresenceClientResponse{ ClientFields: types.PresenceClientResponse{
Presence: presenceID.String(), Presence: presenceID.String(),

View File

@ -21,26 +21,42 @@ import (
"github.com/matrix-org/gomatrixserverlib" "github.com/matrix-org/gomatrixserverlib"
) )
//go:generate stringer -type=Presence -linecomment
type Presence uint8 type Presence uint8
const ( const (
PresenceUnavailable Presence = iota + 1 // unavailable PresenceUnknown Presence = iota
PresenceUnavailable // unavailable
PresenceOnline // online PresenceOnline // online
PresenceOffline // offline 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. // PresenceFromString returns the integer representation of the given input presence.
// Returns false for ok, if input is not a valid presence value. // Returns false for ok, if input is not a valid presence value.
func PresenceFromString(input string) (p Presence, ok bool) { func PresenceFromString(input string) (Presence, bool) {
for i := 0; i < len(_Presence_index)-1; i++ { switch strings.ToLower(input) {
l, r := _Presence_index[i], _Presence_index[i+1] case "unavailable":
if strings.EqualFold(input, _Presence_name[l:r]) { return PresenceUnavailable, true
return Presence(i + 1), true case "online":
return PresenceOnline, true
case "offline":
return PresenceOffline, true
default:
return PresenceUnknown, false
} }
} }
return 0, false
}
type PresenceInternal struct { type PresenceInternal struct {
ClientFields PresenceClientResponse ClientFields PresenceClientResponse

View File

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

View File

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