From 8a8431fc0f3b7f3dbf9feadca4394985b3fe9e14 Mon Sep 17 00:00:00 2001 From: Andre Medeiros Date: Sat, 7 May 2022 11:06:21 -0400 Subject: [PATCH] Add subreddit validation and de-register from old API --- internal/api/accounts.go | 16 ++++++++++++++++ internal/api/api.go | 20 ++++++++++++-------- internal/domain/subreddit.go | 3 ++- 3 files changed, 30 insertions(+), 9 deletions(-) diff --git a/internal/api/accounts.go b/internal/api/accounts.go index 5b19018..1b0e0fa 100644 --- a/internal/api/accounts.go +++ b/internal/api/accounts.go @@ -183,6 +183,22 @@ func (a *api) upsertAccountsHandler(w http.ResponseWriter, r *http.Request) { _ = a.accountRepo.Disassociate(ctx, &acc, &dev) } + go func(apns string) { + url := fmt.Sprintf("https://apollopushserver.xyz/api/new-server-addition?apns_token=%s", apns) + req, err := http.NewRequest("POST", url, nil) + req.Header.Set("Authentication", "Bearer 98g5j89aurqwfcsp9khlnvgd38fa15") + + if err != nil { + a.logger.WithFields(logrus.Fields{ + "apns": apns, + }).Error(err) + return + } + + resp, err := a.httpClient.Do(req) + resp.Body.Close() + }(apns) + w.WriteHeader(http.StatusOK) } diff --git a/internal/api/api.go b/internal/api/api.go index 1766c31..44b1e26 100644 --- a/internal/api/api.go +++ b/internal/api/api.go @@ -22,10 +22,11 @@ import ( ) type api struct { - logger *logrus.Logger - statsd *statsd.Client - reddit *reddit.Client - apns *token.Token + logger *logrus.Logger + statsd *statsd.Client + reddit *reddit.Client + apns *token.Token + httpClient *http.Client accountRepo domain.AccountRepository deviceRepo domain.DeviceRepository @@ -63,11 +64,14 @@ func NewAPI(ctx context.Context, logger *logrus.Logger, statsd *statsd.Client, r watcherRepo := repository.NewPostgresWatcher(pool) userRepo := repository.NewPostgresUser(pool) + client := &http.Client{} + return &api{ - logger: logger, - statsd: statsd, - reddit: reddit, - apns: apns, + logger: logger, + statsd: statsd, + reddit: reddit, + apns: apns, + httpClient: client, accountRepo: accountRepo, deviceRepo: deviceRepo, diff --git a/internal/domain/subreddit.go b/internal/domain/subreddit.go index efb2b2e..5727264 100644 --- a/internal/domain/subreddit.go +++ b/internal/domain/subreddit.go @@ -2,6 +2,7 @@ package domain import ( "context" + "regexp" "strings" validation "github.com/go-ozzo/ozzo-validation/v4" @@ -22,7 +23,7 @@ func (sr *Subreddit) NormalizedName() string { func (sr *Subreddit) Validate() error { return validation.ValidateStruct(sr, - validation.Field(&sr.Name, validation.Required, validation.Length(3, 32)), + validation.Field(&sr.Name, validation.Required, validation.Length(3, 32), validation.Match(regexp.MustCompile("^(?!u_)[a-zA-Z0-9]\\w{1,19}$"))), validation.Field(&sr.SubredditID, validation.Required, validation.Length(4, 9)), ) }