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