feat(api): probe existing account + persist per-account folder mapping
This commit is contained in:
@@ -22,6 +22,8 @@ type AccountView struct {
|
||||
Skipped int64 `json:"skipped"`
|
||||
Errors int64 `json:"errors"`
|
||||
LastError string `json:"last_error,omitempty"`
|
||||
FolderMapping map[string]string `json:"folder_mapping"`
|
||||
ExcludedFolders []string `json:"excluded_folders"`
|
||||
}
|
||||
|
||||
func accountDTO(a store.Account) AccountView {
|
||||
@@ -30,6 +32,8 @@ func accountDTO(a store.Account) AccountView {
|
||||
TestSrcStatus: a.TestSrcStatus, TestDstStatus: a.TestDstStatus,
|
||||
Status: a.Status, Copied: a.Copied, Skipped: a.Skipped, Errors: a.Errors,
|
||||
LastError: a.LastError,
|
||||
FolderMapping: a.FolderMapping,
|
||||
ExcludedFolders: a.ExcludedFolders,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -149,3 +153,97 @@ func (s *Server) handleCreateAccount(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
writeJSON(w, http.StatusCreated, map[string]int64{"id": id})
|
||||
}
|
||||
|
||||
// findAccount returns the account with accID under taskID, or ok=false.
|
||||
func (s *Server) findAccount(r *http.Request, taskID, accID int64) (store.Account, bool) {
|
||||
accs, err := s.store.ListAccountsByTask(r.Context(), taskID)
|
||||
if err != nil {
|
||||
return store.Account{}, false
|
||||
}
|
||||
for _, a := range accs {
|
||||
if a.ID == accID {
|
||||
return a, true
|
||||
}
|
||||
}
|
||||
return store.Account{}, false
|
||||
}
|
||||
|
||||
// handleProbeAccountFolders re-probes an existing account using its stored
|
||||
// (encrypted) credentials, so the operator can (re)map an account added via CSV.
|
||||
func (s *Server) handleProbeAccountFolders(w http.ResponseWriter, r *http.Request) {
|
||||
taskID, err := pathID(r, "id")
|
||||
if err != nil {
|
||||
http.Error(w, "bad id", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
accID, err := pathID(r, "accountId")
|
||||
if err != nil {
|
||||
http.Error(w, "bad account id", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
task, err := s.store.GetTask(r.Context(), taskID)
|
||||
if err != nil {
|
||||
http.Error(w, "not found", http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
a, ok := s.findAccount(r, taskID, accID)
|
||||
if !ok {
|
||||
http.Error(w, "account 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
|
||||
}
|
||||
srcPass, err := crypto.Decrypt(s.cfg.EncKey, a.SrcPassEnc)
|
||||
if err != nil {
|
||||
http.Error(w, "decrypt", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
dstPass, err := crypto.Decrypt(s.cfg.EncKey, a.DstPassEnc)
|
||||
if err != nil {
|
||||
http.Error(w, "decrypt", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
probe := func(ep store.Endpoint, login string, pass []byte) 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(string(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, a.SrcLogin, srcPass),
|
||||
"dst": probe(dstEP, a.DstLogin, dstPass),
|
||||
})
|
||||
}
|
||||
|
||||
// handleSetAccountFolderMapping persists one account's rename map + excluded set.
|
||||
func (s *Server) handleSetAccountFolderMapping(w http.ResponseWriter, r *http.Request) {
|
||||
accID, err := pathID(r, "accountId")
|
||||
if err != nil {
|
||||
http.Error(w, "bad account id", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
var body struct {
|
||||
Mapping map[string]string `json:"mapping"`
|
||||
Excluded []string `json:"excluded"`
|
||||
}
|
||||
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
|
||||
http.Error(w, "bad json", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
if err := s.store.SetAccountFolderMapping(r.Context(), accID, body.Mapping, body.Excluded); err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
w.WriteHeader(http.StatusNoContent)
|
||||
}
|
||||
|
||||
@@ -27,6 +27,8 @@ func (s *Server) Router() http.Handler {
|
||||
api.HandleFunc("POST /api/tasks/{id}/test", s.handleTestAccounts)
|
||||
api.HandleFunc("POST /api/tasks/{id}/run", s.handleRun)
|
||||
api.HandleFunc("POST /api/tasks/{id}/accounts/{accountId}/cancel", s.handleCancelAccount)
|
||||
api.HandleFunc("POST /api/tasks/{id}/accounts/{accountId}/probe", s.handleProbeAccountFolders)
|
||||
api.HandleFunc("PUT /api/tasks/{id}/accounts/{accountId}/folder-mapping", s.handleSetAccountFolderMapping)
|
||||
mux.Handle("/api/", s.requireAuth(api))
|
||||
mux.Handle("/ws", s.requireAuth(http.HandlerFunc(s.handleWS)))
|
||||
|
||||
|
||||
Reference in New Issue
Block a user