diff --git a/server.go b/server.go index 3ddd954..ed8fef1 100644 --- a/server.go +++ b/server.go @@ -6,7 +6,6 @@ import ( "html/template" "io" "io/ioutil" - "math" "net/http" "net/url" "os" @@ -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) } @@ -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) @@ -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")}, diff --git a/stats.go b/stats.go index 8ddd950..6cfa6d7 100644 --- a/stats.go +++ b/stats.go @@ -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) diff --git a/storage.go b/storage.go index b944a2e..aa938b0 100644 --- a/storage.go +++ b/storage.go @@ -7,6 +7,7 @@ import ( "database/sql" "fmt" "html/template" + "math" "math/rand" "net/http" "regexp" @@ -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 diff --git a/test/main.go b/test/main.go index 4611f94..9202865 100644 --- a/test/main.go +++ b/test/main.go @@ -67,8 +67,6 @@ func main() { ExpectJson("Payload", "Bad request: note length not accepted") testNote := "# Hello World!\nThis is a _test_ note!" - testNoteHTML := "
This is a test note!
" - var id string tooLongNote := testNote for len(tooLongNote) < 50000 { @@ -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"). @@ -100,6 +99,7 @@ func main() { id = noteID }) + testNoteHTML := "This is a test note!
" frisby.Create("Test retrieval of new note"). Get(service + "/" + id). Send(). @@ -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").