1
0
Fork 0
mirror of https://github.com/heyarne/airsonic-ui.git synced 2026-05-06 18:33:38 +02:00

Give user the ability to change the server url on login

This commit is contained in:
Arne Schlüter 2018-04-25 13:40:17 +02:00
commit 00e2cf656c
3 changed files with 34 additions and 18 deletions

View file

@ -1,5 +1,5 @@
(ns airsonic-ui.api
(:require [clojure.string :as string]
(:require [clojure.string :as str]
[airsonic-ui.config :as config]))
(defn ^:private uri-escape [s]
@ -7,15 +7,18 @@
(defn url
"Returns an absolute url to an API endpoint"
[endpoint params]
[server endpoint params]
(let [query (->> (assoc params
:f "json"
:c "airsonic-ui-cljs"
:v "1.15.0")
(map (fn [[k v]]
(str (uri-escape (name k)) "=" (uri-escape v))))
(string/join "&"))]
(str config/server "/rest/" endpoint "?" query)))
(str/join "&"))]
(str server (when-not (str/ends-with? server "/") "/") "/rest/" endpoint "?" query)))
(defn song-url [server credentials song]
(url server "stream" (merge {:id (:id song)} credentials)))
(defn ^:private api-error?
"We need to look at the message body because the subsonic api always responds

View file

@ -23,10 +23,11 @@
(re-frame/reg-event-fx
::authenticate
(fn [{:keys [db]} [_ user pass]]
{:db (update db :active-requests inc)
(fn [{:keys [db]} [_ user pass server]]
{:db (-> (update db :active-requests inc)
(assoc :server server))
:http-xhrio {:method :get
:uri (api/url "ping" {:u user :p pass})
:uri (api/url server "ping" {:u user :p pass})
:response-format (ajax/json-response-format {:keywords? true})
:on-success [::auth-success user pass]
:on-failure [::api-failure]}}))
@ -57,7 +58,7 @@
:api-request
(fn [{:keys [db]} [_ endpoint k params]]
{:http-xhrio {:method :get
:uri (api/url endpoint (merge params (:login db)))
:uri (api/url (:server db) endpoint (merge params (:login db)))
:response-format (ajax/json-response-format {:keywords? true})
:on-success [::api-success k]
:on-failure [::api-failure]}}))
@ -76,16 +77,13 @@
;; musique
(defn ->song-url [song credentials]
(api/url "stream" (merge {:id (:id song)} credentials)))
; TODO: Make play, next and previous a bit prettier and more DRY
(re-frame/reg-event-fx
; sets up the db, starts to play a song and adds the rest to a playlist
::play-songs
(fn [{:keys [db]} [_ songs song]]
{:play-song (->song-url song (:login db))
{:play-song (api/song-url (:server db) (:login db) song)
:db (-> db
(assoc-in [:currently-playing :item] song)
(assoc-in [:currently-playing :playlist] songs))}))
@ -97,7 +95,7 @@
current (-> db :currently-playing :item)
next (first (rest (drop-while #(not= % current) playlist)))]
(when next
{:play-song (->song-url next (:login db))
{:play-song (api/song-url (:server db) (:login db) next)
:db (assoc-in db [:currently-playing :item] next)}))))
(re-frame/reg-event-fx
@ -107,7 +105,7 @@
current (-> db :currently-playing :item)
previous (last (take-while #(not= % current) playlist))]
(when previous
{:play-song (->song-url previous (:login db))
{:play-song (api/song-url (:server db) (:login db) previous)
:db (assoc-in db [:currently-playing :item] previous)}))))
(re-frame/reg-event-fx

View file

@ -1,27 +1,42 @@
(ns airsonic-ui.views
(:require [re-frame.core :refer [dispatch subscribe]]
[reagent.core :as r]
[airsonic-ui.config :as config]
[airsonic-ui.routes :as routes]
[airsonic-ui.events :as events]
[airsonic-ui.subs :as subs]))
(defn- >reset!
"Sends all target values to the given atom"
[atom]
#(reset! atom (.. % -target -value)))
;; login form
(defn login-form []
(let [user (r/atom "")
pass (r/atom "")]
pass (r/atom "")
server (r/atom config/server)]
(fn []
[:div
[:div
[:span "User"]
[:input {:type "text"
:name "user"
:on-change #(reset! user (-> % .-target .-value))}]]
:on-change (>reset! user)}]]
[:div
[:span "Password"]
[:input {:type "password" :name "pass" :on-change #(reset! pass (-> % .-target .-value))}]]
[:input {:type "password"
:name "pass"
:on-change (>reset! pass)}]]
[:div
[:button {:on-click #(dispatch [::events/authenticate @user @pass])} "Submit"]]])))
[:span "Server"]
[:input {:type "text"
:name "server"
:on-change (>reset! server)
:value @server}]]
[:div
[:button {:on-click #(dispatch [::events/authenticate @user @pass @server])} "Submit"]]])))
;; album list (start page)