-
Notifications
You must be signed in to change notification settings - Fork 1
/
mailboxes.go
98 lines (90 loc) · 2.29 KB
/
mailboxes.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
package helpscout
import (
"fmt"
"time"
)
type respMailboxes struct {
Embedded struct {
Mailboxes []struct {
ID uint64 `json:"id"`
Name string `json:"name"`
Slug string `json:"slug"`
Email string `json:"email"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
Links struct {
Fields struct {
Href string `json:"href"`
} `json:"fields"`
Folders struct {
Href string `json:"href"`
} `json:"folders"`
Self struct {
Href string `json:"href"`
} `json:"self"`
} `json:"_links"`
} `json:"mailboxes"`
} `json:"_embedded"`
Links struct {
First struct {
Href string `json:"href"`
} `json:"first"`
Last struct {
Href string `json:"href"`
} `json:"last"`
Page struct {
Href string `json:"href"`
Templated bool `json:"templated"`
} `json:"page"`
Self struct {
Href string `json:"href"`
} `json:"self"`
} `json:"_links"`
Page struct {
Size int `json:"size"`
TotalElements int `json:"totalElements"`
TotalPages int `json:"totalPages"`
Number int `json:"number"`
} `json:"page"`
}
// SetMailboxID sets the current mailbox ID
func (h *HelpScout) SetMailboxID(id uint64) {
h.MailboxID = id
h.MailboxSelected = true
}
// DeselectMailbox set no currently selected mailbox
func (h *HelpScout) DeselectMailbox() {
h.MailboxID = 0
h.MailboxSelected = false
}
// SelectMailbox searches for a mailbox ID with the given ID,
// mailbox name, or email address and selects it
func (h *HelpScout) SelectMailbox(mailbox interface{}) error {
r, _, _, err := h.Exec("mailboxes", nil, &respMailboxes{}, "")
if err != nil {
return err
}
resp := r.(*respMailboxes)
h.DeselectMailbox()
L:
for _, m := range resp.Embedded.Mailboxes {
switch mailbox.(type) {
case string:
if m.Email == mailbox || m.Name == mailbox {
h.SetMailboxID(m.ID)
break L
}
case uint64:
if m.ID == mailbox {
h.SetMailboxID(m.ID)
break L
}
default:
return fmt.Errorf("%Ts aren't supported for selecting a mailbox", mailbox)
}
}
if !h.MailboxSelected {
return fmt.Errorf("couldn't find mailbox named/with id '%v'", mailbox)
}
return nil
}