github.com/lologarithm/mattermost-server@v5.3.2-0.20181002060438-c82a84ed765b+incompatible/app/command_join.go (about)

     1  // Copyright (c) 2016-present Mattermost, Inc. All Rights Reserved.
     2  // See License.txt for license information.
     3  
     4  package app
     5  
     6  import (
     7  	"strings"
     8  
     9  	goi18n "github.com/nicksnyder/go-i18n/i18n"
    10  
    11  	"github.com/mattermost/mattermost-server/model"
    12  )
    13  
    14  type JoinProvider struct {
    15  }
    16  
    17  const (
    18  	CMD_JOIN = "join"
    19  )
    20  
    21  func init() {
    22  	RegisterCommandProvider(&JoinProvider{})
    23  }
    24  
    25  func (me *JoinProvider) GetTrigger() string {
    26  	return CMD_JOIN
    27  }
    28  
    29  func (me *JoinProvider) GetCommand(a *App, T goi18n.TranslateFunc) *model.Command {
    30  	return &model.Command{
    31  		Trigger:          CMD_JOIN,
    32  		AutoComplete:     true,
    33  		AutoCompleteDesc: T("api.command_join.desc"),
    34  		AutoCompleteHint: T("api.command_join.hint"),
    35  		DisplayName:      T("api.command_join.name"),
    36  	}
    37  }
    38  
    39  func (me *JoinProvider) DoCommand(a *App, args *model.CommandArgs, message string) *model.CommandResponse {
    40  	channelName := message
    41  
    42  	if strings.HasPrefix(message, "~") {
    43  		channelName = message[1:]
    44  	}
    45  
    46  	result := <-a.Srv.Store.Channel().GetByName(args.TeamId, channelName, true)
    47  	if result.Err != nil {
    48  		return &model.CommandResponse{Text: args.T("api.command_join.list.app_error"), ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL}
    49  	}
    50  
    51  	channel := result.Data.(*model.Channel)
    52  
    53  	if channel.Name != channelName {
    54  		return &model.CommandResponse{ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL, Text: args.T("api.command_join.missing.app_error")}
    55  	}
    56  
    57  	switch channel.Type {
    58  	case model.CHANNEL_OPEN:
    59  		if !a.SessionHasPermissionToChannel(args.Session, channel.Id, model.PERMISSION_JOIN_PUBLIC_CHANNELS) {
    60  			return &model.CommandResponse{Text: args.T("api.command_join.fail.app_error"), ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL}
    61  		}
    62  	case model.CHANNEL_PRIVATE:
    63  		if !a.SessionHasPermissionToChannel(args.Session, channel.Id, model.PERMISSION_READ_CHANNEL) {
    64  			return &model.CommandResponse{Text: args.T("api.command_join.fail.app_error"), ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL}
    65  		}
    66  	default:
    67  		return &model.CommandResponse{Text: args.T("api.command_join.fail.app_error"), ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL}
    68  	}
    69  
    70  	if err := a.JoinChannel(channel, args.UserId); err != nil {
    71  		return &model.CommandResponse{Text: args.T("api.command_join.fail.app_error"), ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL}
    72  	}
    73  
    74  	team, err := a.GetTeam(channel.TeamId)
    75  	if err != nil {
    76  		return &model.CommandResponse{Text: args.T("api.command_join.fail.app_error"), ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL}
    77  	}
    78  
    79  	return &model.CommandResponse{GotoLocation: args.SiteURL + "/" + team.Name + "/channels/" + channel.Name}
    80  }