2023-02-12 20:31:51 +08:00
|
|
|
import { getAssetIDByName } from "@vendetta/ui/assets";
|
|
|
|
|
import { showToast } from "@vendetta/ui/toasts";
|
2023-07-09 08:50:00 +08:00
|
|
|
|
|
|
|
|
import { currentSettings, pluginState, verboseLog } from ".";
|
|
|
|
|
import { Activity } from "../../defs";
|
2023-02-12 20:31:51 +08:00
|
|
|
import Constants from "./constants";
|
2023-07-09 08:50:00 +08:00
|
|
|
import { SelfPresenceStore } from "./modules";
|
|
|
|
|
import { clearActivity, fetchAsset, sendRequest } from "./utils/activity";
|
|
|
|
|
import { fetchLatestScrobble } from "./utils/lastfm";
|
2023-02-15 18:15:10 +08:00
|
|
|
|
2023-04-05 17:43:33 +08:00
|
|
|
enum ActivityType {
|
|
|
|
|
PLAYING = 0,
|
|
|
|
|
STREAMING = 1,
|
|
|
|
|
LISTENING = 2,
|
|
|
|
|
COMPETING = 5
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-12 20:31:51 +08:00
|
|
|
/**
|
|
|
|
|
* Fetches the current scrobble and sets the activity.
|
|
|
|
|
* This function is actively called by the updateInterval.
|
|
|
|
|
*/
|
2023-02-15 18:15:10 +08:00
|
|
|
async function update() {
|
2023-02-12 20:31:51 +08:00
|
|
|
verboseLog("--> Fetching last track...");
|
|
|
|
|
|
|
|
|
|
if (!currentSettings.username) {
|
|
|
|
|
showToast("Last.fm username is not set!", getAssetIDByName("Small"));
|
2023-02-15 18:15:10 +08:00
|
|
|
flush(); // Flush as we always need reinitialization
|
2023-02-12 20:31:51 +08:00
|
|
|
throw new Error("Username is not set");
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-16 21:47:54 +08:00
|
|
|
if (currentSettings.ignoreSpotify) {
|
2023-07-09 08:17:28 +08:00
|
|
|
const spotifyActivity = SelfPresenceStore.findActivity(act => act.sync_id);
|
|
|
|
|
if (spotifyActivity) {
|
|
|
|
|
verboseLog("--> Spotify is currently playing, aborting...");
|
|
|
|
|
clearActivity();
|
|
|
|
|
return;
|
2023-03-16 21:47:54 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-15 15:04:37 +08:00
|
|
|
const lastTrack = await fetchLatestScrobble().catch(async (err) => {
|
2023-02-12 20:31:51 +08:00
|
|
|
verboseLog("--> An error occurred while fetching the last track, aborting...");
|
2023-02-15 18:15:10 +08:00
|
|
|
clearActivity();
|
2023-02-12 20:31:51 +08:00
|
|
|
throw err;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (!lastTrack.nowPlaying) {
|
|
|
|
|
verboseLog("--> Last track is not currently playing, aborting...");
|
2023-02-15 18:15:10 +08:00
|
|
|
clearActivity();
|
|
|
|
|
return;
|
2023-02-12 20:31:51 +08:00
|
|
|
}
|
|
|
|
|
|
2023-02-15 18:15:10 +08:00
|
|
|
verboseLog("--> Track fetched!");
|
2023-02-12 20:31:51 +08:00
|
|
|
|
2023-07-09 08:50:00 +08:00
|
|
|
if (pluginState.lastTrackUrl === lastTrack.url) {
|
2023-02-12 20:31:51 +08:00
|
|
|
verboseLog("--> Last track is the same as the previous one, aborting...");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const activity = {
|
2023-02-12 21:32:36 +08:00
|
|
|
name: currentSettings.appName || Constants.DEFAULT_APP_NAME,
|
2023-02-15 18:15:10 +08:00
|
|
|
flags: 0,
|
2023-07-09 08:17:28 +08:00
|
|
|
type: currentSettings.listeningTo ? ActivityType.LISTENING : ActivityType.PLAYING,
|
2023-02-12 20:31:51 +08:00
|
|
|
details: lastTrack.name,
|
|
|
|
|
state: `by ${lastTrack.artist}`,
|
2023-02-15 18:15:10 +08:00
|
|
|
application_id: Constants.APPLICATION_ID,
|
2023-02-12 20:31:51 +08:00
|
|
|
} as Activity;
|
|
|
|
|
|
2023-07-09 08:50:00 +08:00
|
|
|
pluginState.lastTrackUrl = lastTrack.url;
|
2023-02-12 20:31:51 +08:00
|
|
|
|
2023-02-16 21:58:36 +08:00
|
|
|
if (activity.name.includes("{{")) {
|
|
|
|
|
for (const key in lastTrack) {
|
|
|
|
|
activity.name = activity.name.replace(`{{${key}}}`, lastTrack[key]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-12 20:31:51 +08:00
|
|
|
// Set timestamps
|
|
|
|
|
if (currentSettings.showTimestamp) {
|
|
|
|
|
activity.timestamps = {
|
|
|
|
|
start: Date.now() / 1000 | 0
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-12 21:47:31 +08:00
|
|
|
if (lastTrack.album) {
|
2023-02-12 20:31:51 +08:00
|
|
|
activity.assets = {
|
2023-04-27 14:37:08 +08:00
|
|
|
large_image: (await fetchAsset([lastTrack.albumArt]))[0],
|
2023-02-12 21:47:31 +08:00
|
|
|
large_text: `on ${lastTrack.album}`
|
2023-02-12 22:48:12 +08:00
|
|
|
};
|
2023-02-12 20:31:51 +08:00
|
|
|
}
|
|
|
|
|
|
2023-02-15 18:15:10 +08:00
|
|
|
verboseLog("--> Setting activity...");
|
|
|
|
|
verboseLog(activity);
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
sendRequest(activity);
|
|
|
|
|
} catch (err) {
|
2023-02-12 20:31:51 +08:00
|
|
|
verboseLog("--> An error occurred while setting the activity");
|
2023-02-15 18:15:10 +08:00
|
|
|
clearActivity();
|
2023-02-12 20:31:51 +08:00
|
|
|
throw err;
|
2023-02-15 18:15:10 +08:00
|
|
|
}
|
2023-02-12 20:31:51 +08:00
|
|
|
|
|
|
|
|
verboseLog("--> Successfully set activity!");
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-15 18:15:10 +08:00
|
|
|
/** Stops and reset everything, can be started again with `initialize()` */
|
|
|
|
|
export function flush() {
|
2023-02-12 20:31:51 +08:00
|
|
|
console.log("--> Flushing...");
|
2023-07-09 08:50:00 +08:00
|
|
|
pluginState.lastActivity = null;
|
|
|
|
|
pluginState.lastTrackUrl = null;
|
2023-02-15 15:04:37 +08:00
|
|
|
|
2023-07-09 08:50:00 +08:00
|
|
|
pluginState.updateInterval && clearInterval(pluginState.updateInterval);
|
2023-02-15 18:15:10 +08:00
|
|
|
clearActivity();
|
2023-02-12 20:31:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Initializes the plugin */
|
|
|
|
|
export async function initialize() {
|
|
|
|
|
console.log("--> Initializing...");
|
|
|
|
|
|
2023-07-09 08:50:00 +08:00
|
|
|
if (pluginState.pluginStopped) {
|
2023-02-12 20:31:51 +08:00
|
|
|
throw new Error("Plugin is already stopped!");
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-15 18:15:10 +08:00
|
|
|
flush();
|
2023-02-12 20:31:51 +08:00
|
|
|
|
|
|
|
|
let tries = 0;
|
|
|
|
|
|
2023-02-15 18:15:10 +08:00
|
|
|
await update().catch((err) => {
|
2023-02-12 20:31:51 +08:00
|
|
|
console.error(err);
|
|
|
|
|
tries++;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Periodically fetches the current scrobble and sets the activity
|
2023-07-09 08:50:00 +08:00
|
|
|
pluginState.updateInterval = setInterval(
|
2023-02-12 20:31:51 +08:00
|
|
|
() => update()
|
2023-02-15 18:15:10 +08:00
|
|
|
.then(() => {
|
|
|
|
|
tries = 0;
|
|
|
|
|
})
|
2023-02-12 20:31:51 +08:00
|
|
|
.catch(err => {
|
|
|
|
|
console.error(err);
|
|
|
|
|
|
|
|
|
|
if (++tries > 3) {
|
|
|
|
|
console.error("Failed to fetch/set activity 3 times, aborting...");
|
2023-02-15 18:15:10 +08:00
|
|
|
flush();
|
2023-02-12 20:31:51 +08:00
|
|
|
}
|
|
|
|
|
}),
|
2023-04-07 22:02:32 +08:00
|
|
|
(Number(currentSettings.timeInterval) || Constants.DEFAULT_TIME_INTERVAL) * 1000
|
2023-02-12 20:31:51 +08:00
|
|
|
);
|
|
|
|
|
}
|