mirror of
https://github.com/christianselig/apollo-backend
synced 2024-11-25 21:27:42 +00:00
HTTP code updates
This commit is contained in:
parent
17a37a3b01
commit
0afa10d64d
8 changed files with 82 additions and 68 deletions
|
@ -25,7 +25,7 @@ func (a *api) notificationsAccountHandler(w http.ResponseWriter, r *http.Request
|
||||||
|
|
||||||
anr := &accountNotificationsRequest{}
|
anr := &accountNotificationsRequest{}
|
||||||
if err := json.NewDecoder(r.Body).Decode(anr); err != nil {
|
if err := json.NewDecoder(r.Body).Decode(anr); err != nil {
|
||||||
a.errorResponse(w, r, 500, err.Error())
|
a.errorResponse(w, r, 500, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -35,18 +35,18 @@ func (a *api) notificationsAccountHandler(w http.ResponseWriter, r *http.Request
|
||||||
|
|
||||||
dev, err := a.deviceRepo.GetByAPNSToken(ctx, apns)
|
dev, err := a.deviceRepo.GetByAPNSToken(ctx, apns)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
a.errorResponse(w, r, 500, err.Error())
|
a.errorResponse(w, r, 500, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
acct, err := a.accountRepo.GetByRedditID(ctx, rid)
|
acct, err := a.accountRepo.GetByRedditID(ctx, rid)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
a.errorResponse(w, r, 500, err.Error())
|
a.errorResponse(w, r, 500, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := a.deviceRepo.SetNotifiable(ctx, &dev, &acct, anr.InboxNotifications, anr.WatcherNotifications, anr.GlobalMute); err != nil {
|
if err := a.deviceRepo.SetNotifiable(ctx, &dev, &acct, anr.InboxNotifications, anr.WatcherNotifications, anr.GlobalMute); err != nil {
|
||||||
a.errorResponse(w, r, 500, err.Error())
|
a.errorResponse(w, r, 500, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -62,19 +62,19 @@ func (a *api) getNotificationsAccountHandler(w http.ResponseWriter, r *http.Requ
|
||||||
|
|
||||||
dev, err := a.deviceRepo.GetByAPNSToken(ctx, apns)
|
dev, err := a.deviceRepo.GetByAPNSToken(ctx, apns)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
a.errorResponse(w, r, 500, err.Error())
|
a.errorResponse(w, r, 500, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
acct, err := a.accountRepo.GetByRedditID(ctx, rid)
|
acct, err := a.accountRepo.GetByRedditID(ctx, rid)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
a.errorResponse(w, r, 500, err.Error())
|
a.errorResponse(w, r, 500, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
inbox, watchers, global, err := a.deviceRepo.GetNotifiable(ctx, &dev, &acct)
|
inbox, watchers, global, err := a.deviceRepo.GetNotifiable(ctx, &dev, &acct)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
a.errorResponse(w, r, 500, err.Error())
|
a.errorResponse(w, r, 500, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -93,18 +93,18 @@ func (a *api) disassociateAccountHandler(w http.ResponseWriter, r *http.Request)
|
||||||
|
|
||||||
dev, err := a.deviceRepo.GetByAPNSToken(ctx, apns)
|
dev, err := a.deviceRepo.GetByAPNSToken(ctx, apns)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
a.errorResponse(w, r, 500, err.Error())
|
a.errorResponse(w, r, 500, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
acct, err := a.accountRepo.GetByRedditID(ctx, rid)
|
acct, err := a.accountRepo.GetByRedditID(ctx, rid)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
a.errorResponse(w, r, 500, err.Error())
|
a.errorResponse(w, r, 500, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := a.accountRepo.Disassociate(ctx, &acct, &dev); err != nil {
|
if err := a.accountRepo.Disassociate(ctx, &acct, &dev); err != nil {
|
||||||
a.errorResponse(w, r, 500, err.Error())
|
a.errorResponse(w, r, 500, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -119,13 +119,13 @@ func (a *api) upsertAccountsHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
dev, err := a.deviceRepo.GetByAPNSToken(ctx, apns)
|
dev, err := a.deviceRepo.GetByAPNSToken(ctx, apns)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
a.errorResponse(w, r, 422, err.Error())
|
a.errorResponse(w, r, 422, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
laccs, err := a.accountRepo.GetByAPNSToken(ctx, apns)
|
laccs, err := a.accountRepo.GetByAPNSToken(ctx, apns)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
a.errorResponse(w, r, 422, err.Error())
|
a.errorResponse(w, r, 422, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -136,7 +136,7 @@ func (a *api) upsertAccountsHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
var raccs []domain.Account
|
var raccs []domain.Account
|
||||||
if err := json.NewDecoder(r.Body).Decode(&raccs); err != nil {
|
if err := json.NewDecoder(r.Body).Decode(&raccs); err != nil {
|
||||||
a.errorResponse(w, r, 422, err.Error())
|
a.errorResponse(w, r, 422, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
for _, acc := range raccs {
|
for _, acc := range raccs {
|
||||||
|
@ -145,7 +145,7 @@ func (a *api) upsertAccountsHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
ac := a.reddit.NewAuthenticatedClient(reddit.SkipRateLimiting, acc.RefreshToken, acc.AccessToken)
|
ac := a.reddit.NewAuthenticatedClient(reddit.SkipRateLimiting, acc.RefreshToken, acc.AccessToken)
|
||||||
tokens, err := ac.RefreshTokens(ctx)
|
tokens, err := ac.RefreshTokens(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
a.errorResponse(w, r, 422, err.Error())
|
a.errorResponse(w, r, 422, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -158,12 +158,13 @@ func (a *api) upsertAccountsHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
me, err := ac.Me(ctx)
|
me, err := ac.Me(ctx)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
a.errorResponse(w, r, 422, err.Error())
|
a.errorResponse(w, r, 422, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if me.NormalizedUsername() != acc.NormalizedUsername() {
|
if me.NormalizedUsername() != acc.NormalizedUsername() {
|
||||||
a.errorResponse(w, r, 422, "nice try")
|
err := fmt.Errorf("wrong user: expected %s, got %s", me.NormalizedUsername(), acc.NormalizedUsername())
|
||||||
|
a.errorResponse(w, r, 401, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -171,12 +172,12 @@ func (a *api) upsertAccountsHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
acc.AccountID = me.ID
|
acc.AccountID = me.ID
|
||||||
|
|
||||||
if err := a.accountRepo.CreateOrUpdate(ctx, &acc); err != nil {
|
if err := a.accountRepo.CreateOrUpdate(ctx, &acc); err != nil {
|
||||||
a.errorResponse(w, r, 422, err.Error())
|
a.errorResponse(w, r, 422, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := a.accountRepo.Associate(ctx, &acc, &dev); err != nil {
|
if err := a.accountRepo.Associate(ctx, &acc, &dev); err != nil {
|
||||||
a.errorResponse(w, r, 422, err.Error())
|
a.errorResponse(w, r, 422, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -218,7 +219,7 @@ func (a *api) upsertAccountHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
a.logger.WithFields(logrus.Fields{
|
a.logger.WithFields(logrus.Fields{
|
||||||
"err": err,
|
"err": err,
|
||||||
}).Info("failed to parse request json")
|
}).Info("failed to parse request json")
|
||||||
a.errorResponse(w, r, 422, err.Error())
|
a.errorResponse(w, r, 422, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -229,7 +230,7 @@ func (a *api) upsertAccountHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
a.logger.WithFields(logrus.Fields{
|
a.logger.WithFields(logrus.Fields{
|
||||||
"err": err,
|
"err": err,
|
||||||
}).Info("failed to refresh token")
|
}).Info("failed to refresh token")
|
||||||
a.errorResponse(w, r, 422, err.Error())
|
a.errorResponse(w, r, 422, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -245,15 +246,14 @@ func (a *api) upsertAccountHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
a.logger.WithFields(logrus.Fields{
|
a.logger.WithFields(logrus.Fields{
|
||||||
"err": err,
|
"err": err,
|
||||||
}).Info("failed to grab user details from Reddit")
|
}).Info("failed to grab user details from Reddit")
|
||||||
a.errorResponse(w, r, 500, err.Error())
|
a.errorResponse(w, r, 500, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if me.NormalizedUsername() != acct.NormalizedUsername() {
|
if me.NormalizedUsername() != acct.NormalizedUsername() {
|
||||||
a.logger.WithFields(logrus.Fields{
|
err := fmt.Errorf("wrong user: expected %s, got %s", me.NormalizedUsername(), acct.NormalizedUsername())
|
||||||
"err": err,
|
a.logger.WithFields(logrus.Fields{"err": err}).Warn("user is not who they say they are")
|
||||||
}).Info("user is not who they say they are")
|
a.errorResponse(w, r, 401, err)
|
||||||
a.errorResponse(w, r, 422, "nice try")
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -266,7 +266,7 @@ func (a *api) upsertAccountHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
a.logger.WithFields(logrus.Fields{
|
a.logger.WithFields(logrus.Fields{
|
||||||
"err": err,
|
"err": err,
|
||||||
}).Info("failed fetching device from database")
|
}).Info("failed fetching device from database")
|
||||||
a.errorResponse(w, r, 500, err.Error())
|
a.errorResponse(w, r, 500, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -275,7 +275,7 @@ func (a *api) upsertAccountHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
a.logger.WithFields(logrus.Fields{
|
a.logger.WithFields(logrus.Fields{
|
||||||
"err": err,
|
"err": err,
|
||||||
}).Info("failed updating account in database")
|
}).Info("failed updating account in database")
|
||||||
a.errorResponse(w, r, 500, err.Error())
|
a.errorResponse(w, r, 500, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -283,7 +283,7 @@ func (a *api) upsertAccountHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
a.logger.WithFields(logrus.Fields{
|
a.logger.WithFields(logrus.Fields{
|
||||||
"err": err,
|
"err": err,
|
||||||
}).Info("failed associating account with device")
|
}).Info("failed associating account with device")
|
||||||
a.errorResponse(w, r, 500, err.Error())
|
a.errorResponse(w, r, 500, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -128,7 +128,7 @@ func (a *api) Routes() *mux.Router {
|
||||||
|
|
||||||
func (a *api) testBugsnagHandler(w http.ResponseWriter, r *http.Request) {
|
func (a *api) testBugsnagHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
if err := bugsnag.Notify(fmt.Errorf("Test error")); err != nil {
|
if err := bugsnag.Notify(fmt.Errorf("Test error")); err != nil {
|
||||||
a.errorResponse(w, r, 500, err.Error())
|
a.errorResponse(w, r, 500, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
w.WriteHeader(http.StatusOK)
|
w.WriteHeader(http.StatusOK)
|
||||||
|
|
|
@ -15,7 +15,7 @@ type sendMessageRequest struct {
|
||||||
func (a *api) contactHandler(w http.ResponseWriter, r *http.Request) {
|
func (a *api) contactHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
smr := &sendMessageRequest{}
|
smr := &sendMessageRequest{}
|
||||||
if err := json.NewDecoder(r.Body).Decode(smr); err != nil {
|
if err := json.NewDecoder(r.Body).Decode(smr); err != nil {
|
||||||
a.errorResponse(w, r, 500, err.Error())
|
a.errorResponse(w, r, 500, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -27,7 +27,7 @@ func (a *api) contactHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if _, err := smtp2go.Send(msg); err != nil {
|
if _, err := smtp2go.Send(msg); err != nil {
|
||||||
a.errorResponse(w, r, 500, err.Error())
|
a.errorResponse(w, r, 500, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
w.WriteHeader(http.StatusOK)
|
w.WriteHeader(http.StatusOK)
|
||||||
|
|
|
@ -23,7 +23,7 @@ func (a *api) upsertDeviceHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
d := &domain.Device{}
|
d := &domain.Device{}
|
||||||
if err := json.NewDecoder(r.Body).Decode(d); err != nil {
|
if err := json.NewDecoder(r.Body).Decode(d); err != nil {
|
||||||
a.errorResponse(w, r, 500, err.Error())
|
a.errorResponse(w, r, 500, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -31,7 +31,7 @@ func (a *api) upsertDeviceHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
d.GracePeriodExpiresAt = d.ExpiresAt.Add(domain.DeviceGracePeriodAfterReceiptExpiry)
|
d.GracePeriodExpiresAt = d.ExpiresAt.Add(domain.DeviceGracePeriodAfterReceiptExpiry)
|
||||||
|
|
||||||
if err := a.deviceRepo.CreateOrUpdate(ctx, d); err != nil {
|
if err := a.deviceRepo.CreateOrUpdate(ctx, d); err != nil {
|
||||||
a.errorResponse(w, r, 500, err.Error())
|
a.errorResponse(w, r, 500, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -49,13 +49,13 @@ func (a *api) testDeviceHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
a.logger.WithFields(logrus.Fields{
|
a.logger.WithFields(logrus.Fields{
|
||||||
"err": err,
|
"err": err,
|
||||||
}).Info("failed fetching device from database")
|
}).Info("failed fetching device from database")
|
||||||
a.errorResponse(w, r, 500, err.Error())
|
a.errorResponse(w, r, 500, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
accs, err := a.accountRepo.GetByAPNSToken(ctx, tok)
|
accs, err := a.accountRepo.GetByAPNSToken(ctx, tok)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
a.errorResponse(w, r, 500, err.Error())
|
a.errorResponse(w, r, 500, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -86,7 +86,7 @@ func (a *api) testDeviceHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
a.logger.WithFields(logrus.Fields{
|
a.logger.WithFields(logrus.Fields{
|
||||||
"err": err,
|
"err": err,
|
||||||
}).Info("failed to send test notification")
|
}).Info("failed to send test notification")
|
||||||
a.errorResponse(w, r, 500, err.Error())
|
a.errorResponse(w, r, 500, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
w.WriteHeader(http.StatusOK)
|
w.WriteHeader(http.StatusOK)
|
||||||
|
@ -99,13 +99,13 @@ func (a *api) deleteDeviceHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
dev, err := a.deviceRepo.GetByAPNSToken(ctx, vars["apns"])
|
dev, err := a.deviceRepo.GetByAPNSToken(ctx, vars["apns"])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
a.errorResponse(w, r, 500, err.Error())
|
a.errorResponse(w, r, 500, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
accs, err := a.accountRepo.GetByAPNSToken(ctx, vars["apns"])
|
accs, err := a.accountRepo.GetByAPNSToken(ctx, vars["apns"])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
a.errorResponse(w, r, 500, err.Error())
|
a.errorResponse(w, r, 500, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -2,7 +2,7 @@ package api
|
||||||
|
|
||||||
import "net/http"
|
import "net/http"
|
||||||
|
|
||||||
func (a *api) errorResponse(w http.ResponseWriter, _ *http.Request, status int, message string) {
|
func (a *api) errorResponse(w http.ResponseWriter, _ *http.Request, status int, err error) {
|
||||||
w.Header().Set("X-Apollo-Error", message)
|
w.Header().Set("X-Apollo-Error", err.Error())
|
||||||
http.Error(w, message, status)
|
http.Error(w, err.Error(), status)
|
||||||
}
|
}
|
||||||
|
|
|
@ -34,7 +34,7 @@ func generateNotificationTester(a *api, fun notificationGenerator) func(w http.R
|
||||||
a.logger.WithFields(logrus.Fields{
|
a.logger.WithFields(logrus.Fields{
|
||||||
"err": err,
|
"err": err,
|
||||||
}).Info("failed fetching device from database")
|
}).Info("failed fetching device from database")
|
||||||
a.errorResponse(w, r, 500, err.Error())
|
a.errorResponse(w, r, 500, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -59,7 +59,7 @@ func generateNotificationTester(a *api, fun notificationGenerator) func(w http.R
|
||||||
a.logger.WithFields(logrus.Fields{
|
a.logger.WithFields(logrus.Fields{
|
||||||
"err": err,
|
"err": err,
|
||||||
}).Info("failed to send test notification")
|
}).Info("failed to send test notification")
|
||||||
a.errorResponse(w, r, 500, err.Error())
|
a.errorResponse(w, r, 500, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
w.WriteHeader(http.StatusOK)
|
w.WriteHeader(http.StatusOK)
|
||||||
|
|
|
@ -26,14 +26,14 @@ func (a *api) checkReceiptHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
a.logger.WithFields(logrus.Fields{
|
a.logger.WithFields(logrus.Fields{
|
||||||
"err": err,
|
"err": err,
|
||||||
}).Info("failed verifying receipt")
|
}).Info("failed verifying receipt")
|
||||||
a.errorResponse(w, r, 500, err.Error())
|
a.errorResponse(w, r, 500, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if apns != "" {
|
if apns != "" {
|
||||||
dev, err := a.deviceRepo.GetByAPNSToken(ctx, apns)
|
dev, err := a.deviceRepo.GetByAPNSToken(ctx, apns)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
a.errorResponse(w, r, 500, err.Error())
|
a.errorResponse(w, r, 500, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -45,7 +45,7 @@ func (a *api) checkReceiptHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
accs, err := a.accountRepo.GetByAPNSToken(ctx, apns)
|
accs, err := a.accountRepo.GetByAPNSToken(ctx, apns)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
a.errorResponse(w, r, 500, err.Error())
|
a.errorResponse(w, r, 500, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -2,6 +2,8 @@ package api
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
@ -53,29 +55,30 @@ func (a *api) createWatcherHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
Criteria: watcherCriteria{},
|
Criteria: watcherCriteria{},
|
||||||
}
|
}
|
||||||
if err := json.NewDecoder(r.Body).Decode(cwr); err != nil {
|
if err := json.NewDecoder(r.Body).Decode(cwr); err != nil {
|
||||||
a.errorResponse(w, r, 500, err.Error())
|
a.errorResponse(w, r, 500, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := cwr.Validate(); err != nil {
|
if err := cwr.Validate(); err != nil {
|
||||||
a.errorResponse(w, r, 422, err.Error())
|
a.errorResponse(w, r, 422, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
dev, err := a.deviceRepo.GetByAPNSToken(ctx, apns)
|
dev, err := a.deviceRepo.GetByAPNSToken(ctx, apns)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
a.errorResponse(w, r, 422, err.Error())
|
a.errorResponse(w, r, 422, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
accs, err := a.accountRepo.GetByAPNSToken(ctx, apns)
|
accs, err := a.accountRepo.GetByAPNSToken(ctx, apns)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
a.errorResponse(w, r, 422, err.Error())
|
a.errorResponse(w, r, 422, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(accs) == 0 {
|
if len(accs) == 0 {
|
||||||
a.errorResponse(w, r, 422, "can't create watchers without accounts")
|
err := errors.New("cannot create watchers without account")
|
||||||
|
a.errorResponse(w, r, 422, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -89,7 +92,8 @@ func (a *api) createWatcherHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if !found {
|
if !found {
|
||||||
a.errorResponse(w, r, 422, "yeah nice try")
|
err := errors.New("account not associated with device")
|
||||||
|
a.errorResponse(w, r, 401, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -110,7 +114,7 @@ func (a *api) createWatcherHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
if cwr.Type == "subreddit" || cwr.Type == "trending" {
|
if cwr.Type == "subreddit" || cwr.Type == "trending" {
|
||||||
srr, err := ac.SubredditAbout(ctx, cwr.Subreddit)
|
srr, err := ac.SubredditAbout(ctx, cwr.Subreddit)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
a.errorResponse(w, r, 422, err.Error())
|
a.errorResponse(w, r, 422, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -122,7 +126,7 @@ func (a *api) createWatcherHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
sr = domain.Subreddit{SubredditID: srr.ID, Name: srr.Name}
|
sr = domain.Subreddit{SubredditID: srr.ID, Name: srr.Name}
|
||||||
_ = a.subredditRepo.CreateOrUpdate(ctx, &sr)
|
_ = a.subredditRepo.CreateOrUpdate(ctx, &sr)
|
||||||
default:
|
default:
|
||||||
a.errorResponse(w, r, 500, err.Error())
|
a.errorResponse(w, r, 500, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -139,12 +143,13 @@ func (a *api) createWatcherHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
} else if cwr.Type == "user" {
|
} else if cwr.Type == "user" {
|
||||||
urr, err := ac.UserAbout(ctx, cwr.User)
|
urr, err := ac.UserAbout(ctx, cwr.User)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
a.errorResponse(w, r, 500, err.Error())
|
a.errorResponse(w, r, 500, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if !urr.AcceptFollowers {
|
if !urr.AcceptFollowers {
|
||||||
a.errorResponse(w, r, 422, "no followers accepted")
|
err := errors.New("user has followers disabled")
|
||||||
|
a.errorResponse(w, r, 403, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -152,19 +157,20 @@ func (a *api) createWatcherHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
err = a.userRepo.CreateOrUpdate(ctx, &u)
|
err = a.userRepo.CreateOrUpdate(ctx, &u)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
a.errorResponse(w, r, 500, err.Error())
|
a.errorResponse(w, r, 500, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
watcher.Type = domain.UserWatcher
|
watcher.Type = domain.UserWatcher
|
||||||
watcher.WatcheeID = u.ID
|
watcher.WatcheeID = u.ID
|
||||||
} else {
|
} else {
|
||||||
a.errorResponse(w, r, 422, "unknown watcher type")
|
err := fmt.Errorf("unknown watcher type: %s", cwr.Type)
|
||||||
|
a.errorResponse(w, r, 422, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := a.watcherRepo.Create(ctx, &watcher); err != nil {
|
if err := a.watcherRepo.Create(ctx, &watcher); err != nil {
|
||||||
a.errorResponse(w, r, 422, err.Error())
|
a.errorResponse(w, r, 422, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -179,13 +185,17 @@ func (a *api) editWatcherHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
vars := mux.Vars(r)
|
vars := mux.Vars(r)
|
||||||
id, err := strconv.ParseInt(vars["watcherID"], 10, 64)
|
id, err := strconv.ParseInt(vars["watcherID"], 10, 64)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
a.errorResponse(w, r, 422, err.Error())
|
a.errorResponse(w, r, 422, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
watcher, err := a.watcherRepo.GetByID(ctx, id)
|
watcher, err := a.watcherRepo.GetByID(ctx, id)
|
||||||
if err != nil || watcher.Device.APNSToken != vars["apns"] {
|
if err != nil {
|
||||||
a.errorResponse(w, r, 422, "nice try")
|
a.errorResponse(w, r, 422, err)
|
||||||
|
return
|
||||||
|
} else if watcher.Device.APNSToken != vars["apns"] {
|
||||||
|
err := fmt.Errorf("wrong device for watcher %d", watcher.ID)
|
||||||
|
a.errorResponse(w, r, 422, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -194,7 +204,7 @@ func (a *api) editWatcherHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := json.NewDecoder(r.Body).Decode(ewr); err != nil {
|
if err := json.NewDecoder(r.Body).Decode(ewr); err != nil {
|
||||||
a.errorResponse(w, r, 500, err.Error())
|
a.errorResponse(w, r, 500, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -207,7 +217,7 @@ func (a *api) editWatcherHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
watcher.Domain = strings.ToLower(ewr.Criteria.Domain)
|
watcher.Domain = strings.ToLower(ewr.Criteria.Domain)
|
||||||
|
|
||||||
if err := a.watcherRepo.Update(ctx, &watcher); err != nil {
|
if err := a.watcherRepo.Update(ctx, &watcher); err != nil {
|
||||||
a.errorResponse(w, r, 500, err.Error())
|
a.errorResponse(w, r, 500, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -220,13 +230,17 @@ func (a *api) deleteWatcherHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
vars := mux.Vars(r)
|
vars := mux.Vars(r)
|
||||||
id, err := strconv.ParseInt(vars["watcherID"], 10, 64)
|
id, err := strconv.ParseInt(vars["watcherID"], 10, 64)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
a.errorResponse(w, r, 422, err.Error())
|
a.errorResponse(w, r, 422, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
watcher, err := a.watcherRepo.GetByID(ctx, id)
|
watcher, err := a.watcherRepo.GetByID(ctx, id)
|
||||||
if err != nil || watcher.Device.APNSToken != vars["apns"] {
|
if err != nil {
|
||||||
a.errorResponse(w, r, 422, "nice try")
|
a.errorResponse(w, r, 422, err)
|
||||||
|
return
|
||||||
|
} else if watcher.Device.APNSToken != vars["apns"] {
|
||||||
|
err := fmt.Errorf("wrong device for watcher %d", watcher.ID)
|
||||||
|
a.errorResponse(w, r, 422, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -257,7 +271,7 @@ func (a *api) listWatchersHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
watchers, err := a.watcherRepo.GetByDeviceAPNSTokenAndAccountRedditID(ctx, apns, redditID)
|
watchers, err := a.watcherRepo.GetByDeviceAPNSTokenAndAccountRedditID(ctx, apns, redditID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
a.errorResponse(w, r, 400, err.Error())
|
a.errorResponse(w, r, 400, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue