Skip to content

Commit

Permalink
Merge pull request #72 from mbaraa/dev
Browse files Browse the repository at this point in the history
release v0.1.2
  • Loading branch information
mbaraa authored Jun 9, 2024
2 parents 2266b6e + 2b01135 commit 6a29256
Show file tree
Hide file tree
Showing 33 changed files with 501 additions and 173 deletions.
2 changes: 2 additions & 0 deletions app/cmd/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,13 @@ func StartServer(staticFS embed.FS) error {
apisHandler.HandleFunc("GET /logout", apis.HandleLogout)
apisHandler.HandleFunc("GET /search-suggestion", apis.HandleSearchSuggestions)
apisHandler.HandleFunc("GET /song", gHandler.OptionalAuthApi(songApi.HandlePlaySong))
apisHandler.HandleFunc("GET /song/single", gHandler.OptionalAuthApi(songApi.HandleGetSong))
apisHandler.HandleFunc("PUT /song/playlist", gHandler.AuthApi(playlistsApi.HandleToggleSongInPlaylist))
apisHandler.HandleFunc("PUT /song/playlist/plays", gHandler.AuthApi(songApi.HandleIncrementSongPlaysInPlaylist))
apisHandler.HandleFunc("PUT /song/playlist/upvote", gHandler.AuthApi(songApi.HandleUpvoteSongPlaysInPlaylist))
apisHandler.HandleFunc("PUT /song/playlist/downvote", gHandler.AuthApi(songApi.HandleDownvoteSongPlaysInPlaylist))
apisHandler.HandleFunc("GET /playlist/all", gHandler.AuthApi(playlistsApi.HandleGetPlaylistsForPopover))
apisHandler.HandleFunc("GET /playlist", gHandler.AuthApi(playlistsApi.HandleGetPlaylist))
apisHandler.HandleFunc("POST /playlist", gHandler.AuthApi(playlistsApi.HandleCreatePlaylist))
apisHandler.HandleFunc("PUT /playlist/public", gHandler.AuthApi(playlistsApi.HandleTogglePublicPlaylist))
apisHandler.HandleFunc("PUT /playlist/join", gHandler.AuthApi(playlistsApi.HandleToggleJoinPlaylist))
Expand Down
2 changes: 1 addition & 1 deletion app/handlers/apis/history.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func (h *historyApi) HandleGetMoreHistoryItems(w http.ResponseWriter, r *http.Re

outBuf := bytes.NewBuffer([]byte{})
for idx, s := range recentPlays {
song.Song(s, []string{"Played " + s.AddedAt},
song.Song(s, []string{s.AddedAt},
[]templ.Component{
playlist.PlaylistsPopup((idx+1)*page, s.YtId),
},
Expand Down
22 changes: 22 additions & 0 deletions app/handlers/apis/playlist.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,28 @@ func (p *playlistApi) HandleToggleJoinPlaylist(w http.ResponseWriter, r *http.Re
}
}

func (p *playlistApi) HandleGetPlaylist(w http.ResponseWriter, r *http.Request) {
profileId, profileIdCorrect := r.Context().Value(handlers.ProfileIdKey).(uint)
if !profileIdCorrect {
w.Write([]byte("🤷‍♂️"))
return
}

playlistId := r.URL.Query().Get("playlist-id")
if playlistId == "" {
w.WriteHeader(http.StatusBadRequest)
return
}

playlist, _, err := p.service.Get(playlistId, profileId)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
log.Errorln(err)
return
}
_ = json.NewEncoder(w).Encode(playlist)
}

func (p *playlistApi) HandleDeletePlaylist(w http.ResponseWriter, r *http.Request) {
profileId, profileIdCorrect := r.Context().Value(handlers.ProfileIdKey).(uint)
if !profileIdCorrect {
Expand Down
18 changes: 18 additions & 0 deletions app/handlers/apis/songs.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"dankmuzikk/services/history"
"dankmuzikk/services/playlists/songs"
"dankmuzikk/services/youtube/download"
"encoding/json"
"fmt"
"net/http"
)
Expand Down Expand Up @@ -130,3 +131,20 @@ func (s *songDownloadHandler) HandlePlaySong(w http.ResponseWriter, r *http.Requ
return
}
}

func (s *songDownloadHandler) HandleGetSong(w http.ResponseWriter, r *http.Request) {
id := r.URL.Query().Get("id")
if id == "" {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte("missing song's yt id"))
return
}

song, err := s.songsService.GetSong(id)
if err != nil {
log.Errorln(err)
w.WriteHeader(http.StatusInternalServerError)
return
}
_ = json.NewEncoder(w).Encode(song)
}
47 changes: 41 additions & 6 deletions app/services/history/history.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,21 +66,56 @@ func (h *Service) Get(profileId, page uint) ([]entities.Song, error) {
songs = append(songs, song)
}

return songs, nil
songsFr := make([]entities.Song, 0)
for i := 0; i < len(songs); i++ {
playTimes := 1
for i < len(songs)-1 && songs[i+1].YtId == songs[i].YtId {
playTimes++
i++
}
songs[i].AddedAt = fmt.Sprintf("Played %s - %s", times(playTimes), songs[i].AddedAt)
songsFr = append(songsFr, songs[i])
}

return songsFr, nil
}

func whenDidItHappen(t time.Time) string {
now := time.Now().UTC()
switch {
case t.Day() == now.Day() && t.Month() == now.Month() && t.Year() == now.Year():
return "today"
return "Today"
case t.Day()+1 == now.Day() && t.Month() == now.Month() && t.Year() == now.Year():
return "yesterday"
return "Yesterday"
case t.Day()+5 < now.Day() && t.Month() == now.Month() && t.Year() == now.Year():
return "last week"
return "Last week"
case t.Day() == now.Day() && t.Month()+1 == now.Month() && t.Year() == now.Year():
return "last month"
return "Last month"
default:
return fmt.Sprintf("%s %s %s", t.Format("January"), nth(t.Day()), t.Format("2006"))
}
}

func nth(n int) string {
switch {
case n%10 == 1:
return fmt.Sprintf("%dst", n)
case n%10 == 2:
return fmt.Sprintf("%dnd", n)
case n%10 == 3:
return fmt.Sprintf("%drd", n)
default:
return fmt.Sprintf("%dth", n)
}
}

func times(times int) string {
switch {
case times == 1:
return "once"
case times > 1:
return fmt.Sprintf("%d times", times)
default:
return t.Format("2, January, 2006")
return ""
}
}
33 changes: 32 additions & 1 deletion app/static/css/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,38 @@
@tailwind components;
@tailwind utilities;

:root {
--animation-duration: 0.4s;
}

.animate-in {
animation: animate-down calc(var(--animation-duration) -0.1s) ease-in-out;
}

main {
animation: animate-up calc(var(--animation-duration) -0.1s) ease-in-out;
}

@keyframes animate-down {
0% {
transform: translateY(-10px);
}

100% {
transform: translateY(0px);
}
}

@keyframes animate-up {
0% {
transform: translateY(10px);
}

100% {
transform: translateY(0px);
}
}

html,
body {
overscroll-behavior-y: none;
Expand All @@ -16,7 +48,6 @@ table {
}

* {
--animation-duration: 0.4s;
-webkit-transition:
all var(--animation-duration),
background-color var(--animation-duration),
Expand Down
87 changes: 87 additions & 0 deletions app/static/js/player.js
Original file line number Diff line number Diff line change
Expand Up @@ -617,6 +617,38 @@ async function playSong(song) {
await updateSongPlays();
}

/**
* @param {string} songYtId
*/
async function fetchSongMeta(songYtId) {
Utils.showLoading();
return await fetch(`/api/song/single?id=${songYtId}`)
.then((res) => res.json())
.then((s) => s)
.catch((err) => {
console.error(err);
})
.finally(() => {
Utils.hideLoading();
});
}

/**
* @param {string} playlistPubId
*/
async function fetchPlaylistMeta(playlistPubId) {
Utils.showLoading();
return await fetch(`/api/playlist?playlist-id=${playlistPubId}`)
.then((res) => res.json())
.then((p) => p)
.catch((err) => {
console.error(err);
})
.finally(() => {
Utils.hideLoading();
});
}

/**
* @param {Song} song
*/
Expand All @@ -631,6 +663,14 @@ async function playSingleSong(song) {
await playSong(song);
}

/**
* @param {string} songYtId
*/
async function playSingleSongId(songYtId) {
const song = await fetchSongMeta(songYtId);
await playSingleSong(song);
}

/**
* @param {Song} song
*/
Expand All @@ -647,6 +687,14 @@ async function playSingleSongNext(song) {
alert(`Playing ${song.title} next!`);
}

/**
* @param {string} songYtId
*/
async function playSingleSongNextId(songYtId) {
const song = await fetchSongMeta(songYtId);
await playSingleSongNext(song);
}

/**
* @param {Playlist} playlist
*/
Expand All @@ -670,6 +718,14 @@ async function playPlaylistNext(playlist) {
alert(`Playing ${playlist.title} next!`);
}

/**
* @param {string} playlistPubId
*/
async function playPlaylistNextId(playlistPubId) {
const playlist = await fetchPlaylistMeta(playlistPubId);
await playPlaylistNext(playlist);
}

/**
* @param {Playlist} playlist
*/
Expand All @@ -687,6 +743,14 @@ async function appendPlaylistToCurrentQueue(playlist) {
alert(`Playing ${playlist.title} next!`);
}

/**
* @param {string} playlistPubId
*/
async function appendPlaylistToCurrentQueueId(playlistPubId) {
const playlist = await fetchPlaylistMeta(playlistPubId);
await appendPlaylistToCurrentQueue(playlist);
}

/**
* @param {string} songYtId
* @param {Playlist} playlist
Expand Down Expand Up @@ -722,6 +786,15 @@ async function playSongFromPlaylist(songYtId, playlist) {
await playSong(songToPlay);
}

/**
* @param {string} songYtId
* @param {string} playlistPubId
*/
async function playSongFromPlaylistId(songYtId, playlistPubId) {
const playlist = await fetchPlaylistMeta(playlistPubId);
await playSongFromPlaylist(songYtId, playlist);
}

/**
* @param {Song} song
*/
Expand All @@ -735,6 +808,14 @@ function appendSongToCurrentQueue(song) {
alert(`Added ${song.title} to the queue!`);
}

/**
* @param {string} songYtId
*/
async function appendSongToCurrentQueueId(songYtId) {
const song = await fetchSongMeta(songYtId);
appendSongToCurrentQueue(song);
}

function addSongToPlaylist() {
throw new Error("not implemented!");
}
Expand Down Expand Up @@ -975,12 +1056,18 @@ window.Player.downloadSongToDevice = downloadSongToDevice;
window.Player.showPlayer = show;
window.Player.hidePlayer = hide;
window.Player.playSingleSong = playSingleSong;
window.Player.playSingleSongId = playSingleSongId;
window.Player.playSingleSongNext = playSingleSongNext;
window.Player.playSingleSongNextId = playSingleSongNextId;
window.Player.playSongFromPlaylist = playSongFromPlaylist;
window.Player.playSongFromPlaylistId = playSongFromPlaylistId;
window.Player.playPlaylistNext = playPlaylistNext;
window.Player.playPlaylistNextId = playPlaylistNextId;
window.Player.removeSongFromPlaylist = removeSongFromPlaylist;
window.Player.addSongToQueue = appendSongToCurrentQueue;
window.Player.addSongToQueueId = appendSongToCurrentQueueId;
window.Player.appendPlaylistToCurrentQueue = appendPlaylistToCurrentQueue;
window.Player.appendPlaylistToCurrentQueueId = appendPlaylistToCurrentQueueId;
window.Player.stopMuzikk = stopMuzikk;
window.Player.expand = () => expand();
window.Player.collapse = () => collapse();
Loading

0 comments on commit 6a29256

Please sign in to comment.