Skip to content

Commit

Permalink
Merge pull request #10 from NotAProton/main
Browse files Browse the repository at this point in the history
  • Loading branch information
NotAProton authored Dec 4, 2021
2 parents 7de1c4f + 7c17e96 commit 4fa9b7d
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 26 deletions.
8 changes: 7 additions & 1 deletion src/adminHandlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package main

import (
"encoding/json"
"errors"
"net/http"
"strconv"
"strings"
Expand Down Expand Up @@ -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)
}

Expand Down
2 changes: 1 addition & 1 deletion src/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ const (
)

var (
version string = "1.1.0"
version string = "1.2.2"
reg *regexp.Regexp
regForHTTP *regexp.Regexp
)
Expand Down
12 changes: 8 additions & 4 deletions src/sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
}
1 change: 0 additions & 1 deletion src/static/admin/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Admin Panel</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/water.css@2/out/water.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/gridjs/dist/themermaid.min.css">
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/gridjs/dist/gridjs.umd.js"></script>
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
Expand Down
2 changes: 1 addition & 1 deletion src/static/admin/login/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
36 changes: 18 additions & 18 deletions src/static/admin/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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'
Expand All @@ -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
})
})
Expand Down

0 comments on commit 4fa9b7d

Please sign in to comment.