//printThreadHead renders head part of thread page with cookie. func (t *threadCGI) printThreadHead(path, id string, page int, ca *thread.Cache, rss string) error { switch { case ca.HasRecord(): if !t.IsBot() { download.GetCache(true, ca) } else { log.Println("bot detected, not get cache") } case t.CheckGetCache(): ca.Subscribe() if t.Req.FormValue("search_new_file") == "" { download.GetCache(true, ca) } default: t.Print404(nil, id) return errors.New("no records") } var access string var newcookie []*http.Cookie if ca.HasRecord() && id == "" && page == 0 { cookie, err := t.Req.Cookie("access") if err == nil { access = cookie.Value } else { log.Println(err) } newcookie = t.setCookie(ca, access) } t.Header(path, rss, newcookie, false) return nil }
//bg waits for at least one record in the cache. func bg(c *thread.Cache, wg *sync.WaitGroup) { w := 2 * time.Second newest, err := recentlist.Newest(c.Datfile) var done chan struct{} go func() { wg.Wait() done <- struct{}{} }() if err == nil && (newest.Stamp == c.Stamp()) { return } for { select { case <-done: return case <-time.After(w): w += time.Second if c.HasRecord() || w >= 5*time.Second { return } } } }
//setFromCache adds cache.datfile/timestamp pair if not exists. func setFromCache(tx *bolt.Tx, ca *thread.Cache) { _, err := getTime(tx, ca.Datfile) if err == nil { return } var firstStamp int64 rec := ca.LoadRecords(record.Alive) switch { case !ca.HasRecord(): firstStamp = ca.RecentStamp() case len(rec) > 0: firstStamp = rec[rec.Keys()[0]].Stamp default: firstStamp = time.Now().Add(-24 * time.Hour).Unix() } for { _, err = getThread(tx, firstStamp) if err != nil { break } firstStamp++ } setEntry(tx, firstStamp, ca.Datfile) }
//printThreadTop renders toppart of thread page. func (t *threadCGI) printThreadTop(path, id string, nPage int, ca *thread.Cache) { var lastrec *record.Record var resAnchor string recs := ca.LoadRecords(record.Alive) ids := recs.Keys() if ca.HasRecord() && nPage == 0 && id == "" && len(ids) > 0 { lastrec = recs[ids[len(ids)-1]] resAnchor = t.ResAnchor(lastrec.ID[:8], cfg.ThreadURL, t.Path(), false) } s := struct { Path string Cache *thread.Cache Lastrec *record.Record ResAnchor template.HTML cgi.Defaults }{ path, ca, lastrec, template.HTML(resAnchor), *t.Defaults(), } cgi.RenderTemplate("thread_top", s, t.WR) }