github.com/rajatvaryani/mattermost-server@v5.11.1+incompatible/model/post_list.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  	"sort"
    10  )
    11  
    12  type PostList struct {
    13  	Order []string         `json:"order"`
    14  	Posts map[string]*Post `json:"posts"`
    15  }
    16  
    17  func NewPostList() *PostList {
    18  	return &PostList{
    19  		Order: make([]string, 0),
    20  		Posts: make(map[string]*Post),
    21  	}
    22  }
    23  
    24  func (o *PostList) ToSlice() []*Post {
    25  	var posts []*Post
    26  	for _, id := range o.Order {
    27  		posts = append(posts, o.Posts[id])
    28  	}
    29  	return posts
    30  }
    31  
    32  func (o *PostList) WithRewrittenImageURLs(f func(string) string) *PostList {
    33  	copy := *o
    34  	copy.Posts = make(map[string]*Post)
    35  	for id, post := range o.Posts {
    36  		copy.Posts[id] = post.WithRewrittenImageURLs(f)
    37  	}
    38  	return &copy
    39  }
    40  
    41  func (o *PostList) StripActionIntegrations() {
    42  	posts := o.Posts
    43  	o.Posts = make(map[string]*Post)
    44  	for id, post := range posts {
    45  		pcopy := *post
    46  		pcopy.StripActionIntegrations()
    47  		o.Posts[id] = &pcopy
    48  	}
    49  }
    50  
    51  func (o *PostList) ToJson() string {
    52  	copy := *o
    53  	copy.StripActionIntegrations()
    54  	b, err := json.Marshal(&copy)
    55  	if err != nil {
    56  		return ""
    57  	} else {
    58  		return string(b)
    59  	}
    60  }
    61  
    62  func (o *PostList) MakeNonNil() {
    63  	if o.Order == nil {
    64  		o.Order = make([]string, 0)
    65  	}
    66  
    67  	if o.Posts == nil {
    68  		o.Posts = make(map[string]*Post)
    69  	}
    70  
    71  	for _, v := range o.Posts {
    72  		v.MakeNonNil()
    73  	}
    74  }
    75  
    76  func (o *PostList) AddOrder(id string) {
    77  
    78  	if o.Order == nil {
    79  		o.Order = make([]string, 0, 128)
    80  	}
    81  
    82  	o.Order = append(o.Order, id)
    83  }
    84  
    85  func (o *PostList) AddPost(post *Post) {
    86  
    87  	if o.Posts == nil {
    88  		o.Posts = make(map[string]*Post)
    89  	}
    90  
    91  	o.Posts[post.Id] = post
    92  }
    93  
    94  func (o *PostList) Extend(other *PostList) {
    95  	for _, postId := range other.Order {
    96  		if _, ok := o.Posts[postId]; !ok {
    97  			o.AddPost(other.Posts[postId])
    98  			o.AddOrder(postId)
    99  		}
   100  	}
   101  }
   102  
   103  func (o *PostList) SortByCreateAt() {
   104  	sort.Slice(o.Order, func(i, j int) bool {
   105  		return o.Posts[o.Order[i]].CreateAt > o.Posts[o.Order[j]].CreateAt
   106  	})
   107  }
   108  
   109  func (o *PostList) Etag() string {
   110  
   111  	id := "0"
   112  	var t int64 = 0
   113  
   114  	for _, v := range o.Posts {
   115  		if v.UpdateAt > t {
   116  			t = v.UpdateAt
   117  			id = v.Id
   118  		} else if v.UpdateAt == t && v.Id > id {
   119  			t = v.UpdateAt
   120  			id = v.Id
   121  		}
   122  	}
   123  
   124  	orderId := ""
   125  	if len(o.Order) > 0 {
   126  		orderId = o.Order[0]
   127  	}
   128  
   129  	return Etag(orderId, id, t)
   130  }
   131  
   132  func (o *PostList) IsChannelId(channelId string) bool {
   133  	for _, v := range o.Posts {
   134  		if v.ChannelId != channelId {
   135  			return false
   136  		}
   137  	}
   138  
   139  	return true
   140  }
   141  
   142  func PostListFromJson(data io.Reader) *PostList {
   143  	var o *PostList
   144  	json.NewDecoder(data).Decode(&o)
   145  	return o
   146  }