diff --git a/pkgs/plugintypes/app.go b/pkgs/plugintypes/app.go index 1a785a8..f37134e 100644 --- a/pkgs/plugintypes/app.go +++ b/pkgs/plugintypes/app.go @@ -27,6 +27,8 @@ type App interface { SetPostParameter(path string, parameter string, values []string) error // Render markdown as text (without HTML) 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. @@ -77,4 +79,6 @@ type RenderContext interface { GetURL() string // Get the blog name GetBlog() string + // Check if user is logged in + IsLoggedIn() bool } diff --git a/pkgs/yaegiwrappers/go_goblog_app-app-pkgs-plugintypes.go b/pkgs/yaegiwrappers/go_goblog_app-app-pkgs-plugintypes.go index b620115..e7fb3d2 100644 --- a/pkgs/yaegiwrappers/go_goblog_app-app-pkgs-plugintypes.go +++ b/pkgs/yaegiwrappers/go_goblog_app-app-pkgs-plugintypes.go @@ -85,6 +85,7 @@ type _go_goblog_app_app_pkgs_plugintypes_App struct { WGetDatabase func() plugintypes.Database WGetHTTPClient func() *http.Client WGetPost func(path string) (plugintypes.Post, error) + WIsLoggedIn func(req *http.Request) bool WPurgeCache func() WRenderMarkdownAsText func(markdown string) (text string, err 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) { 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() { 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 type _go_goblog_app_app_pkgs_plugintypes_RenderContext struct { - IValue interface{} - WGetBlog func() string - WGetPath func() string - WGetURL func() string + IValue interface{} + WGetBlog func() string + WGetPath func() string + WGetURL func() string + WIsLoggedIn func() bool } 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 { 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 type _go_goblog_app_app_pkgs_plugintypes_SetApp struct { diff --git a/plugins.go b/plugins.go index 4c6d27b..da09a0e 100644 --- a/plugins.go +++ b/plugins.go @@ -123,6 +123,10 @@ func (a *goBlog) RenderMarkdownAsText(markdown string) (text string, err error) return a.renderText(markdown) } +func (a *goBlog) IsLoggedIn(req *http.Request) bool { + return a.isLoggedIn(req) +} + func (p *post) GetPath() string { return p.Path } diff --git a/plugins/aitldr/src/aitldr/aitldr.go b/plugins/aitldr/src/aitldr/aitldr.go index ff59e8e..7217fcc 100644 --- a/plugins/aitldr/src/aitldr/aitldr.go +++ b/plugins/aitldr/src/aitldr/aitldr.go @@ -25,9 +25,10 @@ func GetPlugin() ( plugintypes.SetConfig, plugintypes.SetApp, plugintypes.PostCreatedHook, plugintypes.PostUpdatedHook, plugintypes.UIPost, plugintypes.UI2, + plugintypes.Middleware, ) { p := &plugin{} - return p, p, p, p, p, p + return p, p, p, p, p, p, p } func (p *plugin) SetApp(app plugintypes.App) { @@ -46,6 +47,25 @@ func (p *plugin) PostUpdated(post plugintypes.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" 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") 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; }" diff --git a/render.go b/render.go index 20cc7e1..5f5fb3b 100644 --- a/render.go +++ b/render.go @@ -120,9 +120,10 @@ func (a *goBlog) checkRenderData(r *http.Request, data *renderData) { // Plugins if data.prc == nil { data.prc = &pluginRenderContext{ - blog: data.BlogString, - path: r.URL.Path, - url: a.getFullAddress(r.URL.Path), + blog: data.BlogString, + path: r.URL.Path, + url: a.getFullAddress(r.URL.Path), + loggedIn: a.isLoggedIn(r), } } // Data @@ -134,9 +135,10 @@ func (a *goBlog) checkRenderData(r *http.Request, data *renderData) { // Plugins type pluginRenderContext struct { - blog string - path string - url string + blog string + path string + url string + loggedIn bool } func (d *pluginRenderContext) GetBlog() string { @@ -150,3 +152,7 @@ func (d *pluginRenderContext) GetPath() string { func (d *pluginRenderContext) GetURL() string { return d.url } + +func (d *pluginRenderContext) IsLoggedIn() bool { + return d.loggedIn +} diff --git a/ui.go b/ui.go index d4f9914..64ae038 100644 --- a/ui.go +++ b/ui.go @@ -907,7 +907,7 @@ func (a *goBlog) renderPost(hb *htmlbuilder.HtmlBuilder, rd *renderData) { a.renderPostReactions(hb, p) // Post edit actions if rd.LoggedIn() { - hb.WriteElementOpen("div", "class", "actions") + hb.WriteElementOpen("div", "class", "actions", "id", "posteditactions") // Update hb.WriteElementOpen("form", "method", "post", "action", rd.Blog.getRelativePath("/editor")+"#update") hb.WriteElementOpen("input", "type", "hidden", "name", "editoraction", "value", "loadupdate")