This repository has been archived by the owner on Jun 12, 2023. It is now read-only.
/
status.go
141 lines (117 loc) · 3.76 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
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package main
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"github.com/kyokomi/emoji"
"github.com/logrusorgru/aurora"
"strings"
"text/template"
"time"
)
func GetCurrentStatus(c *Config) (*UserStatus, error) {
// Construct and execute request
variables := make(map[string]interface{})
rawResponse, err := SendApiRequest(c.Data.Token, retrievalQuery, variables)
if err != nil {
return nil, fmt.Errorf("could not send API request: %w", err)
}
responseData := RetrieveUserStatusQueryResponse{}
err = json.Unmarshal(rawResponse, &responseData)
if err != nil {
return nil, fmt.Errorf("could not unmarshal response data: %w", err)
}
return &responseData.Viewer.Status, nil
}
type UpdateStatusInput struct {
Config *Config
Emoji string
Message string
ExpiresAt *string
Organization *string
LimitedAvailability *bool
}
func UpdateStatus(input *UpdateStatusInput) (*UserStatus, error) {
// Validate emoji
if input.Emoji != "" && (!strings.HasPrefix(input.Emoji, ":") || !strings.HasSuffix(input.Emoji, ":")) {
return nil, errors.New("invalid emoji format, please supply a valid emoji")
}
// Construct and send query
variables := make(map[string]interface{})
updateInput := UpdateStatusMutationInput{
Message: input.Message,
Emoji: input.Emoji,
}
// Handle status expiry, add to variables
if input.ExpiresAt != nil && *input.ExpiresAt != "" {
duration, err := time.ParseDuration(*input.ExpiresAt)
if err != nil {
return nil, fmt.Errorf("could not parse duration for expiresAt value: %w", err)
}
now := time.Now()
updateInput.ExpiresAt = now.Add(duration).UTC().Format(time.RFC3339Nano)
}
// Handle organization, add to variables
if input.Organization != nil {
if *input.Organization != "" {
// Look up organization by name (login)
orgId, err := LookupOrganization(input.Config, *input.Organization)
if err != nil {
return nil, fmt.Errorf("could not lookup organization by name: %w", err)
}
updateInput.OrganizationId = orgId
} else {
updateInput.OrganizationId = *input.Organization
}
}
// Handle limitedAvailability (busy), add to variables
if input.LimitedAvailability != nil {
updateInput.LimitedAvailability = *input.LimitedAvailability
}
variables["input"] = updateInput
rawResponse, err := SendApiRequest(input.Config.Data.Token, updateMutation, variables)
if err != nil {
return nil, fmt.Errorf("could not send API request: %w", err)
}
responseData := UpdateUserStatusMutationResponse{}
err = json.Unmarshal(rawResponse, &responseData)
if err != nil {
return nil, fmt.Errorf("could not unmarshal response data: %w", err)
}
return &responseData.ChangeUserStatus.Status, nil
}
func FormatStatus(status *UserStatus) (string, error) {
if status.Message == "" {
return "👉 Status not set.", nil
}
templateFuncs := template.FuncMap{
"printEmoji": func(text string) string {
if text == "" {
return ""
}
return emoji.Sprint(text)
},
"isEmptyString": func(str string) bool {
return str == ""
},
"formatBold": func(content interface{}) string {
return aurora.Bold(content).String()
},
}
tpl, err := template.New("status-template").Funcs(templateFuncs).Parse(`
{{ formatBold "Status" }}: {{ printEmoji .Emoji }}{{ .Message }}
🚫 Busy: {{ formatBold .IndicatesLimitedAvailability }}
⏱ {{ if isEmptyString .ExpiresAt }}Status does not expire. {{else}} Expires at {{ .ExpiresAt }} {{end}}
🏢 {{ if isEmptyString .Organization.Name }}Visible for everyone {{ else }} Visible for {{ .Organization.Name}} {{ end }}
`)
if err != nil {
return "", fmt.Errorf("could not create status template: %w", err)
}
var buf bytes.Buffer
err = tpl.Execute(&buf, status)
if err != nil {
return "", fmt.Errorf("could not execute template: %w", err)
}
return buf.String(), nil
}