Skip to content

Commit

Permalink
Create best, ask, and show pages (#122)
Browse files Browse the repository at this point in the history
  • Loading branch information
johnwarden committed Jan 27, 2023
1 parent 0392bcc commit e4800b7
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 12 deletions.
31 changes: 21 additions & 10 deletions frontpage.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,18 @@ func (d frontPageData) IsNewPage() bool {
return d.Ranking == "new"
}

func (d frontPageData) IsBestPage() bool {
return d.Ranking == "best"
}

func (d frontPageData) IsAskPage() bool {
return d.Ranking == "ask"
}

func (d frontPageData) IsShowPage() bool {
return d.Ranking == "show"
}

func (d frontPageData) GravityString() string {
return fmt.Sprintf("%.2f", d.Params.Gravity)
}
Expand Down Expand Up @@ -113,7 +125,7 @@ const frontPageSQL = `
limit 90;
`

const newPageSQL = `
const hnPageSQL = `
with parameters as (select %f as priorWeight, %f as overallPriorWeight)
select
id
Expand All @@ -132,7 +144,7 @@ const newPageSQL = `
, cast((sampleTime-submissionTime)/3600 as real) as ageHours
from dataset join stories using (id) join parameters
where sampleTime = (select max(sampleTime) from dataset)
order by newRank nulls last
order by %s
limit 90;
`

Expand Down Expand Up @@ -204,16 +216,15 @@ func getFrontPageStories(ctx context.Context, ndb newsDatabase, ranking string,
if statements[ranking] == nil || params != defaultFrontPageParams {

var sql string
if ranking == "new" {
sql = fmt.Sprintf(newPageSQL, priorWeight, overallPriorWeight)
} else {
if ranking == "quality" {
orderBy := "qnRank nulls last"
if ranking == "hntop" {
orderBy = "topRank nulls last"
} else if ranking == "new" {
orderBy = "newRank nulls last"
}
sql = fmt.Sprintf(frontPageSQL, priorWeight, overallPriorWeight, gravity, penaltyWeight, qnRankFormulaSQL, orderBy)
} else {
orderBy := "topRank nulls last"
if ranking != "hntop" {
orderBy = fmt.Sprintf("%sRank nulls last", ranking)
}
sql = fmt.Sprintf(hnPageSQL, priorWeight, overallPriorWeight, orderBy)
}

s, err = ndb.db.Prepare(sql)
Expand Down
3 changes: 3 additions & 0 deletions httpserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ func (app app) httpServer(onPanic func(error)) *http.Server {
router.GET("/", middleware("hntop", l, onPanic, app.frontpageHandler("hntop")))
// router.GET("/hntop", middleware("hntop", l, onPanic, app.frontpageHandler("hntop")))
router.GET("/new", middleware("new", l, onPanic, app.frontpageHandler("new")))
router.GET("/best", middleware("best", l, onPanic, app.frontpageHandler("best")))
router.GET("/ask", middleware("ask", l, onPanic, app.frontpageHandler("ask")))
router.GET("/show", middleware("show", l, onPanic, app.frontpageHandler("show")))
router.GET("/stats", middleware("stats", l, onPanic, app.statsHandler()))

server.Handler = app.cacheAndCompressMiddleware(router)
Expand Down
12 changes: 12 additions & 0 deletions statspage.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,18 @@ func (d StatsPageData) IsNewPage() bool {
return false
}

func (d StatsPageData) IsBestPage() bool {
return false
}

func (d StatsPageData) IsAskPage() bool {
return false
}

func (d StatsPageData) IsShowPage() bool {
return false
}

var ErrStoryIDNotFound = httperror.New(404, "Story ID not found")

func statsPage(ndb newsDatabase, w io.Writer, r *http.Request, params StatsPageParams) error {
Expand Down
7 changes: 5 additions & 2 deletions templates/header.html.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@
<svg style="flex-shrink: 0" class="icon" viewBox="0 0 200 200" xmlns="http://www.w3.org/2000/svg"><circle cx="100" cy="100" r="99" fill="#94caff" paint-order="markers fill stroke"/><circle cx="100" cy="100" r="57.269" fill="#4a9ced" paint-order="markers fill stroke"/><circle cx="100" cy="100" r="30.288" fill="#005fbe" paint-order="markers fill stroke"/></svg>
<a href="/" class="header-title">Quality News</a>
<div>
<!--<a class="nav-link {{if .IsQualityPage}}active{{end}}" href="/">qn&nbsp;top</a> |
<a class="nav-link {{if .IsHNTopPage}}active{{end}}" href="/hntop">hn&nbsp;top</a> |-->
<!--<a class="nav-link {{if .IsQualityPage}}active{{end}}" href="/">qn&nbsp;top</a> |-->
<a class="nav-link {{if .IsHNTopPage}}active{{end}}" href="/">top</a> |
<a class="nav-link {{if .IsNewPage}}active{{end}}" href="/new">new</a> |
<a class="nav-link {{if .IsBestPage}}active{{end}}" href="/best">best</a> |
<a class="nav-link {{if .IsAskPage}}active{{end}}" href="/ask">ask</a> |
<a class="nav-link {{if .IsShowPage}}active{{end}}" href="/show">show</a> |
<a class="nav-link" href="https://news.ycombinator.com/submit" target="_blank" rel="noopener noreferrer">submit</a> |
<a class="nav-link" href="https://github.com/social-protocols/news#readme">about</a>
</div>
Expand Down

0 comments on commit e4800b7

Please sign in to comment.