Add subreddit validation and de-register from old API

This commit is contained in:
Andre Medeiros 2022-05-07 11:06:21 -04:00
parent 310567fde2
commit 8a8431fc0f
3 changed files with 30 additions and 9 deletions

View file

@ -183,6 +183,22 @@ func (a *api) upsertAccountsHandler(w http.ResponseWriter, r *http.Request) {
_ = a.accountRepo.Disassociate(ctx, &acc, &dev) _ = 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) w.WriteHeader(http.StatusOK)
} }

View file

@ -26,6 +26,7 @@ type api struct {
statsd *statsd.Client statsd *statsd.Client
reddit *reddit.Client reddit *reddit.Client
apns *token.Token apns *token.Token
httpClient *http.Client
accountRepo domain.AccountRepository accountRepo domain.AccountRepository
deviceRepo domain.DeviceRepository deviceRepo domain.DeviceRepository
@ -63,11 +64,14 @@ func NewAPI(ctx context.Context, logger *logrus.Logger, statsd *statsd.Client, r
watcherRepo := repository.NewPostgresWatcher(pool) watcherRepo := repository.NewPostgresWatcher(pool)
userRepo := repository.NewPostgresUser(pool) userRepo := repository.NewPostgresUser(pool)
client := &http.Client{}
return &api{ return &api{
logger: logger, logger: logger,
statsd: statsd, statsd: statsd,
reddit: reddit, reddit: reddit,
apns: apns, apns: apns,
httpClient: client,
accountRepo: accountRepo, accountRepo: accountRepo,
deviceRepo: deviceRepo, deviceRepo: deviceRepo,

View file

@ -2,6 +2,7 @@ package domain
import ( import (
"context" "context"
"regexp"
"strings" "strings"
validation "github.com/go-ozzo/ozzo-validation/v4" validation "github.com/go-ozzo/ozzo-validation/v4"
@ -22,7 +23,7 @@ func (sr *Subreddit) NormalizedName() string {
func (sr *Subreddit) Validate() error { func (sr *Subreddit) Validate() error {
return validation.ValidateStruct(sr, 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)), validation.Field(&sr.SubredditID, validation.Required, validation.Length(4, 9)),
) )
} }