2025-01-28 04:19:08 +00:00
|
|
|
package web
|
|
|
|
|
|
|
|
import (
|
2025-01-31 17:55:07 +00:00
|
|
|
"fmt"
|
2025-01-30 06:21:17 +00:00
|
|
|
"log"
|
2025-01-28 04:19:08 +00:00
|
|
|
"net/http"
|
2025-01-30 06:21:17 +00:00
|
|
|
"strings"
|
2025-01-29 22:54:29 +00:00
|
|
|
"time"
|
2025-01-28 04:19:08 +00:00
|
|
|
|
|
|
|
"github.com/Jaculabilis/intake/core"
|
|
|
|
"github.com/Jaculabilis/intake/web/html"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (env *Env) getItem(writer http.ResponseWriter, req *http.Request) {
|
|
|
|
source := req.PathValue("source")
|
|
|
|
id := req.PathValue("id")
|
|
|
|
|
|
|
|
item, err := core.GetItem(env.db, source, id)
|
|
|
|
if err != nil {
|
2025-01-31 00:55:02 +00:00
|
|
|
http.Error(writer, err.Error(), 500)
|
2025-01-28 04:19:08 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
html.Item(writer, html.ItemData{Item: item})
|
|
|
|
}
|
2025-01-28 05:04:21 +00:00
|
|
|
|
|
|
|
func (env *Env) deleteItem(writer http.ResponseWriter, req *http.Request) {
|
|
|
|
source := req.PathValue("source")
|
|
|
|
id := req.PathValue("id")
|
|
|
|
|
|
|
|
_, err := core.DeactivateItem(env.db, source, id)
|
|
|
|
if err != nil {
|
2025-01-31 00:55:02 +00:00
|
|
|
http.Error(writer, err.Error(), 500)
|
2025-01-28 05:04:21 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
item, err := core.GetItem(env.db, source, id)
|
|
|
|
if err != nil {
|
2025-01-31 00:55:02 +00:00
|
|
|
http.Error(writer, err.Error(), 500)
|
2025-01-28 05:04:21 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
html.Item(writer, html.ItemData{Item: item})
|
|
|
|
}
|
2025-01-29 22:54:29 +00:00
|
|
|
|
|
|
|
func (env *Env) doAction(writer http.ResponseWriter, req *http.Request) {
|
|
|
|
source := req.PathValue("source")
|
|
|
|
id := req.PathValue("id")
|
|
|
|
action := req.PathValue("action")
|
|
|
|
|
2025-01-31 00:04:31 +00:00
|
|
|
state, err := core.GetState(env.db, source)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("error: failed to load state for %s: %v", source, err)
|
|
|
|
}
|
|
|
|
|
2025-01-31 16:00:07 +00:00
|
|
|
envs, err := core.GetEnvs(env.db, source)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("error: failed to get envs for %s: %v", source, err)
|
|
|
|
}
|
|
|
|
|
2025-01-29 22:54:29 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2025-02-05 21:21:31 +00:00
|
|
|
postProcess, err := core.GetSourcePostProcessor(env.db, source)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("error: failed to get source post-processor: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
newItem, newState, err := core.ExecuteItemAction(item, argv, envs, state, time.Minute, postProcess)
|
2025-01-29 22:54:29 +00:00
|
|
|
if err != nil {
|
|
|
|
http.Error(writer, err.Error(), 500)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2025-01-31 17:55:07 +00:00
|
|
|
if err = env.db.Transact(func(tx core.DB) error {
|
|
|
|
if _err := core.UpdateItems(tx, []core.Item{newItem}); err != nil {
|
|
|
|
return fmt.Errorf("failed to update item: %v", _err)
|
|
|
|
}
|
|
|
|
if _err := core.SetState(tx, source, newState); err != nil {
|
|
|
|
return fmt.Errorf("failed to set state for %s: %v", source, _err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}); err != nil {
|
2025-01-29 22:54:29 +00:00
|
|
|
http.Error(writer, err.Error(), 500)
|
2025-01-31 00:04:31 +00:00
|
|
|
}
|
|
|
|
|
2025-01-29 22:54:29 +00:00
|
|
|
html.Item(writer, html.ItemData{Item: newItem})
|
|
|
|
}
|
2025-01-30 06:21:17 +00:00
|
|
|
|
|
|
|
func (env *Env) massDeactivate(writer http.ResponseWriter, req *http.Request) {
|
|
|
|
if err := req.ParseForm(); err != nil {
|
|
|
|
log.Printf("error parsing form data: %v", err)
|
|
|
|
http.Error(writer, "", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for _, item := range req.PostForm["items"] {
|
|
|
|
i := strings.Index(item, "/")
|
|
|
|
if i == -1 {
|
|
|
|
log.Printf("error: invalid source/item: %s", item)
|
|
|
|
http.Error(writer, "", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, item := range req.PostForm["items"] {
|
|
|
|
i := strings.Index(item, "/")
|
|
|
|
source := item[:i]
|
|
|
|
id := item[i+1:]
|
|
|
|
active, err := core.DeactivateItem(env.db, source, id)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("error: failed to deactivate %s/%s: %v", source, id, err)
|
|
|
|
}
|
|
|
|
if active {
|
|
|
|
log.Printf("deactivated %s/%s", source, id)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
writer.Header()["HX-Refresh"] = []string{"true"}
|
|
|
|
http.Error(writer, "ok", http.StatusNoContent)
|
|
|
|
}
|