Skip to content

Commit

Permalink
feat: remove multiple tracks
Browse files Browse the repository at this point in the history
  • Loading branch information
Jimjardland committed Oct 4, 2019
1 parent 38f7c85 commit 19ebbfb
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 25 deletions.
35 changes: 25 additions & 10 deletions __tests__/Queue_test.re
Original file line number Diff line number Diff line change
@@ -1,14 +1,29 @@
open Jest;
open Expect;

describe("Queue", () =>
Expect.(
describe("#SonosDecode", () =>
describe("#trackPosition", () =>
test("should handle track position", () =>
expect(Queue.trackPosition(~first="1", ~queueAt=1., ()))
|> toEqual("1")
)
)
describe("#SonosDecode", () =>
describe("#trackPosition", () =>
test("should handle track position", () =>
expect(Queue.trackPosition(~first="1", ~queueAt=1., ()))
|> toEqual("1")
)
)
);
)
describe("#handleRemoveArgs", () => {
test("no args", () => {
expect(Queue.handleRemoveArgs(""))
|> toEqual((1, 1))
})
test("returns 1 param correctly", () => {
expect(Queue.handleRemoveArgs("9"))
|> toEqual((9, 1))
})
test("returns 2 params correctly", () => {
expect(Queue.handleRemoveArgs("3 3"))
|> toEqual((3, 3))
})
test("returns 2 params correctly even though you add more", () => {
expect(Queue.handleRemoveArgs("3 3 jimmyärsnygg"))
|> toEqual((3, 3))
})
})
53 changes: 39 additions & 14 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions src/Commands.re
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type t =
| Time
| Toplist
| UnhandledCommand
| RemoveMultipleTracks
| UnknownCommand(string)
| Unmute
| Volume;
Expand All @@ -44,6 +45,7 @@ let make = text => {
| "classics" => EasterEgg(IteamClassics)
| "blame" => Blame
| "clear" => Clear
| "remove" => RemoveMultipleTracks
| "cq"
| "currentqueue"
| "gq"
Expand Down Expand Up @@ -120,6 +122,7 @@ let commandToString =
| PlayTrack => "playback-playtrack"
| Previous => "playback-previous"
| Queue => "queue"
| RemoveMultipleTracks => "remove"
| SpotifyCopy(_) => "spotify-copy"
| Search => "search"
| Time => "time"
Expand Down
1 change: 1 addition & 0 deletions src/Event.re
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ let make = (~command, ~args, ~user, ()) => {
| FullQueue => Queue.full()
| Clear => Queue.clear()
| Queue => Queue.AsLastTrack.make(args, ())
| RemoveMultipleTracks => Queue.removeMultipleTracks(args)

/* Player control */
| Play => PlayerControl.play()
Expand Down
2 changes: 2 additions & 0 deletions src/SonosSpecialCase.re
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

[@bs.send] external removeMultipleTracks: (Sonos.Methods.sonosDevice, int, int) => Js.Promise.t('a) = "removeTracksFromQueue";
31 changes: 30 additions & 1 deletion src/commands/Queue.re
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,35 @@ let clear = () =>
)
|> catch(_ => `Failed("Failed to clear queue") |> resolve);

let handleRemoveArgs = args => {
switch (args) {
| "" => (1, 1)
| args =>
let input = args |> Js.String.split(" ");
let numberOfTracks =
input
->Belt.Array.get(0)
->Belt.Option.getWithDefault("1")
->int_of_string;
let index =
input
->Belt.Array.get(1)
->Belt.Option.getWithDefault("1")
->int_of_string;
(numberOfTracks, index);
};
}

let removeMultipleTracks = args => {
let (numberOfTracks, index) = handleRemoveArgs(args)

SonosSpecialCase.removeMultipleTracks(device, index, numberOfTracks)
|> then_(_ =>
`Ok(Slack.Block.make([`Section("I removed them for you!")]))
|> resolve
);
};

let current = () =>
queueWithFallback()
|> then_(({items}) =>
Expand Down Expand Up @@ -242,4 +271,4 @@ let multiple = tracks => {

resolve(`Ok(Slack.Block.make([`Section(message)])));
});
};
};

0 comments on commit 19ebbfb

Please sign in to comment.