From 9dd9e5f8b058d9fcc0d9aeacad0c700a59200a82 Mon Sep 17 00:00:00 2001 From: SrGooglo Date: Mon, 23 Jan 2023 23:18:06 +0000 Subject: [PATCH] added sync model --- .../app/src/models/sync/cores/spotifyCore.js | 87 +++++++++++++++++++ packages/app/src/models/sync/index.js | 11 +++ 2 files changed, 98 insertions(+) create mode 100644 packages/app/src/models/sync/cores/spotifyCore.js create mode 100644 packages/app/src/models/sync/index.js diff --git a/packages/app/src/models/sync/cores/spotifyCore.js b/packages/app/src/models/sync/cores/spotifyCore.js new file mode 100644 index 00000000..3365a30a --- /dev/null +++ b/packages/app/src/models/sync/cores/spotifyCore.js @@ -0,0 +1,87 @@ +export default class SpotifySyncModel { + static get spotify_redirect_uri() { + return window.location.origin + "/callbacks/sync/spotify" + } + + static get spotify_authorize_endpoint() { + return "https://accounts.spotify.com/authorize?response_type=code&client_id={{client_id}}&scope={{scope}}&redirect_uri={{redirect_uri}}&response_type=code" + } + + static async authorizeAccount() { + const scopes = [ + "user-read-private", + "user-modify-playback-state", + "user-read-currently-playing", + "user-read-playback-state", + "streaming", + ] + + const { client_id } = await SpotifySyncModel.get_client_id() + + const parsedUrl = SpotifySyncModel.spotify_authorize_endpoint + .replace("{{client_id}}", client_id) + .replace("{{scope}}", scopes.join(" ")) + .replace("{{redirect_uri}}", SpotifySyncModel.spotify_redirect_uri) + + // open on a new tab + window.open(parsedUrl, "_blank") + } + + static async get_client_id() { + const { data } = await app.api.customRequest("main", { + method: "GET", + url: `/sync/spotify/client_id`, + }) + + return data + } + + static async syncAuthCode(code) { + const { data } = await app.api.customRequest("main", { + method: "POST", + url: `/sync/spotify/auth`, + data: { + redirect_uri: SpotifySyncModel.spotify_redirect_uri, + code, + }, + }) + + return data + } + + static async unlinkAccount() { + const { data } = await app.api.customRequest("main", { + method: "POST", + url: `/sync/spotify/unlink`, + }) + + return data + } + + static async isAuthorized() { + const { data } = await app.api.customRequest("main", { + method: "GET", + url: `/sync/spotify/is_authorized`, + }) + + return data + } + + static async getData() { + const { data } = await app.api.customRequest("main", { + method: "GET", + url: `/sync/spotify/data`, + }) + + return data + } + + static async getCurrentPlaying() { + const { data } = await app.api.customRequest("main", { + method: "GET", + url: `/sync/spotify/currently_playing`, + }) + + return data + } +} \ No newline at end of file diff --git a/packages/app/src/models/sync/index.js b/packages/app/src/models/sync/index.js new file mode 100644 index 00000000..a706f285 --- /dev/null +++ b/packages/app/src/models/sync/index.js @@ -0,0 +1,11 @@ +import SpotifySyncModel from "./spotify" + +export default class SyncModel { + static get bridge() { + return window.app?.api.withEndpoints("main") + } + + static get spotifyCore() { + return SpotifySyncModel + } +} \ No newline at end of file