diff options
author | KushBlazingJudah <59340248+KushBlazingJudah@users.noreply.github.com> | 2021-11-07 00:59:12 -0300 |
---|---|---|
committer | FChannel <> | 2022-06-19 12:53:29 -0700 |
commit | 7116705959bad9244893434039a0da9b500f5043 (patch) | |
tree | 54fd00840cbcfc9907d37b3bfb376fde87f48bb1 | |
parent | 972223c992ca5aa5e5d93cff3b2ee4e30182025b (diff) |
last commit of the night
-rw-r--r-- | client.go | 84 | ||||
-rw-r--r-- | main.go | 101 | ||||
-rw-r--r-- | outboxGet.go | 2 | ||||
-rw-r--r-- | outboxPost.go | 10 | ||||
-rw-r--r-- | routes/post.go | 63 |
5 files changed, 114 insertions, 146 deletions
@@ -1,13 +1,9 @@ package main import ( - "fmt" "html/template" - "log" - "net/http" "regexp" "strings" - "time" "github.com/FChannel0/FChannel-Server/activitypub" "github.com/FChannel0/FChannel-Server/config" @@ -17,86 +13,6 @@ import ( _ "github.com/lib/pq" ) -var Key *string = new(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 CatalogGet(w http.ResponseWriter, r *http.Request, collection activitypub.Collection) error { - t := template.Must(template.New("").Funcs(template.FuncMap{ - "showArchive": func() bool { - col := GetActorCollectionDBTypeLimit(collection.Actor.Id, "Archive", 1) - - if len(col.OrderedItems) > 0 { - return true - } - return false - }, - "sub": sub}).ParseFiles("./static/main.html", "./static/ncatalog.html", "./static/top.html")) - - actor := collection.Actor - - var returnData PageData - returnData.Board.Name = actor.Name - returnData.Board.PrefName = actor.PreferredUsername - returnData.Board.InReplyTo = "" - returnData.Board.To = actor.Outbox - returnData.Board.Actor = *actor - returnData.Board.Summary = actor.Summary - returnData.Board.ModCred, _ = GetPasswordFromSession(r) - returnData.Board.Domain = config.Domain - returnData.Board.Restricted = actor.Restricted - returnData.Key = *Key - returnData.ReturnTo = "catalog" - - returnData.Board.Post.Actor = actor.Id - - var err error - returnData.Instance, err = db.GetActorFromDB(config.Domain) - if err != nil { - return err - } - - capt, err := db.GetRandomCaptcha() - if err != nil { - return err - } - returnData.Board.Captcha = config.Domain + "/" + capt - returnData.Board.CaptchaCode = util.GetCaptchaCode(returnData.Board.Captcha) - - returnData.Title = "/" + actor.Name + "/ - " + actor.PreferredUsername - - returnData.Boards = db.Boards - - returnData.Posts = collection.OrderedItems - - returnData.Themes = &Themes - returnData.ThemeCookie = getThemeCookie(ctx) - - err := t.ExecuteTemplate(w, "layout", returnData) - if err != nil { - // TODO: actual error handler - log.Printf("CatalogGet: %s\n", err) - } -} - func MediaProxy(url string) string { re := regexp.MustCompile("(.+)?" + config.Domain + "(.+)?") @@ -65,9 +65,9 @@ func main() { panic(err) } - go StartupArchive(DB) + go db.StartupArchive() - go CheckInactive(DB) + go db.CheckInactive() db.Boards, err = db.GetBoardCollection() if err != nil { @@ -120,11 +120,6 @@ func main() { app.Static("/public", "./public") app.Static("/static", "./views") - // Pass variables such as the DB - app.Use(func(c *fiber.Ctx) { - c.Locals("db", DB) - }) - /* Main actor */ @@ -163,12 +158,12 @@ func main() { app.Get("/auth", routes.AdminAuth) - app.Get("/"+*Key+"/", routes.AdminIndex) + app.Get("/"+config.Key+"/", routes.AdminIndex) - app.Get("/"+*Key+"/addboard", routes.AdminAddBoard) + app.Get("/"+config.Key+"/addboard", routes.AdminAddBoard) - app.Get("/"+*Key+"/postnews", routes.AdminPostNews) - app.Get("/"+*Key+"/newsdelete", routes.AdminNewsDelete) + app.Get("/"+config.Key+"/postnews", routes.AdminPostNews) + app.Get("/"+config.Key+"/newsdelete", routes.AdminNewsDelete) app.Get("/news", routes.NewsGet) /* @@ -216,7 +211,7 @@ func main() { actorDomain[0] = "/" + actorDomain[0] } - if !IsActorLocal(TP + "" + actorDomain[1] + "" + actorDomain[0]) { + if !IsActorLocal(config.TP + "" + actorDomain[1] + "" + actorDomain[0]) { c.Status(fiber.StatusBadRequest) return c.Send([]byte("actor not local")) } @@ -227,13 +222,13 @@ func main() { finger.Subject = "acct:" + actorDomain[0] + "@" + actorDomain[1] link.Rel = "self" link.Type = "application/activity+json" - link.Href = TP + "" + actorDomain[1] + "" + actorDomain[0] + link.Href = config.TP + "" + actorDomain[1] + "" + actorDomain[0] finger.Links = append(finger.Links, link) enc, _ := json.Marshal(finger) - c.Set("Content-Type", activitystreams) + c.Set("Content-Type", config.ActivityStreams) return c.Send(enc) }) @@ -244,10 +239,8 @@ func main() { // 404 handler app.Use(routes.NotFound) - fmt.Println("Server for " + Domain + " running on port " + Port) - - fmt.Println("Mod key: " + *Key) - PrintAdminAuth(DB) + fmt.Println("Mod key: " + config.Key) + PrintAdminAuth() app.Listen(Port) } @@ -288,10 +281,10 @@ func CreateNewActor(board string, prefName string, summary string, authReq []str var path string if board == "" { - path = Domain + path = config.Domain actor.Name = "main" } else { - path = Domain + "/" + board + path = config.Domain + "/" + board actor.Name = board } @@ -429,7 +422,7 @@ func CreatePreviewObject(obj activitypub.ObjectBase) *activitypub.NestedObjectBa nPreview.Type = "Preview" nPreview.Name = obj.Name - nPreview.Href = Domain + "" + href + nPreview.Href = config.Domain + "" + href nPreview.MediaType = obj.MediaType nPreview.Size = obj.Size nPreview.Published = obj.Published @@ -466,7 +459,7 @@ func CreateAttachmentObject(file multipart.File, header *multipart.FileHeader) ( image.Type = "Attachment" image.Name = filename - image.Href = Domain + "/" + tempFile.Name() + image.Href = config.Domain + "/" + tempFile.Name() image.MediaType = contentType image.Size = size image.Published = time.Now().UTC() @@ -488,7 +481,7 @@ func ParseCommentForReplies(comment string, op string) []activitypub.ObjectBase str = strings.Replace(str, "www.", "", 1) str = strings.Replace(str, "http://", "", 1) str = strings.Replace(str, "https://", "", 1) - str = TP + "" + str + str = config.TP + "" + str _, isReply := IsReplyToOP(op, str) if !IsInStringArray(links, str) && isReply { links = append(links, str) @@ -603,7 +596,7 @@ func GetActorReported(w http.ResponseWriter, r *http.Request, db *sql.DB, id str following.Items = GetActorReportedDB(id) enc, _ := json.MarshalIndent(following, "", "\t") - w.Header().Set("Content-Type", activitystreams) + w.Header().Set("Content-Type", config.ActivityStreams) w.Write(enc) } @@ -614,7 +607,7 @@ func GetCollectionFromID(id string) activitypub.Collection { CheckError(err, "could not get collection from id req") - req.Header.Set("Accept", activitystreams) + req.Header.Set("Accept", config.ActivityStreams) resp, err := RouteProxy(req) @@ -638,11 +631,11 @@ func GetCollectionFromID(id string) activitypub.Collection { } func PrintAdminAuth(db *sql.DB) { - query := fmt.Sprintf("select identifier, code from boardaccess where board='%s' and type='admin'", Domain) + query := fmt.Sprintf("select identifier, code from boardaccess where board='%s' and type='admin'", config.Domain) rows, err := db.Query(query) - CheckError(err, "Error getting Domain auth") + CheckError(err, "Error getting config.Domain auth") var code string var identifier string @@ -663,12 +656,12 @@ func IsInStringArray(array []string, value string) bool { } func GetUniqueFilename(_type string) string { - id := RandomID(8) + id := util.RandomID(8) file := "/public/" + id + "." + _type for true { if _, err := os.Stat("." + file); err == nil { - id = RandomID(8) + id = util.RandomID(8) file = "/public/" + id + "." + _type } else { return "/public/" + id + "." + _type @@ -770,7 +763,7 @@ func ResizeAttachmentToPreview(db *sql.DB) { nPreview.Type = "Preview" nPreview.Id = fmt.Sprintf("%s/%s", actor, CreateUniqueID(actor)) nPreview.Name = name - nPreview.Href = Domain + "" + nHref + nPreview.Href = config.Domain + "" + nHref nPreview.MediaType = mediatype nPreview.Size = int64(size) nPreview.Published = published @@ -838,7 +831,7 @@ func GetActorCollectionReq(r *http.Request, collection string) activitypub.Colle _, pass := GetPasswordFromSession(r) - req.Header.Set("Accept", activitystreams) + req.Header.Set("Accept", config.ActivityStreams) req.Header.Set("Authorization", "Basic "+pass) @@ -984,17 +977,29 @@ func HasValidation(w http.ResponseWriter, r *http.Request, actor activitypub.Act } func TemplateFunctions(engine *html.Engine) { - engine.AddFunc( - "mod", mod, - ) + engine.AddFunc("mod", func(i, j int) bool { + return i%j == 0 + }) - engine.AddFunc( - "sub", sub, - ) + engine.AddFunc("sub", func(i, j int) int { + return i - j + }) - engine.AddFunc( - "unixtoreadable", unixToReadable, - ) + engine.AddFunc("add", func(i, j int) int { + return i + j + }) + + engine.AddFunc("unixtoreadable", func(u int) string { + return time.Unix(int64(u), 0).Format("Jan 02, 2006") + }) + + engine.AddFunc("timeToReadableLong", func(t time.Time) string { + return t.Format("01/02/06(Mon)15:04:05") + }) + + engine.AddFunc("timeToUnix", func(t time.Time) string { + return fmt.Sprint(t.Unix()) + }) engine.AddFunc("proxy", MediaProxy) @@ -1018,22 +1023,6 @@ func TemplateFunctions(engine *html.Engine) { return template.HTML(link) }) - engine.AddFunc("add", func(i, j int) int { - return i + j - }) - - engine.AddFunc( - "timeToReadableLong", timeToReadableLong, - ) - - engine.AddFunc( - "timeToUnix", timeToUnix, - ) - - engine.AddFunc( - "sub", sub, - ) - engine.AddFunc("shortExcerpt", func(post activitypub.ObjectBase) string { var returnString string diff --git a/outboxGet.go b/outboxGet.go index cde1551..576a98b 100644 --- a/outboxGet.go +++ b/outboxGet.go @@ -7,8 +7,8 @@ import ( "encoding/json" "github.com/FChannel0/FChannel-Server/activitypub" + "github.com/FChannel0/FChannel-Server/db" _ "github.com/lib/pq" - "golang.org/x/perf/storage/db" ) func GetActorOutbox(w http.ResponseWriter, r *http.Request, db *sql.DB) { diff --git a/outboxPost.go b/outboxPost.go index 7ab6547..3c7936b 100644 --- a/outboxPost.go +++ b/outboxPost.go @@ -13,13 +13,13 @@ import ( "strings" "github.com/FChannel0/FChannel-Server/activitypub" + "github.com/FChannel0/FChannel-Server/db" _ "github.com/lib/pq" - "golang.org/x/perf/storage/db" ) func ParseOutboxRequest(w http.ResponseWriter, r *http.Request, db *sql.DB) { - var activity Activity + var activity activitypub.Activity actor := GetActorFromPath(r.URL.Path, "/") contentType := GetContentType(r.Header.Get("content-type")) @@ -107,7 +107,7 @@ func ParseOutboxRequest(w http.ResponseWriter, r *http.Request, db *sql.DB) { validActor = (activity.Object.Actor != "") validLocalActor = (activity.Actor.Id == actor.Id) - var rActivity Activity + var rActivity activitypub.Activity if validActor && validLocalActor { rActivity = AcceptFollow(activity) rActivity = SetActorFollowingDB(rActivity) @@ -138,8 +138,8 @@ func ParseOutboxRequest(w http.ResponseWriter, r *http.Request, db *sql.DB) { actor := CreateNewBoardDB(*CreateNewActor(name, prefname, summary, authReq, restricted)) if actor.Id != "" { - var board []ObjectBase - var item ObjectBase + var board []activitypub.ObjectBase + var item activitypub.ObjectBase var removed bool = false item.Id = actor.Id diff --git a/routes/post.go b/routes/post.go index 41706c0..ace9c6b 100644 --- a/routes/post.go +++ b/routes/post.go @@ -3,6 +3,7 @@ package routes import ( "regexp" + "github.com/FChannel0/FChannel-Server/activitypub" "github.com/FChannel0/FChannel-Server/config" "github.com/FChannel0/FChannel-Server/db" "github.com/FChannel0/FChannel-Server/util" @@ -102,3 +103,65 @@ func PostGet(ctx *fiber.Ctx) error { "page": returnData, }, "layouts/main") } + +func CatalogGet(ctx *fiber.Ctx) error { + // TODO: + collection := ctx.Locals("collection").(activitypub.Collection) + + // TODO: implement this in template functions + // "showArchive": func() bool { + // col, err := db.GetActorCollectionDBTypeLimit(collection.Actor.Id, "Archive", 1) + // if err != nil { + // // TODO: figure out what to do here + // panic(err) + // } + // + // if len(col.OrderedItems) > 0 { + // return true + // } + // return false + //}, + + actor := collection.Actor + + var returnData PageData + returnData.Board.Name = actor.Name + returnData.Board.PrefName = actor.PreferredUsername + returnData.Board.InReplyTo = "" + returnData.Board.To = actor.Outbox + returnData.Board.Actor = *actor + returnData.Board.Summary = actor.Summary + returnData.Board.ModCred, _ = getPassword(ctx) + returnData.Board.Domain = config.Domain + returnData.Board.Restricted = actor.Restricted + returnData.Key = config.Key + returnData.ReturnTo = "catalog" + + returnData.Board.Post.Actor = actor.Id + + var err error + returnData.Instance, err = db.GetActorFromDB(config.Domain) + if err != nil { + return err + } + + capt, err := db.GetRandomCaptcha() + if err != nil { + return err + } + returnData.Board.Captcha = config.Domain + "/" + capt + returnData.Board.CaptchaCode = util.GetCaptchaCode(returnData.Board.Captcha) + + returnData.Title = "/" + actor.Name + "/ - " + actor.PreferredUsername + + returnData.Boards = db.Boards + + returnData.Posts = collection.OrderedItems + + returnData.Themes = &config.Themes + returnData.ThemeCookie = getThemeCookie(ctx) + + return ctx.Render("ncatalog", fiber.Map{ + "page": returnData, + }, "layouts/main") +} |