aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorknotteye <knotteye@airmail.cc>2021-07-02 14:20:16 -0500
committerknotteye <knotteye@airmail.cc>2021-07-02 14:20:16 -0500
commitfaae38726dd804e3246514d88f93794c23a0cdb2 (patch)
tree4671606919340098b81c398e8478ba616a0432b1
parentad5f4e9ff7e7ab0abab4a964cbf1626d0cdc3365 (diff)
show only the last 3 news items on index, link to a page with all news items
-rw-r--r--client.go23
-rw-r--r--database.go18
-rw-r--r--main.go6
-rw-r--r--static/anews.html44
-rw-r--r--static/index.html2
5 files changed, 86 insertions, 7 deletions
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>{{ .Title }}</title>
+<meta name="description" content="{{ .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: https://github.com/FChannel0.">
+
+<meta property="og:locale" content="en_US" />
+<meta property="og:type" content="website" />
+<meta property="og:url" content="{{ .Board.Domain }}">
+<meta property="og:site_name" content="{{ .Board.Actor.PreferredUsername }}" />
+
+<meta property="og:title" content="{{ .Title }}">
+<meta property="og:description" content="{{ .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: https://github.com/FChannel0.">
+
+<meta name="twitter:title" content="{{ .Title }}">
+<meta name="twitter:description" content="{{ .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: https://github.com/FChannel0.">
+<meta name="twitter:card" content="summary_large_image">
+
+{{ end }}
+
+{{ define "top" }}{{ end }}
+{{ define "content" }}
+<div style="text-align: center; max-width: 800px; margin: 0 auto;">
+ <h1>{{ .Title }}</h1>
+
+ <div style="margin-top:50px;">
+ <table align="center" style="text-align: left;">
+
+ {{ range $i, $e := .NewsItems }}
+ <tr>
+ <td><a href="/news/{{.Time}}">{{unixtoreadable $e.Time}} - {{$e.Title}}</a>
+ {{ if eq $i 0 }}
+ <br><p>{{$e.Content}}</p>
+ {{ end }}
+ </td>
+ </tr>
+ {{ end }}
+ </table>
+ </div>
+
+</div>
+{{ 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 @@
<div style="margin-top:50px;">
<table align="center" style="text-align: left;">
<th>
- <tr>{{ .PreferredUsername }} news</tr>
+ <tr><a href="/news">{{ .PreferredUsername }} news</a></tr>
</th>
{{ range $i, $e := .NewsItems }}