From 257f74873297a6c6b4d14f2d21ffc3adad620c4c Mon Sep 17 00:00:00 2001 From: Christian Claus Date: Wed, 28 Mar 2018 06:02:04 +0200 Subject: [PLT-4340] Channel Mute and "/mute" command #7617 (#7713) * Add command and store changes to allow mute toggling * Change channel muting to use ChannelMember notification structure * Suppress email and push notifications for a muted channel * Make i18n keys issue-compliant * Add notification-cache handling for channel-muting * Add channel handle for channel-muting slash-command * Add unit test for mute command * Merge branch 'master' into PLT-4340 # Conflicts: # app/notification.go * Fix issue that command_mute responses will be overwritten * Fix i18n key for channel muting * Apply new Provider Interface to MuteCommand * Migrate mute notification property to mark_unread PLT-4340 * Make some i18n improvements for command_mute PLT-4340 * Remove de.json translations * Prevent push notifications when channel is muted * Treat Group messages like Direct messages * Fix unit test * Send WS event when the channel member notify props changed --- app/channel.go | 17 ++++++++++ app/command_mute.go | 88 ++++++++++++++++++++++++++++++++++++++++++++++++ app/notification.go | 15 +++++++++ app/notification_test.go | 8 +++++ 4 files changed, 128 insertions(+) create mode 100644 app/command_mute.go (limited to 'app') 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) { -- cgit v1.2.3-1-g7c22