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 (ns airsonic-ui.api
(:require [clojure.string :as string] (:require [clojure.string :as str]
[airsonic-ui.config :as config])) [airsonic-ui.config :as config]))
(defn ^:private uri-escape [s] (defn ^:private uri-escape [s]
@ -7,15 +7,18 @@
(defn url (defn url
"Returns an absolute url to an API endpoint" "Returns an absolute url to an API endpoint"
[endpoint params] [server endpoint params]
(let [query (->> (assoc params (let [query (->> (assoc params
:f "json" :f "json"
:c "airsonic-ui-cljs" :c "airsonic-ui-cljs"
:v "1.15.0") :v "1.15.0")
(map (fn [[k v]] (map (fn [[k v]]
(str (uri-escape (name k)) "=" (uri-escape v)))) (str (uri-escape (name k)) "=" (uri-escape v))))
(string/join "&"))] (str/join "&"))]
(str config/server "/rest/" endpoint "?" query))) (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? (defn ^:private api-error?
"We need to look at the message body because the subsonic api always responds "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 (re-frame/reg-event-fx
::authenticate ::authenticate
(fn [{:keys [db]} [_ user pass]] (fn [{:keys [db]} [_ user pass server]]
{:db (update db :active-requests inc) {:db (-> (update db :active-requests inc)
(assoc :server server))
:http-xhrio {:method :get :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}) :response-format (ajax/json-response-format {:keywords? true})
:on-success [::auth-success user pass] :on-success [::auth-success user pass]
:on-failure [::api-failure]}})) :on-failure [::api-failure]}}))
@ -57,7 +58,7 @@
:api-request :api-request
(fn [{:keys [db]} [_ endpoint k params]] (fn [{:keys [db]} [_ endpoint k params]]
{:http-xhrio {:method :get {: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}) :response-format (ajax/json-response-format {:keywords? true})
:on-success [::api-success k] :on-success [::api-success k]
:on-failure [::api-failure]}})) :on-failure [::api-failure]}}))
@ -76,16 +77,13 @@
;; musique ;; 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 ; TODO: Make play, next and previous a bit prettier and more DRY
(re-frame/reg-event-fx (re-frame/reg-event-fx
; sets up the db, starts to play a song and adds the rest to a playlist ; sets up the db, starts to play a song and adds the rest to a playlist
::play-songs ::play-songs
(fn [{:keys [db]} [_ songs song]] (fn [{:keys [db]} [_ songs song]]
{:play-song (->song-url song (:login db)) {:play-song (api/song-url (:server db) (:login db) song)
:db (-> db :db (-> db
(assoc-in [:currently-playing :item] song) (assoc-in [:currently-playing :item] song)
(assoc-in [:currently-playing :playlist] songs))})) (assoc-in [:currently-playing :playlist] songs))}))
@ -97,7 +95,7 @@
current (-> db :currently-playing :item) current (-> db :currently-playing :item)
next (first (rest (drop-while #(not= % current) playlist)))] next (first (rest (drop-while #(not= % current) playlist)))]
(when next (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)})))) :db (assoc-in db [:currently-playing :item] next)}))))
(re-frame/reg-event-fx (re-frame/reg-event-fx
@ -107,7 +105,7 @@
current (-> db :currently-playing :item) current (-> db :currently-playing :item)
previous (last (take-while #(not= % current) playlist))] previous (last (take-while #(not= % current) playlist))]
(when previous (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)})))) :db (assoc-in db [:currently-playing :item] previous)}))))
(re-frame/reg-event-fx (re-frame/reg-event-fx

View file

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