Skip to content

Commit

Permalink
website: make news functional or some such
Browse files Browse the repository at this point in the history
  • Loading branch information
Wessie committed Feb 26, 2024
1 parent 0c0dda7 commit c102fad
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 40 deletions.
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require (
github.com/BurntSushi/toml v1.3.2
github.com/DATA-DOG/go-sqlmock v1.5.1
github.com/XSAM/otelsql v0.27.0
github.com/alevinval/sse v1.0.2
github.com/alexedwards/scs/v2 v2.7.0
github.com/cenkalti/backoff v2.2.1+incompatible
github.com/davecgh/go-spew v1.1.1
Expand All @@ -23,6 +24,7 @@ require (
github.com/tcolgate/mp3 v0.0.0-20170426193717-e79c5a46d300
github.com/testcontainers/testcontainers-go v0.27.0
github.com/testcontainers/testcontainers-go/modules/mariadb v0.27.0
github.com/yuin/goldmark v1.7.0
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.47.0
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.47.0
go.opentelemetry.io/otel v1.23.0
Expand All @@ -43,7 +45,6 @@ require (
github.com/Azure/go-ansiterm v0.0.0-20230124172434-306776ec8161 // indirect
github.com/Microsoft/go-winio v0.6.1 // indirect
github.com/Microsoft/hcsshim v0.11.4 // indirect
github.com/alevinval/sse v1.0.2 // indirect
github.com/cenkalti/backoff/v4 v4.2.1 // indirect
github.com/containerd/containerd v1.7.11 // indirect
github.com/containerd/log v0.1.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,8 @@ github.com/tklauser/numcpus v0.6.1 h1:ng9scYS7az0Bk4OZLvrNXNSAO2Pxr1XXRAPyjhIx+F
github.com/tklauser/numcpus v0.6.1/go.mod h1:1XfjsgE2zo8GVw7POkMbHENHzVg3GzmoZ9fESEdAacY=
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.7.0 h1:EfOIvIMZIzHdB/R/zVrikYLPPwJlfMcNczJFMs1m6sA=
github.com/yuin/goldmark v1.7.0/go.mod h1:uzxRWxtg69N339t3louHJ7+O03ezfj6PlliRlaOzY1E=
github.com/yusufpapurcu/wmi v1.2.3 h1:E1ctvB7uKFMOJw3fdOW32DwGE9I7t++CRUEMKvFoFiw=
github.com/yusufpapurcu/wmi v1.2.3/go.mod h1:SBZ9tNy3G9/m5Oi98Zks0QjeHVDvuK0qfxQmPyzfmi0=
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.47.0 h1:UNQQKPfTDe1J81ViolILjTKPr9WetKW6uei2hFgJmFs=
Expand Down
20 changes: 20 additions & 0 deletions radio.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ import (
"database/sql/driver"
"encoding/hex"
"fmt"
"html/template"
"math"
"strconv"
"strings"
"time"

"github.com/R-a-dio/valkyrie/util/eventstream"
"github.com/yuin/goldmark"
"golang.org/x/crypto/bcrypt"
)

Expand Down Expand Up @@ -815,6 +817,24 @@ type NewsPost struct {
Private bool
}

func (np NewsPost) BodyParsed() (template.HTML, error) {
var buf bytes.Buffer
err := goldmark.Convert([]byte(np.Body), &buf)
if err != nil {
return "", err
}
return template.HTML(buf.String()), nil
}

func (np NewsPost) HeaderParsed() (template.HTML, error) {
var buf bytes.Buffer
err := goldmark.Convert([]byte(np.Header), &buf)
if err != nil {
return "", err
}
return template.HTML(buf.String()), nil
}

// HasRequired tells if you all required fields in a news post are filled,
// returns the field name that is missing and a boolean
func (np NewsPost) HasRequired() (string, bool) {
Expand Down
22 changes: 4 additions & 18 deletions templates/default/home.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -45,30 +45,16 @@
</div>
</div>
<div class="columns">
{{range .News}}
<article class="column message is-small m-3 p-0">
<div class="message-header">
<p>Hello World</p>
<p>{{.Title}}</p>
</div>
<div class="message-body">
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque risus mi, tempus quis placerat ut, porta nec nulla. Vestibulum rhoncus ac ex sit amet fringilla. Nullam gravida purus diam, et dictum felis venenatis efficitur. Aenean ac eleifend lacus, in mollis lectus. Donec sodales, arcu et sollicitudin porttitor, tortor urna tempor ligula, id porttitor mi magna a neque. Donec dui urna, vehicula et sem eget, facilisis sodales sem.
</div>
</article>
<article class="column message is-small m-3 p-0">
<div class="message-header">
<p>Hello World</p>
</div>
<div class="message-body">
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque risus mi, tempus quis placerat ut, porta nec nulla. Vestibulum rhoncus ac ex sit amet fringilla. Nullam gravida purus diam, et dictum felis venenatis efficitur. Aenean ac eleifend lacus, in mollis lectus. Donec sodales, arcu et sollicitudin porttitor, tortor urna tempor ligula, id porttitor mi magna a neque. Donec dui urna, vehicula et sem eget, facilisis sodales sem.
</div>
</article>
<article class="column message is-small m-3 p-0">
<div class="message-header">
<p>Hello World</p>
</div>
<div class="message-body">
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque risus mi, tempus quis placerat ut, porta nec nulla. Vestibulum rhoncus ac ex sit amet fringilla. Nullam gravida purus diam, et dictum felis venenatis efficitur. Aenean ac eleifend lacus, in mollis lectus. Donec sodales, arcu et sollicitudin porttitor, tortor urna tempor ligula, id porttitor mi magna a neque. Donec dui urna, vehicula et sem eget, facilisis sodales sem.
{{.HeaderParsed}}
</div>
</article>
{{end}}
</div>
</div>
</section>
Expand Down
24 changes: 8 additions & 16 deletions templates/default/news-single.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<section class="section">
<div class="container">
<div class="content">
{{template "news-post" .}}
{{template "news-post" .Entry}}
{{template "news-comments" .}}
</div>
</div>
Expand All @@ -12,27 +12,19 @@
{{define "news-post"}}
<div class="columns block has-background-white-ter mb-5">
<article class="message column is-12 disable-message-hover">
<a href="/news/{{/*$.ID*/}}">
<a href="/news/{{.ID}}">
<div class="block message-header">
<span>{{/*$.Title*/}}News Title</span>
<span>~{{/*$.User.Username*/}}Username</span>
<span>{{.Title}}</span>
<span>{{with .User}}{{or .Username "Anonymous"}}{{else}}Anonymous{{end}}</span>
</div>
</a>
<div class="block message-header has-background-white-ter has-text-grey-dark m-1 p-1 disable-message-hover">
<span class="has-text-left"></span>
<time datetime="{{/*.CreatedAt.Unix*/}}" data-type="absolute">{{/*.CreatedAt | AbsoluteDate*/}}News Created At</time>
<time datetime="{{.CreatedAt.Unix}}" data-type="absolute">{{.CreatedAt | AbsoluteDate}}</time>
</div>
<div class="message-body pt-0 disable-message-border">
{{.BodyParsed}}
</div>
<div class="message-body pt-0 disable-message-border">{{/*$.News.Body*/}}
<p>When the great provincial armies were gradually disbanded under Hideyoshi
and Ieyasu, many out-of-work samurai roamed the country redundant in an era
of peace. Musashi was one such samurai, a "ronin" or "wave man".</p> <p>There were
still samurai retainers to the Tokugawas and provincial lords, but their numbers
were few. The hordes of redundant samurai found themselves living in a society
which was completely based on the old chivalry, but at the same time they were
apart from a society in which there was no place for men at arms.</p> <p>They became
an inverted class, keeping the old chivalry alive by devotion to military arts
with the fervour only the Japanese possess. This was the time of the flowering
of Kendo.</p></div>
</article>
</div>
{{end}}
Expand Down
9 changes: 5 additions & 4 deletions templates/default/news.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,22 @@
</div>
</div>
</section>
{{printjson .}}
{{end}}

{{define "news-post"}}
<div class="column is-3-fullhd is-4-desktop is-half-tablet">
<a href="/news/{{$.ID}}">
<a href="/news/{{.ID}}">
<article class="message m-3 p-0">
<div class="message-header">
<span>{{$.Title}}</span>
<span>~{{$.User.Username}}</span>
<span>{{.Title}}</span>
<span>~{{.User.Username}}</span>
</div>
<div class="message-header has-background-white-ter has-text-grey-dark m-1 p-1">
<span class="has-text-left"></span>
<time datetime="{{.CreatedAt.Unix}}" data-type="absolute">{{.CreatedAt | AbsoluteDate}}</time>
</div>
<div class="message-body pt-0 disable-message-border">{{$.Header}}</div>
<div class="message-body pt-0 disable-message-border">{{.HeaderParsed}}</div>
</article>
</a>
</div>
Expand Down
45 changes: 44 additions & 1 deletion website/public/news.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ package public

import (
"net/http"
"strconv"

radio "github.com/R-a-dio/valkyrie"
"github.com/R-a-dio/valkyrie/website/middleware"
"github.com/go-chi/chi/v5"
)

type NewsInput struct {
Expand Down Expand Up @@ -43,8 +45,49 @@ func (s State) GetNews(w http.ResponseWriter, r *http.Request) {
}
}

type NewsEntryInput struct {
middleware.Input

Entry radio.NewsPost
}

func (NewsEntryInput) TemplateBundle() string {
return "news-single"
}

func NewNewsEntryInput(ns radio.NewsStorage, r *http.Request) (*NewsEntryInput, error) {
ctx := r.Context()

id := chi.URLParamFromCtx(ctx, "NewsID")
iid, err := strconv.Atoi(id)
if err != nil {
return nil, err
}
newsid := radio.NewsPostID(iid)

post, err := ns.Get(newsid)
if err != nil {
return nil, err
}

return &NewsEntryInput{
Input: middleware.InputFromContext(ctx),
Entry: *post,
}, nil
}

func (s State) GetNewsEntry(w http.ResponseWriter, r *http.Request) {
s.errorHandler(w, r, nil)
input, err := NewNewsEntryInput(s.Storage.News(r.Context()), r)
if err != nil {
s.errorHandler(w, r, err)
return
}

err = s.Templates.Execute(w, r, input)
if err != nil {
s.errorHandler(w, r, err)
return
}
}

func (s State) PostNewsEntry(w http.ResponseWriter, r *http.Request) {
Expand Down

0 comments on commit c102fad

Please sign in to comment.