github.com/space0122/mattermost-server@v5.11.1+incompatible/cmd/mattermost/commands/channelargs.go (about)

     1  // Copyright (c) 2016-present Mattermost, Inc. All Rights Reserved.
     2  // See License.txt for license information.
     3  
     4  package commands
     5  
     6  import (
     7  	"fmt"
     8  	"strings"
     9  
    10  	"github.com/mattermost/mattermost-server/app"
    11  	"github.com/mattermost/mattermost-server/model"
    12  )
    13  
    14  const CHANNEL_ARG_SEPARATOR = ":"
    15  
    16  func getChannelsFromChannelArgs(a *app.App, channelArgs []string) []*model.Channel {
    17  	channels := make([]*model.Channel, 0, len(channelArgs))
    18  	for _, channelArg := range channelArgs {
    19  		channel := getChannelFromChannelArg(a, channelArg)
    20  		channels = append(channels, channel)
    21  	}
    22  	return channels
    23  }
    24  
    25  func parseChannelArg(channelArg string) (string, string) {
    26  	result := strings.SplitN(channelArg, CHANNEL_ARG_SEPARATOR, 2)
    27  	if len(result) == 1 {
    28  		return "", channelArg
    29  	}
    30  	return result[0], result[1]
    31  }
    32  
    33  func getChannelFromChannelArg(a *app.App, channelArg string) *model.Channel {
    34  	teamArg, channelPart := parseChannelArg(channelArg)
    35  	if teamArg == "" && channelPart == "" {
    36  		return nil
    37  	}
    38  
    39  	var channel *model.Channel
    40  	if teamArg != "" {
    41  		team := getTeamFromTeamArg(a, teamArg)
    42  		if team == nil {
    43  			return nil
    44  		}
    45  
    46  		if result := <-a.Srv.Store.Channel().GetByNameIncludeDeleted(team.Id, channelPart, true); result.Err == nil {
    47  			channel = result.Data.(*model.Channel)
    48  		} else {
    49  			fmt.Println(result.Err.Error())
    50  		}
    51  	}
    52  
    53  	if channel == nil {
    54  		if result := <-a.Srv.Store.Channel().Get(channelPart, true); result.Err == nil {
    55  			channel = result.Data.(*model.Channel)
    56  		}
    57  	}
    58  
    59  	return channel
    60  }