Bluesky app fork with some witchin' additions 💫
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

bskyweb: remove waitlist email endpoint (#3127)

authored by

Jake Gold and committed by
GitHub
7a592d81 f61d1e1f

+5 -132
-70
bskyweb/cmd/bskyweb/mailmodo.go
··· 1 - package main 2 - 3 - import ( 4 - "bytes" 5 - "context" 6 - "crypto/sha256" 7 - "encoding/json" 8 - "fmt" 9 - "net/http" 10 - "time" 11 - ) 12 - 13 - type Mailmodo struct { 14 - httpClient *http.Client 15 - APIKey string 16 - BaseURL string 17 - ListName string 18 - } 19 - 20 - func NewMailmodo(apiKey, listName string) *Mailmodo { 21 - return &Mailmodo{ 22 - APIKey: apiKey, 23 - BaseURL: "https://api.mailmodo.com/api/v1", 24 - httpClient: &http.Client{}, 25 - ListName: listName, 26 - } 27 - } 28 - 29 - func (m *Mailmodo) request(ctx context.Context, httpMethod string, apiMethod string, data any) error { 30 - endpoint := fmt.Sprintf("%s/%s", m.BaseURL, apiMethod) 31 - js, err := json.Marshal(data) 32 - if err != nil { 33 - return fmt.Errorf("Mailmodo JSON encoding failed: %w", err) 34 - } 35 - req, err := http.NewRequestWithContext(ctx, httpMethod, endpoint, bytes.NewBuffer(js)) 36 - if err != nil { 37 - return fmt.Errorf("Mailmodo HTTP creating request %s %s failed: %w", httpMethod, apiMethod, err) 38 - } 39 - req.Header.Set("mmApiKey", m.APIKey) 40 - req.Header.Set("Content-Type", "application/json") 41 - 42 - res, err := m.httpClient.Do(req) 43 - if err != nil { 44 - return fmt.Errorf("Mailmodo HTTP making request %s %s failed: %w", httpMethod, apiMethod, err) 45 - } 46 - defer res.Body.Close() 47 - 48 - status := struct { 49 - Success bool `json:"success"` 50 - Message string `json:"message"` 51 - }{} 52 - if err := json.NewDecoder(res.Body).Decode(&status); err != nil { 53 - return fmt.Errorf("Mailmodo HTTP parsing response %s %s failed: %w", httpMethod, apiMethod, err) 54 - } 55 - if !status.Success { 56 - return fmt.Errorf("Mailmodo API response %s %s failed: %s", httpMethod, apiMethod, status.Message) 57 - } 58 - return nil 59 - } 60 - 61 - func (m *Mailmodo) AddToList(ctx context.Context, email string) error { 62 - return m.request(ctx, "POST", "addToList", map[string]any{ 63 - "listName": m.ListName, 64 - "email": email, 65 - "data": map[string]any{ 66 - "email_hashed": fmt.Sprintf("%x", sha256.Sum256([]byte(email))), 67 - }, 68 - "created_at": time.Now().UTC().Format(time.RFC3339), 69 - }) 70 - }
-12
bskyweb/cmd/bskyweb/main.go
··· 41 41 EnvVars: []string{"ATP_APPVIEW_HOST", "ATP_PDS_HOST"}, 42 42 }, 43 43 &cli.StringFlag{ 44 - Name: "mailmodo-api-key", 45 - Usage: "Mailmodo API key", 46 - Required: false, 47 - EnvVars: []string{"MAILMODO_API_KEY"}, 48 - }, 49 - &cli.StringFlag{ 50 - Name: "mailmodo-list-name", 51 - Usage: "Mailmodo contact list to add email addresses to", 52 - Required: false, 53 - EnvVars: []string{"MAILMODO_LIST_NAME"}, 54 - }, 55 - &cli.StringFlag{ 56 44 Name: "http-address", 57 45 Usage: "Specify the local IP/port to bind to", 58 46 Required: false,
+5 -50
bskyweb/cmd/bskyweb/server.go
··· 2 2 3 3 import ( 4 4 "context" 5 - "encoding/json" 6 5 "errors" 7 6 "fmt" 8 7 "io/fs" 9 - "io/ioutil" 10 8 "net/http" 11 9 "os" 12 10 "os/signal" ··· 29 27 ) 30 28 31 29 type Server struct { 32 - echo *echo.Echo 33 - httpd *http.Server 34 - mailmodo *Mailmodo 35 - xrpcc *xrpc.Client 30 + echo *echo.Echo 31 + httpd *http.Server 32 + xrpcc *xrpc.Client 36 33 } 37 34 38 35 func serve(cctx *cli.Context) error { 39 36 debug := cctx.Bool("debug") 40 37 httpAddress := cctx.String("http-address") 41 38 appviewHost := cctx.String("appview-host") 42 - mailmodoAPIKey := cctx.String("mailmodo-api-key") 43 - mailmodoListName := cctx.String("mailmodo-list-name") 44 39 45 40 // Echo 46 41 e := echo.New() 47 - 48 - // Mailmodo client. 49 - mailmodo := NewMailmodo(mailmodoAPIKey, mailmodoListName) 50 42 51 43 // create a new session (no auth) 52 44 xrpcc := &xrpc.Client{ ··· 77 69 // server 78 70 // 79 71 server := &Server{ 80 - echo: e, 81 - mailmodo: mailmodo, 82 - xrpcc: xrpcc, 72 + echo: e, 73 + xrpcc: xrpcc, 83 74 } 84 75 85 76 // Create the HTTP server. ··· 220 211 e.GET("/profile/:handleOrDID/post/:rkey", server.WebPost) 221 212 e.GET("/profile/:handleOrDID/post/:rkey/liked-by", server.WebGeneric) 222 213 e.GET("/profile/:handleOrDID/post/:rkey/reposted-by", server.WebGeneric) 223 - 224 - // Mailmodo 225 - e.POST("/api/waitlist", server.apiWaitlist) 226 214 227 215 // Start the server. 228 216 log.Infof("starting server address=%s", httpAddress) ··· 398 386 data["requestHost"] = req.Host 399 387 return c.Render(http.StatusOK, "profile.html", data) 400 388 } 401 - 402 - func (srv *Server) apiWaitlist(c echo.Context) error { 403 - type jsonError struct { 404 - Error string `json:"error"` 405 - } 406 - 407 - // Read the API request. 408 - type apiRequest struct { 409 - Email string `json:"email"` 410 - } 411 - 412 - bodyReader := http.MaxBytesReader(c.Response(), c.Request().Body, 16*1024) 413 - payload, err := ioutil.ReadAll(bodyReader) 414 - if err != nil { 415 - return err 416 - } 417 - var req apiRequest 418 - if err := json.Unmarshal(payload, &req); err != nil { 419 - return c.JSON(http.StatusBadRequest, jsonError{Error: "Invalid API request"}) 420 - } 421 - 422 - if req.Email == "" { 423 - return c.JSON(http.StatusBadRequest, jsonError{Error: "Please enter a valid email address."}) 424 - } 425 - 426 - if err := srv.mailmodo.AddToList(c.Request().Context(), req.Email); err != nil { 427 - log.Errorf("adding email to waitlist failed: %s", err) 428 - return c.JSON(http.StatusBadRequest, jsonError{ 429 - Error: "Storing email in waitlist failed. Please enter a valid email address.", 430 - }) 431 - } 432 - return c.JSON(http.StatusOK, map[string]bool{"success": true}) 433 - }