-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathreaction.go
More file actions
134 lines (113 loc) · 4.01 KB
/
reaction.go
File metadata and controls
134 lines (113 loc) · 4.01 KB
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
package reaction
import (
stream_chat "github.com/GetStream/stream-chat-go/v5"
"github.com/MakeNowJust/heredoc"
"github.com/spf13/cobra"
"github.com/GetStream/stream-cli/pkg/config"
"github.com/GetStream/stream-cli/pkg/utils"
)
func NewCmds() []*cobra.Command {
return []*cobra.Command{
getCmd(),
sendCmd(),
deleteCmd(),
}
}
func getCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "get-reactions [message-id]",
Short: "Get reactions for a message",
Example: heredoc.Doc(`
# Get reactions for a [08f64828-3bba-42bd-8430-c26a3634ee5c] message
$ stream-cli chat get-reactions 08f64828-3bba-42bd-8430-c26a3634ee5c --output-format json
`),
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
c, err := config.GetConfig(cmd).GetClient(cmd)
if err != nil {
return err
}
msgID := args[0]
resp, err := c.GetReactions(cmd.Context(), msgID, nil)
if err != nil {
return err
}
return utils.PrintObject(cmd, resp.Reactions)
},
}
fl := cmd.Flags()
fl.StringP("output-format", "o", "json", "[optional] Output format. Can be json or tree")
return cmd
}
func sendCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "send-reaction --message-id [message-id] --user-id [user-id] --reaction-type [reaction-type]",
Short: "Send a reaction to a message",
Long: heredoc.Doc(`
Stream Chat has built-in support for user Reactions. Common examples are
likes, comments, loves, etc. Reactions can be customized so that you
are able to use any type of reaction your application requires.
`),
Example: heredoc.Doc(`
# Send a reaction to a [08f64828-3bba-42bd-8430-c26a3634ee5c] message
$ stream-cli chat send-reaction --message-id 08f64828-3bba-42bd-8430-c26a3634ee5c --user-id 12345 --reaction-type like
`),
RunE: func(cmd *cobra.Command, args []string) error {
c, err := config.GetConfig(cmd).GetClient(cmd)
if err != nil {
return err
}
reactionType, _ := cmd.Flags().GetString("reaction-type")
msgID, _ := cmd.Flags().GetString("message-id")
userID, _ := cmd.Flags().GetString("user-id")
r := &stream_chat.Reaction{Type: reactionType}
_, err = c.SendReaction(cmd.Context(), r, msgID, userID)
if err != nil {
return err
}
cmd.Println("Successfully sent reaction")
return nil
},
}
fl := cmd.Flags()
fl.StringP("reaction-type", "r", "", "[required] The reaction type to send")
fl.StringP("message-id", "m", "", "[required] The message id to send the reaction to")
fl.StringP("user-id", "u", "", "[required] The user id of the user sending the reaction")
_ = cmd.MarkFlagRequired("reaction-type")
_ = cmd.MarkFlagRequired("message-id")
_ = cmd.MarkFlagRequired("user-id")
return cmd
}
func deleteCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "delete-reaction --message-id [message-id] --reaction-type [reaction-type] --user-id [user-id]",
Short: "Delete a reaction from a message",
Example: heredoc.Doc(`
# Delete a reaction from [08f64828-3bba-42bd-8430-c26a3634ee5c] message
$ stream-cli chat delete-reaction --message-id 08f64828-3bba-42bd-8430-c26a3634ee5c --reaction-type like --user-id 12345
`),
RunE: func(cmd *cobra.Command, args []string) error {
c, err := config.GetConfig(cmd).GetClient(cmd)
if err != nil {
return err
}
reactionType, _ := cmd.Flags().GetString("reaction-type")
userID, _ := cmd.Flags().GetString("user-id")
msgID, _ := cmd.Flags().GetString("message-id")
_, err = c.DeleteReaction(cmd.Context(), msgID, reactionType, userID)
if err != nil {
return err
}
cmd.Println("Successfully deleted reaction")
return nil
},
}
fl := cmd.Flags()
fl.StringP("reaction-type", "r", "", "[required] The reaction type to delete")
fl.StringP("message-id", "m", "", "[required] The message id to delete the reaction from")
fl.StringP("user-id", "u", "", "[required] The user id of the user deleting the reaction")
_ = cmd.MarkFlagRequired("reaction-type")
_ = cmd.MarkFlagRequired("message-id")
_ = cmd.MarkFlagRequired("user-id")
return cmd
}