Files
discord-decomp/modules/spotify/SpotifyUtils.tsx
T
2026-11-01 12:12:55 +02:00

111 lines
4.3 KiB
TypeScript

// Metro module 10941 (f10942).
import RunningGameStore_native from "../game_detection/RunningGameStore.native";
import SpotifyProtocolStore from "./SpotifyProtocolStore";
import SpotifyStore from "./SpotifyStore";
import { SPOTIFY_APP_PROTOCOL, SpotifyResourceTypes, getSpotifyResourceType } from "./SpotifyConstants";
import { PlatformTypes } from "../../Constants";
import Durations from "../../utils/Durations";
import Platforms from "../../lib/Platforms";
import { setActiveDevice, getProfile } from "./SpotifyActionCreators";
import { getMetadata } from "../../actions/UserActivityActionCreators";
let c7368_11;
const asString = function asString(a0) {
if (typeof a0 === "string") {
return a0;
}
throw new Error("value is not a string");
};
c7368_11 = 30 * Durations.Millis.SECOND;
export function isSpotifyPlayable(a0) {
return a0.getActiveSocketAndDevice() != null || SpotifyProtocolStore.isProtocolRegistered();
}
export function ensureSpotifyPlayable() {
const activeSocketAndDevice = SpotifyStore.getActiveSocketAndDevice();
if (activeSocketAndDevice != null) {
return Promise.resolve(activeSocketAndDevice);
}
if (SpotifyProtocolStore.isProtocolRegistered()) {
const playableComputerDevices = SpotifyStore.getPlayableComputerDevices();
if (RunningGameStore_native.isObservedAppRunning(Platforms.get(PlatformTypes.SPOTIFY).name) && playableComputerDevices.length > 0) {
const r44 = playableComputerDevices[0];
const { socket: socket3, device: device2 } = r44;
setActiveDevice(socket3.accountId, device2.id);
return Promise.resolve({
socket: socket3,
device: device2
});
}
return new Promise((c26874_0, c26874_1) => {
let c26874_2;
const onSpotifyStoreChange2 = function onSpotifyStoreChange() {
const playableComputerDevices2 = SpotifyStore.getPlayableComputerDevices();
const _loop = function _loop(socket2, device) {
if (playableComputerDevices.find((playableComputerDevice) => playableComputerDevice.device.id === device.id) == null) {
clearTimeout(c26874_2);
SpotifyStore.removeChangeListener(onSpotifyStoreChange2);
setImmediate(() => {
setActiveDevice(socket2.accountId, device.id);
c26874_0({
socket: socket2,
device
});
});
}
};
for (const item of playableComputerDevices2) {
_loop(item.socket, item.device);
}
};
c26874_2 = setTimeout(() => {
SpotifyStore.removeChangeListener(onSpotifyStoreChange2);
c26874_1(new Error("timeout launching spotify"));
}, c7368_11);
SpotifyStore.addChangeListener(onSpotifyStoreChange2);
window.open(`${SPOTIFY_APP_PROTOCOL}:`);
});
}
return Promise.reject(new Error("protocol is not registered"));
}
export function isSpotifyPremium() {
const activeSocketAndDevice = SpotifyStore.getActiveSocketAndDevice();
return activeSocketAndDevice != null ? activeSocketAndDevice.socket.isPremium : null;
}
export function ensureSpotifyPremium() {
const activeSocketAndDevice = SpotifyStore.getActiveSocketAndDevice();
if (activeSocketAndDevice == null) {
return Promise.reject(new Error("no active profile"));
}
const socket = activeSocketAndDevice.socket;
return socket.isPremium ? Promise.resolve() : getProfile(socket.accountId, socket.accessToken).then(() => {
if (socket.isPremium) {
return void 0;
}
return Promise.reject(new Error("spotify account is not premium"));
});
}
export async function getSpotifyMetadataFromActivity(c26873_0, c26873_1) {
let c26873_4;
c26873_4 = void 0;
let c30305_0 = await getMetadata(c26873_0, c26873_1);
const type2 = c30305_0.type;
let type = type2;
if (type2 == null) {
type = SpotifyResourceTypes.TRACK;
}
asString(type);
let c30305_1 = getSpotifyResourceType(type);
if (c30305_1 === null) {
throw new Error(`invalid type ${c30305_0.type}`);
}
const context_uri = typeof c30305_0.context_uri === "string" ? c30305_0.context_uri : void 0;
const album_id = c30305_0.album_id;
asString(album_id);
return {
context_uri,
album_id,
artist_ids: Array.isArray(c30305_0.artist_ids) ? c30305_0.artist_ids.map(asString) : [],
type: c30305_1,
button_urls: Array.isArray(c30305_0.button_urls) ? c30305_0.button_urls.map(asString) : []
};
}