Skip to content

Commit

Permalink
feat: check if track exists before queuing up a new track (#54)
Browse files Browse the repository at this point in the history
  • Loading branch information
faroutchris authored and believer committed Sep 20, 2019
1 parent cc6176a commit ccc2719
Show file tree
Hide file tree
Showing 8 changed files with 1,186 additions and 1,213 deletions.
4 changes: 2 additions & 2 deletions __tests__/Utils_test.re
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ open Jest;
open Utils;
open Expect;

[@bs.module "jest-date-mock"] external clear: unit => unit = "";
[@bs.module "jest-date-mock"] external advanceTo: float => unit = "";
[@bs.module "jest-date-mock"] external clear: unit => unit = "clear";
[@bs.module "jest-date-mock"] external advanceTo: float => unit = "advanceTo";

afterEach(clear);

Expand Down
2,264 changes: 1,101 additions & 1,163 deletions package-lock.json

Large diffs are not rendered by default.

23 changes: 12 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"dev": "nodemon lib/js/src/Server.bs.js",
"server": "node lib/js/src/Server.bs.js",
"build": "bsb -make-world",
"start": "bsb -make-world -w",
"start": "cross-env BS_WATCH_CLEAR=true bsb -make-world -w",
"clean": "bsb -clean-world",
"prepare": "npm run build",
"test": "is-ci \"test:ci\" \"test:watch\"",
Expand All @@ -31,25 +31,26 @@
},
"license": "MIT",
"devDependencies": {
"@ahrefs/bs-dotenv": "1.0.3",
"@glennsl/bs-jest": "0.4.8",
"@glennsl/bs-json": "3.0.0",
"all-contributors-cli": "6.4.0",
"@ahrefs/bs-dotenv": "2.0.0",
"@glennsl/bs-jest": "0.4.9",
"@glennsl/bs-json": "5.0.1",
"all-contributors-cli": "6.9.1",
"bs-axios": "0.0.42",
"bs-express": "0.12.0",
"bs-platform": "5.0.3",
"bs-express": "0.12.1",
"bs-platform": "5.1.0",
"cross-env": "6.0.0",
"is-ci-cli": "1.1.1",
"jest-date-mock": "1.0.7",
"nodemon": "1.19.0"
"nodemon": "1.19.2"
},
"dependencies": {
"@wejay/spotify": "0.1.5",
"body-parser": "1.19.0",
"bs-sonos": "0.1.1",
"date-fns": "1.30.1",
"dotenv": "8.0.0",
"express": "4.16.4",
"sonos": "1.11.0"
"dotenv": "8.1.0",
"express": "4.17.1",
"sonos": "1.12.3"
},
"jest": {
"setupFiles": [
Expand Down
2 changes: 1 addition & 1 deletion src/Config.re
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
type t;

[@bs.val] external fromStringWithEncoding: string => t = "Buffer.from";
[@bs.send] external toString: (t, string) => string = "";
[@bs.send] external toString: (t, string) => string = "toString";

Dotenv.config();

Expand Down
2 changes: 1 addition & 1 deletion src/commands/EasterEgg.re
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,4 @@ let isEasterEgg = () => {
->resolve
)
);
};
};
100 changes: 66 additions & 34 deletions src/commands/Queue.re
Original file line number Diff line number Diff line change
Expand Up @@ -78,52 +78,84 @@ let full = () =>
})
|> catch(_ => `Failed("Failed to get full queue") |> resolve);

module Exists = {
type t =
| InQueue
| NotInQueue;

let inQueue = trackUri => {
queueWithFallback()
|> then_(({items}) => {
let existsInQueue =
items
->Belt.Array.map(item => Utils.sonosUriToSpotifyUri(item.uri))
->Belt.Array.some(uri => uri == trackUri);

resolve(existsInQueue ? InQueue : NotInQueue);
});
};
};

let last = track => {
let parsedTrack = Utils.parsedTrack(track);

device->queueAsLast(parsedTrack)
|> then_(queuedTrack =>
Services.getCurrentTrack()
|> then_(({queuePosition}: currentTrackResponse) => {
let {firstTrackNumberEnqueued}: queueResponse =
queuedTrack->queueResponse;

let message =
"Sweet! Your track is number *"
++ trackPosition(
~first=firstTrackNumberEnqueued,
~queueAt=queuePosition,
(),
)
++ "* in the queue :musical_note:";

`Ok(message) |> resolve;
})
)
|> catch(_ => `Failed("Failed to queue track") |> resolve);
Exists.inQueue(parsedTrack)
|> then_((existsInQueue: Exists.t) =>
switch (existsInQueue) {
| InQueue => resolve(`Ok(Messages.trackExistsInQueue))
| NotInQueue =>
device->queueAsLast(parsedTrack)
|> then_(queuedTrack =>
Services.getCurrentTrack()
|> then_(({queuePosition}: currentTrackResponse) => {
let {firstTrackNumberEnqueued}: queueResponse =
queuedTrack->queueResponse;

let message =
"Sweet! Your track is number *"
++ trackPosition(
~first=firstTrackNumberEnqueued,
~queueAt=queuePosition,
(),
)
++ "* in the queue :musical_note:";

`Ok(message) |> resolve;
})
)
|> catch(_ => `Failed("Failed to queue track") |> resolve)
}
);
};

let next = track => {
let parsedTrack = Utils.parsedTrack(track);

Services.getCurrentTrack()
|> then_(({position, queuePosition}) =>
device->queue(parsedTrack, int_of_float(queuePosition) + 1)
|> then_(() => {
let message =
switch (position, queuePosition) {
| (0., 0.) => "Your track will play right now"
| _ => "Your track will play right after the current"
};

`Ok(message) |> resolve;
})
|> catch(_ => `Failed("Failed to queue track") |> resolve)
Exists.inQueue(parsedTrack)
|> then_((existsInQueue: Exists.t) =>
switch (existsInQueue) {
| InQueue => resolve(`Ok(Messages.trackExistsInQueue))
| NotInQueue =>
Services.getCurrentTrack()
|> then_(({position, queuePosition}) =>
device->queue(parsedTrack, int_of_float(queuePosition) + 1)
|> then_(() => {
let message =
switch (position, queuePosition) {
| (0., 0.) => "Your track will play right now"
| _ => "Your track will play right after the current"
};

`Ok(message) |> resolve;
})
|> catch(_ => `Failed("Failed to queue track") |> resolve)
)
}
);
};

let multiple = tracks => {
tracks->Belt.Array.forEach(track => last(track) |> ignore);

Js.Promise.resolve(`Ok("Your tracks have been queued!"));
resolve(`Ok("Your tracks have been queued!"));
};
2 changes: 2 additions & 0 deletions src/utils/Messages.re
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,5 @@ let unknownCommand = command =>
let thisIsWejay = "This is Wejay!\nhttps://media.giphy.com/media/Ny4Ian52lZDz2/giphy.gif";

let cantSkipEasterEgg = "Can't skip this track :hatching_chick:";

let trackExistsInQueue = "Hey, this track already exists in the queue!";
2 changes: 1 addition & 1 deletion src/utils/Regex.re
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ module Patterns = {
};

let replaceByRe = (str, regex, replaceBy) =>
Js.String.replaceByRe(regex, replaceBy, str);
Js.String.replaceByRe(regex, replaceBy, str);

0 comments on commit ccc2719

Please sign in to comment.