apollo-backend/internal/api/live_activities.go

44 lines
971 B
Go
Raw Permalink Normal View History

2022-10-19 13:37:41 +00:00
package api
import (
2022-11-01 02:33:11 +00:00
"context"
2022-10-19 13:37:41 +00:00
"encoding/json"
"net/http"
"time"
"github.com/christianselig/apollo-backend/internal/domain"
)
func (a *api) createLiveActivityHandler(w http.ResponseWriter, r *http.Request) {
2022-11-01 02:33:11 +00:00
ctx, cancel := context.WithCancel(r.Context())
defer cancel()
2022-10-19 13:37:41 +00:00
la := &domain.LiveActivity{}
if err := json.NewDecoder(r.Body).Decode(la); err != nil {
a.errorResponse(w, r, 400, err)
return
}
if _, err := a.liveActivityRepo.Get(ctx, la.APNSToken); err == nil {
a.errorResponse(w, r, 400, ErrDuplicateAPNSToken)
2022-10-19 13:37:41 +00:00
return
}
ac := a.reddit.NewAuthenticatedClient(la.RedditAccountID, la.RefreshToken, la.AccessToken)
rtr, err := ac.RefreshTokens(ctx)
if err != nil {
a.errorResponse(w, r, 500, err)
return
}
la.RefreshToken = rtr.RefreshToken
2022-10-27 19:47:26 +00:00
la.TokenExpiresAt = time.Now().Add(1 * time.Hour)
2022-10-19 13:37:41 +00:00
if err := a.liveActivityRepo.Create(ctx, la); err != nil {
a.errorResponse(w, r, 500, err)
return
}
w.WriteHeader(http.StatusOK)
}