From f6f13bbd2fe3bf831eeb7f9afb18295d53dfc205 Mon Sep 17 00:00:00 2001 From: Andre Medeiros Date: Sat, 21 May 2022 10:26:23 -0400 Subject: [PATCH] Allow editing watcher subreddits too --- internal/api/watcher.go | 65 +++++++++++++++++++++++-- internal/repository/postgres_watcher.go | 16 +++--- 2 files changed, 71 insertions(+), 10 deletions(-) diff --git a/internal/api/watcher.go b/internal/api/watcher.go index 23c21b6..ff71fc9 100644 --- a/internal/api/watcher.go +++ b/internal/api/watcher.go @@ -183,7 +183,11 @@ func (a *api) editWatcherHandler(w http.ResponseWriter, r *http.Request) { ctx := r.Context() vars := mux.Vars(r) - id, err := strconv.ParseInt(vars["watcherID"], 10, 64) + apns := vars["apns"] + wid := vars["watcherID"] + rid := vars["redditID"] + + id, err := strconv.ParseInt(wid, 10, 64) if err != nil { a.errorResponse(w, r, 422, err) return @@ -209,13 +213,68 @@ func (a *api) editWatcherHandler(w http.ResponseWriter, r *http.Request) { } watcher.Label = ewr.Label - watcher.Author = strings.ToLower(ewr.Criteria.Author) - watcher.Subreddit = strings.ToLower(ewr.Criteria.Subreddit) + watcher.Author = strings.ToLower(ewr.User) + watcher.Subreddit = strings.ToLower(ewr.Subreddit) watcher.Upvotes = ewr.Criteria.Upvotes watcher.Keyword = strings.ToLower(ewr.Criteria.Keyword) watcher.Flair = strings.ToLower(ewr.Criteria.Flair) watcher.Domain = strings.ToLower(ewr.Criteria.Domain) + if watcher.Type == domain.SubredditWatcher { + lsr := strings.ToLower(watcher.Subreddit) + if watcher.WatcheeLabel != lsr { + accs, err := a.accountRepo.GetByAPNSToken(ctx, apns) + if err != nil { + a.errorResponse(w, r, 422, err) + return + } + + if len(accs) == 0 { + err := errors.New("cannot create watchers without account") + a.errorResponse(w, r, 422, err) + return + } + + account := accs[0] + found := false + for _, acc := range accs { + if acc.AccountID == rid { + found = true + account = acc + } + } + + if !found { + err := errors.New("account not associated with device") + a.errorResponse(w, r, 401, err) + return + } + + ac := a.reddit.NewAuthenticatedClient(account.AccountID, account.RefreshToken, account.AccessToken) + + srr, err := ac.SubredditAbout(ctx, lsr) + if err != nil { + a.errorResponse(w, r, 422, err) + return + } + + sr, err := a.subredditRepo.GetByName(ctx, lsr) + if err != nil { + switch err { + case domain.ErrNotFound: + // Might be that we don't know about that subreddit yet + sr = domain.Subreddit{SubredditID: srr.ID, Name: srr.Name} + _ = a.subredditRepo.CreateOrUpdate(ctx, &sr) + default: + a.errorResponse(w, r, 500, err) + return + } + } + + watcher.WatcheeID = sr.ID + } + } + if err := a.watcherRepo.Update(ctx, &watcher); err != nil { a.errorResponse(w, r, 500, err) return diff --git a/internal/repository/postgres_watcher.go b/internal/repository/postgres_watcher.go index d8cfbbb..2f686dc 100644 --- a/internal/repository/postgres_watcher.go +++ b/internal/repository/postgres_watcher.go @@ -245,19 +245,21 @@ func (p *postgresWatcherRepository) Update(ctx context.Context, watcher *domain. query := ` UPDATE watchers - SET author = $2, - subreddit = $3, - upvotes = $4, - keyword = $5, - flair = $6, - domain = $7, - label = $8 + SET watchee_id = $2, + author = $3, + subreddit = $4, + upvotes = $5, + keyword = $6, + flair = $7, + domain = $8, + label = $9 WHERE id = $1` res, err := p.conn.Exec( ctx, query, watcher.ID, + watcher.WatcheeID, watcher.Author, watcher.Subreddit, watcher.Upvotes,