From 7801a06daa244775c54e549087125a1d709f3405 Mon Sep 17 00:00:00 2001 From: NotAProton Date: Sat, 4 Dec 2021 20:57:04 +0530 Subject: [PATCH 1/2] Make returnreported return 204 if no reports --- src/adminHandlers.go | 8 ++++++- src/sql.go | 12 +++++++---- src/static/admin/index.html | 1 - src/static/admin/login/script.js | 2 +- src/static/admin/script.js | 36 ++++++++++++++++---------------- 5 files changed, 34 insertions(+), 25 deletions(-) diff --git a/src/adminHandlers.go b/src/adminHandlers.go index a1bcb6e..f1b89e3 100644 --- a/src/adminHandlers.go +++ b/src/adminHandlers.go @@ -18,6 +18,7 @@ package main import ( "encoding/json" + "errors" "net/http" "strconv" "strings" @@ -152,14 +153,19 @@ func returnReported(w http.ResponseWriter, r *http.Request) { } jsonData, err := dbQueryReported(p) + if errors.Is(err, errnoEnt) { + w.WriteHeader(http.StatusNoContent) + return + } + if err != nil { logger.Println(getRequestId(r) + err.Error()) w.WriteHeader(http.StatusInternalServerError) return } + w.Header().Set("Content-Type", "application/json; charset=utf-8") w.WriteHeader(http.StatusOK) - w.Header().Set("Content-Type", "application/json") w.Write(jsonData) } diff --git a/src/sql.go b/src/sql.go index ad8edca..eeb63db 100644 --- a/src/sql.go +++ b/src/sql.go @@ -19,12 +19,14 @@ package main import ( "database/sql" "encoding/json" + "errors" "fmt" _ "github.com/go-sql-driver/mysql" ) var db *sql.DB +var errnoEnt error = errors.New("no entries") func connectDb() { var err error @@ -262,7 +264,7 @@ func dbQueryReported(page int) ([]byte, error) { Voted_by string `json:"voted_by"` } - var users []Entry + var entries []Entry for rows.Next() { var id, times_reported, tempVotedfordeletion int @@ -276,10 +278,12 @@ func dbQueryReported(page int) ([]byte, error) { } else { votedfordeletion = true } - users = append(users, Entry{id, domain, path, destination, times_reported, hashed_IP, votedfordeletion, voted_by}) + entries = append(entries, Entry{id, domain, path, destination, times_reported, hashed_IP, votedfordeletion, voted_by}) } - - entryBytes, _ := json.MarshalIndent(&users, "", " ") + if len(entries) == 0 { + return nil, errnoEnt + } + entryBytes, _ := json.MarshalIndent(&entries, "", " ") return entryBytes, nil } diff --git a/src/static/admin/index.html b/src/static/admin/index.html index 526ba06..842cc0e 100644 --- a/src/static/admin/index.html +++ b/src/static/admin/index.html @@ -5,7 +5,6 @@ Admin Panel - diff --git a/src/static/admin/login/script.js b/src/static/admin/login/script.js index f2a6cf3..dde677d 100644 --- a/src/static/admin/login/script.js +++ b/src/static/admin/login/script.js @@ -5,7 +5,7 @@ $('#login').attr('disabled',true); let u = $('#username').val() let p = $('#password').val() fetch(domain+'admin/api/newreftoken', { -method: "post", +method: "POST", headers: { 'Accept': 'application/json', 'Content-Type': 'application/json' diff --git a/src/static/admin/script.js b/src/static/admin/script.js index 91de77c..320b1e3 100644 --- a/src/static/admin/script.js +++ b/src/static/admin/script.js @@ -15,7 +15,7 @@ function regenTokens() { } let r = localStorage.getItem('reftoken'); fetch(domain+'admin/api/newacctoken', { - method: "post", + method: "POST", headers: { 'Accept': 'application/json', 'Content-Type': 'application/json' @@ -49,26 +49,26 @@ function getReported() { let bearer = 'Bearer ' + sessionStorage.getItem('acctoken') let page='1' fetch(domain + 'admin/api/getreported?page=' + page, { - method: 'post', + method: 'POST', headers: { 'Authorization': bearer, 'Content-Type': 'application/x-www-form-url-encoded' } }) - .then (res => { - console.log(res.text) - if (res.headers.get('content-type') == 'text/plain; charset=utf-8') { - $('#table').text('No reported links so far') - } - }) - .then(res => res.json()) + .then(res => { + if (res.status == 204) { + $('#table').text('No reported links so far') + } else { + res.json() .then(data => { - data.forEach(function(obj) { - obj.link = obj.domain + "/" + obj.path - delete obj.domain - delete obj.path - makeTable(data) + data.forEach(function(obj) { + obj.link = obj.domain + "/" + obj.path + delete obj.domain + delete obj.path + }) + makeTable(data) }) + } }) .catch(error => ({ message: 'Something bad happened ' + error @@ -115,7 +115,7 @@ function refreshTable() { let bearer = 'Bearer ' + sessionStorage.getItem('acctoken') let page='1' fetch(domain + 'admin/api/getreported?page=' + page, { - method: 'post', + method: 'POST', headers: { 'Authorization': bearer, 'Content-Type': 'application/x-www-form-url-encoded' @@ -142,18 +142,18 @@ function renderTable(data) { } function voteDelete(link) { -domain=link.substr(0,link.indexOf('/')); +linkdomain=link.substr(0,link.indexOf('/')); path=link.substr(link.indexOf('/')+1); let bearer = 'Bearer ' + sessionStorage.getItem('acctoken') fetch(domain + 'admin/api/votedelete', { -method: "post", +method: "POST", headers: { 'Authorization': bearer, 'Accept': 'application/json', 'Content-Type': 'application/json' }, body: JSON.stringify({ - domain: domain, + domain: linkdomain, path: path }) }) From 7c17e965fffe02ee5f3e72a2b4403ffa205b7f76 Mon Sep 17 00:00:00 2001 From: NotAProton Date: Sat, 4 Dec 2021 21:00:05 +0530 Subject: [PATCH 2/2] Bump server version --- src/main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main.go b/src/main.go index 3b25756..9acfab3 100644 --- a/src/main.go +++ b/src/main.go @@ -34,7 +34,7 @@ const ( ) var ( - version string = "1.1.0" + version string = "1.2.2" reg *regexp.Regexp regForHTTP *regexp.Regexp )