🐛 fix(console-setting): ensure announcements are returned in newest-first order
Summary • Added stable, descending sort to `GetAnnouncements()` so that the API always returns the latest announcements first. • Introduced helper `getPublishTime()` to safely parse `publishDate` (RFC 3339) and fall back to zero value on failure. • Switched to `sort.SliceStable` for deterministic ordering when timestamps are identical. • Imported the standard `sort` package and removed redundant, duplicate date parsing. Impact Front-end no longer needs to perform client-side sorting; the latest announcement is guaranteed to appear at the top on all platforms and clients.
This commit is contained in:
@@ -7,6 +7,7 @@ import (
|
|||||||
"regexp"
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
"sort"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@@ -210,8 +211,23 @@ func validateFAQ(faqStr string) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func getPublishTime(item map[string]interface{}) time.Time {
|
||||||
|
if v, ok := item["publishDate"]; ok {
|
||||||
|
if s, ok2 := v.(string); ok2 {
|
||||||
|
if t, err := time.Parse(time.RFC3339, s); err == nil {
|
||||||
|
return t
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return time.Time{}
|
||||||
|
}
|
||||||
|
|
||||||
func GetAnnouncements() []map[string]interface{} {
|
func GetAnnouncements() []map[string]interface{} {
|
||||||
return getJSONList(GetConsoleSetting().Announcements)
|
list := getJSONList(GetConsoleSetting().Announcements)
|
||||||
|
sort.SliceStable(list, func(i, j int) bool {
|
||||||
|
return getPublishTime(list[i]).After(getPublishTime(list[j]))
|
||||||
|
})
|
||||||
|
return list
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetFAQ() []map[string]interface{} {
|
func GetFAQ() []map[string]interface{} {
|
||||||
|
|||||||
Reference in New Issue
Block a user