-
Notifications
You must be signed in to change notification settings - Fork 13
/
conversation.go
167 lines (136 loc) · 5.52 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
package mixin
import (
"context"
"fmt"
"time"
)
const (
ConversationCategoryContact = "CONTACT"
ConversationCategoryGroup = "GROUP"
ParticipantActionCreate = "CREATE"
ParticipantActionAdd = "ADD"
ParticipantActionRemove = "REMOVE"
ParticipantActionJoin = "JOIN"
ParticipantActionExit = "EXIT"
ParticipantActionRole = "ROLE"
ParticipantRoleOwner = "OWNER"
ParticipantRoleAdmin = "ADMIN"
ParticipantRoleMember = ""
)
// Participant conversation participant
type Participant struct {
Action string `json:"action,omitempty"`
Type string `json:"type,omitempty"`
UserID string `json:"user_id,omitempty"`
Role string `json:"role,omitempty"`
CreatedAt time.Time `json:"created_at,omitempty"`
}
// Conversation conversation
type Conversation struct {
ConversationID string `json:"conversation_id,omitempty"`
CreatorID string `json:"creator_id,omitempty"`
Category string `json:"category,omitempty"`
Name string `json:"name,omitempty"`
IconURL string `json:"icon_url,omitempty"`
Announcement string `json:"announcement,omitempty"`
CreatedAt string `json:"created_at,omitempty"`
CodeID string `json:"code_id,omitempty"`
CodeURL string `json:"code_url,omitempty"`
Participants []*Participant `json:"participants,omitempty"`
}
type CreateConversationInput struct {
Category string `json:"category,omitempty"`
ConversationID string `json:"conversation_id,omitempty"`
Name string `json:"name,omitempty"`
Participants []*Participant `json:"participants,omitempty"`
}
type ConversationUpdate struct {
Name string `json:"name,omitempty"`
Announcement string `json:"announcement,omitempty"`
}
// CreateConversation crate conversation
func (c *Client) CreateConversation(ctx context.Context, input *CreateConversationInput) (*Conversation, error) {
var conversation Conversation
if err := c.Post(ctx, "/conversations", input, &conversation); err != nil {
return nil, err
}
return &conversation, nil
}
// UpdateConversation update conversation
func (c *Client) UpdateConversation(ctx context.Context, conversationID string, input ConversationUpdate) (*Conversation, error) {
var conversation Conversation
if err := c.Post(ctx, fmt.Sprintf("/conversations/%s", conversationID), input, &conversation); err != nil {
return nil, err
}
return &conversation, nil
}
// CreateContactConversation create a conversation with a mixin messenger user
func (c *Client) CreateContactConversation(ctx context.Context, userID string) (*Conversation, error) {
return c.CreateConversation(ctx, &CreateConversationInput{
Category: ConversationCategoryContact,
ConversationID: UniqueConversationID(c.ClientID, userID),
Participants: []*Participant{{UserID: userID}},
})
}
// CreateGroupConversation create a group in mixin messenger with given participants
func (c *Client) CreateGroupConversation(ctx context.Context, conversationID, name string, participants []*Participant) (*Conversation, error) {
return c.CreateConversation(ctx, &CreateConversationInput{
Category: ConversationCategoryGroup,
ConversationID: conversationID,
Name: name,
Participants: participants,
})
}
// ReadConversation read conversation
func (c *Client) ReadConversation(ctx context.Context, conversationID string) (*Conversation, error) {
uri := fmt.Sprintf("/conversations/%s", conversationID)
var conversation Conversation
if err := c.Get(ctx, uri, nil, &conversation); err != nil {
return nil, err
}
return &conversation, nil
}
// Update conversation announcement
func (c *Client) UpdateConversationAnnouncement(ctx context.Context, conversationID, announcement string) (*Conversation, error) {
return c.UpdateConversation(ctx, conversationID, ConversationUpdate{Announcement: announcement})
}
func (c *Client) ManageConversation(ctx context.Context, conversationID, action string, participants []*Participant) (*Conversation, error) {
uri := fmt.Sprintf("/conversations/%s/participants/%s", conversationID, action)
var conversation Conversation
if err := c.Post(ctx, uri, participants, &conversation); err != nil {
return nil, err
}
return &conversation, nil
}
func (c *Client) AddParticipants(ctx context.Context, conversationID string, users ...string) (*Conversation, error) {
var participants []*Participant
for _, user := range users {
participants = append(participants, &Participant{UserID: user})
}
return c.ManageConversation(ctx, conversationID, ParticipantActionAdd, participants)
}
func (c *Client) RemoveParticipants(ctx context.Context, conversationID string, users ...string) (*Conversation, error) {
var participants []*Participant
for _, user := range users {
participants = append(participants, &Participant{UserID: user})
}
return c.ManageConversation(ctx, conversationID, ParticipantActionRemove, participants)
}
func (c *Client) AdminParticipants(ctx context.Context, conversationID string, users ...string) (*Conversation, error) {
var participants []*Participant
for _, user := range users {
participants = append(participants, &Participant{
UserID: user,
Role: ParticipantRoleAdmin,
})
}
return c.ManageConversation(ctx, conversationID, ParticipantActionRole, participants)
}
func (c *Client) RotateConversation(ctx context.Context, conversationID string) (*Conversation, error) {
uri := fmt.Sprintf("/conversations/%s/rotate", conversationID)
var conversation Conversation
if err := c.Post(ctx, uri, nil, &conversation); err != nil {
return nil, err
}
return &conversation, nil
}