Skip to content

Commit

Permalink
rework workflow stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
radityaharya authored Apr 22, 2024
1 parent 2a95d13 commit d4cf3b8
Show file tree
Hide file tree
Showing 16 changed files with 472 additions and 685 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/dev-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,4 @@ jobs:
ghcr.io/radityaharya/${{ matrix.image_name }}:latest
${{ secrets.DOCKERHUB_USERNAME }}/${{ matrix.image_name }}:${{ github.sha }}
${{ secrets.DOCKERHUB_USERNAME }}/${{ matrix.image_name }}:latest
platforms: linux/amd64
platforms: linux/amd64
6 changes: 3 additions & 3 deletions next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ const config = {
},
};

let sentryConfig
let millionConfig
let sentryConfig;
let millionConfig;

if (process.env.SENTRY) {
sentryConfig = withSentryConfig(
Expand Down Expand Up @@ -68,4 +68,4 @@ const prodConfig = {
},
};

export default process.env.NODE_ENV === "development" ? config : prodConfig;
export default process.env.NODE_ENV === "development" ? config : prodConfig;
6 changes: 4 additions & 2 deletions src/components/main-nav.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ export function MainNav() {
onClick={() => resetReactFlow()}
className={cn(
"transition-colors hover:text-foreground/80",
/\/workflow(?!s)/.test(pathname) ? "text-foreground" : "text-foreground/60",
/\/workflow(?!s)/.test(pathname)
? "text-foreground"
: "text-foreground/60",
)}
>
Builder
Expand Down Expand Up @@ -97,7 +99,7 @@ export function SiteNav({ className }: { className?: string }) {
: "",
pathname === "/" ? "absolute" : "",
pathname.startsWith("/auth")
? "bg-transparent absolute backdrop-blur-none"
? "absolute bg-transparent backdrop-blur-none"
: "",
)}
>
Expand Down
73 changes: 73 additions & 0 deletions src/lib/workflow/Base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,4 +130,77 @@ export class Base {
);
}
}

static isPlaylistTrackObject(
obj: any,
): obj is SpotifyApi.PlaylistTrackObject {
return obj?.hasOwnProperty("track");
}

static isPlaylistTrackObjectArray(
obj: any,
): obj is SpotifyApi.PlaylistTrackObject[] {
return (
Array.isArray(obj) &&
obj.every((item: any) => this.isPlaylistTrackObject(item))
);
}

/**
* Retrieves the tracks from the given sources.
*
* @param sources - An array of sources from which to retrieve the tracks.
* @returns An array of tracks.
* @throws {Error} If the source type is invalid.
*/
static getTracks(sources: any[]) {
const tracks = [] as SpotifyApi.TrackObjectFull[];

for (const source of sources) {
let trackSource;

if (source.hasOwnProperty("tracks")) {
trackSource = source.tracks;
} else if (source.hasOwnProperty("items")) {
trackSource = source.items;
} else if (
source.hasOwnProperty("track") &&
typeof source.track != "object"
) {
trackSource = source.track ? [source.track] : [];
} else if (Array.isArray(source)) {
trackSource = source;
}

if (!trackSource) continue;

if (trackSource.hasOwnProperty("tracks")) {
for (const track of trackSource) {
if (track.track && track.track.type === "track") {
tracks.push(track.track as SpotifyApi.TrackObjectFull);
} else if (track.track && track.type === "track") {
throw new Error("Invalid source type");
}
}
} else if (
Array.isArray(trackSource) &&
typeof trackSource[0] == "object"
) {
for (const track of trackSource) {
if (track.track && track.track.type === "track") {
tracks.push(track.track as SpotifyApi.TrackObjectFull);
} else if (track.track && track.type === "track") {
tracks.push(track as SpotifyApi.TrackObjectFull);
} else {
throw new Error("Invalid source type");
}
}
} else {
console.error("ERROR", trackSource);
throw new Error("Invalid source type");
}
}

return tracks;
}
}
9 changes: 3 additions & 6 deletions src/lib/workflow/Combiner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,11 @@ import { Base } from "./Base";
import _ from "radash";
import type { AccessToken } from "./Base";
import { Logger } from "../log";
import type SpotifyWebApi from "spotify-web-api-node";

const log = new Logger("Combiner");
export default class Combiner extends Base {
constructor(accessToken: AccessToken) {
super(accessToken);
}

static push(sources: any[], params: {}) {
static push(spClient: SpotifyWebApi, sources: any[], params: {}) {
log.debug("Push Sources:", sources);
log.info("Pushing...");
const result = [] as SpotifyApi.PlaylistTrackObject[];
Expand All @@ -34,7 +31,7 @@ export default class Combiner extends Base {
return obj?.hasOwnProperty("track");
}

static alternate(sources: any[], params: {}) {
static alternate(spClient: SpotifyWebApi, sources: any[], params: {}) {
log.debug("Alternate Sources:", sources);
log.info("Alternating...");
const result = [] as SpotifyApi.PlaylistTrackObject[];
Expand Down
155 changes: 17 additions & 138 deletions src/lib/workflow/Filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,57 +4,19 @@ import { Base } from "./Base";
import * as _ from "radash";
import type { AccessToken } from "./Base";
import { Logger } from "../log";
import type SpotifyWebApi from "spotify-web-api-node";

const log = new Logger("Workflow");
export default class Filter extends Base {
constructor(accessToken: AccessToken) {
super(accessToken);
}
static isPlaylistTrackObject(
obj: any,
): obj is SpotifyApi.PlaylistTrackObject {
return obj?.hasOwnProperty("track");
}

static isPlaylistTrackObjectArray(
obj: any,
): obj is SpotifyApi.PlaylistTrackObject[] {
return (
Array.isArray(obj) &&
obj.every((item: any) => this.isPlaylistTrackObject(item))
);
}

static filter(
spClient: SpotifyWebApi,
sources: any[],
params: { filterKey: string; filterValue: string },
) {
log.info("Filtering...");
log.debug("Filter Sources:", sources);

let tracks = [] as any;

if (
Array.isArray(sources) &&
Array.isArray(sources[0]) &&
Filter.isPlaylistTrackObjectArray(sources[0])
) {
// If the first source is an array of PlaylistTrackObjects, assume all sources are
tracks = sources.flat();
} else if (Array.isArray(sources) && sources[0]!.hasOwnProperty("tracks")) {
// If the first source has a 'tracks' property that is an array, assume all sources do
for (const source of sources) {
tracks.push(...source.tracks);
}
} else if (Filter.isPlaylistTrackObjectArray(sources)) {
tracks = sources;
} else {
throw new Error(
`Invalid source type: ${typeof sources[0]} in ${
sources[0]
} located in sources: ${JSON.stringify(sources)}`,
);
}
const tracks = this.getTracks(sources);

if (Array.isArray(tracks)) {
const res = tracks.filter((track: any) => {
Expand Down Expand Up @@ -133,105 +95,40 @@ export default class Filter extends Base {
}
}

static dedupeTracks(sources: any[], params: {}) {
static dedupeTracks(spClient: SpotifyWebApi, sources: any[], params: {}) {
log.info("Deduping tracks...");
log.debug("DedupeTracks Sources:", sources);

let tracks = [] as any;

if (
Array.isArray(sources) &&
Array.isArray(sources[0]) &&
Filter.isPlaylistTrackObjectArray(sources[0])
) {
// If the first source is an array of PlaylistTrackObjects, assume all sources are
tracks = sources.flat();
} else if (Array.isArray(sources) && sources[0]!.hasOwnProperty("tracks")) {
// If the first source has a 'tracks' property that is an array, assume all sources do
for (const source of sources) {
tracks.push(...source.tracks);
}
} else if (Filter.isPlaylistTrackObjectArray(sources)) {
tracks = sources;
} else {
throw new Error(
`Invalid source type: ${typeof sources[0]} in ${
sources[0]
} located in sources: ${JSON.stringify(sources)}`,
);
}
const tracks = this.getTracks(sources);

if (Array.isArray(tracks)) {
return [
...new Map(tracks.map((item) => [item.id, item])).values(),
] as SpotifyApi.PlaylistTrackObject[];
return [...new Map(tracks.map((item) => [item.id, item])).values()];
}
return [];
}

static dedupeArtists(sources: any[], params: {}) {
static dedupeArtists(spClient: SpotifyWebApi, sources: any[], params: {}) {
log.info("Deduping artists...");
log.debug("DedupeArtists Sources:", sources);
let tracks = [] as any;

if (
Array.isArray(sources) &&
Array.isArray(sources[0]) &&
Filter.isPlaylistTrackObjectArray(sources[0])
) {
// If the first source is an array of PlaylistTrackObjects, assume all sources are
tracks = sources.flat();
} else if (Array.isArray(sources) && sources[0]!.hasOwnProperty("tracks")) {
// If the first source has a 'tracks' property that is an array, assume all sources do
for (const source of sources) {
tracks.push(...source.tracks);
}
} else if (Filter.isPlaylistTrackObjectArray(sources)) {
tracks = sources;
} else {
throw new Error(
`Invalid source type: ${typeof sources[0]} in ${
sources[0]
} located in sources: ${JSON.stringify(sources)}`,
);
}
const tracks = this.getTracks(sources);

if (_.isArray(tracks)) {
return _.unique(tracks, (track): string | number | symbol =>
_.get(track, "track.artists[0].id"),
) as SpotifyApi.PlaylistTrackObject[];
);
}
return [];
}

static match(
spClient: SpotifyWebApi,
sources: any[],
params: { matchKey: string; matchValue: string },
) {
log.info("Matching...");
log.debug("Match Sources:", sources);

let tracks = [] as any;

if (
Array.isArray(sources) &&
Array.isArray(sources[0]) &&
Filter.isPlaylistTrackObjectArray(sources[0])
) {
tracks = sources.flat();
} else if (Array.isArray(sources) && sources[0]!.hasOwnProperty("tracks")) {
for (const source of sources) {
tracks.push(...source.tracks);
}
} else if (Filter.isPlaylistTrackObjectArray(sources)) {
tracks = sources;
} else {
throw new Error(
`Invalid source type: ${typeof sources[0]} in ${
sources[0]
} located in sources: ${JSON.stringify(sources)}`,
);
}
const tracks = this.getTracks(sources);

if (Array.isArray(tracks)) {
const res = tracks.filter((track: any) => {
Expand Down Expand Up @@ -310,33 +207,15 @@ export default class Filter extends Base {
}
}

static limit(sources: any[], params: { limit?: number }) {
static limit(
spClient: SpotifyWebApi,
sources: any[],
params: { limit?: number },
) {
log.info("Limiting...");
log.debug("Limit Sources:", sources);

let tracks = [] as any;

if (
Array.isArray(sources) &&
Array.isArray(sources[0]) &&
Filter.isPlaylistTrackObjectArray(sources[0])
) {
// If the first source is an array of PlaylistTrackObjects, assume all sources are
tracks = sources.flat();
} else if (Array.isArray(sources) && sources[0]!.hasOwnProperty("tracks")) {
// If the first source has a 'tracks' property that is an array, assume all sources do
for (const source of sources) {
tracks.push(...source.tracks);
}
} else if (Filter.isPlaylistTrackObjectArray(sources)) {
tracks = sources;
} else {
throw new Error(
`Invalid source type: ${typeof sources[0]} in ${
sources[0]
} located in sources: ${JSON.stringify(sources)}`,
);
}
const tracks = this.getTracks(sources);

if (Array.isArray(tracks)) {
return tracks.slice(0, params.limit);
Expand Down
Loading

0 comments on commit d4cf3b8

Please sign in to comment.