Skip to content

Commit

Permalink
fix(exists-in-queue): handle same track with different uri
Browse files Browse the repository at this point in the history
  • Loading branch information
believer committed Sep 22, 2019
1 parent 92f3579 commit 63c1aa3
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 46 deletions.
2 changes: 1 addition & 1 deletion src/Event.re
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ module Message = {
| Clear => Queue.clear()
| CurrentQueue => Queue.current()
| FullQueue => Queue.full()
| Queue => Queue.last(args)
| Queue => Queue.AsLastTrack.make(args, ())
| SpotifyCopy(tracks) => Queue.multiple(tracks)

/* Player control */
Expand Down
6 changes: 3 additions & 3 deletions src/Routes.re
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ module IndexRoute = {
);
};

module VerificationRoute = {
module VerificationCallback = {
open Decode;

let make = body => {
Expand Down Expand Up @@ -75,7 +75,7 @@ module EventRoute = {
res
|> (
switch (Decode.EventResponse.make(body)) {
| UrlVerification => VerificationRoute.make(body)
| UrlVerification => VerificationCallback.make(body)
| EventCallback(event) => EventCallback.make(event)
| UnknownEvent => badRequest
}
Expand All @@ -95,7 +95,7 @@ module ActionRoute = {
body |> Decode.Action.make;
let track = actions[0].value;

Queue.last(track)
Queue.AsLastTrack.make(track, ())
|> then_(message => {
Elastic.log(
~command=Human(Commands.Queue),
Expand Down
6 changes: 3 additions & 3 deletions src/commands/EasterEgg.re
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,15 @@ module Playlist = {
let run =
Queue.(
fun
| IteamClassics => Playlist.iteamClassics->last
| IteamClassics => Playlist.iteamClassics->AsLastTrack.make()
| FreeBird => Track.freeBird->next
| Friday =>
Utils.isFriday()
? Track.friday->next
: Js.Promise.resolve(`Ok("Sorry, it's not Friday"))
| Rednex => Track.rednex->next
| Shoreline => Track.shoreline->next
| Slowdance => Playlist.slowdance->last
| Slowdance => Playlist.slowdance->AsLastTrack.make()
| Tequila => Track.tequila->next
| WWW => Track.worldwideweb->next
);
Expand All @@ -62,4 +62,4 @@ let isEasterEgg = () => {
->resolve
)
);
};
};
99 changes: 60 additions & 39 deletions src/commands/Queue.re
Original file line number Diff line number Diff line change
Expand Up @@ -84,48 +84,69 @@ module Exists = {
| 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);
});
trackUri
->SpotifyUtils.trackId
->Belt.Option.getWithDefault("")
->Spotify.getSpotifyTrack
|> then_((spotify: Spotify.WejayTrack.t) =>
queueWithFallback()
|> then_(({items}) => {
/* Only checking URI is not enough, so we do a
* naive artist/track name match as well. Sonos
* sometimes uses a different track URI
* than what was pasted in Wejay because
* of different regions or markets */
let existsInQueue =
items->Belt.Array.some(item =>
Utils.sonosUriToSpotifyUri(item.uri) === trackUri
|| item.artist === spotify.artist
&& item.title === spotify.name
);

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

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

Exists.inQueue(parsedTrack)
|> then_((existsInQueue: Exists.t) =>
switch (existsInQueue) {
| InQueue => resolve(`Ok(Message.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:";
module AsLastTrack = {
let queue = track => {
device->queueAsLast(track)
|> 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;
})
);
};

`Ok(message) |> resolve;
})
)
|> catch(_ => `Failed("Failed to queue track") |> resolve)
}
);
let make = (track, ~skipExists=false, ()) => {
let parsedTrack = Utils.parsedTrack(track);

skipExists
? queue(parsedTrack)
: Exists.inQueue(parsedTrack)
|> then_((existsInQueue: Exists.t) =>
switch (existsInQueue) {
| InQueue => resolve(`Ok(Message.trackExistsInQueue))
| NotInQueue =>
queue(parsedTrack)
|> catch(_ => `Failed("Failed to queue track") |> resolve)
}
);
};
};

let next = track => {
Expand Down Expand Up @@ -171,7 +192,7 @@ let multiple = tracks => {
switch (curr) {
| Exists.InQueue => (inQueue + 1, notInQueue)
| NotInQueue =>
last(parsedTracks[i]) |> ignore;
AsLastTrack.make(parsedTracks[i], ~skipExists=true, ()) |> ignore;

(inQueue, notInQueue + 1);
}
Expand Down

0 comments on commit 63c1aa3

Please sign in to comment.