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
// 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
}

View File

@ -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 {

View File

@ -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
}

View File

@ -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; }"

View File

@ -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
}

2
ui.go
View File

@ -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")