From d5c96cb036514fb1f0b504da31a47da79a2403df Mon Sep 17 00:00:00 2001 From: "Apple\\Apple" Date: Mon, 16 Jun 2025 20:05:54 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20fix(console-setting):=20ensure?= =?UTF-8?q?=20announcements=20are=20returned=20in=20newest-first=20order?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- setting/console_setting/validation.go | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/setting/console_setting/validation.go b/setting/console_setting/validation.go index 51a84849..fda6453d 100644 --- a/setting/console_setting/validation.go +++ b/setting/console_setting/validation.go @@ -7,6 +7,7 @@ import ( "regexp" "strings" "time" + "sort" ) var ( @@ -210,8 +211,23 @@ func validateFAQ(faqStr string) error { 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{} { - 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{} {