<commit_before>(ns trak.action_handlers
  (:require [datascript.core :as d]
            [trak.utils :as utils]
            [trak.api :as api]
            [trak.globals :as globals]
            [trak.db :as db]))


(def api-result-handler)

(defmulti handler (fn [action params *db] action))

(defmethod handler :find-albums [_ _ _]
  (api/find-albums "blues"))

(defmethod handler :api-call-result [_ params db]
  (utils/info "Api call result " params)
  (api-result-handler (:call params) (:response params) db))

(defmethod handler :login-on-arrival [_ params db]
  (utils/info "Trying to log in user on arrival " params)
  (when-let [auth (globals/get-spotify-auth-token)]
    (utils/info db)
    (d/transact! db [{:me/identifier :me
                      :me/status     :loading}])
    (d/transact! db [(merge {:me/identifier :me}
                            (db/convert-atrrs-to-datascript auth :me))])
    (utils/info (:access_token auth))
    (api/me (:access_token auth))))


(defmulti api-result-handler (fn [api result *db] api))

(defmethod api-result-handler :find-albums [action response *db]
  (utils/info "Action received " action response)
  (when-let [{{{albums :items} :albums} :body} response]
    (d/transact! *db (db/json-to-datascript albums :album))))

(defmethod api-result-handler :me [action response *db]
  (utils/info "Action received " action response)
  (cond
    (= 401 (:status response)) (d/transact! *db [{:me/identifier :me :me/status :logged-out}])
    (= 200 (:status response))
    (d/transact! *db [(merge {:me/identifier :me :me/status :logged-in}
                             (db/convert-atrrs-to-datascript (:body response) :me))])))<commit_msg>Retrieve a user's playlist in action handlers<commit_after>(ns trak.action_handlers
  (:require [datascript.core :as d]
            [trak.utils :as utils]
            [trak.api :as api]
            [trak.globals :as globals]
            [trak.db :as db]))


(def api-result-handler)

(defmulti handler (fn [action params *db]
                    (utils/debug "Action handler " action " with params " params)
                    action))

(defmethod handler :find-albums [_ _ _]
  (api/find-albums "blues"))

(defmethod handler :api-call-result [_ params db]
  (api-result-handler (:call params) (:response params) db))

(defmethod handler :login-on-arrival [_ params db]
  (when-let [auth (globals/get-spotify-auth-token)]
    (d/transact! db [(merge {:me/identifier :me
                             :me/status     :loading}
                            (db/convert-atrrs-to-datascript auth :me))])
    (api/me (:access_token auth))
    ))


(defmulti api-result-handler (fn [api result *db]
                               (utils/debug "API result handler " api " with result " result)
                               api))

(defmethod api-result-handler :find-albums [action response *db]
  (when-let [{{{albums :items} :albums} :body} response]
    (d/transact! *db (db/json-to-datascript albums :album))))

(defmethod api-result-handler :me [action response *db]
  (cond
    (= 401 (:status response)) (d/transact! *db [{:me/identifier :me :me/status :logged-out}])
    (= 200 (:status response))
    (do
      (d/transact! *db [(merge {:me/identifier :me :me/status :logged-in}
                               (db/convert-atrrs-to-datascript (:body response) :me))])
      (let [me (db/me @*db)]
        (api/user-playlists (:me/href me) (:me/access_token me))))))

(defmethod api-result-handler :user-playlists [action response *db]
  (let [body (:body response)
        items (:items body)
        db-playlists {:playlists/limit    (:limit body)
                      :playlists/next     (utils/ifnil? (:next body) -1)
                      :playlists/offset   (:offset body)
                      :playlists/previous (utils/ifnil? (:previous body) -1)
                      :playlists/total    (:total body)
                      :playlists/owner    :me}
        owned-items (map (fn [item] (merge item {:owner :me})) items)
        db-items (db/json-to-datascript owned-items :playlist)
        ]
    (d/transact! *db [db-playlists])
    (d/transact! *db db-items)
    ))
{'fraction_non_alphanumeric': 0.14965169349027144, 'fraction_numerical': 0.0033629594042757626, 'mean_word_length': 2.424342105263158, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5154613', 'n_tokens_mistral': 1402, 'n_tokens_neox': 1335, 'n_words': 353}