github.com/mattermosttest/mattermost-server/v5@v5.0.0-20200917143240-9dfa12e121f9/model/status.go (about) 1 // Copyright (c) 2015-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_OUT_OF_OFFICE = "ooo" 13 STATUS_OFFLINE = "offline" 14 STATUS_AWAY = "away" 15 STATUS_DND = "dnd" 16 STATUS_ONLINE = "online" 17 STATUS_CACHE_SIZE = SESSION_CACHE_SIZE 18 STATUS_CHANNEL_TIMEOUT = 20000 // 20 seconds 19 STATUS_MIN_UPDATE_TIME = 120000 // 2 minutes 20 ) 21 22 type Status struct { 23 UserId string `json:"user_id"` 24 Status string `json:"status"` 25 Manual bool `json:"manual"` 26 LastActivityAt int64 `json:"last_activity_at"` 27 ActiveChannel string `json:"active_channel,omitempty" db:"-"` 28 } 29 30 func (o *Status) ToJson() string { 31 oCopy := *o 32 oCopy.ActiveChannel = "" 33 b, _ := json.Marshal(oCopy) 34 return string(b) 35 } 36 37 func (o *Status) ToClusterJson() string { 38 b, _ := json.Marshal(o) 39 return string(b) 40 } 41 42 func StatusFromJson(data io.Reader) *Status { 43 var o *Status 44 json.NewDecoder(data).Decode(&o) 45 return o 46 } 47 48 func StatusListToJson(u []*Status) string { 49 uCopy := make([]Status, len(u)) 50 for i, s := range u { 51 sCopy := *s 52 sCopy.ActiveChannel = "" 53 uCopy[i] = sCopy 54 } 55 56 b, _ := json.Marshal(uCopy) 57 return string(b) 58 } 59 60 func StatusListFromJson(data io.Reader) []*Status { 61 var statuses []*Status 62 json.NewDecoder(data).Decode(&statuses) 63 return statuses 64 } 65 66 func StatusMapToInterfaceMap(statusMap map[string]*Status) map[string]interface{} { 67 interfaceMap := map[string]interface{}{} 68 for _, s := range statusMap { 69 // Omitted statues mean offline 70 if s.Status != STATUS_OFFLINE { 71 interfaceMap[s.UserId] = s.Status 72 } 73 } 74 return interfaceMap 75 }