This repository has been archived by the owner on Sep 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
presence.go
99 lines (87 loc) · 2.57 KB
/
presence.go
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package tat
import (
"encoding/json"
"fmt"
"time"
)
// UserPresence struct
type UserPresence struct {
Username string `bson:"username" json:"username"`
Fullname string `bson:"fullname" json:"fullname"`
}
// Presence struct
type Presence struct {
ID string `bson:"_id,omitempty" json:"_id"`
Status string `bson:"status" json:"status"`
Topic string `bson:"topic" json:"topic"`
DatePresence int64 `bson:"datePresence" json:"datePresence"`
DateTimePresence time.Time `bson:"dateTimePresence" json:"dateTimePresence"`
UserPresence UserPresence `bson:"userPresence" json:"userPresence"`
}
// PresenceCriteria used by Presences List
type PresenceCriteria struct {
Skip int
Limit int
IDPresence string
Status string
Topic string
Username string
DateMinPresence string
DateMaxPresence string
SortBy string
}
// PresencesJSON represents list of presences with count for total
type PresencesJSON struct {
Count int `json:"count"`
Presences []Presence `json:"presences"`
}
// PresenceJSONOut represents a presence
type PresenceJSONOut struct {
Presence Presence `json:"presence"`
}
// PresenceJSON represents a status on a topic
type PresenceJSON struct {
Status string `json:"status" binding:"required"`
Username string `json:"username,omitempty"`
Topic string
}
// PresenceAddAndGet adds a new presence and get presences on topic
func (c *Client) PresenceAddAndGet(topic, status string) (*PresencesJSON, error) {
p := PresenceJSON{Status: status}
jsonStr, err := json.Marshal(p)
if err != nil {
return nil, err
}
body, err := c.reqWant("POST", 200, "/presenceget"+topic, jsonStr)
if err != nil {
return nil, err
}
out := &PresencesJSON{}
if err := json.Unmarshal(body, out); err != nil {
return nil, err
}
return out, nil
}
// PresenceDelete deletes a presence
func (c *Client) PresenceDelete(topic, username string) error {
p := PresenceJSON{Username: username}
jsonStr, err := json.Marshal(p)
if err != nil {
return err
}
_, err = c.reqWant("DELETE", 200, "/presences"+topic, jsonStr)
return err
}
// PresenceList returns presences on topic
func (c *Client) PresenceList(topic string, skip, limit int) (*PresencesJSON, error) {
path := fmt.Sprintf("/presences%s?skip=%d&limit=%d", topic, skip, limit)
body, err := c.reqWant("GET", 200, path, nil)
if err != nil {
return nil, err
}
out := &PresencesJSON{}
if err := json.Unmarshal(body, out); err != nil {
return nil, err
}
return out, nil
}