summaryrefslogtreecommitdiffstats
path: root/model/channel.go
blob: 7109500d4d880188a327bfb6a3f6e2b5dd1b098a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
// Copyright (c) 2015 Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.

package model

import (
	"encoding/json"
	"io"
	"unicode/utf8"
)

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"`
	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"`
}

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.UpdateAt)
}

func (o *Channel) ExtraEtag(memberLimit int) string {
	return Etag(o.Id, o.ExtraUpdateAt, memberLimit)
}

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 utf8.RuneCountInString(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 utf8.RuneCountInString(o.Header) > 1024 {
		return NewAppError("Channel.IsValid", "Invalid header", "id="+o.Id)
	}

	if utf8.RuneCountInString(o.Purpose) > 128 {
		return NewAppError("Channel.IsValid", "Invalid purpose", "id="+o.Id)
	}

	if len(o.CreatorId) > 26 {
		return NewAppError("Channel.IsValid", "Invalid creator id", "")
	}

	return nil
}

func (o *Channel) PreSave() {
	if o.Id == "" {
		o.Id = NewId()
	}

	o.CreateAt = GetMillis()
	o.UpdateAt = o.CreateAt
	o.ExtraUpdateAt = o.CreateAt
}

func (o *Channel) PreUpdate() {
	o.UpdateAt = GetMillis()
}

func (o *Channel) ExtraUpdated() {
	o.ExtraUpdateAt = GetMillis()
}

func (o *Channel) PreExport() {
}

func GetDMNameFromIds(userId1, userId2 string) string {
	if userId1 > userId2 {
		return userId2 + "__" + userId1
	} else {
		return userId1 + "__" + userId2
	}
}