/
status.go
60 lines (47 loc) · 1.25 KB
/
status.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
package status
import (
"encoding/json"
"fmt"
"time"
"github.com/42wim/teamscli/internal/client"
"github.com/42wim/teamscli/internal/token"
)
var StatusURL = "https://presence.teams.microsoft.com/v1/me/publishnote"
type request struct {
Expiry string `json:"expiry,omitempty"`
Message string `json:"message"`
}
func setStatus(status, duration string, showonmessage bool, token string) error {
var expiry string
if duration != "" {
d, err := time.ParseDuration(duration)
if err != nil {
return err
}
expiry = time.Now().Add(d).UTC().Format(time.RFC3339Nano)
} else {
t, _ := time.Parse(time.RFC3339Nano, "9999-12-30T23:00:00.000Z")
expiry = t.UTC().Format(time.RFC3339Nano)
}
if showonmessage {
status += "<pinnednote></pinnednote>"
}
bReq, err := json.Marshal(&request{
Expiry: expiry,
Message: status,
})
if err != nil {
return fmt.Errorf("json marshal failed: %s", err)
}
return client.PUT(StatusURL, bReq, token)
}
func SetStatusDuration(status, duration string, showonmessage bool) error {
token, err := token.Read()
if err != nil {
return err
}
return setStatus(status, duration, showonmessage, token)
}
func SetStatus(status string, showonmessage bool) error {
return SetStatusDuration(status, "", showonmessage)
}