github.com/jlevesy/mattermost-server@v5.3.2-0.20181003190404-7468f35cb0c8+incompatible/app/command_mute.go (about) 1 // Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved. 2 // See License.txt for license information. 3 4 package app 5 6 import ( 7 "strings" 8 9 "github.com/mattermost/mattermost-server/model" 10 goi18n "github.com/nicksnyder/go-i18n/i18n" 11 ) 12 13 type MuteProvider struct { 14 } 15 16 const ( 17 CMD_MUTE = "mute" 18 ) 19 20 func init() { 21 RegisterCommandProvider(&MuteProvider{}) 22 } 23 24 func (me *MuteProvider) GetTrigger() string { 25 return CMD_MUTE 26 } 27 28 func (me *MuteProvider) GetCommand(a *App, T goi18n.TranslateFunc) *model.Command { 29 return &model.Command{ 30 Trigger: CMD_MUTE, 31 AutoComplete: true, 32 AutoCompleteDesc: T("api.command_mute.desc"), 33 AutoCompleteHint: T("api.command_mute.hint"), 34 DisplayName: T("api.command_mute.name"), 35 } 36 } 37 38 func (me *MuteProvider) DoCommand(a *App, args *model.CommandArgs, message string) *model.CommandResponse { 39 var channel *model.Channel 40 var noChannelErr *model.AppError 41 42 if channel, noChannelErr = a.GetChannel(args.ChannelId); noChannelErr != nil { 43 return &model.CommandResponse{Text: args.T("api.command_mute.no_channel.error"), ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL} 44 } 45 46 channelName := "" 47 splitMessage := strings.Split(message, " ") 48 // Overwrite channel with channel-handle if set 49 if strings.HasPrefix(message, "~") { 50 channelName = splitMessage[0][1:] 51 } else { 52 channelName = splitMessage[0] 53 } 54 55 if len(channelName) > 0 && len(message) > 0 { 56 data := (<-a.Srv.Store.Channel().GetByName(channel.TeamId, channelName, true)).Data 57 58 if data == nil { 59 return &model.CommandResponse{Text: args.T("api.command_mute.error", map[string]interface{}{"Channel": channelName}), ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL} 60 } 61 62 channel = data.(*model.Channel) 63 } 64 65 channelMember := a.ToggleMuteChannel(channel.Id, args.UserId) 66 if channelMember == nil { 67 return &model.CommandResponse{Text: args.T("api.command_mute.not_member.error", map[string]interface{}{"Channel": channelName}), ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL} 68 } 69 70 // Invalidate cache to allow cache lookups while sending notifications 71 a.Srv.Store.Channel().InvalidateCacheForChannelMembersNotifyProps(channel.Id) 72 73 // Direct and Group messages won't have a nice channel title, omit it 74 if channel.Type == model.CHANNEL_DIRECT || channel.Type == model.CHANNEL_GROUP { 75 if channelMember.NotifyProps[model.MARK_UNREAD_NOTIFY_PROP] == model.CHANNEL_NOTIFY_MENTION { 76 publishChannelMemberEvt(a, channelMember, args.UserId) 77 return &model.CommandResponse{Text: args.T("api.command_mute.success_mute_direct_msg"), ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL} 78 } else { 79 publishChannelMemberEvt(a, channelMember, args.UserId) 80 return &model.CommandResponse{Text: args.T("api.command_mute.success_unmute_direct_msg"), ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL} 81 } 82 } 83 84 if channelMember.NotifyProps[model.MARK_UNREAD_NOTIFY_PROP] == model.CHANNEL_NOTIFY_MENTION { 85 publishChannelMemberEvt(a, channelMember, args.UserId) 86 return &model.CommandResponse{Text: args.T("api.command_mute.success_mute", map[string]interface{}{"Channel": channel.DisplayName}), ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL} 87 } else { 88 publishChannelMemberEvt(a, channelMember, args.UserId) 89 return &model.CommandResponse{Text: args.T("api.command_mute.success_unmute", map[string]interface{}{"Channel": channel.DisplayName}), ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL} 90 } 91 } 92 93 func publishChannelMemberEvt(a *App, channelMember *model.ChannelMember, userId string) { 94 evt := model.NewWebSocketEvent(model.WEBSOCKET_EVENT_CHANNEL_MEMBER_UPDATED, "", "", userId, nil) 95 evt.Add("channelMember", channelMember.ToJson()) 96 a.Publish(evt) 97 }