Skip to content

Commit

Permalink
website/public: add more data to faves input
Browse files Browse the repository at this point in the history
  • Loading branch information
Wessie committed May 26, 2024
1 parent e85e0f6 commit bcca992
Showing 1 changed file with 41 additions and 14 deletions.
55 changes: 41 additions & 14 deletions website/public/faves.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,43 @@ package public
import (
"encoding/json"
"fmt"
"html/template"
"net/http"
"time"

radio "github.com/R-a-dio/valkyrie"
"github.com/R-a-dio/valkyrie/errors"
"github.com/R-a-dio/valkyrie/util"
"github.com/R-a-dio/valkyrie/website/middleware"
"github.com/R-a-dio/valkyrie/website/shared"
"github.com/go-chi/chi/v5"
"github.com/gorilla/csrf"
)

const favesPageSize = 100

type FavesInput struct {
middleware.Input
Nickname string
DownloadURL string
Faves []radio.Song
FaveCount int64
Page *shared.Pagination
CSRFTokenInput template.HTML
Nickname string
CanRequest bool
RequestCooldown time.Duration
DownloadURL string
Faves []radio.Song
FaveCount int64
Page *shared.Pagination
}

func (FavesInput) TemplateBundle() string {
return "faves"
}

func NewFavesInput(ss radio.SongStorage, r *http.Request) (*FavesInput, error) {
func NewFavesInput(ss radio.SongStorage, rs radio.RequestStorage, r *http.Request, requestDelay time.Duration) (*FavesInput, error) {
const op errors.Op = "website/public.NewFavesInput"

page, offset, err := getPageOffset(r, favesPageSize)
if err != nil {
return nil, err
return nil, errors.E(op, err)
}

// we support both ?nick=<nick> and /faves/<nick> so we need to see which one we have
Expand All @@ -42,29 +51,47 @@ func NewFavesInput(ss radio.SongStorage, r *http.Request) (*FavesInput, error) {

faves, faveCount, err := ss.FavoritesOf(nickname, favesPageSize, offset)
if err != nil {
return nil, err
return nil, errors.E(op, err)
}

// create download URL
q := r.URL.Query()
q.Set("dl", "true")
dlUrl := *r.URL
dlUrl.RawQuery = q.Encode()

// check if the user can request
lastRequest, err := rs.LastRequest(r.RemoteAddr)
if err != nil {
return nil, errors.E(op, err)
}
cooldown, canRequest := radio.CalculateCooldown(requestDelay, lastRequest)

return &FavesInput{
Nickname: nickname,
DownloadURL: dlUrl.String(),
Faves: faves,
FaveCount: faveCount,
Nickname: nickname,
CanRequest: canRequest,
RequestCooldown: cooldown,
DownloadURL: dlUrl.String(),
Faves: faves,
FaveCount: faveCount,
Page: shared.NewPagination(
page, shared.PageCount(int64(faveCount), favesPageSize),
r.URL,
),
Input: middleware.InputFromRequest(r),
Input: middleware.InputFromRequest(r),
CSRFTokenInput: csrf.TemplateField(r),
}, nil
}

func (s State) GetFaves(w http.ResponseWriter, r *http.Request) {
input, err := NewFavesInput(s.Storage.Song(r.Context()), r)
ctx := r.Context()

input, err := NewFavesInput(
s.Storage.Song(ctx),
s.Storage.Request(ctx),
r,
time.Duration(s.Conf().UserRequestDelay),
)
if err != nil {
s.errorHandler(w, r, err)
return
Expand Down

0 comments on commit bcca992

Please sign in to comment.