github.com/psyb0t/mattermost-server@v4.6.1-0.20180125161845-5503a1351abf+incompatible/model/status.go (about) 1 // Copyright (c) 2016-present Mattermost, Inc. All Rights Reserved. 2 // See License.txt for license information. 3 4 package model 5 6 import ( 7 "encoding/json" 8 "io" 9 ) 10 11 const ( 12 STATUS_OFFLINE = "offline" 13 STATUS_AWAY = "away" 14 STATUS_DND = "dnd" 15 STATUS_ONLINE = "online" 16 STATUS_CACHE_SIZE = SESSION_CACHE_SIZE 17 STATUS_CHANNEL_TIMEOUT = 20000 // 20 seconds 18 STATUS_MIN_UPDATE_TIME = 120000 // 2 minutes 19 ) 20 21 type Status struct { 22 UserId string `json:"user_id"` 23 Status string `json:"status"` 24 Manual bool `json:"manual"` 25 LastActivityAt int64 `json:"last_activity_at"` 26 ActiveChannel string `json:"-" db:"-"` 27 } 28 29 func (o *Status) ToJson() string { 30 b, err := json.Marshal(o) 31 if err != nil { 32 return "" 33 } else { 34 return string(b) 35 } 36 } 37 38 func StatusFromJson(data io.Reader) *Status { 39 decoder := json.NewDecoder(data) 40 var o Status 41 err := decoder.Decode(&o) 42 if err == nil { 43 return &o 44 } else { 45 return nil 46 } 47 } 48 49 func StatusListToJson(u []*Status) string { 50 b, err := json.Marshal(u) 51 if err != nil { 52 return "" 53 } else { 54 return string(b) 55 } 56 } 57 58 func StatusListFromJson(data io.Reader) []*Status { 59 decoder := json.NewDecoder(data) 60 var statuses []*Status 61 err := decoder.Decode(&statuses) 62 if err == nil { 63 return statuses 64 } else { 65 return nil 66 } 67 } 68 69 func StatusMapToInterfaceMap(statusMap map[string]*Status) map[string]interface{} { 70 interfaceMap := map[string]interface{}{} 71 for _, s := range statusMap { 72 // Omitted statues mean offline 73 if s.Status != STATUS_OFFLINE { 74 interfaceMap[s.UserId] = s.Status 75 } 76 } 77 return interfaceMap 78 }