Search Apps Documentation Source Content File Folder Download Copy Actions Download

render_thread.gno

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