github.com/gigforks/mattermost-server@v4.9.1-0.20180619094218-800d97fa55d0+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  	"github.com/mattermost/mattermost-server/model"
     8  	goi18n "github.com/nicksnyder/go-i18n/i18n"
     9  )
    10  
    11  type RenameProvider struct {
    12  }
    13  
    14  const (
    15  	CMD_RENAME = "rename"
    16  )
    17  
    18  func init() {
    19  	RegisterCommandProvider(&RenameProvider{})
    20  }
    21  
    22  func (me *RenameProvider) GetTrigger() string {
    23  	return CMD_RENAME
    24  }
    25  
    26  func (me *RenameProvider) GetCommand(a *App, T goi18n.TranslateFunc) *model.Command {
    27  	return &model.Command{
    28  		Trigger:          CMD_RENAME,
    29  		AutoComplete:     true,
    30  		AutoCompleteDesc: T("api.command_channel_rename.desc"),
    31  		AutoCompleteHint: T("api.command_channel_rename.hint"),
    32  		DisplayName:      T("api.command_channel_rename.name"),
    33  	}
    34  }
    35  
    36  func (me *RenameProvider) DoCommand(a *App, args *model.CommandArgs, message string) *model.CommandResponse {
    37  	channel, err := a.GetChannel(args.ChannelId)
    38  	if err != nil {
    39  		return &model.CommandResponse{Text: args.T("api.command_channel_rename.channel.app_error"), ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL}
    40  	}
    41  
    42  	if channel.Type == model.CHANNEL_OPEN && !a.SessionHasPermissionToChannel(args.Session, args.ChannelId, model.PERMISSION_MANAGE_PUBLIC_CHANNEL_PROPERTIES) {
    43  		return &model.CommandResponse{Text: args.T("api.command_channel_rename.permission.app_error"), ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL}
    44  	}
    45  
    46  	if channel.Type == model.CHANNEL_PRIVATE && !a.SessionHasPermissionToChannel(args.Session, args.ChannelId, model.PERMISSION_MANAGE_PRIVATE_CHANNEL_PROPERTIES) {
    47  		return &model.CommandResponse{Text: args.T("api.command_channel_rename.permission.app_error"), ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL}
    48  	}
    49  
    50  	if channel.Type == model.CHANNEL_GROUP || channel.Type == model.CHANNEL_DIRECT {
    51  		return &model.CommandResponse{Text: args.T("api.command_channel_rename.direct_group.app_error"), ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL}
    52  	}
    53  
    54  	if len(message) == 0 {
    55  		return &model.CommandResponse{Text: args.T("api.command_channel_rename.message.app_error"), ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL}
    56  	} else if len(message) > model.CHANNEL_NAME_UI_MAX_LENGTH {
    57  		return &model.CommandResponse{Text: args.T("api.command_channel_rename.too_long.app_error", map[string]interface{}{"Length": model.CHANNEL_NAME_UI_MAX_LENGTH}), ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL}
    58  	} else if len(message) < model.CHANNEL_NAME_MIN_LENGTH {
    59  		return &model.CommandResponse{Text: args.T("api.command_channel_rename.too_short.app_error", map[string]interface{}{"Length": model.CHANNEL_NAME_MIN_LENGTH}), ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL}
    60  	}
    61  
    62  	patch := &model.ChannelPatch{
    63  		DisplayName: new(string),
    64  	}
    65  	*patch.DisplayName = message
    66  
    67  	_, err = a.PatchChannel(channel, patch, args.UserId)
    68  	if err != nil {
    69  		return &model.CommandResponse{Text: args.T("api.command_channel_rename.update_channel.app_error"), ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL}
    70  	}
    71  
    72  	return &model.CommandResponse{}
    73  }