-
Notifications
You must be signed in to change notification settings - Fork 13
/
circle.go
127 lines (102 loc) · 3.21 KB
/
circle.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
package mixin
import (
"context"
"fmt"
"strconv"
"time"
)
const (
CircleActionAdd = "ADD"
CircleActionRemove = "REMOVE"
CircleItemTypeUsers = "users"
CircleItemTypeConversations = "conversations"
)
type Circle struct {
ID string `json:"circle_id,omitempty"`
Name string `json:"name,omitempty"`
UserID string `json:"user_id,omitempty"`
CreatedAt time.Time `json:"created_at,omitempty"`
}
func (c *Client) ListCircles(ctx context.Context) ([]*Circle, error) {
var circles []*Circle
if err := c.Get(ctx, "/circles", nil, &circles); err != nil {
return nil, err
}
return circles, nil
}
func (c *Client) ReadCircle(ctx context.Context, circleID string) (*Circle, error) {
var circle Circle
if err := c.Get(ctx, "/circles/"+circleID, nil, &circle); err != nil {
return nil, err
}
return &circle, nil
}
type CreateCircleParams struct {
Name string `json:"name,omitempty"`
}
func (c *Client) CreateCircle(ctx context.Context, args CreateCircleParams) (*Circle, error) {
var circle Circle
if err := c.Post(ctx, "/circles", args, &circle); err != nil {
return nil, err
}
return &circle, nil
}
type UpdateCircleParams struct {
CircleID string `json:"circle_id,omitempty"`
Name string `json:"name,omitempty"`
}
func (c *Client) UpdateCircle(ctx context.Context, args UpdateCircleParams) (*Circle, error) {
var circle Circle
body := map[string]interface{}{
"name": args.Name,
}
if err := c.Post(ctx, "/circles/"+args.CircleID, body, &circle); err != nil {
return nil, err
}
return &circle, nil
}
func (c *Client) DeleteCircle(ctx context.Context, circleID string) error {
uri := fmt.Sprintf("/circles/%s/delete", circleID)
return c.Post(ctx, uri, nil, nil)
}
type ManageCircleParams struct {
CircleID string `json:"circle_id,omitempty"`
Action string `json:"action,omitempty"` // ADD or REMOVE
ItemType string `json:"item_type,omitempty"` // users or conversations
ItemID string `json:"item_id,omitempty"` // user_id or conversation_id
}
type CircleItem struct {
CreatedAt time.Time `json:"created_at,omitempty"`
CircleID string `json:"circle_id,omitempty"`
ConversationID string `json:"conversation_id,omitempty"`
UserID string `json:"user_id,omitempty"`
}
func (c *Client) ManageCircle(ctx context.Context, args ManageCircleParams) (*CircleItem, error) {
var items []*CircleItem
uri := fmt.Sprintf("%s/%s/circles", args.ItemType, args.ItemID)
body := []interface{}{map[string]interface{}{
"action": args.Action,
"circle_id": args.CircleID,
}}
if err := c.Post(ctx, uri, body, &items); err != nil {
return nil, err
}
return items[0], nil
}
type ListCircleItemsParams struct {
CircleID string `json:"circle_id,omitempty"`
Offset time.Time `json:"offset,omitempty"`
Limit int `json:"limit,omitempty"`
}
func (c *Client) ListCircleItems(ctx context.Context, args ListCircleItemsParams) ([]*CircleItem, error) {
var items []*CircleItem
uri := fmt.Sprintf("/circles/%s/conversations", args.CircleID)
params := map[string]string{
"offset": args.Offset.Format(time.RFC3339Nano),
"limit": strconv.Itoa(args.Limit),
}
if err := c.Get(ctx, uri, params, &items); err != nil {
return nil, err
}
return items, nil
}