github.com/lologarithm/mattermost-server@v5.3.2-0.20181002060438-c82a84ed765b+incompatible/app/command_channel_rename.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  	goi18n "github.com/nicksnyder/go-i18n/i18n"
     8  
     9  	"github.com/mattermost/mattermost-server/model"
    10  )
    11  
    12  type RenameProvider struct {
    13  }
    14  
    15  const (
    16  	CMD_RENAME = "rename"
    17  )
    18  
    19  func init() {
    20  	RegisterCommandProvider(&RenameProvider{})
    21  }
    22  
    23  func (me *RenameProvider) GetTrigger() string {
    24  	return CMD_RENAME
    25  }
    26  
    27  func (me *RenameProvider) GetCommand(a *App, T goi18n.TranslateFunc) *model.Command {
    28  	return &model.Command{
    29  		Trigger:          CMD_RENAME,
    30  		AutoComplete:     true,
    31  		AutoCompleteDesc: T("api.command_channel_rename.desc"),
    32  		AutoCompleteHint: T("api.command_channel_rename.hint"),
    33  		DisplayName:      T("api.command_channel_rename.name"),
    34  	}
    35  }
    36  
    37  func (me *RenameProvider) DoCommand(a *App, args *model.CommandArgs, message string) *model.CommandResponse {
    38  	channel, err := a.GetChannel(args.ChannelId)
    39  	if err != nil {
    40  		return &model.CommandResponse{
    41  			Text:         args.T("api.command_channel_rename.channel.app_error"),
    42  			ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL,
    43  		}
    44  	}
    45  
    46  	switch channel.Type {
    47  	case model.CHANNEL_OPEN:
    48  		if !a.SessionHasPermissionToChannel(args.Session, args.ChannelId, model.PERMISSION_MANAGE_PUBLIC_CHANNEL_PROPERTIES) {
    49  			return &model.CommandResponse{
    50  				Text:         args.T("api.command_channel_rename.permission.app_error"),
    51  				ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL,
    52  			}
    53  		}
    54  	case model.CHANNEL_PRIVATE:
    55  		if !a.SessionHasPermissionToChannel(args.Session, args.ChannelId, model.PERMISSION_MANAGE_PRIVATE_CHANNEL_PROPERTIES) {
    56  			return &model.CommandResponse{
    57  				Text:         args.T("api.command_channel_rename.permission.app_error"),
    58  				ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL,
    59  			}
    60  		}
    61  	default:
    62  		return &model.CommandResponse{Text: args.T("api.command_channel_rename.direct_group.app_error"), ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL}
    63  	}
    64  
    65  	if len(message) == 0 {
    66  		return &model.CommandResponse{
    67  			Text:         args.T("api.command_channel_rename.message.app_error"),
    68  			ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL,
    69  		}
    70  	} else if len(message) > model.CHANNEL_NAME_UI_MAX_LENGTH {
    71  		return &model.CommandResponse{
    72  			Text: args.T("api.command_channel_rename.too_long.app_error", map[string]interface{}{
    73  				"Length": model.CHANNEL_NAME_UI_MAX_LENGTH,
    74  			}),
    75  			ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL,
    76  		}
    77  	} else if len(message) < model.CHANNEL_NAME_MIN_LENGTH {
    78  		return &model.CommandResponse{
    79  			Text: args.T("api.command_channel_rename.too_short.app_error", map[string]interface{}{
    80  				"Length": model.CHANNEL_NAME_MIN_LENGTH,
    81  			}),
    82  			ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL,
    83  		}
    84  	}
    85  
    86  	patch := &model.ChannelPatch{
    87  		DisplayName: new(string),
    88  	}
    89  	*patch.DisplayName = message
    90  
    91  	_, err = a.PatchChannel(channel, patch, args.UserId)
    92  	if err != nil {
    93  		return &model.CommandResponse{
    94  			Text:         args.T("api.command_channel_rename.update_channel.app_error"),
    95  			ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL,
    96  		}
    97  	}
    98  
    99  	return &model.CommandResponse{}
   100  }