github.com/ashishbhate/mattermost-server@v5.11.1+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_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  	tempChannelId := o.ActiveChannel
    32  	o.ActiveChannel = ""
    33  	b, _ := json.Marshal(o)
    34  	o.ActiveChannel = tempChannelId
    35  	return string(b)
    36  }
    37  
    38  func (o *Status) ToClusterJson() string {
    39  	b, _ := json.Marshal(o)
    40  	return string(b)
    41  }
    42  
    43  func StatusFromJson(data io.Reader) *Status {
    44  	var o *Status
    45  	json.NewDecoder(data).Decode(&o)
    46  	return o
    47  }
    48  
    49  func StatusListToJson(u []*Status) string {
    50  	activeChannels := make([]string, len(u))
    51  	for index, s := range u {
    52  		activeChannels[index] = s.ActiveChannel
    53  		s.ActiveChannel = ""
    54  	}
    55  
    56  	b, _ := json.Marshal(u)
    57  
    58  	for index, s := range u {
    59  		s.ActiveChannel = activeChannels[index]
    60  	}
    61  
    62  	return string(b)
    63  }
    64  
    65  func StatusListFromJson(data io.Reader) []*Status {
    66  	var statuses []*Status
    67  	json.NewDecoder(data).Decode(&statuses)
    68  	return statuses
    69  }
    70  
    71  func StatusMapToInterfaceMap(statusMap map[string]*Status) map[string]interface{} {
    72  	interfaceMap := map[string]interface{}{}
    73  	for _, s := range statusMap {
    74  		// Omitted statues mean offline
    75  		if s.Status != STATUS_OFFLINE {
    76  			interfaceMap[s.UserId] = s.Status
    77  		}
    78  	}
    79  	return interfaceMap
    80  }