github.com/rajatvaryani/mattermost-server@v5.11.1+incompatible/model/channel_count.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  	"crypto/md5"
     8  	"encoding/json"
     9  	"fmt"
    10  	"io"
    11  	"sort"
    12  	"strconv"
    13  )
    14  
    15  type ChannelCounts struct {
    16  	Counts      map[string]int64 `json:"counts"`
    17  	UpdateTimes map[string]int64 `json:"update_times"`
    18  }
    19  
    20  func (o *ChannelCounts) Etag() string {
    21  
    22  	ids := []string{}
    23  	for id := range o.Counts {
    24  		ids = append(ids, id)
    25  	}
    26  	sort.Strings(ids)
    27  
    28  	str := ""
    29  	for _, id := range ids {
    30  		str += id + strconv.FormatInt(o.Counts[id], 10)
    31  	}
    32  
    33  	md5Counts := fmt.Sprintf("%x", md5.Sum([]byte(str)))
    34  
    35  	var update int64 = 0
    36  	for _, u := range o.UpdateTimes {
    37  		if u > update {
    38  			update = u
    39  		}
    40  	}
    41  
    42  	return Etag(md5Counts, update)
    43  }
    44  
    45  func (o *ChannelCounts) ToJson() string {
    46  	b, _ := json.Marshal(o)
    47  	return string(b)
    48  }
    49  
    50  func ChannelCountsFromJson(data io.Reader) *ChannelCounts {
    51  	var o *ChannelCounts
    52  	json.NewDecoder(data).Decode(&o)
    53  	return o
    54  }