github.com/dschalla/mattermost-server@v4.8.1-rc1+incompatible/cmd/platform/channelargs.go (about)

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