apollo-backend/internal/api/watcher.go

261 lines
5.6 KiB
Go
Raw Permalink Normal View History

2021-09-25 16:56:01 +00:00
package api
import (
"context"
"encoding/json"
"net/http"
"strconv"
2021-10-09 14:59:20 +00:00
"strings"
2021-09-25 16:56:01 +00:00
"github.com/christianselig/apollo-backend/internal/domain"
"github.com/gorilla/mux"
)
type watcherCriteria struct {
2021-10-10 15:51:42 +00:00
Author string
2021-09-25 16:56:01 +00:00
Upvotes int64
Keyword string
Flair string
Domain string
}
type createWatcherRequest struct {
2021-10-09 14:59:20 +00:00
Type string
User string
2021-09-25 16:56:01 +00:00
Subreddit string
2021-10-10 15:51:42 +00:00
Label string
2021-09-25 16:56:01 +00:00
Criteria watcherCriteria
}
func (a *api) createWatcherHandler(w http.ResponseWriter, r *http.Request) {
ctx := context.Background()
vars := mux.Vars(r)
apns := vars["apns"]
redditID := vars["redditID"]
2021-09-25 18:02:00 +00:00
cwr := &createWatcherRequest{
Criteria: watcherCriteria{
Upvotes: 0,
Keyword: "",
Flair: "",
Domain: "",
},
}
2021-09-25 16:56:01 +00:00
if err := json.NewDecoder(r.Body).Decode(cwr); err != nil {
a.errorResponse(w, r, 500, err.Error())
return
}
dev, err := a.deviceRepo.GetByAPNSToken(ctx, apns)
if err != nil {
a.errorResponse(w, r, 422, err.Error())
return
}
accs, err := a.accountRepo.GetByAPNSToken(ctx, apns)
if err != nil || len(accs) == 0 {
a.errorResponse(w, r, 422, err.Error())
return
}
account := accs[0]
found := false
for _, acc := range accs {
if acc.AccountID == redditID {
found = true
account = acc
}
}
if !found {
a.errorResponse(w, r, 422, "yeah nice try")
return
}
ac := a.reddit.NewAuthenticatedClient(account.RefreshToken, account.AccessToken)
2021-10-09 14:59:20 +00:00
watcher := domain.Watcher{
2021-10-10 15:51:42 +00:00
Label: cwr.Label,
2021-10-09 14:59:20 +00:00
DeviceID: dev.ID,
AccountID: account.ID,
2021-10-10 15:51:42 +00:00
Author: strings.ToLower(cwr.Criteria.Author),
2021-10-09 14:59:20 +00:00
Upvotes: cwr.Criteria.Upvotes,
Keyword: strings.ToLower(cwr.Criteria.Keyword),
Flair: strings.ToLower(cwr.Criteria.Flair),
Domain: strings.ToLower(cwr.Criteria.Domain),
2021-09-25 16:56:01 +00:00
}
2021-10-10 15:51:42 +00:00
if cwr.Type == "subreddit" || cwr.Type == "trending" {
2021-10-09 14:59:20 +00:00
srr, err := ac.SubredditAbout(cwr.Subreddit)
if err != nil {
a.errorResponse(w, r, 422, err.Error())
return
}
sr, err := a.subredditRepo.GetByName(ctx, cwr.Subreddit)
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.Error())
return
}
}
2021-10-10 15:51:42 +00:00
switch cwr.Type {
case "subreddit":
watcher.Type = domain.SubredditWatcher
case "trending":
watcher.Type = domain.TrendingWatcher
}
2021-10-09 14:59:20 +00:00
watcher.WatcheeID = sr.ID
} else if cwr.Type == "user" {
urr, err := ac.UserAbout(cwr.User)
if err != nil {
2021-09-25 18:02:00 +00:00
a.errorResponse(w, r, 500, err.Error())
return
}
2021-09-25 16:56:01 +00:00
2021-10-09 14:59:20 +00:00
if !urr.AcceptFollowers {
a.errorResponse(w, r, 422, "no followers accepted")
return
}
u := domain.User{UserID: urr.ID, Name: urr.Name}
err = a.userRepo.CreateOrUpdate(ctx, &u)
if err != nil {
a.errorResponse(w, r, 500, err.Error())
return
}
watcher.Type = domain.UserWatcher
watcher.WatcheeID = u.ID
} else {
a.errorResponse(w, r, 422, "unknown watcher type")
return
2021-09-25 16:56:01 +00:00
}
if err := a.watcherRepo.Create(ctx, &watcher); err != nil {
a.errorResponse(w, r, 422, err.Error())
return
}
w.WriteHeader(http.StatusOK)
}
2021-10-10 15:51:42 +00:00
func (a *api) editWatcherHandler(w http.ResponseWriter, r *http.Request) {
2021-09-25 16:56:01 +00:00
ctx := context.Background()
vars := mux.Vars(r)
id, err := strconv.ParseInt(vars["watcherID"], 10, 64)
if err != nil {
a.errorResponse(w, r, 422, err.Error())
return
}
2021-10-10 15:51:42 +00:00
watcher, err := a.watcherRepo.GetByID(ctx, id)
if err != nil || watcher.Device.APNSToken != vars["apns"] {
a.errorResponse(w, r, 422, "nice try")
return
}
2021-09-25 16:56:01 +00:00
2021-10-10 15:51:42 +00:00
ewr := &createWatcherRequest{
Criteria: watcherCriteria{
Upvotes: 0,
Keyword: "",
Flair: "",
Domain: "",
},
}
if err := json.NewDecoder(r.Body).Decode(ewr); err != nil {
a.errorResponse(w, r, 500, err.Error())
return
}
watcher.Label = ewr.Label
watcher.Upvotes = ewr.Criteria.Upvotes
watcher.Keyword = ewr.Criteria.Keyword
watcher.Flair = ewr.Criteria.Flair
watcher.Domain = ewr.Criteria.Domain
if err := a.watcherRepo.Update(ctx, &watcher); err != nil {
a.errorResponse(w, r, 500, err.Error())
return
}
w.WriteHeader(http.StatusOK)
}
func (a *api) deleteWatcherHandler(w http.ResponseWriter, r *http.Request) {
ctx := context.Background()
vars := mux.Vars(r)
id, err := strconv.ParseInt(vars["watcherID"], 10, 64)
2021-09-25 16:56:01 +00:00
if err != nil {
a.errorResponse(w, r, 422, err.Error())
return
}
watcher, err := a.watcherRepo.GetByID(ctx, id)
2021-10-10 15:51:42 +00:00
if err != nil || watcher.Device.APNSToken != vars["apns"] {
2021-09-25 16:56:01 +00:00
a.errorResponse(w, r, 422, "nice try")
return
}
_ = a.watcherRepo.Delete(ctx, id)
w.WriteHeader(http.StatusOK)
}
2021-09-25 18:17:23 +00:00
type watcherItem struct {
2021-10-10 15:51:42 +00:00
ID int64
CreatedAt float64
Type string
Label string
Upvotes int64
Keyword string
Flair string
Domain string
Hits int64
2021-09-25 18:17:23 +00:00
}
func (a *api) listWatchersHandler(w http.ResponseWriter, r *http.Request) {
ctx := context.Background()
vars := mux.Vars(r)
apns := vars["apns"]
redditID := vars["redditID"]
watchers, err := a.watcherRepo.GetByDeviceAPNSTokenAndAccountRedditID(ctx, apns, redditID)
if err != nil {
a.errorResponse(w, r, 400, err.Error())
return
}
wis := make([]watcherItem, len(watchers))
for i, watcher := range watchers {
wi := watcherItem{
2021-10-10 15:51:42 +00:00
ID: watcher.ID,
CreatedAt: watcher.CreatedAt,
Type: watcher.Type.String(),
Label: watcher.Label,
Upvotes: watcher.Upvotes,
Keyword: watcher.Keyword,
Flair: watcher.Flair,
Domain: watcher.Domain,
Hits: watcher.Hits,
2021-09-25 18:17:23 +00:00
}
wis[i] = wi
}
w.WriteHeader(http.StatusOK)
w.Header().Set("Content-Type", "application/json")
_ = json.NewEncoder(w).Encode(wis)
}