github.com/coincircle/mattermost-server@v4.8.1-0.20180321182714-9d701c704416+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  	"github.com/mattermost/mattermost-server/model"
     8  	goi18n "github.com/nicksnyder/go-i18n/i18n"
     9  )
    10  
    11  type JoinProvider struct {
    12  }
    13  
    14  const (
    15  	CMD_JOIN = "join"
    16  )
    17  
    18  func init() {
    19  	RegisterCommandProvider(&JoinProvider{})
    20  }
    21  
    22  func (me *JoinProvider) GetTrigger() string {
    23  	return CMD_JOIN
    24  }
    25  
    26  func (me *JoinProvider) GetCommand(a *App, T goi18n.TranslateFunc) *model.Command {
    27  	return &model.Command{
    28  		Trigger:          CMD_JOIN,
    29  		AutoComplete:     true,
    30  		AutoCompleteDesc: T("api.command_join.desc"),
    31  		AutoCompleteHint: T("api.command_join.hint"),
    32  		DisplayName:      T("api.command_join.name"),
    33  	}
    34  }
    35  
    36  func (me *JoinProvider) DoCommand(a *App, args *model.CommandArgs, message string) *model.CommandResponse {
    37  	if result := <-a.Srv.Store.Channel().GetByName(args.TeamId, message, true); result.Err != nil {
    38  		return &model.CommandResponse{Text: args.T("api.command_join.list.app_error"), ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL}
    39  	} else {
    40  		channel := result.Data.(*model.Channel)
    41  
    42  		if channel.Name == message {
    43  			allowed := false
    44  			if (channel.Type == model.CHANNEL_PRIVATE && a.SessionHasPermissionToChannel(args.Session, channel.Id, model.PERMISSION_READ_CHANNEL)) || channel.Type == model.CHANNEL_OPEN {
    45  				allowed = true
    46  			}
    47  
    48  			if !allowed {
    49  				return &model.CommandResponse{Text: args.T("api.command_join.fail.app_error"), ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL}
    50  			}
    51  
    52  			if err := a.JoinChannel(channel, args.UserId); err != nil {
    53  				return &model.CommandResponse{Text: args.T("api.command_join.fail.app_error"), ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL}
    54  			}
    55  
    56  			team, err := a.GetTeam(channel.TeamId)
    57  			if err != nil {
    58  				return &model.CommandResponse{Text: args.T("api.command_join.fail.app_error"), ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL}
    59  			}
    60  
    61  			return &model.CommandResponse{GotoLocation: args.SiteURL + "/" + team.Name + "/channels/" + channel.Name}
    62  		}
    63  	}
    64  
    65  	return &model.CommandResponse{ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL, Text: args.T("api.command_join.missing.app_error")}
    66  }