bskyweb: remove waitlist email endpoint (#3127)

zio/stable
Jake Gold 2024-03-06 10:33:41 -08:00 committed by GitHub
parent f61d1e1f94
commit 7a592d8140
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 5 additions and 132 deletions

View File

@ -1,70 +0,0 @@
package main
import (
"bytes"
"context"
"crypto/sha256"
"encoding/json"
"fmt"
"net/http"
"time"
)
type Mailmodo struct {
httpClient *http.Client
APIKey string
BaseURL string
ListName string
}
func NewMailmodo(apiKey, listName string) *Mailmodo {
return &Mailmodo{
APIKey: apiKey,
BaseURL: "https://api.mailmodo.com/api/v1",
httpClient: &http.Client{},
ListName: listName,
}
}
func (m *Mailmodo) request(ctx context.Context, httpMethod string, apiMethod string, data any) error {
endpoint := fmt.Sprintf("%s/%s", m.BaseURL, apiMethod)
js, err := json.Marshal(data)
if err != nil {
return fmt.Errorf("Mailmodo JSON encoding failed: %w", err)
}
req, err := http.NewRequestWithContext(ctx, httpMethod, endpoint, bytes.NewBuffer(js))
if err != nil {
return fmt.Errorf("Mailmodo HTTP creating request %s %s failed: %w", httpMethod, apiMethod, err)
}
req.Header.Set("mmApiKey", m.APIKey)
req.Header.Set("Content-Type", "application/json")
res, err := m.httpClient.Do(req)
if err != nil {
return fmt.Errorf("Mailmodo HTTP making request %s %s failed: %w", httpMethod, apiMethod, err)
}
defer res.Body.Close()
status := struct {
Success bool `json:"success"`
Message string `json:"message"`
}{}
if err := json.NewDecoder(res.Body).Decode(&status); err != nil {
return fmt.Errorf("Mailmodo HTTP parsing response %s %s failed: %w", httpMethod, apiMethod, err)
}
if !status.Success {
return fmt.Errorf("Mailmodo API response %s %s failed: %s", httpMethod, apiMethod, status.Message)
}
return nil
}
func (m *Mailmodo) AddToList(ctx context.Context, email string) error {
return m.request(ctx, "POST", "addToList", map[string]any{
"listName": m.ListName,
"email": email,
"data": map[string]any{
"email_hashed": fmt.Sprintf("%x", sha256.Sum256([]byte(email))),
},
"created_at": time.Now().UTC().Format(time.RFC3339),
})
}

View File

@ -40,18 +40,6 @@ func run(args []string) {
// retain old PDS env var for easy transition // retain old PDS env var for easy transition
EnvVars: []string{"ATP_APPVIEW_HOST", "ATP_PDS_HOST"}, EnvVars: []string{"ATP_APPVIEW_HOST", "ATP_PDS_HOST"},
}, },
&cli.StringFlag{
Name: "mailmodo-api-key",
Usage: "Mailmodo API key",
Required: false,
EnvVars: []string{"MAILMODO_API_KEY"},
},
&cli.StringFlag{
Name: "mailmodo-list-name",
Usage: "Mailmodo contact list to add email addresses to",
Required: false,
EnvVars: []string{"MAILMODO_LIST_NAME"},
},
&cli.StringFlag{ &cli.StringFlag{
Name: "http-address", Name: "http-address",
Usage: "Specify the local IP/port to bind to", Usage: "Specify the local IP/port to bind to",

View File

@ -2,11 +2,9 @@ package main
import ( import (
"context" "context"
"encoding/json"
"errors" "errors"
"fmt" "fmt"
"io/fs" "io/fs"
"io/ioutil"
"net/http" "net/http"
"os" "os"
"os/signal" "os/signal"
@ -31,7 +29,6 @@ import (
type Server struct { type Server struct {
echo *echo.Echo echo *echo.Echo
httpd *http.Server httpd *http.Server
mailmodo *Mailmodo
xrpcc *xrpc.Client xrpcc *xrpc.Client
} }
@ -39,15 +36,10 @@ func serve(cctx *cli.Context) error {
debug := cctx.Bool("debug") debug := cctx.Bool("debug")
httpAddress := cctx.String("http-address") httpAddress := cctx.String("http-address")
appviewHost := cctx.String("appview-host") appviewHost := cctx.String("appview-host")
mailmodoAPIKey := cctx.String("mailmodo-api-key")
mailmodoListName := cctx.String("mailmodo-list-name")
// Echo // Echo
e := echo.New() e := echo.New()
// Mailmodo client.
mailmodo := NewMailmodo(mailmodoAPIKey, mailmodoListName)
// create a new session (no auth) // create a new session (no auth)
xrpcc := &xrpc.Client{ xrpcc := &xrpc.Client{
Client: cliutil.NewHttpClient(), Client: cliutil.NewHttpClient(),
@ -78,7 +70,6 @@ func serve(cctx *cli.Context) error {
// //
server := &Server{ server := &Server{
echo: e, echo: e,
mailmodo: mailmodo,
xrpcc: xrpcc, xrpcc: xrpcc,
} }
@ -221,9 +212,6 @@ func serve(cctx *cli.Context) error {
e.GET("/profile/:handleOrDID/post/:rkey/liked-by", server.WebGeneric) e.GET("/profile/:handleOrDID/post/:rkey/liked-by", server.WebGeneric)
e.GET("/profile/:handleOrDID/post/:rkey/reposted-by", server.WebGeneric) e.GET("/profile/:handleOrDID/post/:rkey/reposted-by", server.WebGeneric)
// Mailmodo
e.POST("/api/waitlist", server.apiWaitlist)
// Start the server. // Start the server.
log.Infof("starting server address=%s", httpAddress) log.Infof("starting server address=%s", httpAddress)
go func() { go func() {
@ -398,36 +386,3 @@ func (srv *Server) WebProfile(c echo.Context) error {
data["requestHost"] = req.Host data["requestHost"] = req.Host
return c.Render(http.StatusOK, "profile.html", data) return c.Render(http.StatusOK, "profile.html", data)
} }
func (srv *Server) apiWaitlist(c echo.Context) error {
type jsonError struct {
Error string `json:"error"`
}
// Read the API request.
type apiRequest struct {
Email string `json:"email"`
}
bodyReader := http.MaxBytesReader(c.Response(), c.Request().Body, 16*1024)
payload, err := ioutil.ReadAll(bodyReader)
if err != nil {
return err
}
var req apiRequest
if err := json.Unmarshal(payload, &req); err != nil {
return c.JSON(http.StatusBadRequest, jsonError{Error: "Invalid API request"})
}
if req.Email == "" {
return c.JSON(http.StatusBadRequest, jsonError{Error: "Please enter a valid email address."})
}
if err := srv.mailmodo.AddToList(c.Request().Context(), req.Email); err != nil {
log.Errorf("adding email to waitlist failed: %s", err)
return c.JSON(http.StatusBadRequest, jsonError{
Error: "Storing email in waitlist failed. Please enter a valid email address.",
})
}
return c.JSON(http.StatusOK, map[string]bool{"success": true})
}