github.com/levb/mattermost-server@v5.3.1+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  
    34  type Channel struct {
    35  	Id            string                 `json:"id"`
    36  	CreateAt      int64                  `json:"create_at"`
    37  	UpdateAt      int64                  `json:"update_at"`
    38  	DeleteAt      int64                  `json:"delete_at"`
    39  	TeamId        string                 `json:"team_id"`
    40  	Type          string                 `json:"type"`
    41  	DisplayName   string                 `json:"display_name"`
    42  	Name          string                 `json:"name"`
    43  	Header        string                 `json:"header"`
    44  	Purpose       string                 `json:"purpose"`
    45  	LastPostAt    int64                  `json:"last_post_at"`
    46  	TotalMsgCount int64                  `json:"total_msg_count"`
    47  	ExtraUpdateAt int64                  `json:"extra_update_at"`
    48  	CreatorId     string                 `json:"creator_id"`
    49  	SchemeId      *string                `json:"scheme_id"`
    50  	Props         map[string]interface{} `json:"props" db:"-"`
    51  }
    52  
    53  type ChannelPatch struct {
    54  	DisplayName *string `json:"display_name"`
    55  	Name        *string `json:"name"`
    56  	Header      *string `json:"header"`
    57  	Purpose     *string `json:"purpose"`
    58  }
    59  
    60  func (o *Channel) DeepCopy() *Channel {
    61  	copy := *o
    62  	if copy.SchemeId != nil {
    63  		copy.SchemeId = NewString(*o.SchemeId)
    64  	}
    65  	return &copy
    66  }
    67  
    68  func (o *Channel) ToJson() string {
    69  	b, _ := json.Marshal(o)
    70  	return string(b)
    71  }
    72  
    73  func (o *ChannelPatch) ToJson() string {
    74  	b, _ := json.Marshal(o)
    75  	return string(b)
    76  }
    77  
    78  func ChannelFromJson(data io.Reader) *Channel {
    79  	var o *Channel
    80  	json.NewDecoder(data).Decode(&o)
    81  	return o
    82  }
    83  
    84  func ChannelPatchFromJson(data io.Reader) *ChannelPatch {
    85  	var o *ChannelPatch
    86  	json.NewDecoder(data).Decode(&o)
    87  	return o
    88  }
    89  
    90  func (o *Channel) Etag() string {
    91  	return Etag(o.Id, o.UpdateAt)
    92  }
    93  
    94  func (o *Channel) IsValid() *AppError {
    95  	if len(o.Id) != 26 {
    96  		return NewAppError("Channel.IsValid", "model.channel.is_valid.id.app_error", nil, "", http.StatusBadRequest)
    97  	}
    98  
    99  	if o.CreateAt == 0 {
   100  		return NewAppError("Channel.IsValid", "model.channel.is_valid.create_at.app_error", nil, "id="+o.Id, http.StatusBadRequest)
   101  	}
   102  
   103  	if o.UpdateAt == 0 {
   104  		return NewAppError("Channel.IsValid", "model.channel.is_valid.update_at.app_error", nil, "id="+o.Id, http.StatusBadRequest)
   105  	}
   106  
   107  	if utf8.RuneCountInString(o.DisplayName) > CHANNEL_DISPLAY_NAME_MAX_RUNES {
   108  		return NewAppError("Channel.IsValid", "model.channel.is_valid.display_name.app_error", nil, "id="+o.Id, http.StatusBadRequest)
   109  	}
   110  
   111  	if !IsValidChannelIdentifier(o.Name) {
   112  		return NewAppError("Channel.IsValid", "model.channel.is_valid.2_or_more.app_error", nil, "id="+o.Id, http.StatusBadRequest)
   113  	}
   114  
   115  	if !(o.Type == CHANNEL_OPEN || o.Type == CHANNEL_PRIVATE || o.Type == CHANNEL_DIRECT || o.Type == CHANNEL_GROUP) {
   116  		return NewAppError("Channel.IsValid", "model.channel.is_valid.type.app_error", nil, "id="+o.Id, http.StatusBadRequest)
   117  	}
   118  
   119  	if utf8.RuneCountInString(o.Header) > CHANNEL_HEADER_MAX_RUNES {
   120  		return NewAppError("Channel.IsValid", "model.channel.is_valid.header.app_error", nil, "id="+o.Id, http.StatusBadRequest)
   121  	}
   122  
   123  	if utf8.RuneCountInString(o.Purpose) > CHANNEL_PURPOSE_MAX_RUNES {
   124  		return NewAppError("Channel.IsValid", "model.channel.is_valid.purpose.app_error", nil, "id="+o.Id, http.StatusBadRequest)
   125  	}
   126  
   127  	if len(o.CreatorId) > 26 {
   128  		return NewAppError("Channel.IsValid", "model.channel.is_valid.creator_id.app_error", nil, "", http.StatusBadRequest)
   129  	}
   130  
   131  	return nil
   132  }
   133  
   134  func (o *Channel) PreSave() {
   135  	if o.Id == "" {
   136  		o.Id = NewId()
   137  	}
   138  
   139  	o.CreateAt = GetMillis()
   140  	o.UpdateAt = o.CreateAt
   141  	o.ExtraUpdateAt = 0
   142  }
   143  
   144  func (o *Channel) PreUpdate() {
   145  	o.UpdateAt = GetMillis()
   146  }
   147  
   148  func (o *Channel) IsGroupOrDirect() bool {
   149  	return o.Type == CHANNEL_DIRECT || o.Type == CHANNEL_GROUP
   150  }
   151  
   152  func (o *Channel) Patch(patch *ChannelPatch) {
   153  	if patch.DisplayName != nil {
   154  		o.DisplayName = *patch.DisplayName
   155  	}
   156  
   157  	if patch.Name != nil {
   158  		o.Name = *patch.Name
   159  	}
   160  
   161  	if patch.Header != nil {
   162  		o.Header = *patch.Header
   163  	}
   164  
   165  	if patch.Purpose != nil {
   166  		o.Purpose = *patch.Purpose
   167  	}
   168  }
   169  
   170  func (o *Channel) MakeNonNil() {
   171  	if o.Props == nil {
   172  		o.Props = make(map[string]interface{})
   173  	}
   174  }
   175  
   176  func (o *Channel) AddProp(key string, value interface{}) {
   177  	o.MakeNonNil()
   178  
   179  	o.Props[key] = value
   180  }
   181  
   182  func GetDMNameFromIds(userId1, userId2 string) string {
   183  	if userId1 > userId2 {
   184  		return userId2 + "__" + userId1
   185  	} else {
   186  		return userId1 + "__" + userId2
   187  	}
   188  }
   189  
   190  func GetGroupDisplayNameFromUsers(users []*User, truncate bool) string {
   191  	usernames := make([]string, len(users))
   192  	for index, user := range users {
   193  		usernames[index] = user.Username
   194  	}
   195  
   196  	sort.Strings(usernames)
   197  
   198  	name := strings.Join(usernames, ", ")
   199  
   200  	if truncate && len(name) > CHANNEL_NAME_MAX_LENGTH {
   201  		name = name[:CHANNEL_NAME_MAX_LENGTH]
   202  	}
   203  
   204  	return name
   205  }
   206  
   207  func GetGroupNameFromUserIds(userIds []string) string {
   208  	sort.Strings(userIds)
   209  
   210  	h := sha1.New()
   211  	for _, id := range userIds {
   212  		io.WriteString(h, id)
   213  	}
   214  
   215  	return hex.EncodeToString(h.Sum(nil))
   216  }