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