diff --git a/conversation.go b/conversation.go index 60c50fc70..89ff0a157 100644 --- a/conversation.go +++ b/conversation.go @@ -2,6 +2,7 @@ package slack import ( "context" + "fmt" "net/url" "strconv" "strings" @@ -191,6 +192,97 @@ func (api *Client) UnArchiveConversationContext(ctx context.Context, channelID s return response.Err() } +// MuteConversation updates the user's preference as to adds a channel id to the +// list of muted channel. This method returns the updated list of muted channels. +func (api *Client) MuteConversation(channelID string) ([]string, error) { + return api.MuteConversationContext(context.Background(), channelID) +} + +// MuteConversationContext adds a channel id to the list of muted channel with a custom context. +func (api *Client) MuteConversationContext(ctx context.Context, channelID string) ([]string, error) { + prefs, err := api.GetUserPrefsContext(ctx) + if err != nil { + return nil, err + } + + mutedChannels := strings.Split(prefs.UserPrefs.MutedChannels, ",") + for _, mc := range mutedChannels { + if mc == channelID { + return mutedChannels, nil // noop + } + } + + values := url.Values{ + "token": {api.token}, + "prefs": {fmt.Sprintf("{\"muted_channels\": \"%s,%s\"}", prefs.UserPrefs.MutedChannels, channelID)}, + "reason": {"update-muted-channels"}, + } + response := UserPrefsCarrier{} + + err = api.postMethod(ctx, "users.prefs.set", values, &response) + if err != nil { + return nil, err + } + + if response.Err() != nil { + return nil, response.Err() + } + if response.UserPrefs == nil { + return nil, nil + } + return strings.Split(response.UserPrefs.MutedChannels, ","), nil +} + +// UnMuteConversation updates the user's preference as to remove a channel id from +// the list of muted channel. This method returns the updated list of muted channels. +func (api *Client) UnMuteConversation(channelID string) ([]string, error) { + return api.UnMuteConversationContext(context.Background(), channelID) +} + +// UnMuteConversationContext removes a channel id from the list of muted channel with a custom context. +func (api *Client) UnMuteConversationContext(ctx context.Context, channelID string) ([]string, error) { + prefs, err := api.GetUserPrefsContext(ctx) + if err != nil { + return nil, err + } + + mutedChannels := strings.Split(prefs.UserPrefs.MutedChannels, ",") + update := []string{} + isMuted := false + for _, mc := range mutedChannels { + if mc == channelID { + isMuted = true + continue + } + + update = append(update, mc) + } + + if !isMuted { + return nil, nil // noop + } + + values := url.Values{ + "token": {api.token}, + "prefs": {fmt.Sprintf("{\"muted_channels\": \"%s\"}", strings.Join(update, ","))}, + "reason": {"update-muted-channels"}, + } + response := UserPrefsCarrier{} + + err = api.postMethod(ctx, "users.prefs.set", values, &response) + if err != nil { + return nil, err + } + + if response.Err() != nil { + return nil, response.Err() + } + if response.UserPrefs == nil { + return nil, nil + } + return strings.Split(response.UserPrefs.MutedChannels, ","), nil +} + // SetTopicOfConversation sets the topic for a conversation func (api *Client) SetTopicOfConversation(channelID, topic string) (*Channel, error) { return api.SetTopicOfConversationContext(context.Background(), channelID, topic) diff --git a/conversation_test.go b/conversation_test.go index 3f00374e1..6a452b2d3 100644 --- a/conversation_test.go +++ b/conversation_test.go @@ -263,6 +263,42 @@ func TestUnArchiveConversation(t *testing.T) { } } +func getUsersPrefs(rw http.ResponseWriter, r *http.Request) { + rw.Header().Set("Content-Type", "application/json") + response, _ := json.Marshal(UserPrefsCarrier{ + SlackResponse: SlackResponse{Ok: true}, + UserPrefs: &UserPrefs{ + MutedChannels: "CYYYYYYYY", + }, + }) + rw.Write(response) +} + +func TestMuteConversation(t *testing.T) { + http.HandleFunc("/users.prefs.get", getUsersPrefs) + http.HandleFunc("/users.prefs.set", getUsersPrefs) + once.Do(startServer) + api := New("testing-token", OptionAPIURL("http://"+serverAddr+"/")) + _, err := api.MuteConversation("CXXXXXXXX") + if err != nil { + t.Errorf("Unexpected error: %s", err) + return + } +} + +func TestUnMuteConversation(t *testing.T) { + // Handlers are commented out to avoid double declaration. + // http.HandleFunc("/users.prefs.get", getUsersPrefs) + // http.HandleFunc("/users.prefs.set", okJSONHandler) + once.Do(startServer) + api := New("testing-token", OptionAPIURL("http://"+serverAddr+"/")) + _, err := api.UnMuteConversation("CYYYYYYYY") + if err != nil { + t.Errorf("Unexpected error: %s", err) + return + } +} + func getTestChannel() *Channel { return &Channel{ GroupConversation: GroupConversation{ diff --git a/info.go b/info.go index 9bdb2a944..b3d651234 100644 --- a/info.go +++ b/info.go @@ -6,7 +6,6 @@ import ( "fmt" "net/url" "strconv" - "strings" "time" ) @@ -320,76 +319,17 @@ type UserPrefs struct { TZ string `json:"tz,omitempty"` } +// GetUserPrefs gets the user's preferences. func (api *Client) GetUserPrefs() (*UserPrefsCarrier, error) { - values := url.Values{"token": {api.token}} - response := UserPrefsCarrier{} - - err := api.getMethod(context.Background(), "users.prefs.get", values, &response) - if err != nil { - return nil, err - } - - return &response, response.Err() + return api.GetUserPrefsContext(context.Background()) } -func (api *Client) MuteChat(channelID string) (*UserPrefsCarrier, error) { - prefs, err := api.GetUserPrefs() - if err != nil { - return nil, err - } - - mutedChannels := strings.Split(prefs.UserPrefs.MutedChannels, ",") - for _, mc := range mutedChannels { - if mc == channelID { - return nil, nil // noop - } - } - - values := url.Values{ - "token": {api.token}, - "prefs": {fmt.Sprintf("{\"muted_channels\": \"%s,%s\"}", prefs.UserPrefs.MutedChannels, channelID)}, - "reason": {"update-muted-channels"}, - } - response := UserPrefsCarrier{} - - err = api.postMethod(context.Background(), "users.prefs.set", values, &response) - if err != nil { - return nil, err - } - - return &response, response.Err() -} - -func (api *Client) UnMuteChat(channelID string) (*UserPrefsCarrier, error) { - prefs, err := api.GetUserPrefs() - if err != nil { - return nil, err - } - - mutedChannels := strings.Split(prefs.UserPrefs.MutedChannels, ",") - update := []string{} - isMuted := false - for _, mc := range mutedChannels { - if mc == channelID { - isMuted = true - continue - } - - update = append(update, mc) - } - - if !isMuted { - return nil, nil // noop - } - - values := url.Values{ - "token": {api.token}, - "prefs": {fmt.Sprintf("{\"muted_channels\": \"%s\"}", strings.Join(update, ","))}, - "reason": {"update-muted-channels"}, - } +// GetUserPrefsContext gets the user's preferences with a custom context. +func (api *Client) GetUserPrefsContext(ctx context.Context) (*UserPrefsCarrier, error) { + values := url.Values{"token": {api.token}} response := UserPrefsCarrier{} - err = api.postMethod(context.Background(), "users.prefs.set", values, &response) + err := api.getMethod(ctx, "users.prefs.get", values, &response) if err != nil { return nil, err }