0bb584fe10
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
150 lines
4.7 KiB
Go
150 lines
4.7 KiB
Go
package httpapi
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"github.com/vasyansk/imap-copier/internal/crypto"
|
|
"github.com/vasyansk/imap-copier/internal/imapx"
|
|
"github.com/vasyansk/imap-copier/internal/store"
|
|
)
|
|
|
|
type AccountView struct {
|
|
ID int64 `json:"id"`
|
|
SrcLogin string `json:"src_login"`
|
|
DstLogin string `json:"dst_login"`
|
|
TestSrcStatus string `json:"test_src_status"`
|
|
TestDstStatus string `json:"test_dst_status"`
|
|
Status string `json:"status"`
|
|
Copied int64 `json:"copied"`
|
|
Skipped int64 `json:"skipped"`
|
|
Errors int64 `json:"errors"`
|
|
}
|
|
|
|
func accountDTO(a store.Account) AccountView {
|
|
return AccountView{
|
|
ID: a.ID, SrcLogin: a.SrcLogin, DstLogin: a.DstLogin,
|
|
TestSrcStatus: a.TestSrcStatus, TestDstStatus: a.TestDstStatus,
|
|
Status: a.Status, Copied: a.Copied, Skipped: a.Skipped, Errors: a.Errors,
|
|
}
|
|
}
|
|
|
|
// 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)
|
|
}
|
|
|
|
func (s *Server) handleCreateAccount(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 {
|
|
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
|
|
}
|
|
// Trim surrounding whitespace — pasted app passwords / logins often carry a
|
|
// stray trailing space or newline that the IMAP server rejects.
|
|
body.SrcLogin = strings.TrimSpace(body.SrcLogin)
|
|
body.SrcPass = strings.TrimSpace(body.SrcPass)
|
|
body.DstLogin = strings.TrimSpace(body.DstLogin)
|
|
body.DstPass = strings.TrimSpace(body.DstPass)
|
|
srcEnc, err := crypto.Encrypt(s.cfg.EncKey, []byte(body.SrcPass))
|
|
if err != nil {
|
|
http.Error(w, "encrypt", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
dstEnc, err := crypto.Encrypt(s.cfg.EncKey, []byte(body.DstPass))
|
|
if err != nil {
|
|
http.Error(w, "encrypt", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
id, err := s.store.CreateAccount(r.Context(), store.Account{
|
|
TaskID: taskID, SrcLogin: body.SrcLogin, SrcPassEnc: srcEnc,
|
|
DstLogin: body.DstLogin, DstPassEnc: dstEnc,
|
|
})
|
|
if err != nil {
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
writeJSON(w, http.StatusCreated, map[string]int64{"id": id})
|
|
}
|