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