From 6ef51b7286eeaeffb84c0ff4e1431cef7f02e3ed Mon Sep 17 00:00:00 2001 From: Tim Van Baak Date: Wed, 29 Jan 2025 14:54:29 -0800 Subject: [PATCH] Execute actions from web UI --- web/html/item.html | 12 +++++++++-- web/item.go | 50 ++++++++++++++++++++++++++++++++++++++++++++++ web/main.go | 1 + 3 files changed, 61 insertions(+), 2 deletions(-) diff --git a/web/html/item.html b/web/html/item.html index dec336f..46229c5 100644 --- a/web/html/item.html +++ b/web/html/item.html @@ -13,6 +13,16 @@ >↷ {{- if .Link }} {{ end -}} +{{ range $key, $_ := .Action }} + +{{ end -}} {{ end }} {{ define "item-title" -}} @@ -34,9 +44,7 @@ {{ template "item-buttons" . }} {{ template "item-title" . }} -{{ if .Body }}

{{ raw .Body }}

-{{ end }} {{ template "item-buttons" . }} {{- else -}} diff --git a/web/item.go b/web/item.go index 9a141f2..4a2ad34 100644 --- a/web/item.go +++ b/web/item.go @@ -1,7 +1,9 @@ package web import ( + "encoding/json" "net/http" + "time" "github.com/Jaculabilis/intake/core" "github.com/Jaculabilis/intake/web/html" @@ -35,3 +37,51 @@ func (env *Env) deleteItem(writer http.ResponseWriter, req *http.Request) { } html.Item(writer, html.ItemData{Item: item}) } + +func (env *Env) doAction(writer http.ResponseWriter, req *http.Request) { + source := req.PathValue("source") + id := req.PathValue("id") + action := req.PathValue("action") + + item, err := core.GetItem(env.db, source, id) + if err != nil { + http.Error(writer, err.Error(), 500) + return + } + + if item.Action[action] == nil { + http.Error(writer, "no such action", 500) + return + } + + argv, err := core.GetArgvForAction(env.db, source, action) + if err != nil { + http.Error(writer, err.Error(), 500) + return + } + + itemJson, err := json.Marshal(item) + if err != nil { + http.Error(writer, err.Error(), 500) + return + } + + res, err := core.Execute(source, argv, nil, string(itemJson), time.Minute) + if err != nil { + http.Error(writer, err.Error(), 500) + return + } + if len(res) != 1 { + http.Error(writer, "not exactly one item", 500) + return + } + newItem := res[0] + core.BackfillItem(&newItem, &item) + + if err = core.UpdateItems(env.db, []core.Item{newItem}); err != nil { + http.Error(writer, err.Error(), 500) + return + } + + html.Item(writer, html.ItemData{Item: newItem}) +} diff --git a/web/main.go b/web/main.go index b10c608..cb978f6 100644 --- a/web/main.go +++ b/web/main.go @@ -33,6 +33,7 @@ func RunServer(db *core.DB, addr string, port string) { handleFunc("GET /source/{source}", env.getSource) handleFunc("GET /item/{source}/{id}", env.getItem) handleFunc("DELETE /item/{source}/{id}", env.deleteItem) + handleFunc("POST /item/{source}/{id}/action/{action}", env.doAction) log.Fatal(http.ListenAndServe(bind, nil)) }