From f6eecc720abaefb954fda9a26aab78bb1d706ba4 Mon Sep 17 00:00:00 2001 From: FChannel <=> Date: Wed, 3 Feb 2021 23:50:10 -0800 Subject: sorted board list --- client.go | 43 ++++++++++++++++++++----------------------- main.go | 42 ++++++++++++++++-------------------------- static/admin.html | 4 ++-- static/faq.html | 6 +++++- static/main.html | 2 +- static/manage.html | 8 ++++---- static/ncatalog.html | 6 +++--- static/npost.html | 4 ++-- static/posts.html | 34 +++++++++++++++++----------------- static/rules.html | 6 +++++- 10 files changed, 75 insertions(+), 80 deletions(-) diff --git a/client.go b/client.go index 56f5812..c585209 100644 --- a/client.go +++ b/client.go @@ -15,11 +15,13 @@ import "os" var Key *string = new(string) -var Boards *[]ObjectBase = new([]ObjectBase) +var FollowingBoards []ObjectBase + +var Boards []Board type Board struct{ Name string - Actor string + Actor Actor Summary string PrefName string InReplyTo string @@ -74,23 +76,11 @@ func IndexGet(w http.ResponseWriter, r *http.Request, db *sql.DB) { t := template.Must(template.ParseFiles("./static/main.html", "./static/index.html")) actor := GetActorFromDB(db, Domain) - - var boardCollection []Board - - for _, e := range *Boards { - var board Board - boardActor := GetActor(e.Id) - board.Name = "/" + boardActor.Name + "/" - board.PrefName = boardActor.PreferredUsername - board.Location = "/" + boardActor.Name - boardCollection = append(boardCollection, board) - board.Restricted = boardActor.Restricted - } 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.Boards = boardCollection + data.Boards = Boards data.Board.Name = "" data.Key = *Key data.Board.Domain = Domain @@ -116,7 +106,7 @@ func OutboxGet(w http.ResponseWriter, r *http.Request, db *sql.DB, collection Co returnData.Board.Summary = actor.Summary returnData.Board.InReplyTo = "" returnData.Board.To = actor.Outbox - returnData.Board.Actor = actor.Id + returnData.Board.Actor.Id = actor.Id returnData.Board.ModCred, _ = GetPasswordFromSession(r) returnData.Board.Domain = Domain returnData.Board.Restricted = actor.Restricted @@ -158,7 +148,7 @@ func OutboxGet(w http.ResponseWriter, r *http.Request, db *sql.DB, collection Co DeleteTombstonePosts(&mergeCollection) sort.Sort(ObjectBaseSortDesc(mergeCollection.OrderedItems)) - returnData.Boards = GetBoardCollection(db) + returnData.Boards = Boards offset := 8 start := page * offset @@ -242,7 +232,7 @@ func CatalogGet(w http.ResponseWriter, r *http.Request, db *sql.DB, collection C returnData.Board.PrefName = actor.PreferredUsername returnData.Board.InReplyTo = "" returnData.Board.To = actor.Outbox - returnData.Board.Actor = actor.Id + returnData.Board.Actor.Id = actor.Id returnData.Board.Summary = actor.Summary returnData.Board.ModCred, _ = GetPasswordFromSession(r) returnData.Board.Domain = Domain @@ -254,7 +244,7 @@ func CatalogGet(w http.ResponseWriter, r *http.Request, db *sql.DB, collection C returnData.Title = "/" + actor.Name + "/ - " + actor.PreferredUsername - returnData.Boards = GetBoardCollection(db) + returnData.Boards = Boards returnData.Posts = mergeCollection.OrderedItems @@ -286,7 +276,7 @@ func PostGet(w http.ResponseWriter, r *http.Request, db *sql.DB){ returnData.Board.Name = actor.Name returnData.Board.PrefName = actor.PreferredUsername returnData.Board.To = actor.Outbox - returnData.Board.Actor = actor.Id + returnData.Board.Actor.Id = actor.Id returnData.Board.Summary = actor.Summary returnData.Board.ModCred, _ = GetPasswordFromSession(r) returnData.Board.Domain = Domain @@ -302,7 +292,7 @@ func PostGet(w http.ResponseWriter, r *http.Request, db *sql.DB){ returnData.Key = *Key - returnData.Boards = GetBoardCollection(db) + returnData.Boards = Boards re = regexp.MustCompile("f\\w+-\\w+") @@ -398,7 +388,7 @@ func GetRemoteActor(id string) Actor { func GetBoardCollection(db *sql.DB) []Board { var collection []Board - for _, e := range *Boards { + for _, e := range FollowingBoards { var board Board boardActor := GetActorFromDB(db, e.Id) if boardActor.Id == "" { @@ -407,8 +397,11 @@ func GetBoardCollection(db *sql.DB) []Board { board.Name = "/" + boardActor.Name + "/" board.PrefName = boardActor.PreferredUsername board.Location = "/" + boardActor.Name + board.Actor = boardActor collection = append(collection, board) } + + sort.Sort(BoardSortAsc(collection)) return collection } @@ -425,7 +418,7 @@ func WantToServe(db *sql.DB, actorName string) (Collection, bool) { return collection, true } - for _, e := range *Boards { + for _, e := range FollowingBoards { boardActor := GetActorFromDB(db, e.Id) if boardActor.Id == "" { @@ -757,3 +750,7 @@ 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) Swap(i, j int) { a[i], a[j] = a[j], a[i] } +type BoardSortAsc []Board +func (a BoardSortAsc) Len() int { return len(a) } +func (a BoardSortAsc) Less(i, j int) bool { return a[i].Name < a[j].Name } +func (a BoardSortAsc) Swap(i, j int) { a[i], a[j] = a[j], a[i] } diff --git a/main.go b/main.go index b81ee86..270977c 100644 --- a/main.go +++ b/main.go @@ -50,12 +50,12 @@ func main() { go MakeCaptchas(db, 100) - *Key = CreateClientKey() + *Key = CreateClientKey() - following := GetActorFollowingDB(db, Domain) + FollowingBoards = GetActorFollowingDB(db, Domain) + + Boards = GetBoardCollection(db) - Boards = &following - // root actor is used to follow remote feeds that are not local //name, prefname, summary, auth requirements, restricted if GetConfigValue("instancename") != "" { @@ -445,9 +445,9 @@ func main() { CheckError(err, "error with add board follow resp") - following := GetActorFollowingDB(db, Domain) - - Boards = &following + FollowingBoards = GetActorFollowingDB(db, Domain) + + Boards = GetBoardCollection(db) http.Redirect(w, r, r.Header.Get("Referer"), http.StatusSeeOther) @@ -493,12 +493,8 @@ func main() { adminData.Domain = Domain adminData.IsLocal = IsActorLocal(db, actor.Id) - var boardCollection []Board - - boardCollection = GetBoardCollection(db) - adminData.Title = "Manage /" + actor.Name + "/" - adminData.Boards = boardCollection + adminData.Boards = Boards adminData.Board.Name = actor.Name adminData.Actor = actor.Id adminData.Key = *Key @@ -532,10 +528,7 @@ func main() { adminData.Domain = Domain adminData.Board.ModCred,_ = GetPasswordFromSession(r) - var boardCollection []Board - - boardCollection = GetBoardCollection(db) - adminData.Boards = boardCollection + adminData.Boards = Boards t.ExecuteTemplate(w, "layout", adminData) } @@ -606,7 +599,7 @@ func main() { var removed bool = false item.Id = respActor.Id - for _, e := range *Boards { + for _, e := range FollowingBoards { if e.Id != item.Id { board = append(board, e) } else { @@ -618,7 +611,9 @@ func main() { board = append(board, item) } - *Boards = board + FollowingBoards = board + + Boards = GetBoardCollection(db) } http.Redirect(w, r, r.Header.Get("Referer"), http.StatusSeeOther) @@ -1902,14 +1897,9 @@ func ParseCommentForReply(comment string) string { func GetActorByName(db *sql.DB, name string) Actor { var actor Actor - for _, e := range *Boards { - boardActor := GetActorFromDB(db, e.Id) - if boardActor.Id == "" { - boardActor = GetRemoteActor(e.Id) - } - - if boardActor.Name == name { - actor = boardActor + for _, e := range Boards { + if e.Actor.Name == name { + actor = e.Actor } } diff --git a/static/admin.html b/static/admin.html index f7db70c..a4c61c1 100644 --- a/static/admin.html +++ b/static/admin.html @@ -33,10 +33,10 @@


- +
{{ if .Board.ModCred }} - {{ if eq .Board.ModCred .Board.Domain .Board.Actor }} + {{ if eq .Board.ModCred .Board.Domain .Board.Actor.Id }} [Manage Board] {{ end }} {{ end }} diff --git a/static/manage.html b/static/manage.html index 3a86f7a..06288cd 100644 --- a/static/manage.html +++ b/static/manage.html @@ -16,7 +16,7 @@ [Return] -{{ $actor := .Actor }} +{{ $actor := .Actor.Id }} {{ $board := .Board }} {{ $key := .Key }} {{ if .IsLocal }} @@ -26,7 +26,7 @@

- +