From 638a03b458b71564b1db439e6386d7dbf2a5181c Mon Sep 17 00:00:00 2001 From: knotteye Date: Thu, 1 Jul 2021 14:39:44 -0500 Subject: linkify text and use imageboard name to identify it --- client.go | 5 ++--- static/index.html | 10 +++++----- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/client.go b/client.go index 49c7d0c..dd40f3d 100644 --- a/client.go +++ b/client.go @@ -1,6 +1,5 @@ package main -import "fmt" import "net/http" import "html/template" import "database/sql" @@ -36,7 +35,7 @@ type Board struct{ type PageData struct { Title string - Message string + PreferredUsername string Board Board Pages []int CurrentPage int @@ -81,7 +80,7 @@ func IndexGet(w http.ResponseWriter, r *http.Request, db *sql.DB) { var data PageData data.Title = "Welcome to " + actor.PreferredUsername - data.Message = fmt.Sprintf("%s is a federated image board based on activitypub. The current version of the code running the server is still a work in progress, expect a bumpy ride for the time being. Get the server code here https://github.com/FChannel0", Domain) + data.PreferredUsername = actor.PreferredUsername data.Boards = Boards data.Board.Name = "" data.Key = *Key diff --git a/static/index.html b/static/index.html index 502e527..19c5da2 100644 --- a/static/index.html +++ b/static/index.html @@ -1,6 +1,6 @@ {{ define "header" }} {{ .Title }} - + @@ -8,10 +8,10 @@ - + - + {{ end }} @@ -20,7 +20,7 @@ {{ define "content" }}

{{ .Title }}

-

{{.Message}}

+

{{ .PreferredUsername }} is a federated image board based on activitypub. The current version of the code running the server is still a work in progress, expect a bumpy ride for the time being. Get the server code here.

@@ -30,7 +30,7 @@ {{ range .InstanceIndex }} - + {{ end }}
{{ .Id }}{{ .Id }}
-- cgit v1.2.3 From 4e3848cadbb3fd89b94a7ef24838173939d198db Mon Sep 17 00:00:00 2001 From: knotteye Date: Thu, 1 Jul 2021 17:56:04 -0500 Subject: Add a list of local boards and server news --- client.go | 13 +++++++++++-- database.go | 42 ++++++++++++++++++++++++++++++++++++++++-- databaseschema.psql | 8 +++++++- main.go | 11 +++++++++++ static/index.html | 28 ++++++++++++++++++++++++++++ static/nadmin.html | 8 ++++++++ 6 files changed, 105 insertions(+), 5 deletions(-) diff --git a/client.go b/client.go index dd40f3d..fcb496b 100644 --- a/client.go +++ b/client.go @@ -47,6 +47,7 @@ type PageData struct { Instance Actor InstanceIndex []ObjectBase ReturnTo string + NewsItems []NewsItem } type AdminPage struct { @@ -73,8 +74,15 @@ type Removed struct { Board string } + +type NewsItem struct { + Title string + Content string + Time int +} + func IndexGet(w http.ResponseWriter, r *http.Request, db *sql.DB) { - t := template.Must(template.ParseFiles("./static/main.html", "./static/index.html")) + t := template.Must(template.New("").Funcs(template.FuncMap{"mod": func(i, j int) bool { return i%j == 0 }}).ParseFiles("./static/main.html", "./static/index.html")) actor := GetActorFromDB(db, Domain) @@ -90,8 +98,9 @@ func IndexGet(w http.ResponseWriter, r *http.Request, db *sql.DB) { data.Board.Post.Actor = actor.Id data.Board.Restricted = actor.Restricted data.InstanceIndex = GetCollectionFromReq("https://fchan.xyz/followers").Items + data.NewsItems = getNewsFromDB(db) - t.ExecuteTemplate(w, "layout", data) + t.ExecuteTemplate(w, "layout", data) } func OutboxGet(w http.ResponseWriter, r *http.Request, db *sql.DB, collection Collection){ diff --git a/database.go b/database.go index 9d5d721..5fcebf0 100644 --- a/database.go +++ b/database.go @@ -7,11 +7,12 @@ import "time" import "os" import "strings" import "sort" +import "container/list" func GetActorFromDB(db *sql.DB, id string) Actor { - var nActor Actor + var nActor Actor - query :=`select type, id, name, preferedusername, inbox, outbox, following, followers, restricted, summary, publickeypem from actor where id=$1` + query :=`select type, id, name, preferedusername, inbox, outbox, following, followers, restricted, summary, publickeypem from actor where id=$1` rows, err := db.Query(query, id) @@ -1488,3 +1489,40 @@ func MarkObjectSensitive(db *sql.DB, id string, sensitive bool) { CheckError(err, "error updating sensitive object in cacheactivitystream") } + +func getNewsFromDB(db *sql.DB) []NewsItem { + news := list.New() + query :=`select title, content, time from newsItem order by time desc` + + rows, err := db.Query(query) + + if CheckError(err, "could not get news from db query") != nil { + return make([]NewsItem, 0) + } + + defer rows.Close() + for rows.Next() { + n := NewsItem{} + err = rows.Scan(&n.Title, &n.Content, &n.Time) + news.PushBack(n) + } + + anews := make([]NewsItem, news.Len()) + + i:=0 + for e := news.Front(); e != nil; e = e.Next() { + assert := e.Value.(NewsItem) + anews[i] = assert + i++ + } + + return anews +} + +func WriteNewsToDB(db *sql.DB, news NewsItem) { + query := `insert into newsItem (title, content, time) values ($1, $2, $3)` + + _, err := db.Exec(query, news.Title, news.Content, time.Now().Unix()) + + CheckError(err, "error writing news item") +} diff --git a/databaseschema.psql b/databaseschema.psql index e12813e..43767f3 100644 --- a/databaseschema.psql +++ b/databaseschema.psql @@ -216,7 +216,13 @@ owner varchar(100), file varchar(100) ); +CREATE TABLE IF NOT EXISTS newsItem( +title text, +content text, +time bigint +); + ALTER TABLE actor ADD COLUMN IF NOT EXISTS publicKeyPem varchar(100) default ''; ALTER TABLE activitystream ADD COLUMN IF NOT EXISTS sensitive boolean default false; -ALTER TABLE cacheactivitystream ADD COLUMN IF NOT EXISTS sensitive boolean default false; \ No newline at end of file +ALTER TABLE cacheactivitystream ADD COLUMN IF NOT EXISTS sensitive boolean default false; diff --git a/main.go b/main.go index 972e69e..037f240 100644 --- a/main.go +++ b/main.go @@ -598,6 +598,17 @@ func main() { MakeActivityRequestOutbox(db, newActorActivity) http.Redirect(w, r, "/" + *Key, http.StatusSeeOther) }) + + http.HandleFunc("/" + *Key + "/postnews", func(w http.ResponseWriter, r *http.Request) { + var newsitem NewsItem + + newsitem.Title = r.FormValue("title") + newsitem.Content = r.FormValue("summary") + + WriteNewsToDB(db, newsitem) + + http.Redirect(w, r, "/", http.StatusSeeOther) + }) http.HandleFunc("/verify", func(w http.ResponseWriter, r *http.Request){ if(r.Method == "POST") { diff --git a/static/index.html b/static/index.html index 19c5da2..de06c6c 100644 --- a/static/index.html +++ b/static/index.html @@ -22,6 +22,34 @@

{{ .Title }}

{{ .PreferredUsername }} is a federated image board based on activitypub. The current version of the code running the server is still a work in progress, expect a bumpy ride for the time being. Get the server code here.

+
+ + Local boards + + + {{ range .Boards }} + + + + {{ end }} +
+
{{.Name}} - {{.PrefName}}
+
+ +
+ + {{ .PreferredUsername }} news + + + {{ range .NewsItems }} + + + + {{ end }} +
+

{{.Time}} - {{.Title}}
{{.Content}}

+
+
- + {{ end }}
diff --git a/static/nadmin.html b/static/nadmin.html index 984eb76..877e1e5 100644 --- a/static/nadmin.html +++ b/static/nadmin.html @@ -17,6 +17,14 @@ + +

Post News

+
+
+
+
+
+
  • Subscribed
  • -- cgit v1.2.3 From 168225daa21fe494bcd74ab6c90d6498ecf9f1e3 Mon Sep 17 00:00:00 2001 From: knotteye Date: Thu, 1 Jul 2021 20:26:49 -0500 Subject: Make news list links to full page news articles --- client.go | 34 +++++++++++++++++++++++++++++++++- database.go | 24 ++++++++++++++++++++++++ main.go | 15 +++++++++++++++ static/index.html | 2 +- static/news.html | 32 ++++++++++++++++++++++++++++++++ 5 files changed, 105 insertions(+), 2 deletions(-) create mode 100644 static/news.html diff --git a/client.go b/client.go index fcb496b..38f0d6f 100644 --- a/client.go +++ b/client.go @@ -8,6 +8,7 @@ import "strings" import "strconv" import "sort" import "regexp" +import "time" var Key *string = new(string) @@ -82,7 +83,7 @@ type NewsItem struct { } func IndexGet(w http.ResponseWriter, r *http.Request, db *sql.DB) { - t := template.Must(template.New("").Funcs(template.FuncMap{"mod": func(i, j int) bool { return i%j == 0 }}).ParseFiles("./static/main.html", "./static/index.html")) + t := template.Must(template.New("").Funcs(template.FuncMap{"mod": func(i, j int) bool { return i%j == 0 }, "unixtoreadable": func(u int) string { return time.Unix(int64(u), 0).Format("Jan 02, 2006") }}).ParseFiles("./static/main.html", "./static/index.html")) actor := GetActorFromDB(db, Domain) @@ -103,6 +104,37 @@ func IndexGet(w http.ResponseWriter, r *http.Request, db *sql.DB) { t.ExecuteTemplate(w, "layout", data) } +func NewsGet(w http.ResponseWriter, r *http.Request, db *sql.DB, timestamp int) { + 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/news.html")) + + actor := GetActorFromDB(db, Domain) + + var data PageData + data.PreferredUsername = actor.PreferredUsername + 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 = []NewsItem{NewsItem{}} + + var err error + data.NewsItems[0], err = getNewsItemFromDB(db, timestamp) + + if err != nil { + w.WriteHeader(http.StatusForbidden) + w.Write([]byte("404 no path")) + return + } + + data.Title = actor.PreferredUsername + ": " + data.NewsItems[0].Title + + 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 5fcebf0..037fde2 100644 --- a/database.go +++ b/database.go @@ -1504,6 +1504,9 @@ func getNewsFromDB(db *sql.DB) []NewsItem { for rows.Next() { n := NewsItem{} err = rows.Scan(&n.Title, &n.Content, &n.Time) + if CheckError(err, "error scanning news from db") != nil { + return make([]NewsItem, 0) + } news.PushBack(n) } @@ -1519,6 +1522,27 @@ func getNewsFromDB(db *sql.DB) []NewsItem { return anews } +func getNewsItemFromDB(db *sql.DB, timestamp int) (NewsItem, error) { + var news NewsItem + query := `select title, content, time from newsItem where time=$1 limit 1` + + rows, err := db.Query(query, timestamp) + + if err != nil { + return news, err + } + + defer rows.Close() + rows.Next() + err = rows.Scan(&news.Title, &news.Content, &news.Time) + + if err != nil { + return news, err + } + + return news, nil +} + func WriteNewsToDB(db *sql.DB, news NewsItem) { query := `insert into newsItem (title, content, time) values ($1, $2, $3)` diff --git a/main.go b/main.go index 037f240..4f7f3e8 100644 --- a/main.go +++ b/main.go @@ -286,6 +286,21 @@ func main() { w.WriteHeader(http.StatusForbidden) w.Write([]byte("404 no path")) }) + + http.HandleFunc("/news/", func(w http.ResponseWriter, r *http.Request){ + timestamp := r.URL.Path[6:] + if timestamp[len(timestamp)-1:] == "/" { + timestamp = timestamp[:len(timestamp)-1] + } + + ts, err := strconv.Atoi(timestamp) + if err != nil { + w.WriteHeader(http.StatusForbidden) + w.Write([]byte("404 no path")) + } else { + NewsGet(w, r, db, ts) + } + }) http.HandleFunc("/post", func(w http.ResponseWriter, r *http.Request){ diff --git a/static/index.html b/static/index.html index de06c6c..05197c0 100644 --- a/static/index.html +++ b/static/index.html @@ -44,7 +44,7 @@ {{ range .NewsItems }}

{{.Time}} - {{.Title}}
{{.Content}}

{{unixtoreadable .Time}} - {{.Title}}
diff --git a/static/news.html b/static/news.html new file mode 100644 index 0000000..757c420 --- /dev/null +++ b/static/news.html @@ -0,0 +1,32 @@ +{{ define "header" }} +{{ .Title }} + + + + + + + + + + + + + + +{{ end }} + +{{ define "top" }}{{ end }} +{{ define "content" }} +
+ + {{ range .NewsItems }} +

{{unixtoreadable .Time}} - {{.Title}}


{{.Content}}

+ {{ end }} + +
+{{ end }} +{{ define "bottom" }}{{ end }} + +{{ define "script" }} +{{ end }} -- cgit v1.2.3 From d98441da108725a76d697e358d161c06783cb066 Mon Sep 17 00:00:00 2001 From: knotteye Date: Fri, 2 Jul 2021 07:43:54 -0500 Subject: Display most recents news item and create a table for boards --- client.go | 2 ++ static/index.html | 24 ++++++++++++++---------- 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/client.go b/client.go index 38f0d6f..074b645 100644 --- a/client.go +++ b/client.go @@ -49,6 +49,7 @@ type PageData struct { InstanceIndex []ObjectBase ReturnTo string NewsItems []NewsItem + BoardRemainer []int } type AdminPage struct { @@ -98,6 +99,7 @@ func IndexGet(w http.ResponseWriter, r *http.Request, db *sql.DB) { data.Board.Actor = actor data.Board.Post.Actor = actor.Id 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) diff --git a/static/index.html b/static/index.html index 05197c0..518c012 100644 --- a/static/index.html +++ b/static/index.html @@ -23,17 +23,17 @@

{{ .PreferredUsername }} is a federated image board based on activitypub. The current version of the code running the server is still a work in progress, expect a bumpy ride for the time being. Get the server code here.

- - Local boards - +
+
Local boards
{{ range .Boards }} -
- - + {{ end }} -
-
{{.Name}} - {{.PrefName}}
+ {{ range .BoardRemainer }} +
+ {{ end }} +
+
@@ -42,9 +42,13 @@ {{ .PreferredUsername }} news - {{ range .NewsItems }} + {{ range $i, $e := .NewsItems }} - {{unixtoreadable .Time}} - {{.Title}} + {{unixtoreadable $e.Time}} - {{$e.Title}} + {{ if eq $i 0 }} +

{{$e.Content}}

+ {{ end }} + {{ end }} -- cgit v1.2.3 From 44f5dd0279b18989dda5123f582e01af6cedb810 Mon Sep 17 00:00:00 2001 From: knotteye Date: Fri, 2 Jul 2021 10:24:38 -0500 Subject: Alert user to bad captcha instead of changing the page --- static/main.html | 33 ++++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/static/main.html b/static/main.html index 72ed321..af9947f 100644 --- a/static/main.html +++ b/static/main.html @@ -7,7 +7,38 @@ - + +