feat: folder mapping UI on account add (probe + map modal)
ADD now probes both connections, lists folders on each side, and opens a
mapping modal to route source->destination folders (e.g. Спам -> Spam) so we
append into the existing folder instead of creating a duplicate.
- store: SetTaskFolderMapping (+ round-trip test)
- httpapi: POST /tasks/{id}/probe (test both, return folder lists),
PUT /tasks/{id}/folder-mapping
- web: FolderMappingModal (reuses Modal, size=lg), submitAccount probes then
opens the modal; confirm creates the account and saves the task mapping
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01MMHQTtnQtQqL8muAXHr9kd
This commit is contained in:
@@ -7,6 +7,7 @@ import (
|
||||
"strings"
|
||||
|
||||
"github.com/vasyansk/imap-copier/internal/crypto"
|
||||
"github.com/vasyansk/imap-copier/internal/imapx"
|
||||
"github.com/vasyansk/imap-copier/internal/store"
|
||||
)
|
||||
|
||||
@@ -30,6 +31,76 @@ func accountDTO(a store.Account) AccountView {
|
||||
}
|
||||
}
|
||||
|
||||
// handleProbeFolders tests both logins with the given credentials and returns
|
||||
// the folder list on each side, so the UI can offer a source->destination
|
||||
// folder mapping before the account is created. Credentials are not stored.
|
||||
func (s *Server) handleProbeFolders(w http.ResponseWriter, r *http.Request) {
|
||||
taskID, err := pathID(r, "id")
|
||||
if err != nil {
|
||||
http.Error(w, "bad id", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
task, err := s.store.GetTask(r.Context(), taskID)
|
||||
if err != nil {
|
||||
http.Error(w, "not found", http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
srcEP, err := s.store.GetEndpoint(r.Context(), task.SrcEndpointID)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
dstEP, err := s.store.GetEndpoint(r.Context(), task.DstEndpointID)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
var body struct {
|
||||
SrcLogin string `json:"src_login"`
|
||||
SrcPass string `json:"src_pass"`
|
||||
DstLogin string `json:"dst_login"`
|
||||
DstPass string `json:"dst_pass"`
|
||||
}
|
||||
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
|
||||
http.Error(w, "bad json", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
probe := func(ep store.Endpoint, login, pass string) map[string]any {
|
||||
folders, err := imapx.TestLogin(r.Context(),
|
||||
imapx.Endpoint{Host: ep.Host, Port: ep.Port, TLSMode: ep.TLSMode},
|
||||
strings.TrimSpace(login), strings.TrimSpace(pass))
|
||||
if err != nil {
|
||||
return map[string]any{"ok": false, "error": err.Error(), "folders": []string{}}
|
||||
}
|
||||
return map[string]any{"ok": true, "folders": folders}
|
||||
}
|
||||
writeJSON(w, http.StatusOK, map[string]any{
|
||||
"src": probe(srcEP, body.SrcLogin, body.SrcPass),
|
||||
"dst": probe(dstEP, body.DstLogin, body.DstPass),
|
||||
})
|
||||
}
|
||||
|
||||
// handleSetFolderMapping replaces the task's src->dst folder mapping.
|
||||
func (s *Server) handleSetFolderMapping(w http.ResponseWriter, r *http.Request) {
|
||||
taskID, err := pathID(r, "id")
|
||||
if err != nil {
|
||||
http.Error(w, "bad id", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
var body struct {
|
||||
Mapping map[string]string `json:"mapping"`
|
||||
}
|
||||
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
|
||||
http.Error(w, "bad json", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
if err := s.store.SetTaskFolderMapping(r.Context(), taskID, body.Mapping); err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
w.WriteHeader(http.StatusNoContent)
|
||||
}
|
||||
|
||||
func pathID(r *http.Request, name string) (int64, error) {
|
||||
return strconv.ParseInt(r.PathValue(name), 10, 64)
|
||||
}
|
||||
|
||||
@@ -20,6 +20,8 @@ func (s *Server) Router() http.Handler {
|
||||
api.HandleFunc("GET /api/tasks/{id}", s.handleGetTask)
|
||||
api.HandleFunc("DELETE /api/tasks/{id}", s.handleDeleteTask)
|
||||
api.HandleFunc("POST /api/tasks/{id}/accounts", s.handleCreateAccount)
|
||||
api.HandleFunc("POST /api/tasks/{id}/probe", s.handleProbeFolders)
|
||||
api.HandleFunc("PUT /api/tasks/{id}/folder-mapping", s.handleSetFolderMapping)
|
||||
api.HandleFunc("DELETE /api/tasks/{id}/accounts/{accountId}", s.handleDeleteAccount)
|
||||
api.HandleFunc("POST /api/tasks/{id}/import", s.handleImportCSV)
|
||||
api.HandleFunc("POST /api/tasks/{id}/test", s.handleTestAccounts)
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestSetTaskFolderMappingRoundTrip(t *testing.T) {
|
||||
s := testStore(t)
|
||||
ctx := context.Background()
|
||||
e1, _ := s.CreateEndpoint(ctx, Endpoint{RoleLabel: "s", Host: "a", Port: 993, TLSMode: "ssl"})
|
||||
e2, _ := s.CreateEndpoint(ctx, Endpoint{RoleLabel: "d", Host: "b", Port: 993, TLSMode: "ssl"})
|
||||
taskID, _ := s.CreateTask(ctx, Task{Name: "t", SrcEndpointID: e1, DstEndpointID: e2})
|
||||
|
||||
m := map[string]string{"Спам": "Spam", "Отправленные": "Sent"}
|
||||
if err := s.SetTaskFolderMapping(ctx, taskID, m); err != nil {
|
||||
t.Fatalf("set mapping: %v", err)
|
||||
}
|
||||
got, err := s.GetTask(ctx, taskID)
|
||||
if err != nil {
|
||||
t.Fatalf("get: %v", err)
|
||||
}
|
||||
if got.FolderMapping["Спам"] != "Spam" || got.FolderMapping["Отправленные"] != "Sent" {
|
||||
t.Fatalf("mapping round-trip failed: %+v", got.FolderMapping)
|
||||
}
|
||||
// nil clears to empty object, not null
|
||||
if err := s.SetTaskFolderMapping(ctx, taskID, nil); err != nil {
|
||||
t.Fatalf("clear: %v", err)
|
||||
}
|
||||
got, _ = s.GetTask(ctx, taskID)
|
||||
if got.FolderMapping == nil {
|
||||
t.Fatal("mapping should be non-nil empty map after clear")
|
||||
}
|
||||
}
|
||||
@@ -57,6 +57,14 @@ func (s *Store) ListTasks(ctx context.Context) ([]Task, error) {
|
||||
return out, rows.Err()
|
||||
}
|
||||
|
||||
func (s *Store) SetTaskFolderMapping(ctx context.Context, id int64, mapping map[string]string) error {
|
||||
if mapping == nil {
|
||||
mapping = map[string]string{}
|
||||
}
|
||||
_, err := s.Pool.Exec(ctx, `UPDATE tasks SET folder_mapping=$2 WHERE id=$1`, id, mapping)
|
||||
return err
|
||||
}
|
||||
|
||||
func (s *Store) SetTaskStatus(ctx context.Context, id int64, status string) error {
|
||||
_, err := s.Pool.Exec(ctx, `UPDATE tasks SET status=$2 WHERE id=$1`, id, status)
|
||||
return err
|
||||
|
||||
Reference in New Issue
Block a user