Skip to content

Commit

Permalink
feat: edit thread title (#91)
Browse files Browse the repository at this point in the history
Uses `/?op=1` to signal an "edit thread" render.

* feat: adjust templates for edit title
* feat: adjust db/routes for edit title
* fix: only "edit thread" on first post
  • Loading branch information
decentral1se authored Nov 22, 2024
1 parent a5837ac commit 32e4c58
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 8 deletions.
7 changes: 6 additions & 1 deletion database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -384,11 +384,16 @@ func (d DB) AddPost(content string, threadid, authorid int) (postID int) {
return
}

func (d DB) EditPost(content string, postid int) {
func (d DB) EditPost(content, title string, postid, threadid int) {
stmt := `UPDATE posts set content = ?, lastedit = ? WHERE id = ?`
edit := time.Now()
_, err := d.Exec(stmt, content, edit, postid)
util.Check(err, "edit post %d", postid)

stmt = `UPDATE threads set title = ? WHERE id = ?`
edit = time.Now()
_, err = d.Exec(stmt, title, threadid)
util.Check(err, "edit post title %d", postid)
}

func (d DB) DeletePost(postid int) error {
Expand Down
17 changes: 13 additions & 4 deletions html/edit-post.html
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
{{ template "head" . }}
<main>
<h1>{{ "PostEdit" | translate }}</h1>
<article>{{.Data.Content | markup }}
</article>
<h1> {{ if .IsOP }} Thread preview {{ else }} {{ "PostEdit" | translate }} {{ end }}</h1>
<article style="margin: 0;">
{{ if .IsOP }}
<h2> {{.Data.ThreadTitle }} </h2>
{{ end }}
{{.Data.Content | markup }}
</article>
<form method="POST">
<div class="post-container" >
{{ if .IsOP }}
<label for="title">{{ "Title" | translate }}:</label>
<input required name="title" type="text" id="title" value="{{ .Data.ThreadTitle }}">
<label for="content">{{ "Content" | translate }}:</label>
{{ end }}
<label class="visually-hidden" for="content">{{ "Content" | translate }}:</label>
<textarea required name="content" id="content" placeholder='{{ "TextareaPlaceholder" | translate }}'>{{.Data.Content}}</textarea>
<textarea required name="content" id="content" placeholder='{{ "TextareaPlaceholder" | translate }}'>{{.Data.Content}}</textarea>
<button type="submit">{{ "Save" | translate }}</button>
</div>
<div style="margin-top: 1rem;">
Expand Down
8 changes: 6 additions & 2 deletions html/thread.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,19 @@ <h1>{{ .Data.Title }}</h1>
<input type="hidden" name="thread" value="{{ $threadURL }}">
</form>
</span>
<span style="float: right; margin-right:0.5rem"><a href="/post/edit/{{ $post.ID }}">edit</a></span>
{{ if eq $index 0 }}
<span style="float: right; margin-right:0.5rem"><a href="/post/edit/{{ $post.ID }}?op=1">edit thread</a></span>
{{ else }}
<span style="float: right; margin-right:0.5rem"><a href="/post/edit/{{ $post.ID }}">edit</a></span>
{{ end }}
{{ end }}
<span class="visually-hidden">{{ "Author" | translate }}:</span>
<span><b>{{ $post.Author }}</b>
<span class="visually-hidden"> {{ "Responded" | translate }}:</span>
</span>
<a href="#{{ $post.ID }}">
<span style="margin-left: 0.5rem;">
<time title="{{ $post.Publish | formatDateTime }}" datetime="{{ $post.Publish | formatDate }}">{{ $post.Publish | formatDateRelative }}</time></span></a>
<time title="{{ $post.Publish | formatDateTime }}" datetime="{{ $post.Publish | formatDate }}">{{ $post.Publish | formatDateRelative }}</time></span></a>
{{ if $post.LastEdit.Valid }}
<span style="cursor: pointer;">
<time title="{{ "EditedAt" | translate }} {{ $post.LastEdit.Time | formatDateTime }}" datetime="{{ $post.LastEdit.Time | formatDate }}">*</time>
Expand Down
15 changes: 14 additions & 1 deletion server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ type TemplateData struct {
QuickNav bool
LoggedIn bool
IsAdmin bool
IsOP bool
HasRSS bool
LoggedInID int
ForumName string
Expand Down Expand Up @@ -911,10 +912,22 @@ func (h *RequestHandler) EditPostRoute(res http.ResponseWriter, req *http.Reques
}
if req.Method == "POST" {
content := req.PostFormValue("content")
h.db.EditPost(content, postid)
title := req.PostFormValue("title")
if title == "" {
title = post.ThreadTitle
}
h.db.EditPost(content, title, postid, post.ThreadID)
post.Content = content
post.ThreadTitle = title
}
view := TemplateData{Data: post, QuickNav: loggedIn, HasRSS: h.config.RSS.URL != "", LoggedIn: loggedIn, LoggedInID: userid}
params := req.URL.Query()
posts, err := h.db.GetThread(post.ThreadID)
if _, exists := params["op"]; exists {
if len(posts) > 0 && posts[0].ID == post.ID {
view.IsOP = true
}
}
h.renderView(res, "edit-post", view)
}

Expand Down

0 comments on commit 32e4c58

Please sign in to comment.