From b413f9a7d72284a05d8802f06c51e4a23771d95d Mon Sep 17 00:00:00 2001 From: FChannel <> Date: Fri, 30 Jul 2021 17:10:02 -0700 Subject: moved parseContent to template to cut down on unnecessary loops --- client.go | 22 ++++++---------------- static/posts.html | 41 +++++++++++++++++++++-------------------- 2 files changed, 27 insertions(+), 36 deletions(-) diff --git a/client.go b/client.go index bd5c2fa..e0bee0e 100644 --- a/client.go +++ b/client.go @@ -193,6 +193,9 @@ func OutboxGet(w http.ResponseWriter, r *http.Request, db *sql.DB, collection Co "parseAttachment": func(obj ObjectBase, catalog bool) template.HTML { return ParseAttachment(obj, catalog) }, + "parseContent": func(board Actor, op string, content string, thread ObjectBase) template.HTML { + return ParseContent(db, board, op, content, thread) + }, "parseReplyLink": func(actorId string, op string, id string, content string) template.HTML { actor := FingerActor(actorId) title := strings.ReplaceAll(ParseLinkTitle(actor.Id, op, content), `/\<`, ">") @@ -237,14 +240,6 @@ func OutboxGet(w http.ResponseWriter, r *http.Request, db *sql.DB, collection Co returnData.Boards = Boards returnData.Posts = collection.OrderedItems - for i, e := range returnData.Posts { - returnData.Posts[i].ContentHTML = ParseContent(db, returnData.Board.Actor, e.Id, e.Content, e) - - for j, k := range e.Replies.OrderedItems { - returnData.Posts[i].Replies.OrderedItems[j].ContentHTML = ParseContent(db, returnData.Board.Actor, e.Id, k.Content, e) - } - } - var offset = 15 var pages []int pageLimit := (float64(collection.TotalItems) / float64(offset)) @@ -317,6 +312,9 @@ func PostGet(w http.ResponseWriter, r *http.Request, db *sql.DB){ "parseAttachment": func(obj ObjectBase, catalog bool) template.HTML { return ParseAttachment(obj, catalog) }, + "parseContent": func(board Actor, op string, content string, thread ObjectBase) template.HTML { + return ParseContent(db, board, op, content, thread) + }, "parseReplyLink": func(actorId string, op string, id string, content string) template.HTML { actor := FingerActor(actorId) title := strings.ReplaceAll(ParseLinkTitle(actor.Id, op, content), `/\<`, ">") @@ -382,14 +380,6 @@ func PostGet(w http.ResponseWriter, r *http.Request, db *sql.DB){ if len(returnData.Posts) > 0 { returnData.PostId = shortURL(returnData.Board.To, returnData.Posts[0].Id) - - for i, e := range returnData.Posts { - returnData.Posts[i].ContentHTML = ParseContent(db, returnData.Board.Actor, e.Id, e.Content, e) - - for j, k := range e.Replies.OrderedItems { - returnData.Posts[i].Replies.OrderedItems[j].ContentHTML = ParseContent(db, returnData.Board.Actor, e.Id, k.Content, e) - } - } } t.ExecuteTemplate(w, "layout", returnData) diff --git a/static/posts.html b/static/posts.html index 67a755c..928a51d 100644 --- a/static/posts.html +++ b/static/posts.html @@ -2,6 +2,7 @@ {{ $board := .Board }} {{ $len := len .Posts }} {{ range .Posts }} +{{ $thread := . }} {{ $opId := .Id }} {{ if eq $board.InReplyTo "" }}
@@ -10,16 +11,16 @@
{{ if eq $board.ModCred $board.Domain $board.Actor.Id }} [Delete Post] - {{ end }} + {{ end }} {{ if .Attachment }} {{ if eq $board.ModCred $board.Domain $board.Actor.Id }} - [Ban Media] + [Ban Media] [Delete Attachment] [Mark Sensitive] - {{ end }} + {{ end }} File: {{ (index .Attachment 0).Name }}({{ (index .Attachment 0).Size }}) - +
{{ parseAttachment . false }}
{{ end }} {{ .Name }}{{ if .AttributedTo }} {{.AttributedTo }} {{ else }} Anonymous {{ end }} {{ .TripCode }} {{ .Published }} No. {{ short $board.Actor.Outbox .Id }} {{ if ne .Type "Tombstone" }}[Report]{{ end }} -

{{.ContentHTML}}

+

{{ parseContent $board.Actor $opId .Content $thread }}

{{ if .Replies }} {{ $replies := .Replies }} {{ if gt $replies.TotalItems 5 }} {{ if gt $len 1 }} - {{ $replies.TotalItems }} replies{{ if gt $replies.TotalImgs 0}} and {{ $replies.TotalImgs }} images{{ end }}, Click here to view all. + {{ $replies.TotalItems }} replies{{ if gt $replies.TotalImgs 0}} and {{ $replies.TotalImgs }} images{{ end }}, Click here to view all. {{ end }} {{ end }} {{ range $replies.OrderedItems }} -
+
>>
- {{ if eq $board.ModCred $board.Domain $board.Actor.Id }} + {{ if eq $board.ModCred $board.Domain $board.Actor.Id }} [Delete Post] - {{ end }} + {{ end }} {{ if .Attachment }} {{ if eq $board.ModCred $board.Domain $board.Actor.Id }} - [Ban Media] + [Ban Media] [Delete Attachment] [Mark Sensitive] {{ end }} File {{ (index .Attachment 0).Name }} ({{ (index .Attachment 0).Size }}) - +
{{ parseAttachment . false }}
+ media.style = "display: none;" + } + {{ end }} {{ .Name }}{{ if .AttributedTo }} {{.AttributedTo }} {{ else }} Anonymous {{ end }} {{ .TripCode }} {{ .Published }} No. {{ short $board.Actor.Outbox .Id }} {{ if ne .Type "Tombstone" }}[Report]{{ end }} {{ $parentId := .Id }} @@ -103,7 +104,7 @@ {{ parseReplyLink $board.Actor.Id $opId .Id .Content }} {{ end }} {{ end }} -

{{.ContentHTML}}

+

{{ parseContent $board.Actor $opId .Content $thread }}

@@ -112,13 +113,13 @@ document.getElementById("{{ .Id }}-size").innerText = " (" + convertSize({{ (index .Attachment 0).Size }}) + ")"; document.getElementById("{{ .Id }}-img").innerText = shortImg("{{ (index .Attachment 0).Name }}"); {{ end }} - + {{ end }} {{ end }}