Skip to content

Commit

Permalink
Merge pull request #2837 from alexbcberio/master
Browse files Browse the repository at this point in the history
Fix issue #2831, added some presence settings & migrated to presence helpers
  • Loading branch information
mergify[bot] authored Jan 20, 2021
2 parents d1bd7bd + 1b9abdc commit 1f4477a
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 44 deletions.
18 changes: 16 additions & 2 deletions websites/J/Jellyfin/dist/metadata.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"$schema": "https://schemas.premid.app/metadata/1.3",
"author": {
"name": "alexbcberio",
"id": "202915432175239169"
Expand All @@ -14,7 +15,7 @@
"en": "The Free Software Media System"
},
"service": "Jellyfin",
"version": "1.3.8",
"version": "1.4.0",
"logo": "https://i.imgur.com/nPzEemG.png",
"thumbnail": "https://i.imgur.com/J61378N.png",
"color": "#00698c",
Expand All @@ -25,5 +26,18 @@
"tv",
"media"
],
"regExp": ".*[/]web[/]index[.]html"
"regExp": ".*[/]web[/]index[.]html",
"settings": [
{
"id": "showMediaTimestamps",
"title": "Show media timestamps",
"icon": "fad fa-play",
"value": true
}, {
"id": "showTimestamps",
"title": "Show timestamps",
"icon": "fad fa-stopwatch",
"value": true
}
]
}
79 changes: 37 additions & 42 deletions websites/J/Jellyfin/presence.ts
Original file line number Diff line number Diff line change
Expand Up @@ -283,31 +283,13 @@ const // official website
largeImageKey: PRESENCE_ART_ASSETS.logo
};

let ApiClient: ApiClient;

// generic log style for PMD_[info|error|success] calls
const GENERIC_LOG_STYLE = "font-weight: 800; padding: 2px 5px; color: white;";

/**
* PMD_info - log into the user console info messages
*
* @param {string} txt text to log into the console
*/
function PMD_info(message: string): void {
console.log(
"%cPreMiD%cINFO%c " + message,
GENERIC_LOG_STYLE + "border-radius: 25px 0 0 25px; background: #596cae;",
GENERIC_LOG_STYLE + "border-radius: 0 25px 25px 0; background: #5050ff;",
"color: unset;"
);
}

let presence: Presence;
let ApiClient: ApiClient,
presence: Presence;

/**
* handleAudioPlayback - handles the presence when the audio player is active
*/
function handleAudioPlayback(): void {
async function handleAudioPlayback(): Promise<void> {
// sometimes the buttons are not created fast enough
try {
const audioElem = document.getElementsByTagName("audio")[0],
Expand All @@ -328,15 +310,15 @@ function handleAudioPlayback(): void {
if (!audioElem.paused) {
presenceData.smallImageKey = PRESENCE_ART_ASSETS.play;
presenceData.smallImageText = "Playing";
presenceData.endTimestamp = new Date(
Date.now() + (audioElem.duration - audioElem.currentTime) * 1000
).getTime();

if (await presence.getSetting("showMediaTimestamps")) {
presenceData.endTimestamp = presence.getTimestampsfromMedia(audioElem)[1];
}

// paused
} else {
presenceData.smallImageKey = PRESENCE_ART_ASSETS.pause;
presenceData.smallImageText = "Paused";
delete presenceData.endTimestamp;
}
} catch (e) {
// do nothing
Expand Down Expand Up @@ -429,8 +411,14 @@ async function obtainMediaInfo(itemId: string): Promise<string | MediaInfo> {
}

media[itemId] = "pending";

const res = await fetch(`/Users/${getUserId()}/Items/${itemId}`, {
const basePath = location.pathname.replace(
location.pathname
.split("/")
.slice(-2)
.join("/")
,
""),
res = await fetch(`${basePath}Users/${getUserId()}/Items/${itemId}`, {
credentials: "include",
headers: {
"x-emby-authorization": `MediaBrowser Client="${ApiClient["_appName"]}", Device="${ApiClient["_deviceName"]}", DeviceId="${ApiClient["_deviceId"]}", Version="${ApiClient["_appVersion"]}", Token="${ApiClient["_serverInfo"]["AccessToken"]}"`
Expand Down Expand Up @@ -508,16 +496,15 @@ async function handleVideoPlayback(): Promise<void> {
} else if (!videoPlayerElem.paused) {
presenceData.smallImageKey = PRESENCE_ART_ASSETS.play;
presenceData.smallImageText = "Playing";
presenceData.endTimestamp = new Date(
Date.now() +
(videoPlayerElem.duration - videoPlayerElem.currentTime) * 1000
).getTime();

if (await presence.getSetting("showMediaTimestamps")) {
presenceData.endTimestamp = presence.getTimestampsfromMedia(videoPlayerElem)[1];
}

// paused
} else {
presenceData.smallImageKey = PRESENCE_ART_ASSETS.pause;
presenceData.smallImageText = "Paused";
delete presenceData.endTimestamp;
}
}

Expand Down Expand Up @@ -602,7 +589,7 @@ async function handleWebClient(): Promise<void> {
audioElems[0].classList.contains("mediaPlayerAudio") &&
audioElems[0].src
) {
handleAudioPlayback();
await handleAudioPlayback();
return;
}

Expand Down Expand Up @@ -705,9 +692,9 @@ async function handleWebClient(): Promise<void> {
}

/**
* setDefaultsToPresence - set defaul values to the presenceData object
* setDefaultsToPresence - set default values to the presenceData object
*/
function setDefaultsToPresence(): void {
async function setDefaultsToPresence(): Promise<void> {
if (presenceData.smallImageKey) {
delete presenceData.smallImageKey;
}
Expand All @@ -720,6 +707,10 @@ function setDefaultsToPresence(): void {
if (presenceData.endTimestamp) {
delete presenceData.endTimestamp;
}

if (await presence.getSetting("showTimestamps")) {
presenceData.startTimestamp = Date.now();
}
}

/**
Expand All @@ -746,7 +737,7 @@ async function isJellyfinWebClient(): Promise<boolean> {
* updateData - tick function, this is called several times a second by UpdateData event
*/
async function updateData(): Promise<void> {
setDefaultsToPresence();
await setDefaultsToPresence();

let showPresence = false;

Expand All @@ -761,9 +752,11 @@ async function updateData(): Promise<void> {
await handleWebClient();
}

// force the display of some counter
if (!presenceData.startTimestamp || !presenceData.endTimestamp) {
presenceData.startTimestamp = Date.now();
// hide start timestamp on media playback
if (
presenceData.smallImageKey === PRESENCE_ART_ASSETS.play ||
presenceData.smallImageKey === PRESENCE_ART_ASSETS.pause) {
delete presenceData.startTimestamp;
}

// if jellyfin is detected init/update the presence status
Expand All @@ -781,12 +774,13 @@ async function updateData(): Promise<void> {
* init - check if the presence should be initialized, if so start doing the magic
*/
async function init(): Promise<void> {
let validPage = false;
let validPage = false,
infoMessage;

// jellyfin website
if (location.host === JELLYFIN_URL) {
validPage = true;
PMD_info("Jellyfin website detected");
infoMessage = "Jellyfin website detected";

// web client
} else {
Expand All @@ -800,7 +794,7 @@ async function init(): Promise<void> {
30 * 1000
) {
validPage = true;
PMD_info("Jellyfin web client detected");
infoMessage = "Jellyfin web client detected";
}
}
} catch (e) {
Expand All @@ -813,6 +807,7 @@ async function init(): Promise<void> {
clientId: "669359568391766018"
});

presence.info(infoMessage);
presence.on("UpdateData", updateData);
}
}
Expand Down

0 comments on commit 1f4477a

Please sign in to comment.