summaryrefslogtreecommitdiffstats
path: root/model/session.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/session.go
downloadchat-56e74239d6b34df8f30ef046f0b0ff4ff0866a71.tar.gz
chat-56e74239d6b34df8f30ef046f0b0ff4ff0866a71.tar.bz2
chat-56e74239d6b34df8f30ef046f0b0ff4ff0866a71.zip
first commit
Diffstat (limited to 'model/session.go')
-rw-r--r--model/session.go119
1 files changed, 119 insertions, 0 deletions
diff --git a/model/session.go b/model/session.go
new file mode 100644
index 000000000..9fd3b9ec3
--- /dev/null
+++ b/model/session.go
@@ -0,0 +1,119 @@
+// Copyright (c) 2015 Spinpunch, Inc. All Rights Reserved.
+// See License.txt for license information.
+
+package model
+
+import (
+ "encoding/json"
+ "io"
+)
+
+const (
+ SESSION_TOKEN = "MMSID"
+ SESSION_TIME_WEB_IN_DAYS = 365
+ SESSION_TIME_WEB_IN_SECS = 60 * 60 * 24 * SESSION_TIME_WEB_IN_DAYS
+ SESSION_TIME_MOBILE_IN_DAYS = 365
+ SESSION_TIME_MOBILE_IN_SECS = 60 * 60 * 24 * SESSION_TIME_MOBILE_IN_DAYS
+ SESSION_CACHE_IN_SECS = 60 * 10
+ SESSION_CACHE_SIZE = 10000
+ SESSION_PROP_PLATFORM = "platform"
+ SESSION_PROP_OS = "os"
+ SESSION_PROP_BROWSER = "browser"
+)
+
+type Session struct {
+ Id string `json:"id"`
+ AltId string `json:"alt_id"`
+ CreateAt int64 `json:"create_at"`
+ ExpiresAt int64 `json:"expires_at"`
+ LastActivityAt int64 `json:"last_activity_at"`
+ UserId string `json:"user_id"`
+ TeamId string `json:"team_id"`
+ DeviceId string `json:"device_id"`
+ Roles string `json:"roles"`
+ Props StringMap `json:"props"`
+}
+
+func (me *Session) ToJson() string {
+ b, err := json.Marshal(me)
+ if err != nil {
+ return ""
+ } else {
+ return string(b)
+ }
+}
+
+func SessionFromJson(data io.Reader) *Session {
+ decoder := json.NewDecoder(data)
+ var me Session
+ err := decoder.Decode(&me)
+ if err == nil {
+ return &me
+ } else {
+ return nil
+ }
+}
+
+func (me *Session) PreSave() {
+ if me.Id == "" {
+ me.Id = NewId()
+ }
+
+ me.AltId = NewId()
+
+ me.CreateAt = GetMillis()
+ me.LastActivityAt = me.CreateAt
+
+ if me.Props == nil {
+ me.Props = make(map[string]string)
+ }
+}
+
+func (me *Session) Sanitize() {
+ me.Id = ""
+}
+
+func (me *Session) IsExpired() bool {
+
+ if me.ExpiresAt <= 0 {
+ return false
+ }
+
+ if GetMillis() > me.ExpiresAt {
+ return true
+ }
+
+ return false
+}
+
+func (me *Session) SetExpireInDays(days int64) {
+ me.ExpiresAt = GetMillis() + (1000 * 60 * 60 * 24 * days)
+}
+
+func (me *Session) AddProp(key string, value string) {
+
+ if me.Props == nil {
+ me.Props = make(map[string]string)
+ }
+
+ me.Props[key] = value
+}
+
+func SessionsToJson(o []*Session) string {
+ if b, err := json.Marshal(o); err != nil {
+ return "[]"
+ } else {
+ return string(b)
+ }
+}
+
+func SessionsFromJson(data io.Reader) []*Session {
+ decoder := json.NewDecoder(data)
+ var o []*Session
+ err := decoder.Decode(&o)
+ if err == nil {
+ return o
+ } else {
+ return nil
+ }
+}