Skip to content

Commit

Permalink
feat(MetaBrainz): add presence (PreMiD#6771)
Browse files Browse the repository at this point in the history
* feat(MetaBrainz): prepare metadata

* feat(MetaBrainz): add root and blog subdomain presences

* feat(MetaBrainz): add presence for community forums

* refactor(MetaBrainz): run eslint

* fix(MetaBrainz): use correct image for profile

* chore(MetaBrainz): sort metadata

* style(MetaBrainz): run prettier
  • Loading branch information
theusaf authored Sep 10, 2022
1 parent 57e11a5 commit 730296d
Show file tree
Hide file tree
Showing 3 changed files with 138 additions and 0 deletions.
27 changes: 27 additions & 0 deletions websites/M/MetaBrainz/dist/metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"$schema": "https://schemas.premid.app/metadata/1.7",
"author": {
"name": "theusaf",
"id": "193714715631812608"
},
"service": "MetaBrainz",
"description": {
"en": "The MetaBrainz Foundation is a non-profit that believes in free, open access to data. It has been set up to build community maintained databases and make them available in the public domain or under Creative Commons licenses."
},
"url": [
"metabrainz.org",
"blog.metabrainz.org",
"community.metabrainz.org"
],
"version": "1.0.0",
"logo": "https://i.imgur.com/kQsG9xv.png",
"thumbnail": "https://i.imgur.com/si5Pxzc.png",
"color": "#5aa854",
"category": "other",
"tags": [
"public",
"community",
"data",
"database"
]
}
105 changes: 105 additions & 0 deletions websites/M/MetaBrainz/presence.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
const presence = new Presence({
clientId: "1017489991930232902",
}),
browsingTimestamp = Math.floor(Date.now() / 1000);

presence.on("UpdateData", () => {
const presenceData: PresenceData = {
largeImageKey: "https://i.imgur.com/kQsG9xv.png",
startTimestamp: browsingTimestamp,
},
{ hostname, pathname, search } = window.location;
switch (hostname) {
case "metabrainz.org": {
presenceData.details = "Browsing...";
switch (pathname) {
case "/": {
presenceData.state = "Home page";
break;
}
case "/profile": {
presenceData.details = "Viewing MetaBrainz profile";
presenceData.state = new URLSearchParams(search).get(
"musicbrainz_id"
);
break;
}
default: {
presenceData.state = document.title.split(
" - MetaBrainz Foundation"
)[0];
}
}
break;
}
case "blog.metabrainz.org": {
presenceData.details = "Reading blog";
if (pathname === "/") presenceData.state = "Home page";
else if (pathname.startsWith("/author/")) {
presenceData.state = `Viewing articles by ${
pathname.match(/^\/author\/(.*?)\//)[1]
}`;
} else if (pathname.startsWith("/tag/")) {
presenceData.state = `Viewing articles tagged '${
document.querySelector<HTMLSpanElement>(".page-title > span")
?.textContent || pathname.match(/^\/tag\/(.*?)\//)[1]
}'`;
} else if (pathname.startsWith("/category/")) {
presenceData.state = `Viewing category '${
document.querySelector<HTMLSpanElement>(".page-title > span")
?.textContent || pathname.match(/^\/category\/(.*?)\//)[1]
}'`;
} else if (/^\/\d+\/(\d+\/)?$/.test(pathname)) {
presenceData.state = `Viewing articles created in ${
document.querySelector<HTMLSpanElement>(".page-title > span")
.textContent
}`;
} else {
presenceData.state =
document.querySelector<HTMLHeadingElement>(
".entry-title"
).textContent;
}
break;
}
case "community.metabrainz.org": {
presenceData.details = "Browsing forum";
presenceData.largeImageKey = "https://i.imgur.com/AtrEqxF.png";
if (pathname === "/") presenceData.state = "Home page";
else if (pathname.startsWith("/c/") || /^\/tags\/c\/.+/.test(pathname)) {
presenceData.state = `Viewing category '${
document.querySelector<HTMLSpanElement>(".category-name").textContent
}'`;
} else if (pathname.startsWith("/t/")) {
presenceData.state =
document.querySelector<HTMLAnchorElement>(".fancy-title").textContent;
} else if (/^\/g\/.+/.test(pathname)) {
presenceData.state = `Viewing group '${
document.querySelector<HTMLSpanElement>(".group-info-name")
.textContent
}'`;
} else if (/^\/categories\/?/.test(pathname))
presenceData.state = "Viewing categories";
else if (pathname === "/login-preferences")
presenceData.state = "Logging in";
else if (pathname.startsWith("/u/")) {
presenceData.state = `Viewing profile of ${
document.querySelector<HTMLHeadingElement>(".username").textContent
}`;
presenceData.smallImageKey = document.querySelector<HTMLImageElement>(
".user-profile-avatar > img"
).src;
presenceData.smallImageText =
document.querySelector<HTMLHeadingElement>(".full-name").textContent;
} else {
presenceData.state = document.title.split(
" - MetaBrainz Community Discourse"
)[0];
}
break;
}
}

if (presenceData.details) presence.setActivity(presenceData);
else presence.setActivity();
});
6 changes: 6 additions & 0 deletions websites/M/MetaBrainz/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 730296d

Please sign in to comment.