From 4ac7e2990913fc9c999d2b6a0c1ddd8e0409ec82 Mon Sep 17 00:00:00 2001 From: Eli Ribble Date: Tue, 10 Mar 2026 15:46:17 +0000 Subject: [PATCH] Sort messages by creation time --- api/communication.go | 11 ++++++++++ api/handler.go | 6 ------ api/query_params.go | 25 ++++++++++++++++++++++ html/template/sync/communication-root.html | 1 + 4 files changed, 37 insertions(+), 6 deletions(-) create mode 100644 api/query_params.go diff --git a/api/communication.go b/api/communication.go index ab8d84e9..63ca312d 100644 --- a/api/communication.go +++ b/api/communication.go @@ -3,6 +3,7 @@ package api import ( "context" "net/http" + "slices" "time" "github.com/Gleipnir-Technology/nidus-sync/config" @@ -72,6 +73,16 @@ func listCommunication(ctx context.Context, r *http.Request, org *models.Organiz Type: "water", } } + _by_created := func(a, b communication) int { + if a.Created == b.Created { + return 0 + } else if a.Created.Before(b.Created) { + return 1 + } else { + return -1 + } + } + slices.SortFunc(comms, _by_created) return &contentListCommunication{ Communications: comms, }, nil diff --git a/api/handler.go b/api/handler.go index 451d60af..fcbfbc5e 100644 --- a/api/handler.go +++ b/api/handler.go @@ -19,12 +19,6 @@ import ( var decoder = schema.NewDecoder() -type queryParams struct { - Limit *int `schema:"limit"` - Sort *string `schema:"sort"` - Type *string `schema:"type"` -} - type handlerFunctionGet[T any] func(context.Context, *http.Request, *models.Organization, *models.User, queryParams) (*T, *nhttp.ErrorWithStatus) type wrappedHandler func(http.ResponseWriter, *http.Request) type contentAuthenticated[T any] struct { diff --git a/api/query_params.go b/api/query_params.go new file mode 100644 index 00000000..2b929247 --- /dev/null +++ b/api/query_params.go @@ -0,0 +1,25 @@ +package api + +type queryParams struct { + Limit *int `schema:"limit"` + Sort *string `schema:"sort"` + Type *string `schema:"type"` +} + +func (qp queryParams) SortOrDefault(default_name string, ascending bool) (string, bool) { + if qp.Sort == nil { + return default_name, ascending + } + s := *qp.Sort + if s == "" { + return default_name, ascending + } + a := true + if s[0] == '-' { + a = false + } + if s[0] == '+' || s[0] == '-' { + s = s[1:] + } + return s, a +} diff --git a/html/template/sync/communication-root.html b/html/template/sync/communication-root.html index 0dffd2ce..da79ed75 100644 --- a/html/template/sync/communication-root.html +++ b/html/template/sync/communication-root.html @@ -130,6 +130,7 @@ try { // Build query parameters from filters const params = new URLSearchParams(); + params.append("sort", "-created"); if (this.typeFilter) params.append("type", this.typeFilter); const response = await fetch(