From 80881c42c30bc128c490ffda358c3055ccae9dd3 Mon Sep 17 00:00:00 2001 From: Bora Alper Date: Sun, 1 Jul 2018 17:30:06 +0300 Subject: [PATCH] magneticow: most recent torrents now works correctly --- cmd/magneticow/api.go | 26 ++++++++++++------- .../data/static/scripts/torrents.js | 26 ++++++++++++++----- 2 files changed, 35 insertions(+), 17 deletions(-) diff --git a/cmd/magneticow/api.go b/cmd/magneticow/api.go index 442e3f5..a317da8 100644 --- a/cmd/magneticow/api.go +++ b/cmd/magneticow/api.go @@ -49,10 +49,20 @@ func apiTorrentsHandler(w http.ResponseWriter, r *http.Request) { *tq.Ascending = true } - orderBy, err := parseOrderBy(tq.OrderBy) - if err != nil { - respondError(w, 400, err.Error()) - return + var orderBy persistence.OrderingCriteria + if tq.OrderBy == nil { + if *tq.Query == "" { + orderBy = persistence.ByDiscoveredOn + } else { + orderBy = persistence.ByRelevance + } + } else { + var err error + orderBy, err = parseOrderBy(*tq.OrderBy) + if err != nil { + respondError(w, 400, err.Error()) + return + } } torrents, err := database.QueryTorrents( @@ -87,12 +97,8 @@ func apiStatisticsHandler(w http.ResponseWriter, r *http.Request) { } -func parseOrderBy(s *string) (persistence.OrderingCriteria, error) { - if s == nil { - return persistence.ByRelevance, nil - } - - switch *s { +func parseOrderBy(s string) (persistence.OrderingCriteria, error) { + switch s { case "TOTAL_SIZE": return persistence.ByTotalSize, nil diff --git a/cmd/magneticow/data/static/scripts/torrents.js b/cmd/magneticow/data/static/scripts/torrents.js index c9e1cda..75fa01e 100644 --- a/cmd/magneticow/data/static/scripts/torrents.js +++ b/cmd/magneticow/data/static/scripts/torrents.js @@ -17,10 +17,20 @@ window.onload = function() { title.textContent = query + " - magneticow"; const input = document.getElementsByTagName("input")[0]; input.setAttribute("value", query); - } - else + + setOrderBy("RELEVANCE"); + } else { title.textContent = "Most recent torrents - magneticow"; + ascending = false; + setOrderBy("DISCOVERED_ON"); + } + + if (query) { + const feedAnchor = document.getElementById("feed-anchor"); + feedAnchor.setAttribute("href", "/feed?query=" + encodeURIComponent(query)); + } + load(); }; @@ -63,7 +73,9 @@ function load() { query : query, epoch : epoch, lastID : lastID, - lastOrderedValue: lastOrderedValue + lastOrderedValue: lastOrderedValue, + orderBy : orderBy, + ascending : ascending }); console.log("reqURL", reqURL); @@ -86,6 +98,10 @@ function load() { return; } + const last = torrents[torrents.length - 1]; + lastID = last.id; + lastOrderedValue = orderedValue(last); + for (let t of torrents) { t.size = fileSize(t.size); t.discoveredOn = (new Date(t.discoveredOn * 1000)).toLocaleDateString("en-GB", { @@ -96,10 +112,6 @@ function load() { tbody.innerHTML += Mustache.render(template, t); } - - const last = torrents[torrents.length - 1]; - lastID = last.id; - lastOrderedValue = orderedValue(last); }; req.open("GET", reqURL);