Something went wrong. Try again.
A music player that connects to your cloud/distributed storage. forked from tokono.ma/diffuse
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528module User.Layer exposing (..)
{-| User Layer.
This concerns data that relates to the app,controlled by the user and stored by the user.
**Enclosed** data is data like, the enable-shuffle setting,equalizer settings, or the currently-active-search term.Which is stored in the browser.
**Hypaethral** data is data like, the user's favourites,processed tracks, or the user's sources.Which is stored in the location chosen by the user.
-}
import Dict exposing (Dict)import Enum exposing (Enum)import Equalizerimport Json.Decode as Jsonimport Json.Decode.Ext as Jsonimport Json.Decode.Pipeline exposing (optional)import Json.Encodeimport List.Extra as Listimport Maybe.Extra as Maybeimport Playlistsimport Playlists.Encoding as Playlistsimport Settingsimport Sourcesimport Sources.Encoding as Sourcesimport Task exposing (Task)import Themeimport Timeimport Time.Ext as Timeimport Tracksimport Tracks.Encoding as Tracks
-- 🌳
type Method = Dropbox { accessToken : String, expiresAt : Int, refreshToken : String } | Ipfs { apiOrigin : String } | RemoteStorage { userAddress : String, token : String }
dropboxMethod : MethoddropboxMethod = Dropbox { accessToken = "", expiresAt = 0, refreshToken = "" }
ipfsMethod : MethodipfsMethod = Ipfs { apiOrigin = "https://ipfs.io" }
remoteStorageMethod : MethodremoteStorageMethod = RemoteStorage { userAddress = "", token = "" }
-- 🌳 ░░ ENCLOSED
type alias EnclosedData = { cachedTracks : List String , equalizerSettings : Equalizer.Settings , grouping : Maybe Tracks.Grouping , onlyShowCachedTracks : Bool , onlyShowFavourites : Bool , repeat : Bool , scene : Tracks.Scene , searchTerm : Maybe String , selectedPlaylist : Maybe String , shuffle : Bool , sortBy : Tracks.SortBy , sortDirection : Tracks.SortDirection , theme : Maybe Theme.Id }
-- 🌳 ░░ HYPAETHRAL
type HypaethralBit = Favourites | Playlists | Progress | Settings | Sources | Tracks -- | ModifiedAt
type HypaethralBaggage = BaggageClaimed -- | PlaylistsBaggage PlaylistsBaggageAttributes
type alias HypaethralData = { favourites : List Tracks.Favourite , playlists : List Playlists.Playlist , progress : Dict String Float , settings : Maybe Settings.Settings , sources : List Sources.Source , tracks : List Tracks.Track
-- , modifiedAt : Maybe Time.Posix }
type alias PlaylistsBaggageAttributes = { publicPlaylistsRead : List Json.Value , publicPlaylistsTodo : List String , privatePlaylistsRead : List Json.Value , privatePlaylistsTodo : List String }
-- 🔱 ░░ METHOD
decodeMethod : Json.Value -> Maybe MethoddecodeMethod = Json.decodeValue (Json.map methodFromString Json.string) >> Result.toMaybe >> Maybe.join
encodeMethod : Method -> Json.ValueencodeMethod = methodToString >> Json.Encode.string
methodName : Method -> StringmethodName method = case method of Dropbox _ -> "Dropbox"
Ipfs _ -> "IPFS (using MFS)"
RemoteStorage _ -> "Remote Storage"
methodFromString : String -> Maybe MethodmethodFromString string = case String.split methodSeparator string of [ "DROPBOX", a, e, r ] -> Just (Dropbox { accessToken = a , expiresAt = Maybe.withDefault 0 (String.toInt e) , refreshToken = r } )
[ "IPFS", a ] -> Just (Ipfs { apiOrigin = a })
[ "REMOTE_STORAGE", u, t ] -> Just (RemoteStorage { userAddress = u, token = t })
_ -> Nothing
methodToString : Method -> StringmethodToString method = case method of Dropbox { accessToken, expiresAt, refreshToken } -> String.join methodSeparator [ "DROPBOX" , accessToken , String.fromInt expiresAt , refreshToken ]
Ipfs { apiOrigin } -> String.join methodSeparator [ "IPFS" , apiOrigin ]
RemoteStorage { userAddress, token } -> String.join methodSeparator [ "REMOTE_STORAGE" , userAddress , token ]
methodSeparator : StringmethodSeparator = "___"
methodSupportsPublicData : Method -> BoolmethodSupportsPublicData method = case method of Dropbox _ -> False
Ipfs _ -> False
RemoteStorage _ -> False
-- 🔱 ░░ ENCLOSED
decodeEnclosedData : Json.Value -> Result Json.Error EnclosedDatadecodeEnclosedData = Json.decodeValue enclosedDataDecoder
enclosedDataDecoder : Json.Decoder EnclosedDataenclosedDataDecoder = Json.succeed EnclosedData |> optional "cachedTracks" (Json.list Json.string) [] |> optional "equalizerSettings" Equalizer.settingsDecoder Equalizer.defaultSettings |> optional "grouping" (Json.maybe Tracks.groupingDecoder) Nothing |> optional "onlyShowCachedTracks" Json.bool False |> optional "onlyShowFavourites" Json.bool False |> optional "repeat" Json.bool False |> optional "scene" Tracks.sceneDecoder Tracks.Covers |> optional "searchTerm" (Json.maybe Json.string) Nothing |> optional "selectedPlaylist" (Json.maybe Json.string) Nothing |> optional "shuffle" Json.bool False |> optional "sortBy" Tracks.sortByDecoder Tracks.Album |> optional "sortDirection" Tracks.sortDirectionDecoder Tracks.Asc |> optional "theme" (Json.maybe Theme.idDecoder) Nothing
encodeEnclosedData : EnclosedData -> Json.ValueencodeEnclosedData { cachedTracks, equalizerSettings, grouping, onlyShowCachedTracks, onlyShowFavourites, repeat, scene, searchTerm, selectedPlaylist, shuffle, sortBy, sortDirection, theme } = Json.Encode.object [ ( "cachedTracks", Json.Encode.list Json.Encode.string cachedTracks ) , ( "equalizerSettings", Equalizer.encodeSettings equalizerSettings ) , ( "grouping", Maybe.unwrap Json.Encode.null Tracks.encodeGrouping grouping ) , ( "onlyShowCachedTracks", Json.Encode.bool onlyShowCachedTracks ) , ( "onlyShowFavourites", Json.Encode.bool onlyShowFavourites ) , ( "repeat", Json.Encode.bool repeat ) , ( "scene", Tracks.encodeScene scene ) , ( "searchTerm", Maybe.unwrap Json.Encode.null Json.Encode.string searchTerm ) , ( "selectedPlaylist", Maybe.unwrap Json.Encode.null Json.Encode.string selectedPlaylist ) , ( "shuffle", Json.Encode.bool shuffle ) , ( "sortBy", Tracks.encodeSortBy sortBy ) , ( "sortDirection", Tracks.encodeSortDirection sortDirection ) , ( "theme", Maybe.unwrap Json.Encode.null Theme.encodeId theme ) ]
-- 🔱 ░░ HYPAETHRAL
allHypaethralBits : List HypaethralBitallHypaethralBits = [ Favourites , Playlists , Progress , Settings , Sources , Tracks ]
decodeHypaethralData : Json.Value -> Result Json.Error HypaethralDatadecodeHypaethralData = Json.decodeValue hypaethralDataDecoder
emptyHypaethralData : HypaethralDataemptyHypaethralData = { favourites = [] , playlists = [] , progress = Dict.empty , settings = Nothing , sources = [] , tracks = []
-- , modifiedAt = Nothing }
encodeHypaethralBit : HypaethralBit -> HypaethralData -> Json.ValueencodeHypaethralBit bit { favourites, playlists, progress, settings, sources, tracks, modifiedAt } = case bit of ModifiedAt -> Maybe.unwrap Json.Encode.null Time.encode modifiedAt
_ -> Json.Encode.object [ ( "data" , case bit of Favourites -> Json.Encode.list Tracks.encodeFavourite favourites
ModifiedAt -> Maybe.unwrap Json.Encode.null Time.encode modifiedAt
Playlists -> Json.Encode.list Playlists.encode playlists
Progress -> Json.Encode.dict identity Json.Encode.float progress
Settings -> Maybe.unwrap Json.Encode.null Settings.encode settings
Sources -> Json.Encode.list Sources.encode sources
Tracks -> Json.Encode.list Tracks.encodeTrack tracks ) , ( "modifiedAt" , Maybe.unwrap Json.Encode.null Time.encode modifiedAt ) ]
encodeHypaethralData : HypaethralData -> Json.ValueencodeHypaethralData data = data |> encodedHypaethralDataList |> List.map (Tuple.mapFirst hypaethralBitKey) |> Json.Encode.object
encodedHypaethralDataList : HypaethralData -> List ( HypaethralBit, Json.Value )encodedHypaethralDataList data = List.map (\bit -> ( bit, encodeHypaethralBit bit data )) allHypaethralBits
hypaethralBit : Enum HypaethralBithypaethralBit = allHypaethralBits |> List.map (\bit -> ( hypaethralBitKey bit, bit )) |> Enum.create
hypaethralBitFileName : HypaethralBit -> StringhypaethralBitFileName bit = hypaethralBitKey bit ++ ".json"
hypaethralBitKey : HypaethralBit -> StringhypaethralBitKey bit = case bit of Favourites -> "favourites"
ModifiedAt -> "modified"
Playlists -> "playlists"
Progress -> "progress"
Settings -> "settings"
Sources -> "sources"
Tracks -> "tracks"
hypaethralDataDecoder : Json.Decoder HypaethralDatahypaethralDataDecoder = let optionalWithPossiblyData key dec def a = optional (hypaethralBitKey key) (Json.oneOf [ modifiedAtDecoder dec, noModifiedAt dec ]) { data = def, modifiedAt = Nothing } a in (\fav pla pro set sor tra mod -> { favourites = fav.data , playlists = pla.data , progress = pro.data , settings = set.data , sources = sor.data , tracks = tra.data
-- , modifiedAt = case mod of Just m -> Just (Time.millisToPosix m)
Nothing -> [ fav.modifiedAt , pla.modifiedAt , pro.modifiedAt , set.modifiedAt , sor.modifiedAt , tra.modifiedAt ] |> List.filterMap (Maybe.map Time.posixToMillis) |> List.sort |> List.last |> Maybe.map Time.millisToPosix } ) |> Json.succeed |> optionalWithPossiblyData Favourites (Json.listIgnore Tracks.favouriteDecoder) [] |> optionalWithPossiblyData Playlists (Json.listIgnore Playlists.decoder) [] |> optionalWithPossiblyData Progress (Json.dict Json.float) Dict.empty |> optionalWithPossiblyData Settings (Json.maybe Settings.decoder) Nothing |> optionalWithPossiblyData Sources (Json.listIgnore Sources.decoder) [] |> optionalWithPossiblyData Tracks (Json.listIgnore Tracks.trackDecoder) [] |> optional (hypaethralBitKey ModifiedAt) (Json.maybe Json.int) Nothing
-- merge : HypaethralData -> HypaethralData -> HypaethralData-- merge a b =-- { favourites = List.unique (a.favourites ++ b.favourites)-- , playlists = List.unique (a.playlists ++ b.playlists)-- , progress = List.unique (a.progress ++ b.progress)-- , settings = List.unique (a.settings ++ b.settings)-- , sources = List.unique (a.sources ++ b.sources)-- , tracks = List.unique (a.tracks ++ b.tracks)-- ---- , modifiedAt =-- case ( a.modifiedAt, b.modifiedAt ) of-- ( Just am, Just bm ) ->-- if Time.posixToMillis am > Time.posixToMillis bm then-- Just am-- else-- Just bm-- ( Just am, Nothing ) ->-- Just am-- ( Nothing, Just bm ) ->-- Just bm-- ( Nothing, Nothing ) ->-- Nothing-- }
modifiedAtDecoder : Json.Decoder a -> Json.Decoder { data : a, modifiedAt : Maybe Time.Posix }modifiedAtDecoder decoder = Json.map2 (\d m -> { data = d, modifiedAt = m }) (Json.field "data" decoder) (Json.maybe <| Json.field "modifiedAt" Time.decoder)
noModifiedAt : Json.Decoder a -> Json.Decoder { data : a, modifiedAt : Maybe Time.Posix }noModifiedAt = Json.map (\data -> { data = data , modifiedAt = Nothing } )
putHypaethralJsonBitsTogether : List ( HypaethralBit, Json.Value, HypaethralBaggage ) -> Json.ValueputHypaethralJsonBitsTogether bits = bits |> List.map (\( a, b, _ ) -> ( hypaethralBitKey a, b )) |> Json.Encode.object
retrieveHypaethralData : (HypaethralBit -> Task x (Maybe Json.Value)) -> Task x (List ( HypaethralBit, Maybe Json.Encode.Value ))retrieveHypaethralData retrievalFn = hypaethralBit.list |> List.map (\( _, bit ) -> bit |> retrievalFn |> Task.map (\value -> ( bit, value )) ) |> Task.sequence
saveHypaethralData : (HypaethralBit -> Json.Value -> Task x ()) -> HypaethralData -> Task x ()saveHypaethralData saveFn data = hypaethralBit.list |> List.map (\( _, bit ) -> data |> encodeHypaethralBit bit |> saveFn bit ) |> Task.sequence |> Task.map (always ())
-- 🔱 ░░ BAGGAGE
mapPlaylistsBaggage : (PlaylistsBaggageAttributes -> PlaylistsBaggageAttributes) -> HypaethralBaggage -> HypaethralBaggagemapPlaylistsBaggage fn baggage = case baggage of PlaylistsBaggage p -> PlaylistsBaggage (fn p)
b -> b