aitldr plugin: Add button to regenerate summary

This commit is contained in:
Jan-Lukas Else 2024-02-08 20:54:16 +01:00
parent 9f4dd7226c
commit 272a773b57
6 changed files with 66 additions and 12 deletions

View File

@ -27,6 +27,8 @@ type App interface {
SetPostParameter(path string, parameter string, values []string) error SetPostParameter(path string, parameter string, values []string) error
// Render markdown as text (without HTML) // Render markdown as text (without HTML)
RenderMarkdownAsText(markdown string) (text string, err error) RenderMarkdownAsText(markdown string) (text string, err error)
// Check if user is logged in
IsLoggedIn(req *http.Request) bool
} }
// Database is used to provide access to GoBlog's database. // Database is used to provide access to GoBlog's database.
@ -77,4 +79,6 @@ type RenderContext interface {
GetURL() string GetURL() string
// Get the blog name // Get the blog name
GetBlog() string GetBlog() string
// Check if user is logged in
IsLoggedIn() bool
} }

View File

@ -85,6 +85,7 @@ type _go_goblog_app_app_pkgs_plugintypes_App struct {
WGetDatabase func() plugintypes.Database WGetDatabase func() plugintypes.Database
WGetHTTPClient func() *http.Client WGetHTTPClient func() *http.Client
WGetPost func(path string) (plugintypes.Post, error) WGetPost func(path string) (plugintypes.Post, error)
WIsLoggedIn func(req *http.Request) bool
WPurgeCache func() WPurgeCache func()
WRenderMarkdownAsText func(markdown string) (text string, err error) WRenderMarkdownAsText func(markdown string) (text string, err error)
WSetPostParameter func(path string, parameter string, values []string) error WSetPostParameter func(path string, parameter string, values []string) error
@ -108,6 +109,9 @@ func (W _go_goblog_app_app_pkgs_plugintypes_App) GetHTTPClient() *http.Client {
func (W _go_goblog_app_app_pkgs_plugintypes_App) GetPost(path string) (plugintypes.Post, error) { func (W _go_goblog_app_app_pkgs_plugintypes_App) GetPost(path string) (plugintypes.Post, error) {
return W.WGetPost(path) return W.WGetPost(path)
} }
func (W _go_goblog_app_app_pkgs_plugintypes_App) IsLoggedIn(req *http.Request) bool {
return W.WIsLoggedIn(req)
}
func (W _go_goblog_app_app_pkgs_plugintypes_App) PurgeCache() { func (W _go_goblog_app_app_pkgs_plugintypes_App) PurgeCache() {
W.WPurgeCache() W.WPurgeCache()
} }
@ -260,10 +264,11 @@ func (W _go_goblog_app_app_pkgs_plugintypes_PostUpdatedHook) PostUpdated(post pl
// _go_goblog_app_app_pkgs_plugintypes_RenderContext is an interface wrapper for RenderContext type // _go_goblog_app_app_pkgs_plugintypes_RenderContext is an interface wrapper for RenderContext type
type _go_goblog_app_app_pkgs_plugintypes_RenderContext struct { type _go_goblog_app_app_pkgs_plugintypes_RenderContext struct {
IValue interface{} IValue interface{}
WGetBlog func() string WGetBlog func() string
WGetPath func() string WGetPath func() string
WGetURL func() string WGetURL func() string
WIsLoggedIn func() bool
} }
func (W _go_goblog_app_app_pkgs_plugintypes_RenderContext) GetBlog() string { func (W _go_goblog_app_app_pkgs_plugintypes_RenderContext) GetBlog() string {
@ -275,6 +280,9 @@ func (W _go_goblog_app_app_pkgs_plugintypes_RenderContext) GetPath() string {
func (W _go_goblog_app_app_pkgs_plugintypes_RenderContext) GetURL() string { func (W _go_goblog_app_app_pkgs_plugintypes_RenderContext) GetURL() string {
return W.WGetURL() return W.WGetURL()
} }
func (W _go_goblog_app_app_pkgs_plugintypes_RenderContext) IsLoggedIn() bool {
return W.WIsLoggedIn()
}
// _go_goblog_app_app_pkgs_plugintypes_SetApp is an interface wrapper for SetApp type // _go_goblog_app_app_pkgs_plugintypes_SetApp is an interface wrapper for SetApp type
type _go_goblog_app_app_pkgs_plugintypes_SetApp struct { type _go_goblog_app_app_pkgs_plugintypes_SetApp struct {

View File

@ -123,6 +123,10 @@ func (a *goBlog) RenderMarkdownAsText(markdown string) (text string, err error)
return a.renderText(markdown) return a.renderText(markdown)
} }
func (a *goBlog) IsLoggedIn(req *http.Request) bool {
return a.isLoggedIn(req)
}
func (p *post) GetPath() string { func (p *post) GetPath() string {
return p.Path return p.Path
} }

View File

@ -25,9 +25,10 @@ func GetPlugin() (
plugintypes.SetConfig, plugintypes.SetApp, plugintypes.SetConfig, plugintypes.SetApp,
plugintypes.PostCreatedHook, plugintypes.PostUpdatedHook, plugintypes.PostCreatedHook, plugintypes.PostUpdatedHook,
plugintypes.UIPost, plugintypes.UI2, plugintypes.UIPost, plugintypes.UI2,
plugintypes.Middleware,
) { ) {
p := &plugin{} p := &plugin{}
return p, p, p, p, p, p return p, p, p, p, p, p, p
} }
func (p *plugin) SetApp(app plugintypes.App) { func (p *plugin) SetApp(app plugintypes.App) {
@ -46,6 +47,25 @@ func (p *plugin) PostUpdated(post plugintypes.Post) {
p.summarize(post) p.summarize(post)
} }
func (p *plugin) Handler(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodPost && r.URL.Path == "/x/aitldr" && p.app.IsLoggedIn(r) {
if post, err := p.app.GetPost(r.FormValue("post")); err == nil {
p.summarize(post)
http.Redirect(w, r, post.GetPath(), http.StatusFound)
} else {
next.ServeHTTP(w, r)
}
} else {
next.ServeHTTP(w, r)
}
})
}
func (p *plugin) Prio() int {
return 1000
}
const postParam = "aitldr" const postParam = "aitldr"
func (p *plugin) RenderPost(renderContext plugintypes.RenderContext, post plugintypes.Post, doc *goquery.Document) { func (p *plugin) RenderPost(renderContext plugintypes.RenderContext, post plugintypes.Post, doc *goquery.Document) {
@ -78,6 +98,18 @@ func (p *plugin) RenderPost(renderContext plugintypes.RenderContext, post plugin
hw.WriteElementsClose("i", "div") hw.WriteElementsClose("i", "div")
doc.Find(".h-entry > article > .e-content").BeforeHtml(buf.String()) doc.Find(".h-entry > article > .e-content").BeforeHtml(buf.String())
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.WriteElementClose("form")
doc.Find("#posteditactions").AppendHtml(buttonBuf.String())
}
} }
const customCSS = ".aitldr { border: 1px dashed; padding: 1em; }" const customCSS = ".aitldr { border: 1px dashed; padding: 1em; }"

View File

@ -120,9 +120,10 @@ func (a *goBlog) checkRenderData(r *http.Request, data *renderData) {
// Plugins // Plugins
if data.prc == nil { if data.prc == nil {
data.prc = &pluginRenderContext{ data.prc = &pluginRenderContext{
blog: data.BlogString, blog: data.BlogString,
path: r.URL.Path, path: r.URL.Path,
url: a.getFullAddress(r.URL.Path), url: a.getFullAddress(r.URL.Path),
loggedIn: a.isLoggedIn(r),
} }
} }
// Data // Data
@ -134,9 +135,10 @@ func (a *goBlog) checkRenderData(r *http.Request, data *renderData) {
// Plugins // Plugins
type pluginRenderContext struct { type pluginRenderContext struct {
blog string blog string
path string path string
url string url string
loggedIn bool
} }
func (d *pluginRenderContext) GetBlog() string { func (d *pluginRenderContext) GetBlog() string {
@ -150,3 +152,7 @@ func (d *pluginRenderContext) GetPath() string {
func (d *pluginRenderContext) GetURL() string { func (d *pluginRenderContext) GetURL() string {
return d.url return d.url
} }
func (d *pluginRenderContext) IsLoggedIn() bool {
return d.loggedIn
}

2
ui.go
View File

@ -907,7 +907,7 @@ func (a *goBlog) renderPost(hb *htmlbuilder.HtmlBuilder, rd *renderData) {
a.renderPostReactions(hb, p) a.renderPostReactions(hb, p)
// Post edit actions // Post edit actions
if rd.LoggedIn() { if rd.LoggedIn() {
hb.WriteElementOpen("div", "class", "actions") hb.WriteElementOpen("div", "class", "actions", "id", "posteditactions")
// Update // Update
hb.WriteElementOpen("form", "method", "post", "action", rd.Blog.getRelativePath("/editor")+"#update") hb.WriteElementOpen("form", "method", "post", "action", rd.Blog.getRelativePath("/editor")+"#update")
hb.WriteElementOpen("input", "type", "hidden", "name", "editoraction", "value", "loadupdate") hb.WriteElementOpen("input", "type", "hidden", "name", "editoraction", "value", "loadupdate")