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

Fix/196 block on purchasable #199

Merged
merged 5 commits into from
Nov 20, 2024
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
66 changes: 39 additions & 27 deletions src/player.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import Logger from "./logger";
import {
mousedownCallback,
extractBandFollowInfo,
extractFanTralbumData,
getTralbumDetails,
addAlbumToCart
} from "./utilities.js";
Expand All @@ -24,6 +25,7 @@ export default class Player {
this.createInputButtonPair = createInputButtonPair;
this.createShoppingCartItem = createShoppingCartItem;
this.extractBandFollowInfo = extractBandFollowInfo;
this.extractFanTralbumData = extractFanTralbumData;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what is tralbum? It looks like this is a thing and not a typo, so curious what it means

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's Bandcamp's internal term for a track or album (trrackalbum)

this.getTralbumDetails = getTralbumDetails.bind(this);
this.createInputButtonPair = createInputButtonPair;
this.createShoppingCartItem = createShoppingCartItem;
Expand All @@ -42,6 +44,12 @@ export default class Player {

this.updatePlayerControlInterface();

const {
is_purchased,
part_of_purchased_album
} = this.extractFanTralbumData();
if (is_purchased | part_of_purchased_album) return;

const bandFollowInfo = this.extractBandFollowInfo();
const tralbumId = bandFollowInfo.tralbum_id;
const tralbumType = bandFollowInfo.tralbum_type;
Expand All @@ -53,33 +61,6 @@ export default class Player {
return response.json();
})
.then(tralbumDetails => {
const {
price,
currency,
album_id: tralbumId,
title: itemTitle,
is_purchasable,
type
} = tralbumDetails;
const oneClick = this.createOneClickBuyButton(
price,
currency,
tralbumId,
itemTitle,
is_purchasable,
type
);

const table = document
.createRange()
.createContextualFragment(emptyPlaylistTable)
.querySelector("table");

document.querySelector("ul.tralbumCommands").prepend(table);

const downloadCol = table.querySelector(".download-col");
downloadCol.append(oneClick);

document.querySelectorAll("tr.track_row_view").forEach((row, i) => {
if (tralbumDetails.tracks[i] === undefined) return;

Expand All @@ -103,10 +84,41 @@ export default class Player {
is_purchasable,
type
);

if (!is_purchasable) return;

const downloadCol = row.querySelector(".download-col");
downloadCol.innerHTML = "";
downloadCol.append(oneClick);
});

const {
price,
currency,
album_id: tralbumId,
title: itemTitle,
is_purchasable,
type
} = tralbumDetails;
const oneClick = this.createOneClickBuyButton(
price,
currency,
tralbumId,
itemTitle,
is_purchasable,
type
);
if (!is_purchasable) return;

const table = document
.createRange()
.createContextualFragment(emptyPlaylistTable)
.querySelector("table");

document.querySelector("ul.tralbumCommands").prepend(table);

const downloadCol = table.querySelector(".download-col");
downloadCol.append(oneClick);
})
.catch(error => {
this.log.error(error);
Expand Down
15 changes: 15 additions & 0 deletions src/utilities.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,21 @@ export function extractBandFollowInfo() {
}
}

export function extractFanTralbumData() {
const data = document.querySelector("[data-blob]").getAttribute("data-blob");

if (!data) {
return null;
}

try {
const { fan_tralbum_data } = JSON.parse(data);
return fan_tralbum_data;
} catch (error) {
return null;
}
}

export function getUrl() {
return window.location.href.split("/")[2];
}
Expand Down
76 changes: 57 additions & 19 deletions test/player.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ describe("Player", () => {
Object.assign(document.createElement("div"), { id: "unique-id-2" })
);
player.getTralbumDetails = sinon.stub().resolves(mockResponse);
player.extractFanTralbumData = sinon.stub().returns({
is_purchased: false,
part_of_purchased_album: false
});

createDomNodes(`
<table class="track_list track_table" id="track_table">
Expand Down Expand Up @@ -162,16 +166,6 @@ describe("Player", () => {
expect(player.createOneClickBuyButton).to.have.been.calledThrice;
expect(
player.createOneClickBuyButton.getCall(0)
).to.have.been.calledWithExactly(
mockTralbumDetails.price,
mockTralbumDetails.currency,
mockTralbumDetails.album_id,
mockTralbumDetails.title,
mockTralbumDetails.is_purchasable,
mockTralbumDetails.type
);
expect(
player.createOneClickBuyButton.getCall(1)
).to.have.been.calledWithExactly(
mockTralbumDetails.tracks[0].price,
mockTralbumDetails.tracks[0].currency,
Expand All @@ -181,7 +175,7 @@ describe("Player", () => {
"t"
);
expect(
player.createOneClickBuyButton.getCall(2)
player.createOneClickBuyButton.getCall(1)
).to.have.been.calledWithExactly(
mockTralbumDetails.tracks[1].price,
mockTralbumDetails.tracks[1].currency,
Expand All @@ -190,22 +184,33 @@ describe("Player", () => {
mockTralbumDetails.tracks[1].is_purchasable,
"t"
);
expect(
player.createOneClickBuyButton.getCall(2)
).to.have.been.calledWithExactly(
mockTralbumDetails.price,
mockTralbumDetails.currency,
mockTralbumDetails.album_id,
mockTralbumDetails.title,
mockTralbumDetails.is_purchasable,
mockTralbumDetails.type
);
});

it("should modify DOM correctly", async () => {
await player.init();

const album = document.querySelector("ul.tralbumCommands");
expect(album.querySelectorAll("#unique-id-0")).to.have.length(1);

const rows = document.querySelectorAll("tr.track_row_view");
expect(rows).to.have.length(2);
expect(rows[0].querySelector(".info-col")).to.be.null;
expect(rows[0].querySelectorAll(".download-col")).to.have.length(1);
expect(rows[0].querySelectorAll(`#unique-id-0`)).to.have.length(0); // is purchasable == false

rows.forEach((row, i) => {
expect(row.querySelector(".info-col")).to.be.null;
expect(row.querySelectorAll(".download-col")).to.have.length(1);
expect(row.querySelectorAll(`#unique-id-${i + 1}`)).to.have.length(1);
});
expect(rows[1].querySelector(".info-col")).to.be.null;
expect(rows[1].querySelectorAll(".download-col")).to.have.length(1);
expect(rows[1].querySelectorAll(`#unique-id-1`)).to.have.length(1);

const album = document.querySelector("ul.tralbumCommands");
expect(album.querySelectorAll("#unique-id-2")).to.have.length(1);
});

it("should not fail if more DOM elements than tralbumDetail tracks", async () => {
Expand Down Expand Up @@ -247,6 +252,39 @@ describe("Player", () => {

expect(player.createOneClickBuyButton).to.be.calledOnce;
});

it("should not setup 1-click if 'is_purchased' or 'part_of_purchased_album'", async () => {
{
player.extractFanTralbumData = sinon.stub().returns({
is_purchased: true,
part_of_purchased_album: false
});

await player.init();

expect(player.getTralbumDetails).to.be.not.called;
}
{
player.extractFanTralbumData = sinon.stub().returns({
is_purchased: false,
part_of_purchased_album: true
});

await player.init();

expect(player.getTralbumDetails).to.be.not.called;
}
{
player.extractFanTralbumData = sinon.stub().returns({
is_purchased: true,
part_of_purchased_album: true
});

await player.init();

expect(player.getTralbumDetails).to.be.not.called;
}
});
});

it("should handle errors when getTralbumDetails fails", async () => {
Expand Down
Loading