aboutsummaryrefslogtreecommitdiff
path: root/client.go
diff options
context:
space:
mode:
authorFChannel0 <77419041+FChannel0@users.noreply.github.com>2021-08-20 01:35:26 -0700
committerGitHub <noreply@github.com>2021-08-20 01:35:26 -0700
commit502558e0bdaf67ca4c012fc1983f42430b8854cd (patch)
treed66b015274b962fedb28c52c0efaaa068368f2b1 /client.go
parent21917eb2ea72fe40d70c2c991e821aace5430c23 (diff)
parenta497499817cbf72b295253e1a2bb1011d121ba28 (diff)
Merge pull request #52 from KushBlazingJudah/development
Themes
Diffstat (limited to 'client.go')
-rw-r--r--client.go126
1 files changed, 95 insertions, 31 deletions
diff --git a/client.go b/client.go
index 85a327e..09db0bf 100644
--- a/client.go
+++ b/client.go
@@ -5,6 +5,7 @@ import (
"fmt"
_ "github.com/lib/pq"
"html/template"
+ "log"
"net/http"
"regexp"
"sort"
@@ -53,6 +54,7 @@ type PageData struct {
ReturnTo string
NewsItems []NewsItem
BoardRemainer []int
+ Themes *[]string
}
type AdminPage struct {
@@ -68,6 +70,7 @@ type AdminPage struct {
IsLocal bool
PostBlacklist []PostBlacklist
AutoSubscribe bool
+ Themes *[]string
}
type Report struct {
@@ -93,11 +96,31 @@ type PostBlacklist struct {
Regex string
}
+func mod(i, j int) bool {
+ return i%j == 0
+}
+
+func sub(i, j int) int {
+ return i - j
+}
+
+func unixToReadable(u int) string {
+ return time.Unix(int64(u), 0).Format("Jan 02, 2006")
+}
+
+func timeToReadableLong(t time.Time) string {
+ return t.Format("01/02/06(Mon)15:04:05")
+}
+
+func timeToUnix(t time.Time) string {
+ return fmt.Sprint(t.Unix())
+}
+
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 },
- "sub": func(i, j int) int { return i - j },
- "unixtoreadable": func(u int) string { return time.Unix(int64(u), 0).Format("Jan 02, 2006") }}).ParseFiles("./static/main.html", "./static/index.html"))
+ "mod": mod,
+ "sub": sub,
+ "unixtoreadable": unixToReadable}).ParseFiles("./static/main.html", "./static/index.html"))
actor := GetActorFromDB(db, Domain)
@@ -122,13 +145,19 @@ func IndexGet(w http.ResponseWriter, r *http.Request, db *sql.DB) {
data.InstanceIndex = GetCollectionFromReq("https://fchan.xyz/followers").Items
data.NewsItems = getNewsFromDB(db, 3)
- t.ExecuteTemplate(w, "layout", data)
+ data.Themes = &Themes
+
+ err := t.ExecuteTemplate(w, "layout", data)
+ if err != nil {
+ // TODO: actual error handler
+ log.Printf("IndexGet: %s\n", err)
+ }
}
func NewsGet(w http.ResponseWriter, r *http.Request, db *sql.DB, timestamp int) {
t := template.Must(template.New("").Funcs(template.FuncMap{
- "sub": func(i, j int) int { return i - j },
- "unixtoreadable": func(u int) string { return time.Unix(int64(u), 0).Format("Jan 02, 2006") }}).ParseFiles("./static/main.html", "./static/news.html"))
+ "sub": mod,
+ "unixtoreadable": unixToReadable}).ParseFiles("./static/main.html", "./static/news.html"))
actor := GetActorFromDB(db, Domain)
@@ -155,14 +184,20 @@ func NewsGet(w http.ResponseWriter, r *http.Request, db *sql.DB, timestamp int)
data.Title = actor.PreferredUsername + ": " + data.NewsItems[0].Title
- t.ExecuteTemplate(w, "layout", data)
+ data.Themes = &Themes
+
+ err = t.ExecuteTemplate(w, "layout", data)
+ if err != nil {
+ // TODO: actual error handler
+ log.Printf("NewsGet: %s\n", err)
+ }
}
func AllNewsGet(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 },
- "sub": func(i, j int) int { return i - j },
- "unixtoreadable": func(u int) string { return time.Unix(int64(u), 0).Format("Jan 02, 2006") }}).ParseFiles("./static/main.html", "./static/anews.html"))
+ "mod": mod,
+ "sub": sub,
+ "unixtoreadable": unixToReadable}).ParseFiles("./static/main.html", "./static/anews.html"))
actor := GetActorFromDB(db, Domain)
@@ -179,7 +214,13 @@ func AllNewsGet(w http.ResponseWriter, r *http.Request, db *sql.DB) {
data.Board.Restricted = actor.Restricted
data.NewsItems = getNewsFromDB(db, 0)
- t.ExecuteTemplate(w, "layout", data)
+ data.Themes = &Themes
+
+ err := t.ExecuteTemplate(w, "layout", data)
+ if err != nil {
+ // TODO: actual error handler
+ log.Printf("AllNewsGet: %s\n", err)
+ }
}
func OutboxGet(w http.ResponseWriter, r *http.Request, db *sql.DB, collection Collection) {
@@ -216,13 +257,15 @@ func OutboxGet(w http.ResponseWriter, r *http.Request, db *sql.DB, collection Co
"parseReplyLink": func(actorId string, op string, id string, content string) template.HTML {
actor := FingerActor(actorId)
title := strings.ReplaceAll(ParseLinkTitle(actor.Id, op, content), `/\&lt;`, ">")
- link := "<a href=\"" + actor.Name + "/" + shortURL(actor.Outbox, op) + "#" + shortURL(actor.Outbox, id) + "\" title=\"" + title + "\">&gt;&gt;" + shortURL(actor.Outbox, id) + "</a>"
+ link := "<a href=\"" + actor.Name + "/" + shortURL(actor.Outbox, op) + "#" + shortURL(actor.Outbox, id) + "\" title=\"" + title + "\" class=\"replyLink\">&gt;&gt;" + shortURL(actor.Outbox, id) + "</a>"
return template.HTML(link)
},
"add": func(i, j int) int {
return i + j
},
- "sub": func(i, j int) int { return i - j }}).ParseFiles("./static/main.html", "./static/nposts.html", "./static/top.html", "./static/bottom.html", "./static/posts.html"))
+ "timeToReadableLong": timeToReadableLong,
+ "timeToUnix": timeToUnix,
+ "sub": sub}).ParseFiles("./static/main.html", "./static/nposts.html", "./static/top.html", "./static/bottom.html", "./static/posts.html"))
actor := collection.Actor
@@ -271,7 +314,13 @@ func OutboxGet(w http.ResponseWriter, r *http.Request, db *sql.DB, collection Co
returnData.Pages = pages
returnData.TotalPage = len(returnData.Pages) - 1
- t.ExecuteTemplate(w, "layout", returnData)
+ returnData.Themes = &Themes
+
+ err := t.ExecuteTemplate(w, "layout", returnData)
+ if err != nil {
+ // TODO: actual error handler
+ log.Printf("OutboxGet: %s\n", err)
+ }
}
func CatalogGet(w http.ResponseWriter, r *http.Request, db *sql.DB, collection Collection) {
@@ -296,7 +345,7 @@ func CatalogGet(w http.ResponseWriter, r *http.Request, db *sql.DB, collection C
}
return false
},
- "sub": func(i, j int) int { return i - j }}).ParseFiles("./static/main.html", "./static/ncatalog.html", "./static/top.html"))
+ "sub": sub}).ParseFiles("./static/main.html", "./static/ncatalog.html", "./static/top.html"))
actor := collection.Actor
@@ -326,7 +375,13 @@ func CatalogGet(w http.ResponseWriter, r *http.Request, db *sql.DB, collection C
returnData.Posts = collection.OrderedItems
- t.ExecuteTemplate(w, "layout", returnData)
+ returnData.Themes = &Themes
+
+ err := t.ExecuteTemplate(w, "layout", returnData)
+ if err != nil {
+ // TODO: actual error handler
+ log.Printf("CatalogGet: %s\n", err)
+ }
}
func ArchiveGet(w http.ResponseWriter, r *http.Request, db *sql.DB, collection Collection) {
@@ -343,8 +398,8 @@ func ArchiveGet(w http.ResponseWriter, r *http.Request, db *sql.DB, collection C
"parseAttachment": func(obj ObjectBase, catalog bool) template.HTML {
return ParseAttachment(obj, catalog)
},
- "mod": func(i, j int) bool { return i%j == 0 },
- "sub": func(i, j int) int { return i - j }}).ParseFiles("./static/main.html", "./static/archive.html", "./static/bottom.html"))
+ "mod": mod,
+ "sub": sub}).ParseFiles("./static/main.html", "./static/archive.html", "./static/bottom.html"))
actor := collection.Actor
@@ -374,7 +429,13 @@ func ArchiveGet(w http.ResponseWriter, r *http.Request, db *sql.DB, collection C
returnData.Posts = collection.OrderedItems
- t.ExecuteTemplate(w, "layout", returnData)
+ returnData.Themes = &Themes
+
+ err := t.ExecuteTemplate(w, "layout", returnData)
+ if err != nil {
+ // TODO: actual error handler
+ log.Printf("ArchiveGet: %s\n", err)
+ }
}
func PostGet(w http.ResponseWriter, r *http.Request, db *sql.DB) {
@@ -403,10 +464,12 @@ func PostGet(w http.ResponseWriter, r *http.Request, db *sql.DB) {
"parseReplyLink": func(actorId string, op string, id string, content string) template.HTML {
actor := FingerActor(actorId)
title := strings.ReplaceAll(ParseLinkTitle(actor.Id, op, content), `/\&lt;`, ">")
- link := "<a href=\"" + actor.Name + "/" + shortURL(actor.Outbox, op) + "#" + shortURL(actor.Outbox, id) + "\" title=\"" + title + "\">&gt;&gt;" + shortURL(actor.Outbox, id) + "</a>"
+ link := "<a href=\"" + actor.Name + "/" + shortURL(actor.Outbox, op) + "#" + shortURL(actor.Outbox, id) + "\" title=\"" + title + "\" class=\"replyLink\">&gt;&gt;" + shortURL(actor.Outbox, id) + "</a>"
return template.HTML(link)
},
- "sub": func(i, j int) int { return i - j }}).ParseFiles("./static/main.html", "./static/npost.html", "./static/top.html", "./static/bottom.html", "./static/posts.html"))
+ "timeToReadableLong": timeToReadableLong,
+ "timeToUnix": timeToUnix,
+ "sub": sub}).ParseFiles("./static/main.html", "./static/npost.html", "./static/top.html", "./static/bottom.html", "./static/posts.html"))
path := r.URL.Path
actor := GetActorFromPath(db, path, "/")
@@ -467,7 +530,13 @@ func PostGet(w http.ResponseWriter, r *http.Request, db *sql.DB) {
returnData.PostId = shortURL(returnData.Board.To, returnData.Posts[0].Id)
}
- t.ExecuteTemplate(w, "layout", returnData)
+ returnData.Themes = &Themes
+
+ err := t.ExecuteTemplate(w, "layout", returnData)
+ if err != nil {
+ // TODO: actual error handler
+ log.Printf("PostGet: %s\n", err)
+ }
}
func GetBoardCollection(db *sql.DB) []Board {
@@ -724,13 +793,13 @@ func GetActorsFollowPostFromId(db *sql.DB, actors []string, id string) Collectio
type ObjectBaseSortDesc []ObjectBase
func (a ObjectBaseSortDesc) Len() int { return len(a) }
-func (a ObjectBaseSortDesc) Less(i, j int) bool { return a[i].Updated > a[j].Updated }
+func (a ObjectBaseSortDesc) Less(i, j int) bool { return a[i].Updated.After(a[j].Updated) }
func (a ObjectBaseSortDesc) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
type ObjectBaseSortAsc []ObjectBase
func (a ObjectBaseSortAsc) Len() int { return len(a) }
-func (a ObjectBaseSortAsc) Less(i, j int) bool { return a[i].Published < a[j].Published }
+func (a ObjectBaseSortAsc) Less(i, j int) bool { return a[i].Published.Before(a[j].Published) }
func (a ObjectBaseSortAsc) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
type BoardSortAsc []Board
@@ -886,17 +955,12 @@ func ParseLinkComments(db *sql.DB, board Actor, op string, content string, threa
}
}
- var style string
- if board.Restricted {
- style = "color: #af0a0f;"
- }
-
//replace link with quote format
replyID, isReply := IsReplyToOP(db, op, parsedLink)
if isReply {
id := shortURL(board.Outbox, replyID)
- content = strings.Replace(content, match[i][0], "<a class=\"reply\" style=\""+style+"\" title=\""+quoteTitle+"\" href=\"/"+board.Name+"/"+shortURL(board.Outbox, op)+"#"+id+"\">&gt;&gt;"+id+""+isOP+"</a>", -1)
+ content = strings.Replace(content, match[i][0], "<a class=\"reply\" title=\""+quoteTitle+"\" href=\"/"+board.Name+"/"+shortURL(board.Outbox, op)+"#"+id+"\">&gt;&gt;"+id+""+isOP+"</a>", -1)
} else {
@@ -909,7 +973,7 @@ func ParseLinkComments(db *sql.DB, board Actor, op string, content string, threa
}
if actor.Id != "" {
- content = strings.Replace(content, match[i][0], "<a class=\"reply\" style=\""+style+"\" title=\""+quoteTitle+"\" href=\""+link+"\">&gt;&gt;"+shortURL(board.Outbox, parsedLink)+isOP+" →</a>", -1)
+ content = strings.Replace(content, match[i][0], "<a class=\"reply\" title=\""+quoteTitle+"\" href=\""+link+"\">&gt;&gt;"+shortURL(board.Outbox, parsedLink)+isOP+" →</a>", -1)
}
}
}