aboutsummaryrefslogtreecommitdiff
path: root/syncapi/types/presence.go
blob: 98f3b14d65afa77dd7cb5393f23a21d880f61d98 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// Copyright 2024 New Vector Ltd.
// Copyright 2022 The Matrix.org Foundation C.I.C.
//
// SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
// Please see LICENSE files in the repository root for full details.

package types

import (
	"strings"
	"time"

	"github.com/matrix-org/gomatrixserverlib/spec"
)

type Presence uint8

const (
	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) (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
	}
}

type PresenceInternal struct {
	ClientFields PresenceClientResponse
	StreamPos    StreamPosition `json:"-"`
	UserID       string         `json:"-"`
	LastActiveTS spec.Timestamp `json:"-"`
	Presence     Presence       `json:"-"`
}

// Equals compares p1 with p2.
func (p1 *PresenceInternal) Equals(p2 *PresenceInternal) bool {
	return p1.ClientFields.Presence == p2.ClientFields.Presence &&
		p1.ClientFields.StatusMsg == p2.ClientFields.StatusMsg &&
		p1.UserID == p2.UserID
}

// CurrentlyActive returns the current active state.
func (p *PresenceInternal) CurrentlyActive() bool {
	return time.Since(p.LastActiveTS.Time()).Minutes() < 5
}

// LastActiveAgo returns the time since the LastActiveTS in milliseconds.
func (p *PresenceInternal) LastActiveAgo() int64 {
	return time.Since(p.LastActiveTS.Time()).Milliseconds()
}

type PresenceClientResponse struct {
	CurrentlyActive *bool   `json:"currently_active,omitempty"`
	LastActiveAgo   int64   `json:"last_active_ago,omitempty"`
	Presence        string  `json:"presence"`
	StatusMsg       *string `json:"status_msg,omitempty"`
}