From 56e74239d6b34df8f30ef046f0b0ff4ff0866a71 Mon Sep 17 00:00:00 2001 From: =Corey Hulen Date: Sun, 14 Jun 2015 23:53:32 -0800 Subject: first commit --- model/channel.go | 104 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 104 insertions(+) create mode 100644 model/channel.go (limited to 'model/channel.go') diff --git a/model/channel.go b/model/channel.go new file mode 100644 index 000000000..ab36d46a2 --- /dev/null +++ b/model/channel.go @@ -0,0 +1,104 @@ +// Copyright (c) 2015 Spinpunch, Inc. All Rights Reserved. +// See License.txt for license information. + +package model + +import ( + "encoding/json" + "io" +) + +const ( + CHANNEL_OPEN = "O" + CHANNEL_PRIVATE = "P" + CHANNEL_DIRECT = "D" + DEFAULT_CHANNEL = "town-square" +) + +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"` + Description string `json:"description"` + LastPostAt int64 `json:"last_post_at"` + TotalMsgCount int64 `json:"total_msg_count"` +} + +func (o *Channel) ToJson() string { + b, err := json.Marshal(o) + if err != nil { + return "" + } else { + return string(b) + } +} + +func ChannelFromJson(data io.Reader) *Channel { + decoder := json.NewDecoder(data) + var o Channel + err := decoder.Decode(&o) + if err == nil { + return &o + } else { + return nil + } +} + +func (o *Channel) Etag() string { + return Etag(o.Id, o.LastPostAt) +} + +func (o *Channel) IsValid() *AppError { + + if len(o.Id) != 26 { + return NewAppError("Channel.IsValid", "Invalid Id", "") + } + + if o.CreateAt == 0 { + return NewAppError("Channel.IsValid", "Create at must be a valid time", "id="+o.Id) + } + + if o.UpdateAt == 0 { + return NewAppError("Channel.IsValid", "Update at must be a valid time", "id="+o.Id) + } + + if len(o.DisplayName) > 64 { + return NewAppError("Channel.IsValid", "Invalid display name", "id="+o.Id) + } + + if len(o.Name) > 64 { + return NewAppError("Channel.IsValid", "Invalid name", "id="+o.Id) + } + + if !IsValidChannelIdentifier(o.Name) { + return NewAppError("Channel.IsValid", "Name must be 2 or more lowercase alphanumeric characters", "id="+o.Id) + } + + if !(o.Type == CHANNEL_OPEN || o.Type == CHANNEL_PRIVATE || o.Type == CHANNEL_DIRECT) { + return NewAppError("Channel.IsValid", "Invalid type", "id="+o.Id) + } + + if len(o.Description) > 1024 { + return NewAppError("Channel.IsValid", "Invalid description", "id="+o.Id) + } + + return nil +} + +func (o *Channel) PreSave() { + if o.Id == "" { + o.Id = NewId() + } + + o.CreateAt = GetMillis() + o.UpdateAt = o.CreateAt +} + +func (o *Channel) PreUpdate() { + o.UpdateAt = GetMillis() +} -- cgit v1.2.3-1-g7c22