Skip to content

Commit

Permalink
aitldr plugin: add button to delete summary
Browse files Browse the repository at this point in the history
  • Loading branch information
jlelse committed Dec 4, 2024
1 parent bcd70c1 commit 575df89
Showing 1 changed file with 26 additions and 2 deletions.
28 changes: 26 additions & 2 deletions plugins/aitldr/src/aitldr/aitldr.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,13 @@ func (p *plugin) Handler(next http.Handler) http.Handler {
} else {
next.ServeHTTP(w, r)
}
} else if r.Method == http.MethodPost && r.URL.Path == "/x/aitldr/delete" && p.app.IsLoggedIn(r) {
if post, err := p.app.GetPost(r.FormValue("post")); err == nil {
p.deleteSummary(post)
http.Redirect(w, r, post.GetPath(), http.StatusFound)
} else {
next.ServeHTTP(w, r)
}
} else {
next.ServeHTTP(w, r)
}
Expand All @@ -78,21 +85,28 @@ func (p *plugin) Prio() int {
const postParam = "aitldr"

func (p *plugin) RenderPost(renderContext plugintypes.RenderContext, post plugintypes.Post, doc *goquery.Document) {
tldr := post.GetFirstParameterValue(postParam)

// Add re-generation button
if renderContext.IsLoggedIn() {
buttonBuf := bufferpool.Get()
defer bufferpool.Put(buttonBuf)
buttonHw := htmlbuilder.NewHtmlBuilder(buttonBuf)
buttonHw.WriteElementOpen("form", "method", "post", "action", "/x/aitldr")
buttonHw.WriteElementOpen("input", "type", "hidden", "name", "post", "value", post.GetPath())
buttonHw.WriteElementOpen("input", "type", "submit", "value", "Regenerate AI summary")
buttonHw.WriteElementOpen("input", "type", "submit", "value", "(Re-)Generate AI summary")
buttonHw.WriteElementClose("form")
if tldr != "" {
buttonHw.WriteElementOpen("form", "method", "post", "action", "/x/aitldr/delete")
buttonHw.WriteElementOpen("input", "type", "hidden", "name", "post", "value", post.GetPath())
buttonHw.WriteElementOpen("input", "type", "submit", "value", "Delete AI summary")
buttonHw.WriteElementClose("form")
}

doc.Find("#posteditactions").AppendHtml(buttonBuf.String())
}

// If the post has a summary, display it
tldr := post.GetFirstParameterValue(postParam)
if tldr == "" {
return
}
Expand Down Expand Up @@ -251,3 +265,13 @@ func (p *plugin) createPrompt(post plugintypes.Post) string {
}
return prompt
}

func (p *plugin) deleteSummary(post plugintypes.Post) {
err := p.app.SetPostParameter(post.GetPath(), postParam, []string{})
if err != nil {
log.Println("aitldr plugin:", err.Error())
return
}

p.app.PurgeCache()
}

0 comments on commit 575df89

Please sign in to comment.