Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(CritiqueBrainz): add presence #6776

Merged
merged 6 commits into from
Sep 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);

EncryptedDev marked this conversation as resolved.
Show resolved Hide resolved
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/"
}
}