mirror of
https://github.com/christianselig/apollo-backend
synced 2024-11-25 13:17:42 +00:00
43 lines
971 B
Go
43 lines
971 B
Go
package api
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"net/http"
|
|
"time"
|
|
|
|
"github.com/christianselig/apollo-backend/internal/domain"
|
|
)
|
|
|
|
func (a *api) createLiveActivityHandler(w http.ResponseWriter, r *http.Request) {
|
|
ctx, cancel := context.WithCancel(r.Context())
|
|
defer cancel()
|
|
|
|
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)
|
|
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
|
|
la.TokenExpiresAt = time.Now().Add(1 * time.Hour)
|
|
|
|
if err := a.liveActivityRepo.Create(ctx, la); err != nil {
|
|
a.errorResponse(w, r, 500, err)
|
|
return
|
|
}
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
}
|