Skip to content

Commit

Permalink
Refactor over18 skipping logics.
Browse files Browse the repository at this point in the history
  • Loading branch information
robertabcd committed Sep 18, 2020
1 parent 1ed585a commit 7b58b33
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 20 deletions.
19 changes: 6 additions & 13 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,14 @@ package main

import (
"net/http"

"github.com/gorilla/mux"
)

var routesSkipOver18 = map[string]bool{
"atomfeed": true,
}

type Context struct {
R *http.Request

isOver18CheckSkipped bool
hasOver18Cookie bool
isCrawler bool
skipOver18 bool
hasOver18Cookie bool
isCrawler bool
}

func (c *Context) Request() *http.Request {
Expand All @@ -24,18 +18,17 @@ func (c *Context) Request() *http.Request {

func (c *Context) MergeFromRequest(r *http.Request) error {
c.R = r
_, c.isOver18CheckSkipped = routesSkipOver18[mux.CurrentRoute(r).GetName()]
c.hasOver18Cookie = checkOver18Cookie(r)
c.isCrawler = isCrawlerUserAgent(r)
return nil
}

func (c *Context) IsOver18CheckSkipped() bool {
return c.isOver18CheckSkipped
func (c *Context) SetSkipOver18() {
c.skipOver18 = true
}

func (c *Context) IsOver18() bool {
return c.hasOver18Cookie
return c.hasOver18Cookie || c.skipOver18
}

func (c *Context) IsCrawler() bool {
Expand Down
13 changes: 6 additions & 7 deletions pttweb.go
Original file line number Diff line number Diff line change
Expand Up @@ -594,6 +594,7 @@ func handleBoardAtomFeed(c *Context, w http.ResponseWriter) error {

timeout := BbsIndexLastPageCacheTimeout

c.SetSkipOver18()
brd, err := getBoardByName(c, brdname)
if err != nil {
return err
Expand Down Expand Up @@ -809,15 +810,13 @@ func hasPermViewBoard(c *Context, brd *pttbbs.Board) error {
return NewNotFoundError(fmt.Errorf("no permission: %s", brd.BrdName))
}
if brd.Over18 {
if config.EnableOver18Cookie {
if c.IsCrawler() || c.IsOver18CheckSkipped() {
// Crawlers or machine-to-machine requests.
} else if !c.IsOver18() {
return shouldAskOver18Error(c)
}
} else {
if !config.EnableOver18Cookie {
return NewNotFoundError(ErrOver18CookieNotEnabled)
}
if !c.IsCrawler() && !c.IsOver18() {
return shouldAskOver18Error(c)
}
// Ok
}
return nil
}
Expand Down

0 comments on commit 7b58b33

Please sign in to comment.