This repository has been archived by the owner on Oct 9, 2023. It is now read-only.
forked from GetStream/stream-chat-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reaction.go
99 lines (76 loc) · 2.5 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
package stream_chat
import (
"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:"-,extra"`
}
type reactionResponse struct {
Message *Message `json:"message"`
Reaction *Reaction `json:"reaction"`
}
type reactionRequest struct {
Reaction *Reaction `json:"reaction"`
}
// SendReaction sends a reaction to message with given ID
func (ch *Channel) SendReaction(reaction *Reaction, messageID string, userID string) (*Message, 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")
}
var resp reactionResponse
reaction.UserID = userID
p := path.Join("messages", url.PathEscape(messageID), "reaction")
req := reactionRequest{Reaction: reaction}
err := ch.client.makeRequest(http.MethodPost, p, nil, req, &resp)
return resp.Message, err
}
// DeleteReaction removes a reaction from message with given ID
func (ch *Channel) DeleteReaction(messageID string, reactionType string, userID string) (*Message, 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 := map[string][]string{
"user_id": {userID},
}
var resp reactionResponse
err := ch.client.makeRequest(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.Message, nil
}
type reactionsResponse struct {
Reactions []*Reaction `json:"reactions"`
}
// GetReactions returns list of the reactions for message with given ID.
// options: Pagination params, ie {"limit":{10}, "idlte": {10}}
func (ch *Channel) GetReactions(messageID string, options map[string][]string) ([]*Reaction, error) {
if messageID == "" {
return nil, errors.New("message ID is empty")
}
p := path.Join("messages", url.PathEscape(messageID), "reactions")
var resp reactionsResponse
err := ch.client.makeRequest(http.MethodGet, p, options, nil, &resp)
return resp.Reactions, err
}