diff options
-rw-r--r-- | api/command_mute_test.go | 114 | ||||
-rw-r--r-- | app/channel.go | 17 | ||||
-rw-r--r-- | app/command_mute.go | 88 | ||||
-rw-r--r-- | app/notification.go | 15 | ||||
-rw-r--r-- | app/notification_test.go | 8 | ||||
-rw-r--r-- | i18n/en.json | 36 | ||||
-rw-r--r-- | model/websocket_message.go | 75 |
7 files changed, 316 insertions, 37 deletions
diff --git a/api/command_mute_test.go b/api/command_mute_test.go new file mode 100644 index 000000000..6b2dad944 --- /dev/null +++ b/api/command_mute_test.go @@ -0,0 +1,114 @@ +// Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved. +// See License.txt for license information. + +package api + +import ( + "strings" + "testing" + + "github.com/mattermost/mattermost-server/model" + "github.com/nicksnyder/go-i18n/i18n" +) + +func TestMuteCommand(t *testing.T) { + th := Setup().InitBasic() + defer th.TearDown() + + i18n.MustLoadTranslationFile("../i18n/en.json") + T, _ := i18n.Tfunc("en") + + // Create client and users + Client := th.BasicClient + team := th.BasicTeam + user1 := Client.Must(Client.GetMe("")).Data.(*model.User) + user2 := th.BasicUser2 + + // Mute channel1 directly with '/mute' + channel1 := &model.Channel{DisplayName: "AA", Name: "aa" + model.NewId() + "a", Type: model.CHANNEL_OPEN, TeamId: team.Id} + channel1 = Client.Must(Client.CreateChannel(channel1)).Data.(*model.Channel) + Client.Must(Client.JoinChannel(channel1.Id)) + + channel1M := Client.Must(Client.GetChannelMember(channel1.Id, user1.Id)).Data.(*model.ChannelMember) + if channel1M.NotifyProps[model.MARK_UNREAD_NOTIFY_PROP] == model.CHANNEL_NOTIFY_MENTION { + t.Fatal("channel shouldn't be muted on initial setup") + } + + rs := Client.Must(Client.Command(channel1.Id, "/mute")).Data.(*model.CommandResponse) + if !strings.EqualFold(rs.Text, T("api.command_mute.success_mute", map[string]interface{}{"Channel": channel1.DisplayName})) { + t.Fatal("failed to mute channel") + } + + channel1M = Client.Must(Client.GetChannelMember(channel1.Id, user1.Id)).Data.(*model.ChannelMember) + if channel1M.NotifyProps[model.MARK_UNREAD_NOTIFY_PROP] == model.CHANNEL_NOTIFY_ALL { + t.Fatal("channel should be muted") + } + + rs = Client.Must(Client.Command(channel1.Id, "/mute")).Data.(*model.CommandResponse) + if !strings.EqualFold(rs.Text, T("api.command_mute.success_unmute", map[string]interface{}{"Channel": channel1.DisplayName})) { + t.Fatal("failed to mute channel") + } + + channel1M = Client.Must(Client.GetChannelMember(channel1.Id, user1.Id)).Data.(*model.ChannelMember) + if channel1M.NotifyProps[model.MARK_UNREAD_NOTIFY_PROP] == model.CHANNEL_NOTIFY_MENTION { + t.Fatal("channel shouldn't be muted anymore") + } + + // Mute channel2 via channel1 with chan-handle '/mute ~aa' + channel2 := &model.Channel{DisplayName: "BB", Name: "bb" + model.NewId() + "a", Type: model.CHANNEL_PRIVATE, TeamId: team.Id} + channel2 = Client.Must(Client.CreateChannel(channel2)).Data.(*model.Channel) + Client.Must(Client.JoinChannel(channel2.Id)) + Client.Must(Client.AddChannelMember(channel2.Id, user2.Id)) + + channel2M := Client.Must(Client.GetChannelMember(channel2.Id, user1.Id)).Data.(*model.ChannelMember) + if channel2M.NotifyProps[model.MARK_UNREAD_NOTIFY_PROP] == model.CHANNEL_NOTIFY_MENTION { + t.Fatal("channel shouldn't be muted on initial setup") + } + + rs = Client.Must(Client.Command(channel1.Id, "/mute ~" + channel2.Name)).Data.(*model.CommandResponse) + if !strings.EqualFold(rs.Text, T("api.command_mute.success_mute", map[string]interface{}{"Channel": channel2.DisplayName})) { + t.Fatal("failed to mute channel") + } + + channel2M = Client.Must(Client.GetChannelMember(channel2.Id, user1.Id)).Data.(*model.ChannelMember) + if channel2M.NotifyProps[model.MARK_UNREAD_NOTIFY_PROP] == model.CHANNEL_NOTIFY_ALL { + t.Fatal("channel should be muted") + } + + rs = Client.Must(Client.Command(channel1.Id, "/mute ~" + channel2.Name)).Data.(*model.CommandResponse) + if !strings.EqualFold(rs.Text, T("api.command_mute.success_unmute", map[string]interface{}{"Channel": channel2.DisplayName})) { + t.Fatal("failed to mute channel") + } + + channel2M = Client.Must(Client.GetChannelMember(channel2.Id, user1.Id)).Data.(*model.ChannelMember) + if channel2M.NotifyProps[model.MARK_UNREAD_NOTIFY_PROP] == model.CHANNEL_NOTIFY_MENTION { + t.Fatal("channel shouldn't be muted anymore") + } + + // Mute direct message + channel3 := Client.Must(Client.CreateDirectChannel(user2.Id)).Data.(*model.Channel) + channel3M := Client.Must(Client.GetChannelMember(channel3.Id, user1.Id)).Data.(*model.ChannelMember) + if channel3M.NotifyProps[model.MARK_UNREAD_NOTIFY_PROP] == model.CHANNEL_NOTIFY_MENTION { + t.Fatal("channel shouldn't be muted on initial setup") + } + + rs = Client.Must(Client.Command(channel3.Id, "/mute")).Data.(*model.CommandResponse) + if !strings.EqualFold(rs.Text, T("api.command_mute.success_mute_direct_msg")) { + t.Fatal("failed to mute channel") + } + + channel3M = Client.Must(Client.GetChannelMember(channel3.Id, user1.Id)).Data.(*model.ChannelMember) + if channel3M.NotifyProps[model.MARK_UNREAD_NOTIFY_PROP] == model.CHANNEL_NOTIFY_ALL { + t.Fatal("channel should be muted") + } + + rs = Client.Must(Client.Command(channel3.Id, "/mute")).Data.(*model.CommandResponse) + if !strings.EqualFold(rs.Text, T("api.command_mute.success_unmute_direct_msg")) { + t.Fatal("failed to mute channel") + } + + channel3M = Client.Must(Client.GetChannelMember(channel3.Id, user1.Id)).Data.(*model.ChannelMember) + if channel3M.NotifyProps[model.MARK_UNREAD_NOTIFY_PROP] == model.CHANNEL_NOTIFY_MENTION { + t.Fatal("channel shouldn't be muted anymore") + } +} diff --git a/app/channel.go b/app/channel.go index eadb94c2f..4e405dd93 100644 --- a/app/channel.go +++ b/app/channel.go @@ -482,6 +482,10 @@ func (a *App) UpdateChannelMemberNotifyProps(data map[string]string, channelId s } else { a.InvalidateCacheForUser(userId) a.InvalidateCacheForChannelMembersNotifyProps(channelId) + // Notify the clients that the member notify props changed + evt := model.NewWebSocketEvent(model.WEBSOCKET_EVENT_CHANNEL_MEMBER_UPDATED, "", "", userId, nil) + evt.Add("channelMember", member.ToJson()) + a.Publish(evt) return member, nil } } @@ -1481,3 +1485,16 @@ func (a *App) GetDirectChannel(userId1, userId2 string) (*model.Channel, *model. } return result.Data.(*model.Channel), nil } + +func (a *App) ToggleMuteChannel(channelId string, userId string) *model.ChannelMember { + member := (<-a.Srv.Store.Channel().GetMember(channelId, userId)).Data.(*model.ChannelMember) + + if member.NotifyProps[model.MARK_UNREAD_NOTIFY_PROP] == model.CHANNEL_NOTIFY_MENTION { + member.NotifyProps[model.MARK_UNREAD_NOTIFY_PROP] = model.CHANNEL_MARK_UNREAD_ALL + } else { + member.NotifyProps[model.MARK_UNREAD_NOTIFY_PROP] = model.CHANNEL_NOTIFY_MENTION + } + + a.Srv.Store.Channel().UpdateMember(member) + return member +} diff --git a/app/command_mute.go b/app/command_mute.go new file mode 100644 index 000000000..072e79f92 --- /dev/null +++ b/app/command_mute.go @@ -0,0 +1,88 @@ +// Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved. +// See License.txt for license information. + +package app + +import ( + "strings" + + "github.com/mattermost/mattermost-server/model" + goi18n "github.com/nicksnyder/go-i18n/i18n" +) + +type MuteProvider struct { +} + +const ( + CMD_MUTE = "mute" +) + +func init() { + RegisterCommandProvider(&MuteProvider{}) +} + +func (me *MuteProvider) GetTrigger() string { + return CMD_MUTE +} + +func (me *MuteProvider) GetCommand(a *App, T goi18n.TranslateFunc) *model.Command { + return &model.Command{ + Trigger: CMD_MUTE, + AutoComplete: true, + AutoCompleteDesc: T("api.command_mute.desc"), + AutoCompleteHint: T("api.command_mute.hint"), + DisplayName: T("api.command_mute.name"), + } +} + +func (me *MuteProvider) DoCommand(a *App, args *model.CommandArgs, message string) *model.CommandResponse { + var channel *model.Channel + var noChannelErr *model.AppError + + if channel, noChannelErr = a.GetChannel(args.ChannelId); noChannelErr != nil { + return &model.CommandResponse{Text: args.T("api.command_mute.error", map[string]interface{}{"Channel": channel.DisplayName}), ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL} + } + + // Overwrite channel with channel-handle if set + if strings.HasPrefix(message, "~") { + splitMessage := strings.Split(message, " ") + chanHandle := strings.Split(splitMessage[0], "~")[1] + data := (<-a.Srv.Store.Channel().GetByName(channel.TeamId, chanHandle, true)).Data + + if data == nil { + return &model.CommandResponse{Text: args.T("api.command_mute.error", map[string]interface{}{"Channel": chanHandle}), ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL} + } + + channel = data.(*model.Channel) + } + + channelMember := a.ToggleMuteChannel(channel.Id, args.UserId) + + // Invalidate cache to allow cache lookups while sending notifications + a.Srv.Store.Channel().InvalidateCacheForChannelMembersNotifyProps(channel.Id) + + // Direct and Group messages won't have a nice channel title, omit it + if channel.Type == model.CHANNEL_DIRECT || channel.Type == model.CHANNEL_GROUP { + if channelMember.NotifyProps[model.MARK_UNREAD_NOTIFY_PROP] == model.CHANNEL_NOTIFY_MENTION { + publishChannelMemberEvt(a, channelMember, args.UserId) + return &model.CommandResponse{Text: args.T("api.command_mute.success_mute_direct_msg"), ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL} + } else { + publishChannelMemberEvt(a, channelMember, args.UserId) + return &model.CommandResponse{Text: args.T("api.command_mute.success_unmute_direct_msg"), ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL} + } + } + + if channelMember.NotifyProps[model.MARK_UNREAD_NOTIFY_PROP] == model.CHANNEL_NOTIFY_MENTION { + publishChannelMemberEvt(a, channelMember, args.UserId) + return &model.CommandResponse{Text: args.T("api.command_mute.success_mute", map[string]interface{}{"Channel": channel.DisplayName}), ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL} + } else { + publishChannelMemberEvt(a, channelMember, args.UserId) + return &model.CommandResponse{Text: args.T("api.command_mute.success_unmute", map[string]interface{}{"Channel": channel.DisplayName}), ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL} + } +} + +func publishChannelMemberEvt(a *App, channelMember *model.ChannelMember, userId string) { + evt := model.NewWebSocketEvent(model.WEBSOCKET_EVENT_CHANNEL_MEMBER_UPDATED, "", "", userId, nil) + evt.Add("channelMember", channelMember.ToJson()) + a.Publish(evt) +} diff --git a/app/notification.go b/app/notification.go index bb0c8703f..181ad4aac 100644 --- a/app/notification.go +++ b/app/notification.go @@ -163,6 +163,14 @@ func (a *App) SendNotifications(post *model.Post, team *model.Team, channel *mod } } + // Remove the user as recipient when the user has muted the channel. + if channelMuted, ok := channelMemberNotifyPropsMap[id][model.MARK_UNREAD_NOTIFY_PROP]; ok { + if channelMuted == model.CHANNEL_MARK_UNREAD_MENTION { + l4g.Debug("Channel muted for user_id %v, channel_mute %v", id, channelMuted) + userAllowsEmails = false + } + } + //If email verification is required and user email is not verified don't send email. if a.Config().EmailSettings.RequireEmailVerification && !profileMap[id].EmailVerified { l4g.Error("Skipped sending notification email to %v, address not verified. [details: user_id=%v]", profileMap[id].Email, id) @@ -980,6 +988,13 @@ func DoesNotifyPropsAllowPushNotification(user *model.User, channelNotifyProps m userNotify := userNotifyProps[model.PUSH_NOTIFY_PROP] channelNotify, ok := channelNotifyProps[model.PUSH_NOTIFY_PROP] + // If the channel is muted do not send push notifications + if channelMuted, ok := channelNotifyProps[model.MARK_UNREAD_NOTIFY_PROP]; ok { + if channelMuted == model.CHANNEL_MARK_UNREAD_MENTION { + return false + } + } + if post.IsSystemMessage() { return false } diff --git a/app/notification_test.go b/app/notification_test.go index 5fc1d152c..05574eb08 100644 --- a/app/notification_test.go +++ b/app/notification_test.go @@ -783,6 +783,14 @@ func TestDoesNotifyPropsAllowPushNotification(t *testing.T) { if DoesNotifyPropsAllowPushNotification(user, channelNotifyProps, post, true) { t.Fatal("Should have returned false") } + + // WHEN default is ALL and channel is MUTED + userNotifyProps[model.PUSH_NOTIFY_PROP] = model.USER_NOTIFY_ALL + user.NotifyProps = userNotifyProps + channelNotifyProps[model.MARK_UNREAD_NOTIFY_PROP] = model.CHANNEL_MARK_UNREAD_MENTION + if DoesNotifyPropsAllowPushNotification(user, channelNotifyProps, post, false) { + t.Fatal("Should have returned false") + } } func TestDoesStatusAllowPushNotification(t *testing.T) { diff --git a/i18n/en.json b/i18n/en.json index cf3e88b5a..6b387404b 100644 --- a/i18n/en.json +++ b/i18n/en.json @@ -1015,6 +1015,38 @@ "translation": "shrug" }, { + "id": "api.command_mute.desc", + "translation": "Turns off desktop, email and push notifications for the current channel or the [channel] specified." + }, + { + "id": "api.command_mute.hint", + "translation": "[channel]" + }, + { + "id": "api.command_mute.name", + "translation": "mute" + }, + { + "id": "api.command_mute.error", + "translation": "Could not find the channel {{.Channel}}." + }, + { + "id": "api.command_mute.success_mute", + "translation": "You will not receive notifications for {{.Channel}} until channel mute is turned off." + }, + { + "id": "api.command_mute.success_unmute", + "translation": "{{.Channel}} is no longer muted." + }, + { + "id": "api.command_mute.success_mute_direct_msg", + "translation": "You will not receive notifications for this channel until channel mute is turned off." + }, + { + "id": "api.command_mute.success_unmute_direct_msg", + "translation": "This channel is no longer muted." + }, + { "id": "api.compliance.init.debug", "translation": "Initializing compliance API routes" }, @@ -4571,6 +4603,10 @@ "translation": "Invalid email notification value" }, { + "id": "model.channel_member.is_valid.mute_value.app_error", + "translation": "Invalid muting value" + }, + { "id": "model.channel_member.is_valid.notify_level.app_error", "translation": "Invalid notify level" }, diff --git a/model/websocket_message.go b/model/websocket_message.go index 9013ab428..4a547bb6a 100644 --- a/model/websocket_message.go +++ b/model/websocket_message.go @@ -10,43 +10,44 @@ import ( ) const ( - WEBSOCKET_EVENT_TYPING = "typing" - WEBSOCKET_EVENT_POSTED = "posted" - WEBSOCKET_EVENT_POST_EDITED = "post_edited" - WEBSOCKET_EVENT_POST_DELETED = "post_deleted" - WEBSOCKET_EVENT_CHANNEL_DELETED = "channel_deleted" - WEBSOCKET_EVENT_CHANNEL_CREATED = "channel_created" - WEBSOCKET_EVENT_CHANNEL_UPDATED = "channel_updated" - WEBSOCKET_EVENT_DIRECT_ADDED = "direct_added" - WEBSOCKET_EVENT_GROUP_ADDED = "group_added" - WEBSOCKET_EVENT_NEW_USER = "new_user" - WEBSOCKET_EVENT_ADDED_TO_TEAM = "added_to_team" - WEBSOCKET_EVENT_LEAVE_TEAM = "leave_team" - WEBSOCKET_EVENT_UPDATE_TEAM = "update_team" - WEBSOCKET_EVENT_DELETE_TEAM = "delete_team" - WEBSOCKET_EVENT_USER_ADDED = "user_added" - WEBSOCKET_EVENT_USER_UPDATED = "user_updated" - WEBSOCKET_EVENT_USER_ROLE_UPDATED = "user_role_updated" - WEBSOCKET_EVENT_MEMBERROLE_UPDATED = "memberrole_updated" - WEBSOCKET_EVENT_USER_REMOVED = "user_removed" - WEBSOCKET_EVENT_PREFERENCE_CHANGED = "preference_changed" - WEBSOCKET_EVENT_PREFERENCES_CHANGED = "preferences_changed" - WEBSOCKET_EVENT_PREFERENCES_DELETED = "preferences_deleted" - WEBSOCKET_EVENT_EPHEMERAL_MESSAGE = "ephemeral_message" - WEBSOCKET_EVENT_STATUS_CHANGE = "status_change" - WEBSOCKET_EVENT_HELLO = "hello" - WEBSOCKET_EVENT_WEBRTC = "webrtc" - WEBSOCKET_AUTHENTICATION_CHALLENGE = "authentication_challenge" - WEBSOCKET_EVENT_REACTION_ADDED = "reaction_added" - WEBSOCKET_EVENT_REACTION_REMOVED = "reaction_removed" - WEBSOCKET_EVENT_RESPONSE = "response" - WEBSOCKET_EVENT_EMOJI_ADDED = "emoji_added" - WEBSOCKET_EVENT_CHANNEL_VIEWED = "channel_viewed" - WEBSOCKET_EVENT_PLUGIN_ACTIVATED = "plugin_activated" // EXPERIMENTAL - SUBJECT TO CHANGE - WEBSOCKET_EVENT_PLUGIN_DEACTIVATED = "plugin_deactivated" // EXPERIMENTAL - SUBJECT TO CHANGE - WEBSOCKET_EVENT_ROLE_UPDATED = "role_updated" - WEBSOCKET_EVENT_LICENSE_CHANGED = "license_changed" - WEBSOCKET_EVENT_CONFIG_CHANGED = "config_changed" + WEBSOCKET_EVENT_TYPING = "typing" + WEBSOCKET_EVENT_POSTED = "posted" + WEBSOCKET_EVENT_POST_EDITED = "post_edited" + WEBSOCKET_EVENT_POST_DELETED = "post_deleted" + WEBSOCKET_EVENT_CHANNEL_DELETED = "channel_deleted" + WEBSOCKET_EVENT_CHANNEL_CREATED = "channel_created" + WEBSOCKET_EVENT_CHANNEL_UPDATED = "channel_updated" + WEBSOCKET_EVENT_CHANNEL_MEMBER_UPDATED = "channel_member_updated" + WEBSOCKET_EVENT_DIRECT_ADDED = "direct_added" + WEBSOCKET_EVENT_GROUP_ADDED = "group_added" + WEBSOCKET_EVENT_NEW_USER = "new_user" + WEBSOCKET_EVENT_ADDED_TO_TEAM = "added_to_team" + WEBSOCKET_EVENT_LEAVE_TEAM = "leave_team" + WEBSOCKET_EVENT_UPDATE_TEAM = "update_team" + WEBSOCKET_EVENT_DELETE_TEAM = "delete_team" + WEBSOCKET_EVENT_USER_ADDED = "user_added" + WEBSOCKET_EVENT_USER_UPDATED = "user_updated" + WEBSOCKET_EVENT_USER_ROLE_UPDATED = "user_role_updated" + WEBSOCKET_EVENT_MEMBERROLE_UPDATED = "memberrole_updated" + WEBSOCKET_EVENT_USER_REMOVED = "user_removed" + WEBSOCKET_EVENT_PREFERENCE_CHANGED = "preference_changed" + WEBSOCKET_EVENT_PREFERENCES_CHANGED = "preferences_changed" + WEBSOCKET_EVENT_PREFERENCES_DELETED = "preferences_deleted" + WEBSOCKET_EVENT_EPHEMERAL_MESSAGE = "ephemeral_message" + WEBSOCKET_EVENT_STATUS_CHANGE = "status_change" + WEBSOCKET_EVENT_HELLO = "hello" + WEBSOCKET_EVENT_WEBRTC = "webrtc" + WEBSOCKET_AUTHENTICATION_CHALLENGE = "authentication_challenge" + WEBSOCKET_EVENT_REACTION_ADDED = "reaction_added" + WEBSOCKET_EVENT_REACTION_REMOVED = "reaction_removed" + WEBSOCKET_EVENT_RESPONSE = "response" + WEBSOCKET_EVENT_EMOJI_ADDED = "emoji_added" + WEBSOCKET_EVENT_CHANNEL_VIEWED = "channel_viewed" + WEBSOCKET_EVENT_PLUGIN_ACTIVATED = "plugin_activated" // EXPERIMENTAL - SUBJECT TO CHANGE + WEBSOCKET_EVENT_PLUGIN_DEACTIVATED = "plugin_deactivated" // EXPERIMENTAL - SUBJECT TO CHANGE + WEBSOCKET_EVENT_ROLE_UPDATED = "role_updated" + WEBSOCKET_EVENT_LICENSE_CHANGED = "license_changed" + WEBSOCKET_EVENT_CONFIG_CHANGED = "config_changed" ) type WebSocketMessage interface { |