Skip to content

Commit

Permalink
Switch rymimport to use Lilac
Browse files Browse the repository at this point in the history
  • Loading branch information
abbyfour committed Nov 28, 2024
1 parent d1f4bf4 commit 1c83336
Show file tree
Hide file tree
Showing 14 changed files with 155 additions and 162 deletions.
1 change: 1 addition & 0 deletions src/commands/Lastfm/NowPlaying/NowPlayingBaseCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ export abstract class NowPlayingBaseCommand<
dbUser,
username
);

const renderedComponents = await this.nowPlayingService.renderComponents(
this.ctx,
await Promise.resolve(this.getConfig(senderUser!)),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,20 @@ import streamToString from "stream-to-string";
import {
NoRatingsFileAttatchedError,
TooManyAttachmentsError,
UnknownRatingsImportError,
WrongFileFormatAttachedError,
} from "../../../errors/commands/library";
import { CannotBeUsedAsASlashCommand } from "../../../errors/errors";
import { AlreadyImportingRatingsError } from "../../../errors/external/rateYourMusic";
import { StringArgument } from "../../../lib/context/arguments/argumentTypes/StringArgument";
import { ArgumentsMap } from "../../../lib/context/arguments/types";
import { Emoji } from "../../../lib/emoji/Emoji";
import { SuccessEmbed } from "../../../lib/ui/embeds/SuccessEmbed";
import { WarningEmbed } from "../../../lib/ui/embeds/WarningEmbed";
import { ConcurrentAction } from "../../../services/ConcurrencyService";
import { RateYourMusicIndexingChildCommand } from "./RateYourMusicChildCommand";
import {
ImportRatingsConnector,
ImportRatingsParams,
ImportRatingsResponse,
} from "./connectors";
import { RatingsImportProgressView } from "../../../lib/ui/views/RatingsImportProgressView";
import { RateYourMusicChildCommand } from "./RateYourMusicChildCommand";

const args = {
input: new StringArgument({ index: { start: 0 }, slashCommandOption: false }),
} satisfies ArgumentsMap;

export class ImportRatings extends RateYourMusicIndexingChildCommand<
ImportRatingsResponse,
ImportRatingsParams,
typeof args
> {
connector = new ImportRatingsConnector();

export class ImportRatings extends RateYourMusicChildCommand<typeof args> {
idSeed = "sonamoo high d";
aliases = ["rymimport", "rymsimport"];
description =
Expand All @@ -41,22 +26,11 @@ export class ImportRatings extends RateYourMusicIndexingChildCommand<

slashCommand = true;

async beforeRun() {
if (
await this.concurrencyService.isUserDoingAction(
this.author.id,
ConcurrentAction.RYMImport
)
) {
throw new AlreadyImportingRatingsError();
}
}

async run() {
if (this.payload.isInteraction()) {
await this.reply(
new WarningEmbed().setDescription(
"As of right now, you cannot import with slash commands.\n\nPlease go to https://gowon.bot/import-ratings to import, or use message commands"
`As of right now, you cannot import with slash commands.\n\nPlease use the message command \`${this.prefix}rymimport\``
)
);

Expand All @@ -70,40 +44,25 @@ export class ImportRatings extends RateYourMusicIndexingChildCommand<

const ratings = await this.getRatings();

if (this.payload.isMessage()) {
this.payload.source.react(Emoji.loading);
}

this.concurrencyService.registerUser(
this.ctx,
ConcurrentAction.RYMImport,
this.author.id
);

let response: ImportRatingsResponse;

try {
response = await this.query({
csv: ratings,
user: { discordID: this.author.id },
});
this.unregisterConcurrency();
} catch (e) {
this.unregisterConcurrency();
throw e;
}
const ratingsProgress = this.lilacRatingsService.importProgress(this.ctx, {
discordID: this.author.id,
});

const errors = this.parseErrors(response);
const embed = this.minimalEmbed().setDescription(`Preparing import...`);

if (errors) {
throw new UnknownRatingsImportError();
}
await this.reply(embed);

const embed = new SuccessEmbed().setDescription(
`RateYourMusic ratings imported succesfully! ${Emoji.gowonRated}`
const syncProgressView = new RatingsImportProgressView(
this.ctx,
embed,
ratingsProgress
);

await this.reply(embed);
syncProgressView.subscribeToObservable();

await this.lilacRatingsService.import(this.ctx, ratings, {
discordID: this.author.id,
});
}

private async getRatings(): Promise<string> {
Expand Down Expand Up @@ -164,12 +123,4 @@ export class ImportRatings extends RateYourMusicIndexingChildCommand<

return "";
}

private unregisterConcurrency() {
this.concurrencyService.unregisterUser(
this.ctx,
ConcurrentAction.RYMImport,
this.author.id
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { CommandGroup } from "../../../lib/command/CommandGroup";
import { ParentCommand } from "../../../lib/command/ParentCommand";
import { ArtistRatings } from "./ArtistRatings";
import { Help } from "./Help";
import { ImportRatings } from "./Import";
import { ImportRatings } from "./ImportRatings";
import { Link } from "./Link";
import { Rating } from "./Rating";
import { Ratings } from "./Ratings";
Expand Down
24 changes: 0 additions & 24 deletions src/commands/Lastfm/RateYourMusic/connectors.ts

This file was deleted.

4 changes: 2 additions & 2 deletions src/errors/lilac.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ApolloError, ServerError } from "@apollo/client";
import { ErrorWithSupernovaID } from "../services/analytics/ReportingService";
import { ErrorWithSupernovaID } from "../services/analytics/ErrorReportingService";
import { ClientError } from "./errors";

export function parseLilacError(error: Error): Error {
Expand All @@ -9,7 +9,7 @@ export function parseLilacError(error: Error): Error {
error instanceof ApolloError &&
isServerError(error.networkError)
) {
if (error.networkError.result.supernova_id) {
if (error?.networkError?.result?.supernova_id) {
return new LilacError(
error.message,
error.networkError.result.supernova_id
Expand Down
2 changes: 1 addition & 1 deletion src/lib/command/Command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { GowonService } from "../../services/GowonService";
import { NowPlayingEmbedParsingService } from "../../services/NowPlayingEmbedParsingService";
import { ServiceRegistry } from "../../services/ServicesRegistry";
import { TrackingService } from "../../services/TrackingService";
import { ErrorReportingService } from "../../services/analytics/ReportingService";
import { ErrorReportingService } from "../../services/analytics/ErrorReportingService";
import { ArgumentParsingService } from "../../services/arguments/ArgumentsParsingService";
import { MentionsService } from "../../services/arguments/mentions/MentionsService";
import {
Expand Down
10 changes: 4 additions & 6 deletions src/lib/indexing/MirrorballCommands.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { Command } from "../command/Command";
import { Connector } from "./BaseConnector";
import { ArgumentsMap } from "../context/arguments/types";
import { LastFMService } from "../../services/LastFM/LastFMService";
import { ConcurrencyService } from "../../services/ConcurrencyService";
import { LastFMArguments } from "../../services/LastFM/LastFMArguments";
import { LastFMService } from "../../services/LastFM/LastFMService";
import { ServiceRegistry } from "../../services/ServicesRegistry";
import { Command } from "../command/Command";
import { ArgumentsMap } from "../context/arguments/types";
import { Connector } from "./BaseConnector";

export interface ErrorResponse {
errors: { message: string }[];
Expand All @@ -26,7 +25,6 @@ export abstract class MirrorballBaseCommand<
abstract connector: Connector<ResponseT, ParamsT>;
lastFMService = ServiceRegistry.get(LastFMService);
lastFMArguments = ServiceRegistry.get(LastFMArguments);
concurrencyService = ServiceRegistry.get(ConcurrencyService);

protected readonly progressBarWidth = 15;

Expand Down
11 changes: 9 additions & 2 deletions src/lib/ui/embeds/SuccessEmbed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,17 @@ import { EmbedView } from "../views/EmbedView";
export const successColour = "#02BCA1";

export class SuccessEmbed extends EmbedView {
asDiscordSendable(): EmbedView {
private successEmoji: string = Emoji.checkmark;

public asDiscordSendable(): EmbedView {
return super
.asDiscordSendable()
.setColour(successColour)
.setDescription(`${Emoji.checkmark} ${this.getDescription()}`);
.setDescription(`${this.successEmoji} ${this.getDescription()}`);
}

public setSuccessEmoji(emoji: string): this {
this.successEmoji = emoji;
return this;
}
}
67 changes: 67 additions & 0 deletions src/lib/ui/views/RatingsImportProgressView.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import { Observable, ObservableSubscription } from "@apollo/client";
import { italic } from "../../../helpers/discord";
import {
LilacRatingsImportStage,
RatingsImportProgress,
} from "../../../services/lilac/LilacAPIService.types";
import { GowonContext } from "../../context/Context";
import { Emoji } from "../../emoji/Emoji";
import { displayNumber } from "../displays";
import { ErrorEmbed } from "../embeds/ErrorEmbed";
import { SuccessEmbed } from "../embeds/SuccessEmbed";
import { EmbedView } from "./EmbedView";
import { UnsendableView } from "./View";

export class RatingsImportProgressView extends UnsendableView {
private subscription: ObservableSubscription | undefined;

constructor(
private ctx: GowonContext,
private embed: EmbedView,
private observable: Observable<RatingsImportProgress>
) {
super();
}

public subscribeToObservable(): this {
this.subscription = this.observable.subscribe(async (progress) => {
await this.handleProgress(progress);
});

return this;
}

private async handleProgress(progress: RatingsImportProgress) {
if (progress.stage === LilacRatingsImportStage.Started) {
this.embed
.setDescription(
`${Emoji.loading} Starting import of ${displayNumber(
progress.count,
"rating"
)}...`
)
.editMessage(this.ctx);
} else if (progress.stage === LilacRatingsImportStage.Finished) {
this.embed
.convert(SuccessEmbed)
.setSuccessEmoji(Emoji.gowonRated)
.setDescription(
`Successfully imported ${displayNumber(progress.count, "rating")}!`
)
.editMessage(this.ctx);

this.subscription?.unsubscribe();
} else if (progress.stage === LilacRatingsImportStage.Errored) {
this.embed
.convert(ErrorEmbed)
.setDescription(
`Something went wrong importing your ratings:\n\n${italic(
progress.error
)}`
)
.editMessage(this.ctx);

this.subscription?.unsubscribe();
}
}
}
56 changes: 0 additions & 56 deletions src/services/ConcurrencyService.ts

This file was deleted.

4 changes: 1 addition & 3 deletions src/services/ServicesRegistry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { PermissionsCacheService } from "../lib/permissions/PermissionsCacheServ
import { PermissionsService } from "../lib/permissions/PermissionsService";
import { SettingsService } from "../lib/settings/SettingsService";
import { BaseService } from "./BaseService";
import { ConcurrencyService } from "./ConcurrencyService";
import { DiscordService } from "./Discord/DiscordService";
import { EmojiService } from "./Discord/EmojiService";
import { GuildEventService } from "./Discord/GuildEventService";
Expand All @@ -23,7 +22,7 @@ import { SpotifyService } from "./Spotify/SpotifyService";
import { TimeAndDateService } from "./TimeAndDateService";
import { TrackingService } from "./TrackingService";
import { WordBlacklistService } from "./WordBlacklistService";
import { ErrorReportingService } from "./analytics/ReportingService";
import { ErrorReportingService } from "./analytics/ErrorReportingService";
import { ArgumentParsingService } from "./arguments/ArgumentsParsingService";
import { MentionsService } from "./arguments/mentions/MentionsService";
import { BotStatsService } from "./dbservices/BotStatsService";
Expand Down Expand Up @@ -70,7 +69,6 @@ const services: Service[] = [
CrownsHistoryService,
CrownsUserService,
ComboService,
ConcurrencyService,
DatasourceService,
DiscordService,
EmojiService,
Expand Down
File renamed without changes.
Loading

0 comments on commit 1c83336

Please sign in to comment.