2021-03-25 18:11:00 +03:00
|
|
|
package node
|
|
|
|
|
|
|
|
import (
|
2021-03-30 19:47:25 +03:00
|
|
|
"encoding/json"
|
2021-03-25 18:11:00 +03:00
|
|
|
"net/http"
|
2021-03-30 19:47:25 +03:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"strconv"
|
2021-03-25 18:11:00 +03:00
|
|
|
|
|
|
|
"gitea.difrex.ru/Umbrella/fetcher/i2es"
|
2021-03-30 19:47:25 +03:00
|
|
|
"github.com/gorilla/mux"
|
2021-03-25 18:11:00 +03:00
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
|
|
|
type ssr struct {
|
|
|
|
es ESConf
|
|
|
|
templatesDir string
|
|
|
|
}
|
|
|
|
|
|
|
|
func newSSR(templatesDir string, es ESConf) *ssr {
|
|
|
|
return &ssr{
|
|
|
|
es: es,
|
|
|
|
templatesDir: templatesDir,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-26 11:11:14 +03:00
|
|
|
type PageData struct {
|
2021-04-02 17:11:12 +03:00
|
|
|
Echoes []echo
|
|
|
|
CurrentPage string
|
|
|
|
PageNum int
|
|
|
|
Posts []i2es.ESDoc
|
|
|
|
ShowPaginator bool
|
2021-03-25 18:11:00 +03:00
|
|
|
}
|
|
|
|
|
2021-03-30 19:47:25 +03:00
|
|
|
func (p *PageData) GetDate(date string) string {
|
|
|
|
d, err := strconv.ParseInt(date, 0, 64)
|
|
|
|
if err != nil {
|
|
|
|
return err.Error()
|
|
|
|
}
|
|
|
|
return time.Unix(d, 0).UTC().Format("02 Jan 06 15:04 MST")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *PageData) Inc() int {
|
|
|
|
return p.PageNum + 1
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *PageData) Dec() int {
|
|
|
|
return p.PageNum - 1
|
|
|
|
}
|
|
|
|
|
2021-04-02 17:11:12 +03:00
|
|
|
func (s *ssr) newPageData(page string, posts []i2es.ESDoc, num int, showPaginator bool) *PageData {
|
2021-03-26 11:11:14 +03:00
|
|
|
return &PageData{
|
2021-04-02 17:11:12 +03:00
|
|
|
Echoes: s.es.GetEchoesList(),
|
|
|
|
Posts: posts,
|
|
|
|
CurrentPage: page,
|
|
|
|
PageNum: num,
|
|
|
|
ShowPaginator: showPaginator,
|
2021-03-25 18:11:00 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *ssr) ssrRootHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
tpl, err := s.getTemplate("root")
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-04-02 17:11:12 +03:00
|
|
|
if err := tpl.Execute(w, s.newPageData("feed", s.es.GetLatestPosts(50), 1, true)); err != nil {
|
2021-03-26 11:11:14 +03:00
|
|
|
log.Error(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *ssr) ssrForumHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
tpl, err := s.getTemplate("forum")
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
return
|
2021-03-25 18:11:00 +03:00
|
|
|
}
|
|
|
|
|
2021-03-30 19:47:25 +03:00
|
|
|
vars := mux.Vars(r)
|
|
|
|
|
|
|
|
var num int
|
|
|
|
if _, ok := vars["page"]; ok {
|
|
|
|
num = getPageNum(mux.Vars(r)["page"])
|
|
|
|
}
|
|
|
|
|
2021-04-02 17:11:12 +03:00
|
|
|
bcVal := r.URL.Query().Get("year")
|
|
|
|
bc, _ := strconv.ParseBool(bcVal)
|
|
|
|
|
|
|
|
if bc {
|
|
|
|
if err := tpl.Execute(w, s.newPageData("forum", s.es.GetThreadsYear(num), num, false)); err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := tpl.Execute(w, s.newPageData("forum", s.es.GetThreads(num), num, true)); err != nil {
|
2021-03-30 19:47:25 +03:00
|
|
|
log.Error(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *ssr) threadViewHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
tpl, err := s.getTemplate("thread")
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
topicid, ok := mux.Vars(r)["topicid"]
|
|
|
|
if !ok {
|
|
|
|
log.Warn("empty topicid")
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
w.Write([]byte("error: empty topicid"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
posts := s.es.GetTopic(topicid)
|
|
|
|
thread := "nil"
|
|
|
|
if len(posts) > 0 {
|
|
|
|
thread = posts[0].Subg
|
|
|
|
}
|
|
|
|
|
2021-04-02 17:11:12 +03:00
|
|
|
if err := tpl.Execute(w, s.newPageData(thread, posts, 1, true)); err != nil {
|
2021-03-30 19:47:25 +03:00
|
|
|
log.Error(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func getPageNum(page string) int {
|
|
|
|
i, err := strconv.ParseInt(page, 0, 64)
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
if i < 1 {
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
return int(i)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *ssr) echoViewHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
tpl, err := s.getTemplate("echo")
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
echo, ok := vars["echo"]
|
|
|
|
if !ok {
|
|
|
|
log.Warn("empty echo")
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
w.Write([]byte("error: empty echo"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
page := 1
|
|
|
|
if _, ok := vars["page"]; ok {
|
|
|
|
page = getPageNum(vars["page"])
|
|
|
|
}
|
|
|
|
|
2021-04-02 17:11:12 +03:00
|
|
|
bcVal := r.URL.Query().Get("year")
|
|
|
|
bc, _ := strconv.ParseBool(bcVal)
|
|
|
|
|
|
|
|
if bc {
|
|
|
|
if err := tpl.Execute(w, s.newPageData("forum", s.es.GetThreadsYear(page, echo), page, false)); err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := tpl.Execute(w, s.newPageData(echo, s.es.GetThreads(page, echo), page, true)); err != nil {
|
2021-03-30 19:47:25 +03:00
|
|
|
log.Error(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-02 18:03:28 +03:00
|
|
|
func (s *ssr) docsFormattingHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
tpl, err := s.getTemplate("docs_formatting")
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := tpl.Execute(w, s.newPageData("docs: formatting", nil, 1, false)); err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-30 19:47:25 +03:00
|
|
|
func (s *ssr) singleMessageHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
tpl, err := s.getTemplate("message")
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
msgid, ok := mux.Vars(r)["msgid"]
|
|
|
|
if !ok {
|
|
|
|
log.Warn("empty msgid")
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
w.Write([]byte("error: empty msgid"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-04-02 17:11:12 +03:00
|
|
|
if err := tpl.Execute(w, s.newPageData(msgid, s.es.GetMessage(msgid), 1, false)); err != nil {
|
2021-03-25 18:11:00 +03:00
|
|
|
log.Error(err)
|
|
|
|
}
|
|
|
|
}
|
2021-03-30 19:47:25 +03:00
|
|
|
|
|
|
|
func (s *ssr) searchHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
tpl, err := s.getTemplate("search")
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
q := r.URL.Query().Get("query")
|
|
|
|
if q != "" {
|
|
|
|
m, err := json.Marshal(q)
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
m = []byte("")
|
|
|
|
}
|
|
|
|
|
|
|
|
posts := s.es.DoSearch(string(m))
|
|
|
|
for i := range posts {
|
|
|
|
posts[i].Date = parseTime(posts[i].Date)
|
|
|
|
}
|
|
|
|
|
2021-04-02 17:11:12 +03:00
|
|
|
if err := tpl.Execute(w, s.newPageData("search", posts, 1, false)); err != nil {
|
2021-03-30 19:47:25 +03:00
|
|
|
log.Error(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|