github.com/jfrerich/mattermost-server@v5.8.0-rc2+incompatible/model/channel.go (about)

     1  // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
     2  // See License.txt for license information.
     3  
     4  package model
     5  
     6  import (
     7  	"crypto/sha1"
     8  	"encoding/hex"
     9  	"encoding/json"
    10  	"io"
    11  	"net/http"
    12  	"sort"
    13  	"strings"
    14  	"unicode/utf8"
    15  )
    16  
    17  const (
    18  	CHANNEL_OPEN                   = "O"
    19  	CHANNEL_PRIVATE                = "P"
    20  	CHANNEL_DIRECT                 = "D"
    21  	CHANNEL_GROUP                  = "G"
    22  	CHANNEL_GROUP_MAX_USERS        = 8
    23  	CHANNEL_GROUP_MIN_USERS        = 3
    24  	DEFAULT_CHANNEL                = "town-square"
    25  	CHANNEL_DISPLAY_NAME_MAX_RUNES = 64
    26  	CHANNEL_NAME_MIN_LENGTH        = 2
    27  	CHANNEL_NAME_MAX_LENGTH        = 64
    28  	CHANNEL_NAME_UI_MAX_LENGTH     = 22
    29  	CHANNEL_HEADER_MAX_RUNES       = 1024
    30  	CHANNEL_PURPOSE_MAX_RUNES      = 250
    31  	CHANNEL_CACHE_SIZE             = 25000
    32  
    33  	CHANNEL_SORT_BY_USERNAME = "username"
    34  	CHANNEL_SORT_BY_STATUS   = "status"
    35  )
    36  
    37  type Channel struct {
    38  	Id            string                 `json:"id"`
    39  	CreateAt      int64                  `json:"create_at"`
    40  	UpdateAt      int64                  `json:"update_at"`
    41  	DeleteAt      int64                  `json:"delete_at"`
    42  	TeamId        string                 `json:"team_id"`
    43  	Type          string                 `json:"type"`
    44  	DisplayName   string                 `json:"display_name"`
    45  	Name          string                 `json:"name"`
    46  	Header        string                 `json:"header"`
    47  	Purpose       string                 `json:"purpose"`
    48  	LastPostAt    int64                  `json:"last_post_at"`
    49  	TotalMsgCount int64                  `json:"total_msg_count"`
    50  	ExtraUpdateAt int64                  `json:"extra_update_at"`
    51  	CreatorId     string                 `json:"creator_id"`
    52  	SchemeId      *string                `json:"scheme_id"`
    53  	Props         map[string]interface{} `json:"props" db:"-"`
    54  }
    55  
    56  type ChannelWithTeamData struct {
    57  	Channel
    58  	TeamDisplayName string `json:"team_display_name"`
    59  	TeamName        string `json:"team_name"`
    60  	TeamUpdateAt    int64  `json:"team_update_at"`
    61  }
    62  
    63  type ChannelPatch struct {
    64  	DisplayName *string `json:"display_name"`
    65  	Name        *string `json:"name"`
    66  	Header      *string `json:"header"`
    67  	Purpose     *string `json:"purpose"`
    68  }
    69  
    70  type ChannelForExport struct {
    71  	Channel
    72  	TeamName   string
    73  	SchemeName *string
    74  }
    75  
    76  func (o *Channel) DeepCopy() *Channel {
    77  	copy := *o
    78  	if copy.SchemeId != nil {
    79  		copy.SchemeId = NewString(*o.SchemeId)
    80  	}
    81  	return &copy
    82  }
    83  
    84  func (o *Channel) ToJson() string {
    85  	b, _ := json.Marshal(o)
    86  	return string(b)
    87  }
    88  
    89  func (o *ChannelPatch) ToJson() string {
    90  	b, _ := json.Marshal(o)
    91  	return string(b)
    92  }
    93  
    94  func ChannelFromJson(data io.Reader) *Channel {
    95  	var o *Channel
    96  	json.NewDecoder(data).Decode(&o)
    97  	return o
    98  }
    99  
   100  func ChannelPatchFromJson(data io.Reader) *ChannelPatch {
   101  	var o *ChannelPatch
   102  	json.NewDecoder(data).Decode(&o)
   103  	return o
   104  }
   105  
   106  func (o *Channel) Etag() string {
   107  	return Etag(o.Id, o.UpdateAt)
   108  }
   109  
   110  func (o *Channel) IsValid() *AppError {
   111  	if len(o.Id) != 26 {
   112  		return NewAppError("Channel.IsValid", "model.channel.is_valid.id.app_error", nil, "", http.StatusBadRequest)
   113  	}
   114  
   115  	if o.CreateAt == 0 {
   116  		return NewAppError("Channel.IsValid", "model.channel.is_valid.create_at.app_error", nil, "id="+o.Id, http.StatusBadRequest)
   117  	}
   118  
   119  	if o.UpdateAt == 0 {
   120  		return NewAppError("Channel.IsValid", "model.channel.is_valid.update_at.app_error", nil, "id="+o.Id, http.StatusBadRequest)
   121  	}
   122  
   123  	if utf8.RuneCountInString(o.DisplayName) > CHANNEL_DISPLAY_NAME_MAX_RUNES {
   124  		return NewAppError("Channel.IsValid", "model.channel.is_valid.display_name.app_error", nil, "id="+o.Id, http.StatusBadRequest)
   125  	}
   126  
   127  	if !IsValidChannelIdentifier(o.Name) {
   128  		return NewAppError("Channel.IsValid", "model.channel.is_valid.2_or_more.app_error", nil, "id="+o.Id, http.StatusBadRequest)
   129  	}
   130  
   131  	if !(o.Type == CHANNEL_OPEN || o.Type == CHANNEL_PRIVATE || o.Type == CHANNEL_DIRECT || o.Type == CHANNEL_GROUP) {
   132  		return NewAppError("Channel.IsValid", "model.channel.is_valid.type.app_error", nil, "id="+o.Id, http.StatusBadRequest)
   133  	}
   134  
   135  	if utf8.RuneCountInString(o.Header) > CHANNEL_HEADER_MAX_RUNES {
   136  		return NewAppError("Channel.IsValid", "model.channel.is_valid.header.app_error", nil, "id="+o.Id, http.StatusBadRequest)
   137  	}
   138  
   139  	if utf8.RuneCountInString(o.Purpose) > CHANNEL_PURPOSE_MAX_RUNES {
   140  		return NewAppError("Channel.IsValid", "model.channel.is_valid.purpose.app_error", nil, "id="+o.Id, http.StatusBadRequest)
   141  	}
   142  
   143  	if len(o.CreatorId) > 26 {
   144  		return NewAppError("Channel.IsValid", "model.channel.is_valid.creator_id.app_error", nil, "", http.StatusBadRequest)
   145  	}
   146  
   147  	return nil
   148  }
   149  
   150  func (o *Channel) PreSave() {
   151  	if o.Id == "" {
   152  		o.Id = NewId()
   153  	}
   154  
   155  	o.CreateAt = GetMillis()
   156  	o.UpdateAt = o.CreateAt
   157  	o.ExtraUpdateAt = 0
   158  }
   159  
   160  func (o *Channel) PreUpdate() {
   161  	o.UpdateAt = GetMillis()
   162  }
   163  
   164  func (o *Channel) IsGroupOrDirect() bool {
   165  	return o.Type == CHANNEL_DIRECT || o.Type == CHANNEL_GROUP
   166  }
   167  
   168  func (o *Channel) Patch(patch *ChannelPatch) {
   169  	if patch.DisplayName != nil {
   170  		o.DisplayName = *patch.DisplayName
   171  	}
   172  
   173  	if patch.Name != nil {
   174  		o.Name = *patch.Name
   175  	}
   176  
   177  	if patch.Header != nil {
   178  		o.Header = *patch.Header
   179  	}
   180  
   181  	if patch.Purpose != nil {
   182  		o.Purpose = *patch.Purpose
   183  	}
   184  }
   185  
   186  func (o *Channel) MakeNonNil() {
   187  	if o.Props == nil {
   188  		o.Props = make(map[string]interface{})
   189  	}
   190  }
   191  
   192  func (o *Channel) AddProp(key string, value interface{}) {
   193  	o.MakeNonNil()
   194  
   195  	o.Props[key] = value
   196  }
   197  
   198  func GetDMNameFromIds(userId1, userId2 string) string {
   199  	if userId1 > userId2 {
   200  		return userId2 + "__" + userId1
   201  	} else {
   202  		return userId1 + "__" + userId2
   203  	}
   204  }
   205  
   206  func GetGroupDisplayNameFromUsers(users []*User, truncate bool) string {
   207  	usernames := make([]string, len(users))
   208  	for index, user := range users {
   209  		usernames[index] = user.Username
   210  	}
   211  
   212  	sort.Strings(usernames)
   213  
   214  	name := strings.Join(usernames, ", ")
   215  
   216  	if truncate && len(name) > CHANNEL_NAME_MAX_LENGTH {
   217  		name = name[:CHANNEL_NAME_MAX_LENGTH]
   218  	}
   219  
   220  	return name
   221  }
   222  
   223  func GetGroupNameFromUserIds(userIds []string) string {
   224  	sort.Strings(userIds)
   225  
   226  	h := sha1.New()
   227  	for _, id := range userIds {
   228  		io.WriteString(h, id)
   229  	}
   230  
   231  	return hex.EncodeToString(h.Sum(nil))
   232  }