forked from intercom/intercom-go
-
Notifications
You must be signed in to change notification settings - Fork 1
/
conversation.go
174 lines (154 loc) · 5.91 KB
/
conversation.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package intercom
// ConversationService handles interactions with the API through an ConversationRepository.
type ConversationService struct {
Repository ConversationRepository
}
// ConversationList is a list of Conversations
type ConversationList struct {
Pages PageParams `json:"pages"`
Conversations []Conversation `json:"conversations"`
}
// A Conversation represents a conversation between users and admins in Intercom.
type Conversation struct {
ID string `json:"id"`
CreatedAt int64 `json:"created_at"`
UpdatedAt int64 `json:"updated_at"`
User User `json:"user"`
Assignee Admin `json:"assignee"`
Open bool `json:"open"`
Read bool `json:"read"`
ConversationMessage ConversationMessage `json:"conversation_message"`
ConversationParts ConversationPartList `json:"conversation_parts"`
TagList *TagList `json:"tags"`
}
// A ConversationMessage is the message that started the conversation rendered for presentation
type ConversationMessage struct {
ID string `json:"id"`
Subject string `json:"subject"`
Body string `json:"body"`
Author MessageAddress `json:"author"`
URL string `json:"url"`
}
// A ConversationPartList lists the subsequent Conversation Parts
type ConversationPartList struct {
Parts []ConversationPart `json:"conversation_parts"`
}
// A ConversationPart is a Reply, Note, or Assignment to a Conversation
type ConversationPart struct {
ID string `json:"id"`
PartType string `json:"part_type"`
Body string `json:"body"`
CreatedAt int64 `json:"created_at"`
UpdatedAt int64 `json:"updated_at"`
NotifiedAt int64 `json:"notified_at"`
AssignedTo Admin `json:"assigned_to"`
Author MessageAddress `json:"author"`
}
// The state of Conversations to query
// SHOW_ALL shows all conversations,
// SHOW_OPEN shows only open conversations (only valid for Admin Conversation queries)
// SHOW_CLOSED shows only closed conversations (only valid for Admin Conversation queries)
// SHOW_UNREAD shows only unread conversations (only valid for User Conversation queries)
type ConversationListState int
const (
SHOW_ALL ConversationListState = iota
SHOW_OPEN
SHOW_CLOSED
SHOW_UNREAD
)
// List all Conversations
func (c *ConversationService) ListAll(pageParams PageParams) (ConversationList, error) {
return c.Repository.list(ConversationListParams{PageParams: pageParams})
}
// List Conversations by Admin
func (c *ConversationService) ListByAdmin(admin *Admin, state ConversationListState, pageParams PageParams) (ConversationList, error) {
params := ConversationListParams{
PageParams: pageParams,
Type: "admin",
AdminID: admin.ID.String(),
}
if state == SHOW_OPEN {
params.Open = Bool(true)
}
if state == SHOW_CLOSED {
params.Open = Bool(false)
}
return c.Repository.list(params)
}
// List Conversations by User
func (c *ConversationService) ListByUser(user *User, state ConversationListState, pageParams PageParams) (ConversationList, error) {
params := ConversationListParams{
PageParams: pageParams,
Type: "user",
IntercomUserID: user.ID,
UserID: user.UserID,
Email: user.Email,
}
if state == SHOW_UNREAD {
params.Unread = Bool(true)
}
return c.Repository.list(params)
}
// Find Conversation by conversation id
func (c *ConversationService) Find(id string) (Conversation, error) {
return c.Repository.find(id)
}
// Mark Conversation as read (by a User)
func (c *ConversationService) MarkRead(id string) (Conversation, error) {
return c.Repository.read(id)
}
func (c *ConversationService) Reply(id string, author MessagePerson, replyType ReplyType, body string) (Conversation, error) {
return c.reply(id, author, replyType, body, nil)
}
// Reply to a Conversation by id
func (c *ConversationService) ReplyWithAttachmentURLs(id string, author MessagePerson, replyType ReplyType, body string, attachmentURLs []string) (Conversation, error) {
return c.reply(id, author, replyType, body, attachmentURLs)
}
func (c *ConversationService) reply(id string, author MessagePerson, replyType ReplyType, body string, attachmentURLs []string) (Conversation, error) {
addr := author.MessageAddress()
reply := Reply{
Type: addr.Type,
ReplyType: replyType.String(),
Body: body,
AttachmentURLs: attachmentURLs,
}
if addr.Type == "admin" {
reply.AdminID = addr.ID
} else {
reply.IntercomID = addr.ID
reply.UserID = addr.UserID
reply.Email = addr.Email
}
return c.Repository.reply(id, &reply)
}
// Assign a Conversation to an Admin
func (c *ConversationService) Assign(id string, assigner, assignee *Admin) (Conversation, error) {
assignerAddr := assigner.MessageAddress()
assigneeAddr := assignee.MessageAddress()
reply := Reply{
Type: "admin",
ReplyType: CONVERSATION_ASSIGN.String(),
AdminID: assignerAddr.ID,
AssigneeID: assigneeAddr.ID,
}
return c.Repository.reply(id, &reply)
}
// Open a Conversation (without a body)
func (c *ConversationService) Open(id string, opener *Admin) (Conversation, error) {
return c.reply(id, opener, CONVERSATION_OPEN, "", nil)
}
// Close a Conversation (without a body)
func (c *ConversationService) Close(id string, closer *Admin) (Conversation, error) {
return c.reply(id, closer, CONVERSATION_CLOSE, "", nil)
}
type ConversationListParams struct {
PageParams
Type string `url:"type,omitempty"`
AdminID string `url:"admin_id,omitempty"`
IntercomUserID string `url:"intercom_user_id,omitempty"`
UserID string `url:"user_id,omitempty"`
Email string `url:"email,omitempty"`
Open *bool `url:"open,omitempty"`
Unread *bool `url:"unread,omitempty"`
DisplayAs string `url:"display_as,omitempty"`
}