From faae38726dd804e3246514d88f93794c23a0cdb2 Mon Sep 17 00:00:00 2001 From: knotteye Date: Fri, 2 Jul 2021 14:20:16 -0500 Subject: show only the last 3 news items on index, link to a page with all news items --- client.go | 23 ++++++++++++++++++++++- database.go | 18 +++++++++++++----- main.go | 6 ++++++ static/anews.html | 44 ++++++++++++++++++++++++++++++++++++++++++++ static/index.html | 2 +- 5 files changed, 86 insertions(+), 7 deletions(-) create mode 100644 static/anews.html diff --git a/client.go b/client.go index be79cdd..a5fb5bb 100644 --- a/client.go +++ b/client.go @@ -102,7 +102,7 @@ func IndexGet(w http.ResponseWriter, r *http.Request, db *sql.DB) { data.Board.Restricted = actor.Restricted data.BoardRemainer = make([]int, (len(data.Boards) % 3)+1) data.InstanceIndex = GetCollectionFromReq("https://fchan.xyz/followers").Items - data.NewsItems = getNewsFromDB(db) + data.NewsItems = getNewsFromDB(db, 3) t.ExecuteTemplate(w, "layout", data) } @@ -138,6 +138,27 @@ func NewsGet(w http.ResponseWriter, r *http.Request, db *sql.DB, timestamp int) t.ExecuteTemplate(w, "layout", data) } +func AllNewsGet(w http.ResponseWriter, r *http.Request, db *sql.DB) { + t := template.Must(template.New("").Funcs(template.FuncMap{"unixtoreadable": func(u int) string { return time.Unix(int64(u), 0).Format("Jan 02, 2006") }}).ParseFiles("./static/main.html", "./static/anews.html")) + + actor := GetActorFromDB(db, Domain) + + var data PageData + data.PreferredUsername = actor.PreferredUsername + data.Title = actor.PreferredUsername + " News" + data.Boards = Boards + data.Board.Name = "" + data.Key = *Key + data.Board.Domain = Domain + data.Board.ModCred, _ = GetPasswordFromSession(r) + data.Board.Actor = actor + data.Board.Post.Actor = actor.Id + data.Board.Restricted = actor.Restricted + data.NewsItems = getNewsFromDB(db, 0) + + t.ExecuteTemplate(w, "layout", data) +} + func OutboxGet(w http.ResponseWriter, r *http.Request, db *sql.DB, collection Collection){ t := template.Must(template.ParseFiles("./static/main.html", "./static/nposts.html", "./static/top.html", "./static/bottom.html", "./static/posts.html")) diff --git a/database.go b/database.go index 7dc7f5c..731f160 100644 --- a/database.go +++ b/database.go @@ -1491,15 +1491,23 @@ func MarkObjectSensitive(db *sql.DB, id string, sensitive bool) { //if limit less than 1 return all news items func getNewsFromDB(db *sql.DB, limit int) []NewsItem { - news := []NewsItem + var news []NewsItem + var query string if(limit > 0) { - query :=`select title, content, time from newsItem order by time desc limit $1` + query =`select title, content, time from newsItem order by time desc limit $1` } else { - query :=`select title, content, time from newsItem order by time desc` + query =`select title, content, time from newsItem order by time desc` } - rows, err := db.Query(query, limit) + var rows *sql.Rows + var err error + if(limit > 0) { + rows, err = db.Query(query, limit) + } else { + rows, err = db.Query(query) + } + if CheckError(err, "could not get news from db query") != nil { return news @@ -1512,7 +1520,7 @@ func getNewsFromDB(db *sql.DB, limit int) []NewsItem { if CheckError(err, "error scanning news from db") != nil { return news } - append(news, n) + news = append(news, n) } return news diff --git a/main.go b/main.go index 69e8902..3eb47c0 100644 --- a/main.go +++ b/main.go @@ -293,6 +293,12 @@ func main() { http.HandleFunc("/news/", func(w http.ResponseWriter, r *http.Request){ timestamp := r.URL.Path[6:] + + if(len(timestamp) < 2) { + AllNewsGet(w, r, db) + return + } + if timestamp[len(timestamp)-1:] == "/" { timestamp = timestamp[:len(timestamp)-1] } diff --git a/static/anews.html b/static/anews.html new file mode 100644 index 0000000..6527ce5 --- /dev/null +++ b/static/anews.html @@ -0,0 +1,44 @@ +{{ define "header" }} +{{ .Title }} + + + + + + + + + + + + + + +{{ end }} + +{{ define "top" }}{{ end }} +{{ define "content" }} +
+

{{ .Title }}

+ +
+ + + {{ range $i, $e := .NewsItems }} + + + + {{ end }} +
{{unixtoreadable $e.Time}} - {{$e.Title}} + {{ if eq $i 0 }} +

{{$e.Content}}

+ {{ end }} +
+
+ +
+{{ end }} +{{ define "bottom" }}{{ end }} + +{{ define "script" }} +{{ end }} diff --git a/static/index.html b/static/index.html index 518c012..106a6b3 100644 --- a/static/index.html +++ b/static/index.html @@ -39,7 +39,7 @@
{{ .PreferredUsername }} news + {{ .PreferredUsername }} news {{ range $i, $e := .NewsItems }} -- cgit v1.2.3
-