Skip to content

Commit

Permalink
feat(CritiqueBrainz): add presence (PreMiD#6776)
Browse files Browse the repository at this point in the history
* feat: add presence

* fix: fix various issues

* chore: sort metadata

* refactor: suggested changes
  • Loading branch information
theusaf authored Sep 16, 2022
1 parent e26d87a commit 3194310
Show file tree
Hide file tree
Showing 3 changed files with 181 additions and 0 deletions.
22 changes: 22 additions & 0 deletions websites/C/CritiqueBrainz/dist/metadata.json
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"
]
}
153 changes: 153 additions & 0 deletions websites/C/CritiqueBrainz/presence.ts
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();
});
6 changes: 6 additions & 0 deletions websites/C/CritiqueBrainz/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"extends": "../../../tsconfig.json",
"compilerOptions": {
"outDir": "./dist/"
}
}

0 comments on commit 3194310

Please sign in to comment.