matterbridge/vendor/github.com/mattermost/mattermost-server/model/channel.go

223 lines
5.7 KiB
Go
Raw Normal View History

2017-08-16 21:37:37 +00:00
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
2016-04-10 21:39:38 +00:00
// See License.txt for license information.
package model
import (
2017-03-25 20:04:10 +00:00
"crypto/sha1"
"encoding/hex"
2016-04-10 21:39:38 +00:00
"encoding/json"
"io"
2018-02-08 23:11:04 +00:00
"net/http"
2017-03-25 20:04:10 +00:00
"sort"
"strings"
2016-04-10 21:39:38 +00:00
"unicode/utf8"
)
const (
2016-11-12 21:00:53 +00:00
CHANNEL_OPEN = "O"
CHANNEL_PRIVATE = "P"
CHANNEL_DIRECT = "D"
2017-03-25 20:04:10 +00:00
CHANNEL_GROUP = "G"
CHANNEL_GROUP_MAX_USERS = 8
CHANNEL_GROUP_MIN_USERS = 3
2016-11-12 21:00:53 +00:00
DEFAULT_CHANNEL = "town-square"
CHANNEL_DISPLAY_NAME_MAX_RUNES = 64
2017-03-25 20:04:10 +00:00
CHANNEL_NAME_MIN_LENGTH = 2
2016-11-12 21:00:53 +00:00
CHANNEL_NAME_MAX_LENGTH = 64
2018-02-08 23:11:04 +00:00
CHANNEL_NAME_UI_MAX_LENGTH = 22
2016-11-12 21:00:53 +00:00
CHANNEL_HEADER_MAX_RUNES = 1024
CHANNEL_PURPOSE_MAX_RUNES = 250
2017-03-25 20:04:10 +00:00
CHANNEL_CACHE_SIZE = 25000
2016-04-10 21:39:38 +00:00
)
type Channel struct {
Id string `json:"id"`
CreateAt int64 `json:"create_at"`
UpdateAt int64 `json:"update_at"`
DeleteAt int64 `json:"delete_at"`
TeamId string `json:"team_id"`
Type string `json:"type"`
DisplayName string `json:"display_name"`
Name string `json:"name"`
Header string `json:"header"`
Purpose string `json:"purpose"`
LastPostAt int64 `json:"last_post_at"`
TotalMsgCount int64 `json:"total_msg_count"`
ExtraUpdateAt int64 `json:"extra_update_at"`
CreatorId string `json:"creator_id"`
SchemeId *string `json:"scheme_id"`
Props map[string]interface{} `json:"props" db:"-"`
2016-04-10 21:39:38 +00:00
}
2017-08-16 21:37:37 +00:00
type ChannelPatch struct {
DisplayName *string `json:"display_name"`
Name *string `json:"name"`
Header *string `json:"header"`
Purpose *string `json:"purpose"`
}
type ChannelForExport struct {
Channel
TeamName string
SchemeName *string
}
2018-02-08 23:11:04 +00:00
func (o *Channel) DeepCopy() *Channel {
copy := *o
if copy.SchemeId != nil {
copy.SchemeId = NewString(*o.SchemeId)
}
2018-02-08 23:11:04 +00:00
return &copy
}
2016-04-10 21:39:38 +00:00
func (o *Channel) ToJson() string {
b, _ := json.Marshal(o)
return string(b)
2016-04-10 21:39:38 +00:00
}
2017-08-16 21:37:37 +00:00
func (o *ChannelPatch) ToJson() string {
b, _ := json.Marshal(o)
return string(b)
2017-08-16 21:37:37 +00:00
}
2016-04-10 21:39:38 +00:00
func ChannelFromJson(data io.Reader) *Channel {
var o *Channel
json.NewDecoder(data).Decode(&o)
return o
2016-04-10 21:39:38 +00:00
}
2017-08-16 21:37:37 +00:00
func ChannelPatchFromJson(data io.Reader) *ChannelPatch {
var o *ChannelPatch
json.NewDecoder(data).Decode(&o)
return o
2017-08-16 21:37:37 +00:00
}
2016-04-10 21:39:38 +00:00
func (o *Channel) Etag() string {
return Etag(o.Id, o.UpdateAt)
}
func (o *Channel) IsValid() *AppError {
if len(o.Id) != 26 {
2018-02-08 23:11:04 +00:00
return NewAppError("Channel.IsValid", "model.channel.is_valid.id.app_error", nil, "", http.StatusBadRequest)
2016-04-10 21:39:38 +00:00
}
if o.CreateAt == 0 {
2018-02-08 23:11:04 +00:00
return NewAppError("Channel.IsValid", "model.channel.is_valid.create_at.app_error", nil, "id="+o.Id, http.StatusBadRequest)
2016-04-10 21:39:38 +00:00
}
if o.UpdateAt == 0 {
2018-02-08 23:11:04 +00:00
return NewAppError("Channel.IsValid", "model.channel.is_valid.update_at.app_error", nil, "id="+o.Id, http.StatusBadRequest)
2016-04-10 21:39:38 +00:00
}
2016-11-12 21:00:53 +00:00
if utf8.RuneCountInString(o.DisplayName) > CHANNEL_DISPLAY_NAME_MAX_RUNES {
2018-02-08 23:11:04 +00:00
return NewAppError("Channel.IsValid", "model.channel.is_valid.display_name.app_error", nil, "id="+o.Id, http.StatusBadRequest)
2016-04-10 21:39:38 +00:00
}
if !IsValidChannelIdentifier(o.Name) {
2018-02-08 23:11:04 +00:00
return NewAppError("Channel.IsValid", "model.channel.is_valid.2_or_more.app_error", nil, "id="+o.Id, http.StatusBadRequest)
2016-04-10 21:39:38 +00:00
}
2017-03-25 20:04:10 +00:00
if !(o.Type == CHANNEL_OPEN || o.Type == CHANNEL_PRIVATE || o.Type == CHANNEL_DIRECT || o.Type == CHANNEL_GROUP) {
2018-02-08 23:11:04 +00:00
return NewAppError("Channel.IsValid", "model.channel.is_valid.type.app_error", nil, "id="+o.Id, http.StatusBadRequest)
2016-04-10 21:39:38 +00:00
}
2016-11-12 21:00:53 +00:00
if utf8.RuneCountInString(o.Header) > CHANNEL_HEADER_MAX_RUNES {
2018-02-08 23:11:04 +00:00
return NewAppError("Channel.IsValid", "model.channel.is_valid.header.app_error", nil, "id="+o.Id, http.StatusBadRequest)
2016-04-10 21:39:38 +00:00
}
2016-11-12 21:00:53 +00:00
if utf8.RuneCountInString(o.Purpose) > CHANNEL_PURPOSE_MAX_RUNES {
2018-02-08 23:11:04 +00:00
return NewAppError("Channel.IsValid", "model.channel.is_valid.purpose.app_error", nil, "id="+o.Id, http.StatusBadRequest)
2016-04-10 21:39:38 +00:00
}
if len(o.CreatorId) > 26 {
2018-02-08 23:11:04 +00:00
return NewAppError("Channel.IsValid", "model.channel.is_valid.creator_id.app_error", nil, "", http.StatusBadRequest)
2016-04-10 21:39:38 +00:00
}
return nil
}
func (o *Channel) PreSave() {
if o.Id == "" {
o.Id = NewId()
}
o.CreateAt = GetMillis()
o.UpdateAt = o.CreateAt
o.ExtraUpdateAt = 0
2016-04-10 21:39:38 +00:00
}
func (o *Channel) PreUpdate() {
o.UpdateAt = GetMillis()
}
2017-03-25 20:04:10 +00:00
func (o *Channel) IsGroupOrDirect() bool {
return o.Type == CHANNEL_DIRECT || o.Type == CHANNEL_GROUP
}
2017-08-16 21:37:37 +00:00
func (o *Channel) Patch(patch *ChannelPatch) {
if patch.DisplayName != nil {
o.DisplayName = *patch.DisplayName
}
if patch.Name != nil {
o.Name = *patch.Name
}
if patch.Header != nil {
o.Header = *patch.Header
}
if patch.Purpose != nil {
o.Purpose = *patch.Purpose
}
}
func (o *Channel) MakeNonNil() {
if o.Props == nil {
o.Props = make(map[string]interface{})
}
}
func (o *Channel) AddProp(key string, value interface{}) {
o.MakeNonNil()
o.Props[key] = value
}
2016-04-10 21:39:38 +00:00
func GetDMNameFromIds(userId1, userId2 string) string {
if userId1 > userId2 {
return userId2 + "__" + userId1
} else {
return userId1 + "__" + userId2
}
}
2017-03-25 20:04:10 +00:00
func GetGroupDisplayNameFromUsers(users []*User, truncate bool) string {
usernames := make([]string, len(users))
for index, user := range users {
usernames[index] = user.Username
}
sort.Strings(usernames)
name := strings.Join(usernames, ", ")
if truncate && len(name) > CHANNEL_NAME_MAX_LENGTH {
name = name[:CHANNEL_NAME_MAX_LENGTH]
}
return name
}
func GetGroupNameFromUserIds(userIds []string) string {
sort.Strings(userIds)
h := sha1.New()
for _, id := range userIds {
io.WriteString(h, id)
}
return hex.EncodeToString(h.Sum(nil))
}