Search Apps Documentation Source Content File Folder Download Copy Actions Download

render_thread.gno

5.46 Kb · 234 lines
  1package boards2
  2
  3import (
  4	"strconv"
  5	"strings"
  6
  7	"gno.land/p/gnoland/boards"
  8	"gno.land/p/jeronimoalbi/mdform"
  9	"gno.land/p/leon/svgbtn"
 10	"gno.land/p/moul/md"
 11	"gno.land/p/nt/mux/v0"
 12	"gno.land/p/nt/ufmt/v0"
 13)
 14
 15func renderThread(res *mux.ResponseWriter, req *mux.Request) {
 16	name := req.GetVar("board")
 17	board, found := gBoards.GetByName(name)
 18	if !found {
 19		res.Write("Board not found")
 20		return
 21	}
 22
 23	rawID := req.GetVar("thread")
 24	threadID, err := strconv.Atoi(rawID)
 25	if err != nil {
 26		res.Write("Invalid thread ID: " + rawID)
 27		return
 28	}
 29
 30	thread, found := getThread(board, boards.ID(threadID))
 31	if !found {
 32		res.Write("Thread not found")
 33		return
 34	}
 35
 36	if thread.Hidden {
 37		link := md.Link("inappropriate", makeFlaggingReasonsURI(thread))
 38		res.Write("⚠ Thread has been flagged as " + link)
 39		return
 40	}
 41
 42	res.Write(md.H1(md.Link("Boards", gRealmPath) + " › " + md.Link(board.Name, makeBoardURI(board))))
 43	res.Write(renderPost(thread, req.RawPath, "", 5))
 44}
 45
 46func renderThreadSummary(thread *boards.Post) string {
 47	var (
 48		b           strings.Builder
 49		postURI     = makeThreadURI(thread)
 50		summary     = summaryOf(thread.Title, 80)
 51		creatorLink = userLink(thread.Creator)
 52		roleBadge   = getRoleBadge(thread)
 53		date        = thread.CreatedAt.Format(dateFormat)
 54	)
 55
 56	if boards.IsRepost(thread) {
 57		summary += ``
 58		postURI += ` "This is a thread repost"`
 59	}
 60
 61	b.WriteString(md.H6(md.Link(summary, postURI)))
 62	b.WriteString("Created by " + creatorLink + roleBadge + " on " + date + "  \n")
 63
 64	status := []string{
 65		strconv.Itoa(thread.Replies.Size()) + " replies",
 66		strconv.Itoa(thread.Reposts.Size()) + " reposts",
 67	}
 68	b.WriteString(md.Bold(strings.Join(status, " • ")) + "\n")
 69	return b.String()
 70}
 71
 72func renderCreateThread(res *mux.ResponseWriter, req *mux.Request) {
 73	name := req.GetVar("board")
 74	board, found := gBoards.GetByName(name)
 75	if !found {
 76		res.Write("Board not found")
 77		return
 78	}
 79
 80	form := mdform.New("exec", "CreateThread")
 81	form.Input(
 82		"boardID",
 83		"placeholder", "Board ID",
 84		"value", board.ID.String(),
 85		"readonly", "true",
 86	)
 87	form.Input(
 88		"title",
 89		"placeholder", "Title",
 90		"required", "true",
 91	)
 92	form.Textarea(
 93		"body",
 94		"placeholder", "Content",
 95		"rows", "10",
 96		"required", "true",
 97	)
 98
 99	res.Write(md.H1(board.Name + ": Create Thread"))
100	res.Write(md.Link("← Back to board", makeBoardURI(board)) + "\n\n")
101	res.Write(
102		md.Paragraph(
103			ufmt.Sprintf("Thread will be created in the board: %s", md.Link(board.Name, makeBoardURI(board))),
104		),
105	)
106	res.Write(form.String())
107	res.Write("\n\n**Done?** " + svgbtn.ButtonWithRadius(136, 32, 4, "#E2E2E2", "#54595D", "Return to board", makeBoardURI(board)) + "\n")
108}
109
110func renderEditThread(res *mux.ResponseWriter, req *mux.Request) {
111	name := req.GetVar("board")
112	board, found := gBoards.GetByName(name)
113	if !found {
114		res.Write("Board not found")
115		return
116	}
117
118	rawID := req.GetVar("thread")
119	threadID, err := strconv.Atoi(rawID)
120	if err != nil {
121		res.Write("Invalid thread ID: " + rawID)
122		return
123	}
124
125	thread, found := getThread(board, boards.ID(threadID))
126	if !found {
127		res.Write("Thread not found")
128		return
129	}
130
131	form := mdform.New("exec", "EditThread")
132	form.Input(
133		"boardID",
134		"placeholder", "Board ID",
135		"value", board.ID.String(),
136		"readonly", "true",
137	)
138	form.Input(
139		"threadID",
140		"placeholder", "Thread ID",
141		"value", thread.ID.String(),
142		"readonly", "true",
143	)
144	form.Input(
145		"title",
146		"placeholder", "Title",
147		"value", thread.Title,
148		"required", "true",
149	)
150	form.Textarea(
151		"body",
152		"placeholder", "Content",
153		"rows", "10",
154		"value", thread.Body,
155		"required", "true",
156	)
157
158	res.Write(md.H1(board.Name + ": Edit Thread"))
159	res.Write(md.Link("← Back to thread", makeThreadURI(thread)) + "\n\n")
160	res.Write(
161		md.Paragraph("Editing " + md.Link(thread.Title, makeThreadURI(thread))),
162	)
163	res.Write(form.String())
164	res.Write("\n\n**Done?** " + svgbtn.ButtonWithRadius(136, 32, 4, "#E2E2E2", "#54595D", "Return to thread", makeThreadURI(thread)) + "\n")
165}
166
167func renderRepostThread(res *mux.ResponseWriter, req *mux.Request) {
168	name := req.GetVar("board")
169	board, found := gBoards.GetByName(name)
170	if !found {
171		res.Write("Board not found")
172		return
173	}
174
175	rawID := req.GetVar("thread")
176	threadID, err := strconv.Atoi(rawID)
177	if err != nil {
178		res.Write("Invalid thread ID: " + rawID)
179		return
180	}
181
182	thread, found := getThread(board, boards.ID(threadID))
183	if !found {
184		res.Write("Thread not found")
185		return
186	}
187
188	form := mdform.New("exec", "CreateRepost")
189	form.Input(
190		"boardID",
191		"placeholder", "Board ID",
192		"value", board.ID.String(),
193		"readonly", "true",
194	)
195	form.Input(
196		"threadID",
197		"placeholder", "Thread ID",
198		"value", thread.ID.String(),
199		"readonly", "true",
200	)
201	form.Input(
202		"destinationBoardID",
203		"type", mdform.InputTypeNumber,
204		"placeholder", "Board ID where to repost",
205		"required", "true",
206	)
207	form.Input(
208		"title",
209		"value", thread.Title,
210		"placeholder", "Title",
211		"required", "true",
212	)
213	form.Textarea(
214		"body",
215		"placeholder", "Content",
216		"rows", "10",
217	)
218
219	res.Write(md.H1(board.Name + ": Repost Thread"))
220	res.Write(md.Link("← Back to thread", makeThreadURI(thread)) + "\n\n")
221	res.Write(
222		md.Paragraph(
223			"Threads can be reposted to other open boards or boards where you are a member " +
224				"and are allowed to create new threads.",
225		),
226	)
227	res.Write(
228		md.Paragraph(
229			ufmt.Sprintf("Reposting the thread: %s.", md.Link(thread.Title, makeThreadURI(thread))),
230		),
231	)
232	res.Write(form.String())
233	res.Write("\n\n**Done?** " + svgbtn.ButtonWithRadius(136, 32, 4, "#E2E2E2", "#54595D", "Return to thread", makeThreadURI(thread)) + "\n")
234}