diff --git a/src/web/backend/main.go b/src/web/backend/main.go index b8a2ba1..089ab13 100644 --- a/src/web/backend/main.go +++ b/src/web/backend/main.go @@ -3,18 +3,21 @@ package main import ( "fmt" "net/http" + "net/mail" "slices" "time" "github.com/gin-contrib/cors" "github.com/gin-gonic/gin" + "jupiter-mail.org/web/model" ) type Account struct { - Username string `json:"username"` - Domain string `json:"domain"` - DisplayName string `json:"display_name"` - MailDirectory string `json:"mail_directory"` + Username string `json:"username"` + Domain string `json:"domain"` + DisplayName string `json:"display_name"` + MailDirectory string `json:"mail_directory"` + Activated bool `json:"activated"` } var mockAccounts = []*Account{ @@ -23,18 +26,21 @@ var mockAccounts = []*Account{ Username: "ari", Domain: "example.org", MailDirectory: `/var/mail/example.org/ari/home`, + Activated: true, }, { DisplayName: "Test Account", Username: "testaccount", Domain: "mydomain.ie", MailDirectory: `/var/mail/mydomain.ie/testaccount/home`, + Activated: false, }, { DisplayName: "Cooler One", Username: "otherone", Domain: "cooler.space", MailDirectory: `/var/mail/cooler.space/otherone/home`, + Activated: true, }, } @@ -43,6 +49,7 @@ func main() { r.Use(cors.New(cors.Config{ AllowAllOrigins: true, + AllowHeaders: []string{"Content-Type"}, AllowMethods: []string{"GET", "POST", "PUT", "DELETE"}, MaxAge: 12 * time.Hour, })) @@ -78,6 +85,67 @@ func main() { ctx.JSON(http.StatusOK, accounts) }) + r.POST("/api/v1/accounts", func(ctx *gin.Context) { + type Request struct { + DisplayName string `json:"display_name"` + Username string `json:"username"` + Domain string `json:"domain"` + } + var newAccountData Request + + if err := ctx.BindJSON(&newAccountData); err != nil { + ctx.String(http.StatusBadRequest, "Malformed request data") + return + } + + emailAddressReq := fmt.Sprintf("%s@%s", newAccountData.Username, newAccountData.Domain) + + emailAddress, err := mail.ParseAddress(emailAddressReq) + if err != nil { + ctx.String(http.StatusBadRequest, "Malformed address: %s", emailAddressReq) + return + } + + search := func(account *Account) bool { + return ( + account.Username == newAccountData.Username && + account.Domain == newAccountData.Domain) + } + if slices.ContainsFunc(mockAccounts, search) { + ctx.String(http.StatusBadRequest, "Account already exists: %s", emailAddress.String()) + return + } + + account := model.MailAccount{ + DisplayName: newAccountData.DisplayName, + Username: newAccountData.Username, + Domain: newAccountData.Domain, + } + account.MailDirectory = account.DefaultMailHome() + + mockAccounts = append(mockAccounts, &Account{ + DisplayName: account.DisplayName, + Username: account.Username, + Domain: account.Domain, + MailDirectory: account.MailDirectory, + }) + + /* + account, err := controller.FetchAccountByEmail(email) + if err != nil { + if strings.Contains(err.Error(), "does not exist") { + ctx.Error(fmt.Errorf("Account does not exist: %s", email)) + return + } + log.Fatalf("Failed to fetch account: %v", err) + ctx.Error(errors.New("Failed to fetch account")) + return + } + */ + + ctx.JSON(http.StatusOK, account) + }) + r.GET("/api/v1/accounts/:email", func(ctx *gin.Context) { email := ctx.Params.ByName("email") @@ -142,10 +210,10 @@ func main() { ctx.JSON(http.StatusOK, Settings{ MailDelivery: MailDelivery{ - Version: "v1.2.3", + Version: "Dovecot v1.2.3", }, MailTransfer: MailTransfer{ - Version: "v4.5.6", + Version: "Postfix v4.5.6", }, }) }) diff --git a/src/web/frontend/src/app.css b/src/web/frontend/src/app.css index e17629d..0db21e9 100644 --- a/src/web/frontend/src/app.css +++ b/src/web/frontend/src/app.css @@ -41,7 +41,7 @@ --info: #1a94ff; --success: #92a40a; - --warning: #ffc107; + --warning: #e7ad00; --error: #d42c2c; } @@ -125,3 +125,12 @@ code { transition: background-color .1s, border-color .1s; } + +p.warning { + width: fit-content; + padding: .2em .4em; + color: var(--on-bg-1); + background-color: var(--warning); + border: 1px solid color-mix(in srgb, var(--warning), #000 25%); + border-radius: var(--radius-0); +} diff --git a/src/web/frontend/src/components/ui/AccountListItem.svelte b/src/web/frontend/src/components/ui/AccountListItem.svelte index e13847f..585bf26 100644 --- a/src/web/frontend/src/components/ui/AccountListItem.svelte +++ b/src/web/frontend/src/components/ui/AccountListItem.svelte @@ -1,7 +1,7 @@