summaryrefslogtreecommitdiffstats
path: root/model/channel.go
diff options
context:
space:
mode:
author=Corey Hulen <corey@hulen.com>2015-06-14 23:53:32 -0800
committer=Corey Hulen <corey@hulen.com>2015-06-14 23:53:32 -0800
commit56e74239d6b34df8f30ef046f0b0ff4ff0866a71 (patch)
tree044da29848cf0f5c8607eac34de69065171669cf /model/channel.go
downloadchat-56e74239d6b34df8f30ef046f0b0ff4ff0866a71.tar.gz
chat-56e74239d6b34df8f30ef046f0b0ff4ff0866a71.tar.bz2
chat-56e74239d6b34df8f30ef046f0b0ff4ff0866a71.zip
first commit
Diffstat (limited to 'model/channel.go')
-rw-r--r--model/channel.go104
1 files changed, 104 insertions, 0 deletions
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()
+}