Skip to content

Commit

Permalink
podcasts: improve check for unpublished podcasts
Browse files Browse the repository at this point in the history
The check occurs at the model level,
so anything that retrieves podcasts will now ignore unpublished ones.

This commit doesn't implement 404 handling for missing podcasts in the player,
so this currently just triggers a 200 and a blank screen.

Fixes #269.
  • Loading branch information
MattWindsor91 committed Jul 30, 2023
1 parent dd8d137 commit b220e4e
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 35 deletions.
59 changes: 26 additions & 33 deletions controllers/podcasts.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/UniversityRadioYork/2016-site/models"
"github.com/UniversityRadioYork/2016-site/structs"
"github.com/UniversityRadioYork/2016-site/utils"
myradio "github.com/UniversityRadioYork/myradio-go"
"github.com/UniversityRadioYork/myradio-go"
)

// PodcastController is the controller for the URYPlayer Podcast pages.
Expand All @@ -26,7 +26,6 @@ func NewPodcastController(s *myradio.Session, c *structs.Config) *PodcastControl

// GetAllPodcasts handles the HTTP GET request r for the all postcasts page, writing to w.
func (podcastsC *PodcastController) GetAllPodcasts(w http.ResponseWriter, r *http.Request) {

podcastm := models.NewPodcastModel(podcastsC.session)

vars := mux.Vars(r)
Expand Down Expand Up @@ -75,56 +74,50 @@ func (podcastsC *PodcastController) GetAllPodcasts(w http.ResponseWriter, r *htt

// Get handles the HTTP GET request r for a singular podcast page, writing to w.
func (podcastsC *PodcastController) Get(w http.ResponseWriter, r *http.Request) {

podcastm := models.NewPodcastModel(podcastsC.session)

vars := mux.Vars(r)

id, _ := strconv.Atoi(vars["id"])

podcast, err := podcastm.Get(id)

if err != nil {
podcast, err := podcastsC.getPodcast(r)
if podcast == nil || err != nil {
// TODO(@MattWindsor91): what if the error is not 404?
podcastsC.render404(w, err)
return
}

if podcast.Status != "Published" {
podcastsC.render404(w, nil)
return
}

data := struct {
Podcast *myradio.Podcast
}{
Podcast: podcast,
}
podcastsC.renderTemplate(w, data, "podcast.tmpl")
podcastsC.renderPodcast(w, podcast, "podcast.tmpl")
}

// GetEmbed handles the HTTP GET request r for a singular podcast embed, writing to w.
func (podcastsC *PodcastController) GetEmbed(w http.ResponseWriter, r *http.Request) {

podcastm := models.NewPodcastModel(podcastsC.session)

vars := mux.Vars(r)

id, _ := strconv.Atoi(vars["id"])

podcast, err := podcastm.Get(id)

podcast, err := podcastsC.getPodcast(r)
if err != nil {
//@TODO: Do something proper here, render 404 or something
log.Println(err)
return
}

// No error, but podcast is not available
if podcast == nil {
//@TODO: Do something proper here, render 404 or something
return
}

podcastsC.renderPodcast(w, podcast, "podcast_player.tmpl")
}

func (podcastsC *PodcastController) getPodcast(r *http.Request) (*myradio.Podcast, error) {
id, err := strconv.Atoi(mux.Vars(r)["id"])
if err != nil {
return nil, err
}

return models.NewPodcastModel(podcastsC.session).Get(id)
}

func (podcastsC *PodcastController) renderPodcast(w http.ResponseWriter, podcast *myradio.Podcast, tmpl string) {
data := struct {
Podcast *myradio.Podcast
}{
Podcast: podcast,
}
podcastsC.renderTemplate(w, data, "podcast_player.tmpl")
podcastsC.renderTemplate(w, data, tmpl)
}

func (podcastsC *PodcastController) render404(w http.ResponseWriter, err error) {
Expand Down
16 changes: 14 additions & 2 deletions models/podcasts.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,18 @@ func (m *PodcastModel) GetAllPodcasts(number int, page int) (podcasts []myradio.
}

// Get gets the data required for the Podcast controller from MyRadio.
func (m *PodcastModel) Get(id int) (podcast *myradio.Podcast, err error) {
return m.session.GetPodcastWithShow(id)
// It does not retrieve a podcast if the podcast is unpublished.
func (m *PodcastModel) Get(id int) (*myradio.Podcast, error) {
pod, err := m.session.GetPodcastWithShow(id)
if err != nil {
return nil, err
}

// we should not show unpublished podcasts, regardless of situation
// (https://github.com/UniversityRadioYork/2016-site/issues/269)
if pod.Status != "Published" {
return nil, nil
}

return pod, nil
}

0 comments on commit b220e4e

Please sign in to comment.