2019-06-03 09:32:43 +00:00
|
|
|
package arn
|
|
|
|
|
|
|
|
import (
|
|
|
|
"reflect"
|
|
|
|
"sort"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Postable is a generic interface for Threads, Posts and Messages.
|
|
|
|
type Postable interface {
|
|
|
|
Likeable
|
|
|
|
|
|
|
|
TitleByUser(*User) string
|
|
|
|
HTML() string
|
|
|
|
Parent() PostParent
|
|
|
|
Posts() []*Post
|
|
|
|
CountPosts() int
|
|
|
|
TypeName() string
|
|
|
|
Creator() *User
|
|
|
|
|
|
|
|
// Use Get prefix for these to avoid a
|
|
|
|
// name clash with the internal fields.
|
2019-11-18 05:01:13 +00:00
|
|
|
GetID() ID
|
2019-06-03 09:32:43 +00:00
|
|
|
GetText() string
|
|
|
|
GetCreated() string
|
2019-11-18 05:01:13 +00:00
|
|
|
GetParentID() ID
|
2019-08-27 06:03:29 +00:00
|
|
|
GetParentType() string
|
2019-06-03 09:32:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ToPostables converts a slice of specific types to a slice of generic postables.
|
|
|
|
func ToPostables(sliceOfPosts interface{}) []Postable {
|
|
|
|
var postables []Postable
|
|
|
|
|
|
|
|
v := reflect.ValueOf(sliceOfPosts)
|
|
|
|
|
|
|
|
for i := 0; i < v.Len(); i++ {
|
|
|
|
postable := v.Index(i).Interface().(Postable)
|
|
|
|
postables = append(postables, postable)
|
|
|
|
}
|
|
|
|
|
|
|
|
return postables
|
|
|
|
}
|
|
|
|
|
|
|
|
// SortPostablesLatestFirst ...
|
|
|
|
func SortPostablesLatestFirst(posts []Postable) {
|
|
|
|
sort.Slice(posts, func(i, j int) bool {
|
|
|
|
return posts[i].GetCreated() > posts[j].GetCreated()
|
|
|
|
})
|
|
|
|
}
|