separate sort
This commit is contained in:
@@ -1,3 +1,6 @@
|
||||
// Package migrator provides import/export functionality. It defines Importer and Exporter interfaces
|
||||
// amd implements for disqus (importer only) and "native" remark (both importer and exporter).
|
||||
// Also implements AutoBackup scheduler running exports as backups and saving them locally.
|
||||
package migrator
|
||||
|
||||
import (
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
// Package auth provides oauth2 support as well as related middlewares.
|
||||
package auth
|
||||
|
||||
import (
|
||||
|
||||
+6
-5
@@ -46,7 +46,7 @@ func (s *Server) Run(port int) {
|
||||
log.Print("[INFO] activate rest server")
|
||||
|
||||
// add auth.Developer flag if dev mode is active
|
||||
maybeDevMode := func(mode auth.Mode) (modes []auth.Mode) {
|
||||
maybeWithDevMode := func(mode auth.Mode) (modes []auth.Mode) {
|
||||
modes = append(modes, mode)
|
||||
if s.DevMode {
|
||||
modes = append(modes, auth.Developer)
|
||||
@@ -62,8 +62,8 @@ func (s *Server) Run(port int) {
|
||||
router.Use(middleware.RealIP, Recoverer)
|
||||
router.Use(middleware.Throttle(1000), middleware.Timeout(60*time.Second))
|
||||
router.Use(Limiter(10), AppInfo("remark42", s.Version), Ping, Logger(LogAll))
|
||||
router.Use(auth.Auth(s.SessionStore, s.Admins, maybeDevMode(auth.Anonymous))) // all request by default allow anonymous access
|
||||
|
||||
// all request by default allow anonymous access
|
||||
router.Use(auth.Auth(s.SessionStore, s.Admins, maybeWithDevMode(auth.Anonymous)))
|
||||
router.Use(context.ClearHandler) // if you aren't using gorilla/mux, you need to wrap your handlers with context.ClearHandler
|
||||
|
||||
// auth routes for all providers
|
||||
@@ -72,7 +72,8 @@ func (s *Server) Run(port int) {
|
||||
r.Mount("/"+provider.Name, provider.Routes()) // mount auth providers as /auth/{name}
|
||||
}
|
||||
if len(s.AuthProviders) > 0 {
|
||||
r.Get("/logout", s.AuthProviders[0].LogoutHandler) // shortcut, can be any of providers, all logouts do the same
|
||||
// shortcut, can be any of providers, all logouts do the same - removes cookie
|
||||
r.Get("/logout", s.AuthProviders[0].LogoutHandler)
|
||||
}
|
||||
})
|
||||
|
||||
@@ -88,7 +89,7 @@ func (s *Server) Run(port int) {
|
||||
rapi.Get("/list", s.listCtrl)
|
||||
|
||||
// protected routes, require auth
|
||||
rapi.With(auth.Auth(s.SessionStore, s.Admins, maybeDevMode(auth.Full))).Group(func(rauth chi.Router) {
|
||||
rapi.With(auth.Auth(s.SessionStore, s.Admins, maybeWithDevMode(auth.Full))).Group(func(rauth chi.Router) {
|
||||
rauth.Post("/comment", s.createCommentCtrl)
|
||||
rauth.Put("/comment/{id}", s.updateCommentCtrl)
|
||||
rauth.Get("/user", s.userInfoCtrl)
|
||||
|
||||
+1
-21
@@ -4,7 +4,6 @@ import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"sort"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
@@ -178,26 +177,7 @@ func (b *BoltDB) Find(locator Locator, sortFld string) (comments []Comment, err
|
||||
})
|
||||
})
|
||||
|
||||
// sort result according to sortFld
|
||||
sort.Slice(comments, func(i, j int) bool {
|
||||
switch sortFld {
|
||||
case "+time", "-time", "time":
|
||||
if strings.HasPrefix(sortFld, "-") {
|
||||
return comments[i].Timestamp.After(comments[j].Timestamp)
|
||||
}
|
||||
return comments[i].Timestamp.Before(comments[j].Timestamp)
|
||||
|
||||
case "+score", "-score", "score":
|
||||
if strings.HasPrefix(sortFld, "-") {
|
||||
return comments[i].Score > comments[j].Score
|
||||
}
|
||||
return comments[i].Score < comments[j].Score
|
||||
|
||||
default:
|
||||
return comments[i].Timestamp.Before(comments[j].Timestamp)
|
||||
}
|
||||
})
|
||||
|
||||
comments = sortComments(comments, sortFld)
|
||||
return comments, err
|
||||
}
|
||||
|
||||
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
"fmt"
|
||||
"html/template"
|
||||
"log"
|
||||
"sort"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
@@ -109,3 +110,25 @@ func sanitizeComment(comment Comment) Comment {
|
||||
|
||||
return comment
|
||||
}
|
||||
|
||||
func sortComments(comments []Comment, sortFld string) []Comment {
|
||||
sort.Slice(comments, func(i, j int) bool {
|
||||
switch sortFld {
|
||||
case "+time", "-time", "time":
|
||||
if strings.HasPrefix(sortFld, "-") {
|
||||
return comments[i].Timestamp.After(comments[j].Timestamp)
|
||||
}
|
||||
return comments[i].Timestamp.Before(comments[j].Timestamp)
|
||||
|
||||
case "+score", "-score", "score":
|
||||
if strings.HasPrefix(sortFld, "-") {
|
||||
return comments[i].Score > comments[j].Score
|
||||
}
|
||||
return comments[i].Score < comments[j].Score
|
||||
|
||||
default:
|
||||
return comments[i].Timestamp.Before(comments[j].Timestamp)
|
||||
}
|
||||
})
|
||||
return comments
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user