hide some information for visible but unreleased releases

Signed-off-by: ari melody <ari@arimelody.me>
This commit is contained in:
ari melody 2024-08-05 16:49:49 +01:00
parent 10f19d46db
commit 7914fba52a
6 changed files with 56 additions and 55 deletions

View file

@ -18,6 +18,14 @@ type (
const TOKEN_LENGTH = 64 const TOKEN_LENGTH = 64
const TOKEN_CHARS = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" const TOKEN_CHARS = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
var ADMIN_BYPASS = func() bool {
if os.Getenv("ARIMELODY_ADMIN_BYPASS") == "true" {
fmt.Println("WARNING: Admin login is currently BYPASSED by ARIMELODY_ADMIN_BYPASS.")
return true
}
return false
}()
var ADMIN_ID_DISCORD = func() string { var ADMIN_ID_DISCORD = func() string {
envvar := os.Getenv("DISCORD_ADMIN_ID") envvar := os.Getenv("DISCORD_ADMIN_ID")
if envvar == "" { if envvar == "" {

View file

@ -85,9 +85,9 @@ func MustAuthorise(next http.Handler) http.Handler {
} }
func GetSession(r *http.Request) *Session { func GetSession(r *http.Request) *Session {
if os.Getenv("ARIMELODY_ADMIN_BYPASS") == "true" { // if ADMIN_BYPASS {
return &Session{} // return &Session{}
} // }
var token = "" var token = ""
// is the session token in context? // is the session token in context?

View file

@ -24,10 +24,11 @@ type (
) )
const ( const (
Single ReleaseType = "Single" Single ReleaseType = "single"
Album ReleaseType = "Album" Album ReleaseType = "album"
EP ReleaseType = "EP" EP ReleaseType = "EP"
Compilation ReleaseType = "Compilation" Compilation ReleaseType = "compilation"
Upcoming ReleaseType = "upcoming"
) )
// GETTERS // GETTERS

View file

@ -3,7 +3,6 @@ package view
import ( import (
"net/http" "net/http"
"arimelody.me/arimelody.me/admin"
"arimelody.me/arimelody.me/global" "arimelody.me/arimelody.me/global"
"arimelody.me/arimelody.me/music/model" "arimelody.me/arimelody.me/music/model"
) )
@ -26,14 +25,16 @@ func Handler() http.Handler {
func ServeCatalog() http.Handler { func ServeCatalog() http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
releases := []*model.Release{} releases := []model.Release{}
authorised := admin.GetSession(r) != nil for _, r := range global.Releases {
for _, release := range global.Releases { if r.Visible {
if !release.IsReleased() && !authorised { release := *r
continue if !release.IsReleased() {
release.ReleaseType = model.Upcoming
} }
releases = append(releases, release) releases = append(releases, release)
} }
}
global.ServeTemplate("music.html", releases).ServeHTTP(w, r) global.ServeTemplate("music.html", releases).ServeHTTP(w, r)
}) })

View file

@ -22,6 +22,7 @@ type (
gatewayRelease struct { gatewayRelease struct {
*model.Release *model.Release
Tracks []gatewayTrack Tracks []gatewayTrack
Authorised bool
} }
) )
@ -35,18 +36,23 @@ func ServeRelease() http.Handler {
} }
releaseID := r.URL.Path[1:] releaseID := r.URL.Path[1:]
var release = global.GetRelease(releaseID) var releaseRef = global.GetRelease(releaseID)
if release == nil { if releaseRef == nil {
http.NotFound(w, r) http.NotFound(w, r)
return return
} }
var release = *releaseRef
// only allow authorised users to view unreleased releases // only allow authorised users to view hidden releases
authorised := admin.GetSession(r) != nil authorised := admin.GetSession(r) != nil
if !release.IsReleased() && !authorised { if !authorised && !release.Visible {
admin.MustAuthorise(ServeRelease()).ServeHTTP(w, r) http.NotFound(w, r)
return return
} }
if !authorised && !release.IsReleased() {
release.Tracks = nil
release.Credits = nil
}
w.Header().Add("Content-Type", "application/json") w.Header().Add("Content-Type", "application/json")
err := json.NewEncoder(w).Encode(release) err := json.NewEncoder(w).Encode(release)
@ -71,6 +77,13 @@ func ServeGateway() http.Handler {
return return
} }
// only allow authorised users to view hidden releases
authorised := admin.GetSession(r) != nil
if !release.Visible && !authorised {
http.NotFound(w, r)
return
}
tracks := []gatewayTrack{} tracks := []gatewayTrack{}
for i, track := range release.Tracks { for i, track := range release.Tracks {
tracks = append([]gatewayTrack{{ tracks = append([]gatewayTrack{{
@ -80,16 +93,9 @@ func ServeGateway() http.Handler {
}}, tracks...) }}, tracks...)
} }
// only allow authorised users to view unreleased releases
authorised := admin.GetSession(r) != nil
if !release.IsReleased() && !authorised {
admin.MustAuthorise(ServeGateway()).ServeHTTP(w, r)
return
}
lrw := global.LoggingResponseWriter{ResponseWriter: w, Code: http.StatusOK} lrw := global.LoggingResponseWriter{ResponseWriter: w, Code: http.StatusOK}
global.ServeTemplate("music-gateway.html", gatewayRelease{release, tracks}).ServeHTTP(&lrw, r) global.ServeTemplate("music-gateway.html", gatewayRelease{release, tracks, authorised}).ServeHTTP(&lrw, r)
if lrw.Code != http.StatusOK { if lrw.Code != http.StatusOK {
fmt.Printf("Error rendering music gateway for %s\n", id) fmt.Printf("Error rendering music gateway for %s\n", id)

View file

@ -57,7 +57,11 @@
<span id="year" title="{{.PrintReleaseDate}}">{{.GetReleaseYear}}</span> <span id="year" title="{{.PrintReleaseDate}}">{{.GetReleaseYear}}</span>
</div> </div>
<p id="artist">{{.PrintArtists true true}}</p> <p id="artist">{{.PrintArtists true true}}</p>
{{if .IsReleased}}
<p id="type" class="{{.ReleaseType}}">{{.ReleaseType}}</p> <p id="type" class="{{.ReleaseType}}">{{.ReleaseType}}</p>
{{else}}
<p id="type" class="upcoming">upcoming</p>
{{end}}
<ul id="links"> <ul id="links">
{{if .Buylink}} {{if .Buylink}}
@ -82,6 +86,8 @@
<button id="share">share</button> <button id="share">share</button>
</div> </div>
{{if .IsReleased}}
{{if .Credits}} {{if .Credits}}
<div id="credits"> <div id="credits">
<h2>credits:</h2> <h2>credits:</h2>
@ -121,12 +127,14 @@
{{end}} {{end}}
</div> </div>
{{end}} {{end}}
{{end}}
</div> </div>
{{if .IsReleased}}
<div id="extras"> <div id="extras">
<ul> <ul>
<li><a href="#overview">overview</a></li> <li><a href="#overview">overview</a></li>
{{if .Credits}} {{if .Credits}}
<li><a href="#credits">credits</a></li> <li><a href="#credits">credits</a></li>
{{end}} {{end}}
@ -141,30 +149,7 @@
{{end}} {{end}}
</ul> </ul>
</div> </div>
<!-- <div id="tracks"> --> {{end}}
<!-- <% var file = `/audio/preview/${data.id}.webm` %> -->
<!-- <% if (data.tracks && typeof(data.tracks) == typeof([])) { %> -->
<!-- <% for( let i = 0; i < data.tracks.length; i++ ) { %> -->
<!-- <% -->
<!-- songid = data.tracks[i].title.toLowerCase().replace(/[^a-z0-9]/g, ""); -->
<!-- file = `/audio/preview/${data.id}-${songid}.webm`; -->
<!-- %> -->
<!---->
<!-- <div class="track-preview" id="preview-<%= songid %>"> -->
<!-- <i class="fa-solid fa-play play"></i> -->
<!-- <p><%= data.tracks[i].title %></p> -->
<!-- <audio src="<%= file %>"></audio> -->
<!-- </div> -->
<!---->
<!-- <% } %> -->
<!-- <% } else { %> -->
<!-- <div class="track-preview" id="preview-<%= data.id %>"> -->
<!-- <i class="fa-solid fa-play play"></i> -->
<!-- <p>{{.Title}}</p> -->
<!-- <audio src="<%= file %>"></audio> -->
<!-- </div> -->
<!-- <% } %> -->
<!-- </div> -->
</div> </div>
</main> </main>
{{end}} {{end}}