Skip to content
This repository has been archived by the owner on Sep 26, 2022. It is now read-only.

Commit

Permalink
minor refactorings
Browse files Browse the repository at this point in the history
  • Loading branch information
Christian Müller committed Jan 26, 2018
1 parent 9982258 commit 81a148d
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 29 deletions.
17 changes: 2 additions & 15 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"html/template"
"io"
"io/ioutil"
"math"
"net/http"
"net/url"
"os"
Expand Down Expand Up @@ -80,7 +79,7 @@ func main() {
return c.String(code, statuses[code])
}
defer incViews(n, db)
fraud := fraudelent(n)
fraud := n.fraudelent()
if fraud {
n.Ads = mdTmplHTML(ads)
}
Expand All @@ -94,7 +93,7 @@ func main() {
var content string
if code == http.StatusOK {
defer incViews(n, db)
if fraudelent(n) {
if n.fraudelent() {
code = http.StatusForbidden
content = statuses[code]
c.Logger().Warnf("/%s/export failed (code: %d)", id, code)
Expand Down Expand Up @@ -209,18 +208,6 @@ func main() {
e.Logger.Fatal(e.StartServer(s))
}

func fraudelent(n *Note) bool {
res := rexpLink.FindAllString(n.Text, -1)
if len(res) < 3 {
return false
}
stripped := rexpLink.ReplaceAllString(n.Text, "")
l1 := len(n.Text)
l2 := len(stripped)
return n.Views > 100 &&
int(math.Ceil(100*float64(l1-l2)/float64(l1))) > fraudThreshold
}

func checkRecaptcha(c echo.Context, captchaResp string) bool {
resp, err := http.PostForm("https://www.google.com/recaptcha/api/siteverify", url.Values{
"secret": []string{os.Getenv("RECAPTCHA_SECRET")},
Expand Down
7 changes: 3 additions & 4 deletions stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,9 @@ func flush(db *sql.DB) (int, error) {

func incViews(n *Note, db *sql.DB) {
views := n.Views
if val, ok := stats.Load(n.ID); ok {
intVal, ok := val.(int)
if ok {
views = intVal
if viewsCached, found := stats.Load(n.ID); found {
if val, ok := viewsCached.(int); ok {
views = val
}
}
stats.Store(n.ID, views+1)
Expand Down
13 changes: 13 additions & 0 deletions storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"database/sql"
"fmt"
"html/template"
"math"
"math/rand"
"net/http"
"regexp"
Expand All @@ -31,6 +32,18 @@ type Note struct {
Content, Ads template.HTML
}

func (n *Note) fraudelent() bool {
res := rexpLink.FindAllString(n.Text, -1)
if len(res) < 3 {
return false
}
stripped := rexpLink.ReplaceAllString(n.Text, "")
l1 := len(n.Text)
l2 := len(stripped)
return n.Views > 100 &&
int(math.Ceil(100*float64(l1-l2)/float64(l1))) > fraudThreshold
}

func save(c echo.Context, db *sql.DB, n *Note) (*Note, error) {
if n.Password != "" {
clean := n.Password
Expand Down
20 changes: 10 additions & 10 deletions test/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,6 @@ func main() {
ExpectJson("Payload", "Bad request: note length not accepted")

testNote := "# Hello World!\nThis is a _test_ note!"
testNoteHTML := "<h1>Hello World!</h1>\n<p>This is a <em>test</em> note!</p>"
var id string

tooLongNote := testNote
for len(tooLongNote) < 50000 {
Expand All @@ -84,6 +82,7 @@ func main() {
ExpectJson("Success", false).
ExpectJson("Payload", "Bad request: note length not accepted")

var id string
frisby.Create("Test publishing: correct inputs; no password").
Post(service+"/").
SetData("tos", "on").
Expand All @@ -100,6 +99,7 @@ func main() {
id = noteID
})

testNoteHTML := "<h1>Hello World!</h1>\n<p>This is a <em>test</em> note!</p>"
frisby.Create("Test retrieval of new note").
Get(service + "/" + id).
Send().
Expand All @@ -118,18 +118,18 @@ func main() {
ExpectHeader("Content-type", "text/plain; charset=UTF-8").
ExpectContent(testNote)

// TODO: fix this
// frisby.Create("Test opening fake service on note").
// Get(service + "/" + id + "/asd").
// Send().
// ExpectStatus(404).
// PrintBody().
// ExpectContent("Not found")
frisby.Create("Test opening fake service on note").
Get(service + "/" + id + "/asd").
Send().
ExpectStatus(404).
ExpectContent("Not Found")

// TODO: fix this
// frisby.Create("Test opening fake service on note 2").
// Get(service + "/" + id + "/exports").
// Send().
// ExpectStatus(404).
// ExpectContent("Not found")
// ExpectContent("Not Found")

frisby.Create("Test stats of new note").
Get(service + "/" + id + "/stats").
Expand Down

0 comments on commit 81a148d

Please sign in to comment.