-
Notifications
You must be signed in to change notification settings - Fork 0
/
playlist_helper.js
96 lines (79 loc) · 3.54 KB
/
playlist_helper.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
// Ideally we want to be able to find multiple playlist descriptors in one scan.
// This means we should already know what we're looking for, and *then* fetch paginated results.
// as we go through all the playlists, we keep references to the ones we are looking for.
//
// It is also important that we don't clone the same playlist multiple times, so we
// can take an additional parameter as "target"
//
// What if we wanna scale and have multiple targets? Well, the might mean we also have multiple sources.
// A simple way would be to create a new scan for each target, but that's also inefficient.
// We wanna reduce the number of API calls by doing as much work needed in the same request.
// This means our helper should be able to take a [target-source] parameter and check all of them in one go.
// We already know how to identify our target, which is based on the name. However, that might change based
// on other stuff like creator. Therefore, we should take both target and source as a "closure"/function
// that can be called and check for the match at the source of truth.
//
// Suggested params: [target(), source()], user, client
const PaginatedPlaylist = require('./paginated_playlist').PaginatedPlaylist;
const TimeHelper = require('./time_helper');
async function getMatchedPlaylists(playlistDescriptors, user, client) {
// Sanitize playlistDescriptors:
if (playlistDescriptors == null || playlistDescriptors.size == 0) {
return {};
}
// No need to sanitize user or client here
let pager = new PaginatedPlaylist(user, client);
await pager.init()
// We might run out of things to check for, or pages to check them in.
// If the target matches, we remove the pair from the descriptors.
// If the source matches, we save that as a return value.
// It is possible that the source might match before a duplicate target,
// so the return value set might already have it.
var matchedPlaylists = new Set();
while (pager.getPage() != null) {
playlistDescriptors.forEach(function (descriptor) {
pager.getPage().body.items.some( function (playlist) {
if (playlistDescriptors.size == 0) {
return true;
}
if (descriptor.source(playlist)) {
descriptor.setSourceID(playlist)
matchedPlaylists.add(descriptor);
}
if (descriptor.target(playlist, user)) {
descriptor.setSourceID(null);
matchedPlaylists.delete(descriptor);
playlistDescriptors.delete(descriptor);
}
}
)});
if (playlistDescriptors.size == 0) {
break;
}
await pager.nextPage();
}
return matchedPlaylists;
}
function isReleaseRadarSource(playlist) {
let isReleaseRadar = (playlist.name == "Release Radar" && playlist.owner.id == "spotify")
if (isReleaseRadar) {
console.log('Found Release Radar!');
}
return isReleaseRadar;
}
function isReleaseRadarTarget(playlist, user) {
let isTarget = (playlist.name == getReleaseRadarTargetName() && playlist.owner.id == user.body.id)
if (isTarget) {
console.log(`Found matching target for ${getReleaseRadarTargetName()}, doing nothing.`);
}
return isTarget;
}
function getReleaseRadarTargetName() {
return `Release Radar : ${TimeHelper.getFridayDate()}`
}
module.exports = {
getMatchedPlaylists,
isReleaseRadarSource,
isReleaseRadarTarget,
getReleaseRadarTargetName
};