mirror of https://github.com/jlelse/GoBlog
Allow to edit comments (#42)
parent
8098e77470
commit
bef19c61fb
@ -1,3 +1,4 @@
|
||||
|
||||
# GoBlog
|
||||
|
||||
How to install and run (and other useful information about) GoBlog is explained in the [docs](https://docs.goblog.app/).
|
@ -0,0 +1,48 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"path"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
const commentEditSubPath = "/edit"
|
||||
|
||||
func (a *goBlog) serveCommentsEditor(w http.ResponseWriter, r *http.Request) {
|
||||
id, err := strconv.Atoi(r.FormValue("id"))
|
||||
if err != nil {
|
||||
a.serveError(w, r, "id missing or wrong format", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
comments, err := a.db.getComments(&commentsRequestConfig{id: id})
|
||||
if err != nil {
|
||||
a.serveError(w, r, "failed to query comments from database", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
if len(comments) < 1 {
|
||||
a.serve404(w, r)
|
||||
return
|
||||
}
|
||||
comment := comments[0]
|
||||
blog, bc := a.getBlog(r)
|
||||
if r.Method == http.MethodPost {
|
||||
name := r.FormValue("name")
|
||||
website := r.FormValue("website")
|
||||
commentText := r.FormValue("comment")
|
||||
if err := a.db.updateComment(id, commentText, name, website); err != nil {
|
||||
a.serveError(w, r, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
a.cache.purge()
|
||||
// Resend webmention
|
||||
commentAddress := bc.getRelativePath(path.Join(commentPath, strconv.Itoa(id)))
|
||||
_ = a.createWebmention(a.getFullAddress(commentAddress), a.getFullAddress(comment.Target))
|
||||
// Redirect to comment
|
||||
http.Redirect(w, r, commentAddress, http.StatusFound)
|
||||
return
|
||||
}
|
||||
a.render(w, r, a.renderCommentEditor, &renderData{
|
||||
Data: comment,
|
||||
BlogString: blog,
|
||||
})
|
||||
}
|
@ -0,0 +1,70 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/carlmjohnson/requests"
|
||||
"github.com/spf13/cast"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func Test_commentsEditor(t *testing.T) {
|
||||
|
||||
app := &goBlog{
|
||||
cfg: createDefaultTestConfig(t),
|
||||
}
|
||||
app.cfg.Server.PublicAddress = "https://example.com"
|
||||
|
||||
err := app.initConfig(false)
|
||||
require.NoError(t, err)
|
||||
err = app.initCache()
|
||||
require.NoError(t, err)
|
||||
app.initMarkdown()
|
||||
app.initSessions()
|
||||
|
||||
bc := app.cfg.Blogs[app.cfg.DefaultBlog]
|
||||
|
||||
addr, _, err := app.createComment(bc, "https://example.com/abc", "Test", "Name", "https://example.org", "")
|
||||
require.NoError(t, err)
|
||||
|
||||
splittedAddr := strings.Split(addr, "/")
|
||||
id := cast.ToInt(splittedAddr[len(splittedAddr)-1])
|
||||
|
||||
comments, err := app.db.getComments(&commentsRequestConfig{id: id})
|
||||
require.NoError(t, err)
|
||||
require.Len(t, comments, 1)
|
||||
|
||||
comment := comments[0]
|
||||
|
||||
assert.Equal(t, "/abc", comment.Target)
|
||||
assert.Equal(t, "Test", comment.Comment)
|
||||
assert.Equal(t, "Name", comment.Name)
|
||||
assert.Equal(t, "https://example.org", comment.Website)
|
||||
|
||||
handlerClient := newHandlerClient(http.HandlerFunc(app.serveCommentsEditor))
|
||||
|
||||
requests.URL("https://example.com/comment/edit").
|
||||
Method(http.MethodPost).
|
||||
ParamInt("id", id).
|
||||
Param("name", "Edited name").
|
||||
Param("comment", "Edited comment").
|
||||
Param("website", "").
|
||||
Client(handlerClient).
|
||||
Fetch(context.Background())
|
||||
|
||||
comments, err = app.db.getComments(&commentsRequestConfig{id: id})
|
||||
require.NoError(t, err)
|
||||
require.Len(t, comments, 1)
|
||||
|
||||
comment = comments[0]
|
||||
|
||||
assert.Equal(t, "/abc", comment.Target)
|
||||
assert.Equal(t, "Edited comment", comment.Comment)
|
||||
assert.Equal(t, "Edited name", comment.Name)
|
||||
assert.Equal(t, "", comment.Website)
|
||||
|
||||
}
|
Loading…
Reference in New Issue