WIP: Node. New generation #1

Draft
Difrex wants to merge 18 commits from ssr into master
3 changed files with 56 additions and 1 deletions
Showing only changes of commit 425e447765 - Show all commits

View File

@ -597,6 +597,7 @@ func (es ESConf) GetThreadsYear(pageNum int, echoes ...string) (posts []i2es.ESD
}
func (es ESConf) GetLatestPosts(sum int) []i2es.ESDoc {
log.Debug(sum)
query := fmt.Sprintf(`{"sort": [{"date": {"order": "desc"}}, {"_score": {"order": "desc" }}], "size": %d}`, sum)
log.Debugf("Do %s request", query)

View File

@ -3,6 +3,8 @@ package node
import (
"encoding/json"
"net/http"
"regexp"
"strings"
"time"
"strconv"
@ -12,6 +14,10 @@ import (
log "github.com/sirupsen/logrus"
)
const (
quotesRE = `^(\w+)?>(.*)$`
)
type ssr struct {
es ESConf
templatesDir string
@ -48,6 +54,36 @@ func (p *PageData) Dec() int {
return p.PageNum - 1
}
func (p *PageData) RenderMessage(message string) (msg string) {
return parseQuotes(message)
}
func parseQuotes(message string) string {
var parsed []string
r, err := regexp.Compile(quotesRE)
if err != nil {
log.Error(err)
return message
}
for _, line := range strings.Split(message, "\n") {
if r.MatchString(line) {
author := r.FindAllStringSubmatch(line, 1)
quote := "<figure class=\"text-end\"><blockquote class=\"blockquote quote\"><small>" + author[0][2] + "</small></blockquote>"
if len(author) > 0 && author[0][1] != "" {
quote += `<figcaption class="blockquote-footer">` + author[0][1] + `</figcaption>`
}
quote += "</figure>"
parsed = append(parsed, quote)
} else {
parsed = append(parsed, line)
}
}
return strings.Join(parsed, "\n")
}
func (s *ssr) newPageData(page string, posts []i2es.ESDoc, num int, showPaginator bool) *PageData {
return &PageData{
Echoes: s.es.GetEchoesList(),
@ -65,7 +101,12 @@ func (s *ssr) ssrRootHandler(w http.ResponseWriter, r *http.Request) {
return
}
if err := tpl.Execute(w, s.newPageData("feed", s.es.GetLatestPosts(50), 1, true)); err != nil {
posts := s.es.GetLatestPosts(50)
for i := range posts {
posts[i].Message = parseQuotes(posts[i].Message)
}
if err := tpl.Execute(w, s.newPageData("feed", posts, 1, true)); err != nil {
log.Error(err)
}
}
@ -120,6 +161,10 @@ func (s *ssr) threadViewHandler(w http.ResponseWriter, r *http.Request) {
thread = posts[0].Subg
}
for i := range posts {
posts[i].Message = parseQuotes(posts[i].Message)
}
if err := tpl.Execute(w, s.newPageData(thread, posts, 1, true)); err != nil {
log.Error(err)
}

View File

@ -92,6 +92,15 @@
background-color: #073642;
}
.quote {
color: green;
border: 0;
border-right: 1px;
border-style: solid;
border-color: grey;
padding-right: 0.5em;
}
@media only screen and (max-width: 1000px) {
.hideit {
display: none;