bookmark and reply in editor

This commit is contained in:
Niko Abeler 2022-12-04 15:45:51 +01:00
parent 0d5e6599cf
commit 65fa4f6fb8
5 changed files with 102 additions and 4 deletions

View File

@ -201,15 +201,38 @@ func userEditorPostHandler(repo *owl.Repository) func(http.ResponseWriter, *http
content := r.Form.Get("content") content := r.Form.Get("content")
draft := r.Form.Get("draft") draft := r.Form.Get("draft")
// conditional values
reply_url := r.Form.Get("reply_url")
bookmark_url := r.Form.Get("bookmark_url")
// validate form values // validate form values
if post_type == "article" && title == "" {
userEditorGetHandler(repo)(w, r, ps)
return
}
if post_type == "" { if post_type == "" {
userEditorGetHandler(repo)(w, r, ps) userEditorGetHandler(repo)(w, r, ps)
return return
} }
if post_type == "article" && title == "" {
userEditorGetHandler(repo)(w, r, ps)
return
}
if post_type == "reply" && reply_url == "" {
html, _ := owl.RenderUserError(user, owl.ErrorMessage{
Error: "Missing URL",
Message: "You must provide a URL to reply to",
})
w.Write([]byte(html))
return
}
if post_type == "bookmark" && bookmark_url == "" {
html, _ := owl.RenderUserError(user, owl.ErrorMessage{
Error: "Missing URL",
Message: "You must provide a URL to bookmark",
})
w.Write([]byte(html))
return
}
// TODO: scrape reply_url for title and description
// TODO: scrape bookmark_url for title and description
// create post // create post
post, err := user.CreateNewPostFull(owl.PostMeta{ post, err := user.CreateNewPostFull(owl.PostMeta{
@ -218,6 +241,12 @@ func userEditorPostHandler(repo *owl.Repository) func(http.ResponseWriter, *http
Description: description, Description: description,
Draft: draft == "on", Draft: draft == "on",
Date: time.Now(), Date: time.Now(),
Reply: owl.Reply{
Url: reply_url,
},
Bookmark: owl.Bookmark{
Url: bookmark_url,
},
}, content) }, content)
if err != nil { if err != nil {

View File

@ -29,3 +29,45 @@
<input type="submit" value="Create Note" /> <input type="submit" value="Create Note" />
</form> </form>
</details> </details>
<details>
<summary>Write Reply</summary>
<form action="" method="post">
<h2>Create New Reply</h2>
<input type="hidden" name="csrf_token" value="{{.CsrfToken}}">
<input type="hidden" name="type" value="reply">
<label for="reply_url">Reply To</label>
<input type="text" name="reply_url" placeholder="URL" />
<label for="title">Title</label>
<input type="text" name="title" placeholder="Title" />
<label for="content">Content</label>
<textarea name="content" placeholder="Content" rows="8"></textarea>
<br><br>
<input type="submit" value="Create Reply" />
</form>
</details>
<details>
<summary>Bookmark</summary>
<form action="" method="post">
<h2>Create Bookmark</h2>
<input type="hidden" name="csrf_token" value="{{.CsrfToken}}">
<input type="hidden" name="type" value="bookmark">
<label for="bookmark_url">Bookmark</label>
<input type="text" name="bookmark_url" placeholder="URL" />
<label for="title">Title</label>
<input type="text" name="title" placeholder="Title" />
<label for="content">Content</label>
<textarea name="content" placeholder="Content" rows="8"></textarea>
<br><br>
<input type="submit" value="Create Bookmark" />
</form>
</details>

View File

@ -33,6 +33,18 @@
</p> </p>
{{ end }} {{ end }}
{{ if .Post.Meta.Bookmark.Url }}
<p style="font-style: italic;filter: opacity(80%);">
Bookmark: <a class="u-bookmark-of h-cite" href="{{.Post.Meta.Bookmark.Url}}">
{{ if .Post.Meta.Bookmark.Text }}
{{.Post.Meta.Bookmark.Text}}
{{ else }}
{{.Post.Meta.Bookmark.Url}}
{{ end }}
</a>
</p>
{{ end }}
<div class="e-content"> <div class="e-content">
{{.Content}} {{.Content}}
</div> </div>

View File

@ -30,6 +30,10 @@ type Reply struct {
Url string `yaml:"url"` Url string `yaml:"url"`
Text string `yaml:"text"` Text string `yaml:"text"`
} }
type Bookmark struct {
Url string `yaml:"url"`
Text string `yaml:"text"`
}
type PostMeta struct { type PostMeta struct {
Type string `yaml:"type"` Type string `yaml:"type"`
@ -39,6 +43,7 @@ type PostMeta struct {
Date time.Time `yaml:"date"` Date time.Time `yaml:"date"`
Draft bool `yaml:"draft"` Draft bool `yaml:"draft"`
Reply Reply `yaml:"reply"` Reply Reply `yaml:"reply"`
Bookmark Bookmark `yaml:"bookmark"`
} }
func (pm PostMeta) FormattedDate() string { func (pm PostMeta) FormattedDate() string {
@ -53,6 +58,7 @@ func (pm *PostMeta) UnmarshalYAML(unmarshal func(interface{}) error) error {
Aliases []string `yaml:"aliases"` Aliases []string `yaml:"aliases"`
Draft bool `yaml:"draft"` Draft bool `yaml:"draft"`
Reply Reply `yaml:"reply"` Reply Reply `yaml:"reply"`
Bookmark Bookmark `yaml:"bookmark"`
} }
type S struct { type S struct {
Date string `yaml:"date"` Date string `yaml:"date"`
@ -76,6 +82,7 @@ func (pm *PostMeta) UnmarshalYAML(unmarshal func(interface{}) error) error {
pm.Aliases = t.Aliases pm.Aliases = t.Aliases
pm.Draft = t.Draft pm.Draft = t.Draft
pm.Reply = t.Reply pm.Reply = t.Reply
pm.Bookmark = t.Bookmark
possibleFormats := []string{ possibleFormats := []string{
"2006-01-02", "2006-01-02",

View File

@ -338,6 +338,14 @@ func TestRenderReplyWithText(t *testing.T) {
assertions.AssertContains(t, result, "This is a reply") assertions.AssertContains(t, result, "This is a reply")
} }
func TestRengerPostContainsBookmark(t *testing.T) {
user := getTestUser()
post, _ := user.CreateNewPostFull(owl.PostMeta{Type: "bookmark", Bookmark: owl.Bookmark{Url: "https://example.com/post"}}, "hi")
result, _ := owl.RenderPost(post)
assertions.AssertContains(t, result, "https://example.com/post")
}
func TestOpenGraphTags(t *testing.T) { func TestOpenGraphTags(t *testing.T) {
user := getTestUser() user := getTestUser()
post, _ := user.CreateNewPost("testpost", false) post, _ := user.CreateNewPost("testpost", false)