Commit 257f7487 authored by Christian Claus's avatar Christian Claus Committed by Elias Nahum

[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
parent 71c9dff7
// 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")
}
}
......@@ -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
}
// 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)
}
......@@ -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
}
......
......@@ -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) {
......
......@@ -1014,6 +1014,38 @@
"id": "api.command_shrug.name",
"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"
......@@ -4570,6 +4602,10 @@
"id": "model.channel_member.is_valid.email_value.app_error",
"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"
......
......@@ -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 {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment