refactor(podcast)!: make podcasts global not per user, to match spec

Release-As: 0.15.0
This commit is contained in:
brian-doherty
2022-05-03 16:32:18 -05:00
committed by sentriz
parent e883de8c95
commit 182c96e966
9 changed files with 121 additions and 112 deletions

View File

@@ -169,43 +169,45 @@
</table>
</div>
</div>
<div class="padded box">
<div class="box-title">
<i class="mdi mdi-rss-box"></i> podcasts
</div>
<div class="box-description text-light">
<p>you can add podcasts rss feeds here</p>
</div>
<div class="block-right">
<table id="podcast-preferences">
{{ range $pref := .Podcasts }}
{{ if .User.IsAdmin }}
<div class="padded box">
<div class="box-title">
<i class="mdi mdi-rss-box"></i> podcasts
</div>
<div class="box-description text-light">
<p>you can add podcasts rss feeds here</p>
</div>
<div class="block-right">
<table id="podcast-preferences">
{{ range $pref := .Podcasts }}
<tr>
<form id="podcast-{{ $pref.ID }}-download" action="{{ printf "/admin/download_podcast_do?id=%d" $pref.ID | path }}" method="post"></form>
<form id="podcast-{{ $pref.ID }}-auto-download" action="{{ printf "/admin/update_podcast_do?id=%d" $pref.ID | path }}" method="post"></form>
<form id="podcast-{{ $pref.ID }}-delete" action="{{ printf "/admin/delete_podcast_do?id=%d" $pref.ID | path }}" method="post"></form>
<td>{{ $pref.Title }}</td>
<td><select form="podcast-{{ $pref.ID }}-auto-download" name="setting">
{{ if eq $pref.AutoDownload "latest" }}
<option value="latest" selected="selected">download latest</option>
<option value="none">no auto download</option>
{{ else }}
<option value="none" selected="selected" >no auto download</option>
<option value="latest">download latest</option>
{{ end }}
</select></td>
<td><input form="podcast-{{ $pref.ID }}-download" type="submit" value="download all"></td>
<td><input form="podcast-{{ $pref.ID }}-auto-download" type="submit" value="save"></td>
<td><input form="podcast-{{ $pref.ID }}-delete" type="submit" value="delete"></td>
</tr>
{{ end }}
<tr>
<form id="podcast-{{ $pref.ID }}-download" action="{{ printf "/admin/download_podcast_do?id=%d" $pref.ID | path }}" method="post"></form>
<form id="podcast-{{ $pref.ID }}-auto-download" action="{{ printf "/admin/update_podcast_do?id=%d" $pref.ID | path }}" method="post"></form>
<form id="podcast-{{ $pref.ID }}-delete" action="{{ printf "/admin/delete_podcast_do?id=%d" $pref.ID | path }}" method="post"></form>
<td>{{ $pref.Title }}</td>
<td><select form="podcast-{{ $pref.ID }}-auto-download" name="setting">
{{ if eq $pref.AutoDownload "latest" }}
<option value="latest" selected="selected">download latest</option>
<option value="none">no auto download</option>
{{ else }}
<option value="none" selected="selected" >no auto download</option>
<option value="latest">download latest</option>
{{ end }}
</select></td>
<td><input form="podcast-{{ $pref.ID }}-download" type="submit" value="download all"></td>
<td><input form="podcast-{{ $pref.ID }}-auto-download" type="submit" value="save"></td>
<td><input form="podcast-{{ $pref.ID }}-delete" type="submit" value="delete"></td>
<form id="podcast-add" action="{{ path "/admin/add_podcast_do" }}" method="post"></form>
<td><input form="podcast-add" type="text" name="feed" placeholder="rss feed url"></td>
<td><input form="podcast-add" type="submit" value="save"></td>
</tr>
{{ end }}
<tr>
<form id="podcast-add" action="{{ path "/admin/add_podcast_do" }}" method="post"></form>
<td><input form="podcast-add" type="text" name="feed" placeholder="rss feed url"></td>
<td><input form="podcast-add" type="submit" value="save"></td>
</tr>
</table>
</table>
</div>
</div>
</div>
{{ end }}
<div class="padded box">
<div class="box-title">
<i class="mdi mdi-playlist-music"></i> playlists

View File

@@ -391,7 +391,6 @@ func (c *Controller) ServeDeleteTranscodePrefDo(r *http.Request) *Response {
}
func (c *Controller) ServePodcastAddDo(r *http.Request) *Response {
user := r.Context().Value(CtxUser).(*db.User)
rssURL := r.FormValue("feed")
fp := gofeed.NewParser()
feed, err := fp.ParseURL(rssURL)
@@ -401,7 +400,7 @@ func (c *Controller) ServePodcastAddDo(r *http.Request) *Response {
flashW: []string{fmt.Sprintf("could not create feed: %v", err)},
}
}
if _, err = c.Podcasts.AddNewPodcast(rssURL, feed, user.ID); err != nil {
if _, err = c.Podcasts.AddNewPodcast(rssURL, feed); err != nil {
return &Response{
redirect: "/admin/home",
flashW: []string{fmt.Sprintf("could not create feed: %v", err)},
@@ -454,12 +453,11 @@ func (c *Controller) ServePodcastUpdateDo(r *http.Request) *Response {
}
func (c *Controller) ServePodcastDeleteDo(r *http.Request) *Response {
user := r.Context().Value(CtxUser).(*db.User)
id, err := strconv.Atoi(r.URL.Query().Get("id"))
if err != nil {
return &Response{code: 400, err: "please provide a valid podcast id"}
}
if err := c.Podcasts.DeletePodcast(user.ID, id); err != nil {
if err := c.Podcasts.DeletePodcast(id); err != nil {
return &Response{code: 400, err: "please provide a valid podcast id"}
}
return &Response{

View File

@@ -14,9 +14,8 @@ import (
func (c *Controller) ServeGetPodcasts(r *http.Request) *spec.Response {
params := r.Context().Value(CtxParams).(params.Params)
isIncludeEpisodes := params.GetOrBool("includeEpisodes", true)
user := r.Context().Value(CtxUser).(*db.User)
id, _ := params.GetID("id")
podcasts, err := c.Podcasts.GetPodcastOrAll(user.ID, id.Value, isIncludeEpisodes)
podcasts, err := c.Podcasts.GetPodcastOrAll(id.Value, isIncludeEpisodes)
if err != nil {
return spec.NewError(10, "failed get podcast(s): %s", err)
}
@@ -45,6 +44,10 @@ func (c *Controller) ServeGetNewestPodcasts(r *http.Request) *spec.Response {
}
func (c *Controller) ServeDownloadPodcastEpisode(r *http.Request) *spec.Response {
user := r.Context().Value(CtxUser).(*db.User)
if (!user.IsAdmin) {
return spec.NewError(10, "user not admin")
}
params := r.Context().Value(CtxParams).(params.Params)
id, err := params.GetID("id")
if err != nil || id.Type != specid.PodcastEpisode {
@@ -58,6 +61,9 @@ func (c *Controller) ServeDownloadPodcastEpisode(r *http.Request) *spec.Response
func (c *Controller) ServeCreatePodcastChannel(r *http.Request) *spec.Response {
user := r.Context().Value(CtxUser).(*db.User)
if (!user.IsAdmin) {
return spec.NewError(10, "user not admin")
}
params := r.Context().Value(CtxParams).(params.Params)
rssURL, _ := params.Get("url")
fp := gofeed.NewParser()
@@ -65,7 +71,7 @@ func (c *Controller) ServeCreatePodcastChannel(r *http.Request) *spec.Response {
if err != nil {
return spec.NewError(10, "failed to parse feed: %s", err)
}
if _, err = c.Podcasts.AddNewPodcast(rssURL, feed, user.ID); err != nil {
if _, err = c.Podcasts.AddNewPodcast(rssURL, feed); err != nil {
return spec.NewError(10, "failed to add feed: %s", err)
}
return spec.NewResponse()
@@ -73,7 +79,10 @@ func (c *Controller) ServeCreatePodcastChannel(r *http.Request) *spec.Response {
func (c *Controller) ServeRefreshPodcasts(r *http.Request) *spec.Response {
user := r.Context().Value(CtxUser).(*db.User)
if err := c.Podcasts.RefreshPodcastsForUser(user.ID); err != nil {
if (!user.IsAdmin) {
return spec.NewError(10, "user not admin")
}
if err := c.Podcasts.RefreshPodcasts(); err != nil {
return spec.NewError(10, "failed to refresh feeds: %s", err)
}
return spec.NewResponse()
@@ -81,18 +90,25 @@ func (c *Controller) ServeRefreshPodcasts(r *http.Request) *spec.Response {
func (c *Controller) ServeDeletePodcastChannel(r *http.Request) *spec.Response {
user := r.Context().Value(CtxUser).(*db.User)
if (!user.IsAdmin) {
return spec.NewError(10, "user not admin")
}
params := r.Context().Value(CtxParams).(params.Params)
id, err := params.GetID("id")
if err != nil || id.Type != specid.Podcast {
return spec.NewError(10, "please provide a valid podcast id")
}
if err := c.Podcasts.DeletePodcast(user.ID, id.Value); err != nil {
if err := c.Podcasts.DeletePodcast(id.Value); err != nil {
return spec.NewError(10, "failed to delete podcast: %s", err)
}
return spec.NewResponse()
}
func (c *Controller) ServeDeletePodcastEpisode(r *http.Request) *spec.Response {
user := r.Context().Value(CtxUser).(*db.User)
if (!user.IsAdmin) {
return spec.NewError(10, "user not admin")
}
params := r.Context().Value(CtxParams).(params.Params)
id, err := params.GetID("id")
if err != nil || id.Type != specid.PodcastEpisode {

View File

@@ -169,10 +169,6 @@ func setupAdmin(r *mux.Router, ctrl *ctrladmin.Controller) {
routUser.Handle("/delete_playlist_do", ctrl.H(ctrl.ServeDeletePlaylistDo))
routUser.Handle("/create_transcode_pref_do", ctrl.H(ctrl.ServeCreateTranscodePrefDo))
routUser.Handle("/delete_transcode_pref_do", ctrl.H(ctrl.ServeDeleteTranscodePrefDo))
routUser.Handle("/add_podcast_do", ctrl.H(ctrl.ServePodcastAddDo))
routUser.Handle("/delete_podcast_do", ctrl.H(ctrl.ServePodcastDeleteDo))
routUser.Handle("/download_podcast_do", ctrl.H(ctrl.ServePodcastDownloadDo))
routUser.Handle("/update_podcast_do", ctrl.H(ctrl.ServePodcastUpdateDo))
// admin routes (if session is valid, and is admin)
routAdmin := routUser.NewRoute().Subrouter()
@@ -189,6 +185,10 @@ func setupAdmin(r *mux.Router, ctrl *ctrladmin.Controller) {
routAdmin.Handle("/update_lastfm_api_key_do", ctrl.H(ctrl.ServeUpdateLastFMAPIKeyDo))
routAdmin.Handle("/start_scan_inc_do", ctrl.H(ctrl.ServeStartScanIncDo))
routAdmin.Handle("/start_scan_full_do", ctrl.H(ctrl.ServeStartScanFullDo))
routAdmin.Handle("/add_podcast_do", ctrl.H(ctrl.ServePodcastAddDo))
routAdmin.Handle("/delete_podcast_do", ctrl.H(ctrl.ServePodcastDeleteDo))
routAdmin.Handle("/download_podcast_do", ctrl.H(ctrl.ServePodcastDownloadDo))
routAdmin.Handle("/update_podcast_do", ctrl.H(ctrl.ServePodcastUpdateDo))
// middlewares should be run for not found handler
// https://github.com/gorilla/mux/issues/416