-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathreactions.go
136 lines (112 loc) · 4.68 KB
/
reactions.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
package stream
import (
"context"
"encoding/json"
"errors"
"fmt"
)
// ReactionsClient is a specialized client used to interact with the Reactions endpoints.
type ReactionsClient struct {
client *Client
}
// Add adds a reaction.
func (c *ReactionsClient) Add(ctx context.Context, r AddReactionRequestObject) (*ReactionResponse, error) {
if r.ParentID != "" {
return nil, errors.New("`Parent` not empty. For adding child reactions use `AddChild`")
}
return c.addReaction(ctx, r)
}
// AddChild adds a child reaction to the provided parent.
func (c *ReactionsClient) AddChild(ctx context.Context, parentID string, r AddReactionRequestObject) (*ReactionResponse, error) {
r.ParentID = parentID
return c.addReaction(ctx, r)
}
func (c *ReactionsClient) addReaction(ctx context.Context, r AddReactionRequestObject) (*ReactionResponse, error) {
endpoint := c.client.makeEndpoint("reaction/")
return c.decode(c.client.post(ctx, endpoint, r, c.client.authenticator.reactionsAuth))
}
func (c *ReactionsClient) decode(resp []byte, err error) (*ReactionResponse, error) {
if err != nil {
return nil, err
}
var result ReactionResponse
if err := json.Unmarshal(resp, &result); err != nil {
return nil, err
}
return &result, nil
}
// Update updates the reaction's data and/or target feeds.
func (c *ReactionsClient) Update(ctx context.Context, id string, data map[string]any, targetFeeds []string) (*ReactionResponse, error) {
endpoint := c.client.makeEndpoint("reaction/%s/", id)
reqData := map[string]any{
"data": data,
"target_feeds": targetFeeds,
}
return c.decode(c.client.put(ctx, endpoint, reqData, c.client.authenticator.reactionsAuth))
}
// Get retrieves a reaction having the given id.
func (c *ReactionsClient) Get(ctx context.Context, id string) (*ReactionResponse, error) {
endpoint := c.client.makeEndpoint("reaction/%s/", id)
return c.decode(c.client.get(ctx, endpoint, nil, c.client.authenticator.reactionsAuth))
}
// Delete deletes a reaction having the given id.
// The reaction is permanently deleted and cannot be restored.
// Returned reaction is empty.
// Optional ReactionOption parameters can be provided, such as WithReactionUserID
// to specify a user ID in the query string.
func (c *ReactionsClient) Delete(ctx context.Context, id string, opts ...ReactionOption) (*ReactionResponse, error) {
endpoint := c.client.makeEndpoint("reaction/%s/", id)
for _, opt := range opts {
endpoint.addQueryParam(opt)
}
return c.decode(c.client.delete(ctx, endpoint, nil, c.client.authenticator.reactionsAuth))
}
// SoftDelete soft-deletes a reaction having the given id. It is possible to restore this reaction using ReactionsClient.Restore.
// Optional ReactionOption parameters can be provided, such as WithReactionUserID
// to specify a user ID in the query string.
func (c *ReactionsClient) SoftDelete(ctx context.Context, id string, opts ...ReactionOption) error {
endpoint := c.client.makeEndpoint("reaction/%s/", id)
endpoint.addQueryParam(makeRequestOption("soft", true))
for _, opt := range opts {
endpoint.addQueryParam(opt)
}
_, err := c.client.delete(ctx, endpoint, nil, c.client.authenticator.reactionsAuth)
return err
}
// Restore restores a soft deleted reaction having the given id.
// Optional ReactionOption parameters can be provided, such as WithReactionUserID
// to specify a user ID in the query string.
func (c *ReactionsClient) Restore(ctx context.Context, id string, opts ...ReactionOption) error {
endpoint := c.client.makeEndpoint("reaction/%s/restore/", id)
for _, opt := range opts {
endpoint.addQueryParam(opt)
}
_, err := c.client.put(ctx, endpoint, nil, c.client.authenticator.reactionsAuth)
return err
}
// Filter lists reactions based on the provided criteria and with the specified pagination.
func (c *ReactionsClient) Filter(ctx context.Context, attr FilterReactionsAttribute, opts ...FilterReactionsOption) (*FilterReactionResponse, error) {
endpointURI := fmt.Sprintf("reaction/%s/", attr())
endpoint := c.client.makeEndpoint(endpointURI)
for _, opt := range opts {
endpoint.addQueryParam(opt)
}
resp, err := c.client.get(ctx, endpoint, nil, c.client.authenticator.reactionsAuth)
if err != nil {
return nil, err
}
var result FilterReactionResponse
if err := json.Unmarshal(resp, &result); err != nil {
return nil, err
}
result.meta.attr = attr
return &result, nil
}
// GetNextPageFilteredReactions returns the reactions at the "next" page of a previous *FilterReactionResponse response, if any.
func (c *ReactionsClient) GetNextPageFilteredReactions(ctx context.Context, resp *FilterReactionResponse) (*FilterReactionResponse, error) {
opts, err := resp.parseNext()
if err != nil {
return nil, err
}
return c.Filter(ctx, resp.meta.attr, opts...)
}