Search Apps Documentation Source Content File Folder Download Copy Actions Download

render_thread.gno

1.35 Kb · 59 lines
 1package boards2
 2
 3import (
 4	"strconv"
 5	"strings"
 6
 7	"gno.land/p/gnoland/boards"
 8	"gno.land/p/moul/md"
 9	"gno.land/p/nt/mux"
10)
11
12func renderThread(res *mux.ResponseWriter, req *mux.Request) {
13	name := req.GetVar("board")
14	board, found := gBoards.GetByName(name)
15	if !found {
16		res.Write("Board does not exist: " + name)
17		return
18	}
19
20	rawID := req.GetVar("thread")
21	tID, err := strconv.Atoi(rawID)
22	if err != nil {
23		res.Write("Invalid thread ID: " + rawID)
24		return
25	}
26
27	thread, found := board.Threads.Get(boards.ID(tID))
28	if !found {
29		res.Write("Thread does not exist with ID: " + rawID)
30		return
31	}
32
33	if thread.Hidden {
34		res.Write("Thread with ID: " + rawID + " has been flagged as inappropriate")
35		return
36	}
37
38	res.Write(renderPost(thread, req.RawPath, "", 5))
39}
40
41func renderThreadSummary(thread *boards.Post) string {
42	var (
43		b           strings.Builder
44		postURI     = makeThreadURI(thread)
45		summary     = summaryOf(thread.Title, 80)
46		creatorLink = userLink(thread.Creator)
47		date        = thread.CreatedAt.Format(dateFormat)
48	)
49
50	b.WriteString(md.Bold("≡ "+md.Link(summary, postURI)) + "  \n")
51	b.WriteString("Created by " + creatorLink + " on " + date + "  \n")
52
53	status := []string{
54		strconv.Itoa(thread.Replies.Size()) + " replies",
55		strconv.Itoa(thread.Reposts.Size()) + " reposts",
56	}
57	b.WriteString(md.Bold(strings.Join(status, " • ")) + "\n")
58	return b.String()
59}