forked from PreMiD/Presences
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(CritiqueBrainz): add presence (PreMiD#6776)
* feat: add presence * fix: fix various issues * chore: sort metadata * refactor: suggested changes
- Loading branch information
Showing
3 changed files
with
181 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
{ | ||
"$schema": "https://schemas.premid.app/metadata/1.7", | ||
"author": { | ||
"name": "theusaf", | ||
"id": "193714715631812608" | ||
}, | ||
"service": "CritiqueBrainz", | ||
"description": { | ||
"en": "CritiqueBrainz is a repository for Creative Commons licensed music reviews. Here you can read what other people have written about an album or event and write your own review!" | ||
}, | ||
"url": "critiquebrainz.org", | ||
"version": "1.0.0", | ||
"logo": "https://i.imgur.com/LG31f5s.png", | ||
"thumbnail": "https://i.imgur.com/7ACWqDu.png", | ||
"color": "#36b5b2", | ||
"category": "music", | ||
"tags": [ | ||
"review", | ||
"encyclopedia", | ||
"community" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
const presence = new Presence({ | ||
clientId: "1017821399705985097", | ||
}), | ||
browsingTimestamp = Math.floor(Date.now() / 1000); | ||
|
||
presence.on("UpdateData", () => { | ||
const presenceData: PresenceData = { | ||
largeImageKey: "https://i.imgur.com/LG31f5s.png", | ||
startTimestamp: browsingTimestamp, | ||
}, | ||
{ pathname, href } = window.location, | ||
pathSplit = pathname.split("/").slice(1); | ||
switch (pathSplit[0]) { | ||
case "": { | ||
presenceData.details = "Browsing home page"; | ||
break; | ||
} | ||
case "profile": { | ||
if (pathSplit[1] === "applications") { | ||
switch (pathSplit[2]) { | ||
case "": { | ||
presenceData.details = "Managing applications"; | ||
break; | ||
} | ||
case "create": { | ||
presenceData.details = "Creating a new application"; | ||
break; | ||
} | ||
default: { | ||
if (/\/edit\/?$/.test(pathname)) { | ||
presenceData.details = "Editing an application"; | ||
presenceData.state = document.querySelector<HTMLInputElement>( | ||
"form fieldset input" | ||
).value; | ||
} | ||
} | ||
} | ||
} else if (pathSplit[1] === "edit") | ||
presenceData.details = "Editing profile"; | ||
|
||
break; | ||
} | ||
case "user": { | ||
if (pathSplit[1] === "edit") { | ||
presenceData.details = "Editing profile"; | ||
presenceData.state = document | ||
.querySelector(".nav a") | ||
.textContent.replace(/^\s+|\s+$/g, ""); | ||
} else { | ||
presenceData.details = "Viewing a profile"; | ||
const username = document.querySelector("h2").textContent; | ||
switch (pathSplit[2] || "") { | ||
case "": { | ||
presenceData.state = username; | ||
break; | ||
} | ||
case "info": { | ||
presenceData.state = `Statistics for '${username}'`; | ||
break; | ||
} | ||
} | ||
} | ||
break; | ||
} | ||
case "statistics": { | ||
presenceData.details = "Viewing the top 10 contributors"; | ||
break; | ||
} | ||
case "review": { | ||
presenceData.details = "Browsing reviews"; | ||
switch (pathSplit[1]) { | ||
case "": { | ||
presenceData.state = "Home page"; | ||
break; | ||
} | ||
case "write": { | ||
presenceData.details = "Writing a review"; | ||
presenceData.state = | ||
document.querySelector<HTMLAnchorElement>("dd a").textContent; | ||
break; | ||
} | ||
default: { | ||
if (/^\w{8}-\w{4}-\w{4}-\w{4}-\w{12}$/.test(pathSplit[1])) { | ||
presenceData.largeImageKey = | ||
document.querySelector<HTMLImageElement>(".cover-art")?.src ?? | ||
presenceData.largeImageKey; | ||
switch (pathSplit[2] || "") { | ||
case "": { | ||
presenceData.details = "Reading a review"; | ||
presenceData.state = `${ | ||
document.querySelector<HTMLAnchorElement>("h3 > a") | ||
.textContent | ||
}'s review of ${ | ||
document.querySelector<HTMLAnchorElement>("h2 > a") | ||
.textContent | ||
}`; | ||
if (!document.querySelector(".alert-warning")) { | ||
presenceData.buttons = [ | ||
{ | ||
label: "Read Review", | ||
url: href, | ||
}, | ||
]; | ||
} | ||
break; | ||
} | ||
case "edit": { | ||
presenceData.details = "Editing review"; | ||
presenceData.state = | ||
document.querySelector<HTMLAnchorElement>("dd a").textContent; | ||
break; | ||
} | ||
case "revisions": { | ||
if (!pathSplit[3]) | ||
presenceData.details = "Viewing review revision history"; | ||
else { | ||
presenceData.details = "Viewing review revision"; | ||
presenceData.state = | ||
document.querySelector<HTMLAnchorElement>( | ||
"h2 a" | ||
).textContent; | ||
} | ||
break; | ||
} | ||
case "delete": { | ||
presenceData.details = "Deleting review"; | ||
presenceData.state = document | ||
.querySelector<HTMLParagraphElement>(".lead") | ||
.textContent.match(/"(.*?)"/)[1]; | ||
break; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
break; | ||
} | ||
case "search": { | ||
presenceData.details = "Searching"; | ||
presenceData.state = | ||
document.querySelector<HTMLInputElement>("#input-query").value; | ||
break; | ||
} | ||
default: { | ||
presenceData.details = "Browsing..."; | ||
presenceData.state = document.title.match( | ||
/(.*?)( - CritiqueBrainz$|$)/ | ||
)[1]; | ||
} | ||
} | ||
if (presenceData.details) presence.setActivity(presenceData); | ||
else presence.setActivity(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"extends": "../../../tsconfig.json", | ||
"compilerOptions": { | ||
"outDir": "./dist/" | ||
} | ||
} |