-
Notifications
You must be signed in to change notification settings - Fork 31
/
reaction.go
130 lines (105 loc) · 3.65 KB
/
reaction.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
package stream_chat
import (
"context"
"encoding/json"
"errors"
"net/http"
"net/url"
"path"
)
type Reaction struct {
MessageID string `json:"message_id"`
UserID string `json:"user_id"`
Type string `json:"type"`
// any other fields the user wants to attach a reaction
ExtraData map[string]interface{} `json:"-"`
}
type reactionForJSON Reaction
func (s *Reaction) UnmarshalJSON(data []byte) error {
var s2 reactionForJSON
if err := json.Unmarshal(data, &s2); err != nil {
return err
}
*s = Reaction(s2)
if err := json.Unmarshal(data, &s.ExtraData); err != nil {
return err
}
removeFromMap(s.ExtraData, *s)
return nil
}
func (s Reaction) MarshalJSON() ([]byte, error) {
return addToMapAndMarshal(s.ExtraData, reactionForJSON(s))
}
type ReactionResponse struct {
Message *Message `json:"message"`
Reaction *Reaction `json:"reaction"`
Response
}
type reactionRequest struct {
Reaction *Reaction `json:"reaction"`
}
// SendReaction sends a reaction to message with given ID.
// Deprecated: SendReaction is deprecated, use client.SendReaction instead.
func (ch *Channel) SendReaction(ctx context.Context, reaction *Reaction, messageID, userID string) (*ReactionResponse, error) {
return ch.client.SendReaction(ctx, reaction, messageID, userID)
}
// DeleteReaction removes a reaction from message with given ID.
// Deprecated: DeleteReaction is deprecated, use client.DeleteReaction instead.
func (ch *Channel) DeleteReaction(ctx context.Context, messageID, reactionType, userID string) (*ReactionResponse, error) {
return ch.client.DeleteReaction(ctx, messageID, reactionType, userID)
}
// SendReaction sends a reaction to message with given ID.
func (c *Client) SendReaction(ctx context.Context, reaction *Reaction, messageID, userID string) (*ReactionResponse, error) {
switch {
case reaction == nil:
return nil, errors.New("reaction is nil")
case messageID == "":
return nil, errors.New("message ID must be not empty")
case userID == "":
return nil, errors.New("user ID must be not empty")
}
reaction.UserID = userID
p := path.Join("messages", url.PathEscape(messageID), "reaction")
req := reactionRequest{Reaction: reaction}
var resp ReactionResponse
err := c.makeRequest(ctx, http.MethodPost, p, nil, req, &resp)
return &resp, err
}
// DeleteReaction removes a reaction from message with given ID.
func (c *Client) DeleteReaction(ctx context.Context, messageID, reactionType, userID string) (*ReactionResponse, error) {
switch {
case messageID == "":
return nil, errors.New("message ID is empty")
case reactionType == "":
return nil, errors.New("reaction type is empty")
case userID == "":
return nil, errors.New("user ID is empty")
}
p := path.Join("messages", url.PathEscape(messageID), "reaction", url.PathEscape(reactionType))
params := url.Values{}
params.Set("user_id", userID)
var resp ReactionResponse
err := c.makeRequest(ctx, http.MethodDelete, p, params, nil, &resp)
if err != nil {
return nil, err
}
if resp.Message == nil {
return nil, errors.New("unexpected error: response message nil")
}
return &resp, nil
}
type ReactionsResponse struct {
Reactions []*Reaction `json:"reactions"`
Response
}
// GetReactions returns list of the reactions for message with given ID.
// options: Pagination params, ie {"limit":{"10"}, "idlte": {"10"}}
func (c *Client) GetReactions(ctx context.Context, messageID string, options map[string][]string) (*ReactionsResponse, error) {
if messageID == "" {
return nil, errors.New("message ID is empty")
}
p := path.Join("messages", url.PathEscape(messageID), "reactions")
var resp ReactionsResponse
err := c.makeRequest(ctx, http.MethodGet, p, options, nil, &resp)
return &resp, err
}