Skip to content

Commit

Permalink
Made mute conversation more standard and added test
Browse files Browse the repository at this point in the history
  • Loading branch information
Karim Nahas committed Feb 15, 2021
1 parent 677ea10 commit 5e67b9b
Show file tree
Hide file tree
Showing 3 changed files with 134 additions and 66 deletions.
92 changes: 92 additions & 0 deletions conversation.go
Expand Up @@ -2,6 +2,7 @@ package slack

import (
"context"
"fmt"
"net/url"
"strconv"
"strings"
Expand Down Expand Up @@ -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)
Expand Down
36 changes: 36 additions & 0 deletions conversation_test.go
Expand Up @@ -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{
Expand Down
72 changes: 6 additions & 66 deletions info.go
Expand Up @@ -6,7 +6,6 @@ import (
"fmt"
"net/url"
"strconv"
"strings"
"time"
)

Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit 5e67b9b

Please sign in to comment.