Skip to content

Commit

Permalink
Merge pull request #497 from gowon-bot/new-syncing
Browse files Browse the repository at this point in the history
New Syncing
  • Loading branch information
abbyfour authored Aug 23, 2024
2 parents d26cccd + cbd5324 commit 742f964
Show file tree
Hide file tree
Showing 92 changed files with 1,838 additions and 1,822 deletions.
4 changes: 3 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ WORKDIR /usr/src/app

# Install dependencies
COPY package*.json ./
COPY yarn.lock ./
COPY yarn.lock* ./
RUN yarn

# Copy source
COPY . .

# This line is broken, so... just remove it!
RUN sed -i '/var global = globalSelf || phxWindow || global;/d' node_modules/phoenix/priv/static/phoenix.cjs.js

RUN yarn rebuild

Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ _어떤 꿈조차도 전부 이뤄질 듯한 느낌_

Gowon is a Last.FM discord bot in active development.

Check out the indexing server over at [gowon-bot/mirrorball](https://github.com/gowon-bot/mirrorball)
Check out the indexing server over at [gowon-bot/lilac](https://github.com/gowon-bot/lilac)

Check out the website over at [gowon-bot/gowon.ca](https://github.com/gowon-bot/gowon.ca)

Expand All @@ -20,7 +20,7 @@ Copy `config.example.json` to `config.json`, and fill in all the fields. Then, d

If you want to run a development version of the bot, you can create a `docker-compose.yml.override` to specify exposed ports and a development Dockerfile (where you could setup auto-reload with nodemon).

To start docker-compose, run `docker-compose up`. Note you will have to download and build the Mirrorball docker image. (Available at [gowon-bot/mirrorball](https://github.com/gowon-bot/mirrorball))
To start docker-compose, run `docker-compose up`. Note you will have to download and build the Mirrorball docker image. (Available at [gowon-bot/lilac](https://github.com/gowon-bot/lilac))

## Commands list

Expand Down
1 change: 0 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# NOTE: Ports should be specified in docker-compose.override.yml
version: "3.9"
services:
gowon:
build: .
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"dependencies": {
"@absinthe/socket": "^0.2.1",
"@absinthe/socket-apollo-link": "^0.2.1",
"@apollo/client": "^3.6.2",
"@apollo/client": "3.6.2",
"@discordjs/rest": "^0.3.0",
"@sentry/cli": "^2.21.2",
"@sentry/node": "^7.72.0",
Expand Down Expand Up @@ -50,7 +50,7 @@
},
"scripts": {
"start": "yarn build && node dist/src/index.js",
"build": "tsc --project ./ && yarn sentry:sourcemaps",
"build": "tsc --project ./",
"rebuild": "yarn clean && yarn build",
"clean": "rm -rf dist/",
"build:watch": "tsc -w --project ./",
Expand Down
2 changes: 1 addition & 1 deletion src/commands/Admin/SyncGuild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export default class SyncGuild extends AdminBaseCommand {
idSeed = "billlie moon sua";

description = "Syncs the list of server members with Gowon";
aliases = ["sync", "serversync", "syncserver", "syncservermembers"];
aliases = ["serversync", "syncserver", "syncservermembers"];
usage = "";

adminCommand = true;
Expand Down
55 changes: 16 additions & 39 deletions src/commands/Lastfm/Account/Login.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Message } from "discord.js";
import { User } from "../../../database/entity/User";
import { Stopwatch, sleep } from "../../../helpers";
import { sleep } from "../../../helpers";
import {
ReactionCollectorFilter,
sanitizeForDiscord,
Expand All @@ -9,11 +9,12 @@ import { LastfmLinks } from "../../../helpers/lastfm/LastfmLinks";
import { LilacBaseCommand } from "../../../lib/Lilac/LilacBaseCommand";
import { Payload } from "../../../lib/context/Payload";
import { Emoji, EmojiRaw } from "../../../lib/emoji/Emoji";
import { displayLink, displayProgressBar } from "../../../lib/ui/displays";
import { displayLink } from "../../../lib/ui/displays";
import { InfoEmbed } from "../../../lib/ui/embeds/InfoEmbed";
import { SuccessEmbed } from "../../../lib/ui/embeds/SuccessEmbed";
import { ConfirmationView } from "../../../lib/ui/views/ConfirmationView";
import { EmbedView } from "../../../lib/ui/views/EmbedView";
import { SyncingProgressView } from "../../../lib/ui/views/SyncingProgressView";
import { LastFMSession } from "../../../services/LastFM/converters/Misc";

export default class Login extends LilacBaseCommand {
Expand Down Expand Up @@ -61,9 +62,9 @@ export default class Login extends LilacBaseCommand {
.setDescription(
`Success! You've been logged in as ${sanitizeForDiscord(
user.lastFMUsername
)}\n\nWould you like to index your data?`
)}\n\nWould you like to sync your Last.fm data?`
)
.setFooter(this.indexingHelp);
.setFooter(this.syncHelp);

const confirmationEmbed = new ConfirmationView(
this.ctx,
Expand All @@ -72,47 +73,25 @@ export default class Login extends LilacBaseCommand {
);

if (await confirmationEmbed.awaitConfirmation(this.ctx)) {
this.impromptuIndex(successEmbed);
this.impromptuSync(successEmbed);
}
}
}

private async impromptuIndex(embed: EmbedView) {
await this.lilacUsersService.index(this.ctx, { discordID: this.author.id });
private async impromptuSync(embed: EmbedView) {
await this.lilacUsersService.sync(this.ctx, { discordID: this.author.id });

embed
.setDescription(
`Indexing...\n${displayProgressBar(0, 1, {
width: this.progressBarWidth,
})}\n*Loading...*`
)
.editMessage(this.ctx);

const observable = this.lilacUsersService.indexingProgress(this.ctx, {
const observable = this.lilacUsersService.syncProgress(this.ctx, {
discordID: this.author.id,
});

const stopwatch = new Stopwatch().start();

const subscription = observable.subscribe(async (progress) => {
if (progress.page === progress.totalPages) {
await embed
.setDescription(`${Emoji.checkmark} Done!`)
.editMessage(this.ctx);

subscription.unsubscribe();
} else if (stopwatch.elapsedInMilliseconds >= 3000) {
await embed
.setDescription(
`Indexing...
${displayProgressBar(progress.page, progress.totalPages, {
width: this.progressBarWidth,
})}
*Page ${progress.page}/${progress.totalPages}*`
)
.editMessage(this.ctx);
}
});
const syncingProgressView = new SyncingProgressView(
this.ctx,
embed,
observable
);

syncingProgressView.subscribeToObservable();
}

private async handleCreateSession(
Expand All @@ -131,8 +110,6 @@ ${displayProgressBar(progress.page, progress.totalPages, {

await this.handleLilacLogin(session.username, session.key);
} catch (e) {
console.log(e);

return { success: false };
}

Expand Down
36 changes: 8 additions & 28 deletions src/commands/Lastfm/Friends/Commands/Rating.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import gql from "graphql-tag";
import { FriendsHaveNoRatingsError } from "../../../../errors/commands/friends";
import { asyncMap } from "../../../../helpers";
import { average } from "../../../../helpers/stats";
Expand All @@ -10,9 +9,9 @@ import {
displayRating,
} from "../../../../lib/ui/displays";
import { ServiceRegistry } from "../../../../services/ServicesRegistry";
import { LilacRatingsService } from "../../../../services/lilac/LilacRatingsService";
import { MirrorballRateYourMusicAlbum } from "../../../../services/mirrorball/MirrorballTypes";
import { AlbumCoverService } from "../../../../services/moderation/AlbumCoverService";
import { RatingResponse } from "../../Mirrorball/RateYourMusic/connectors";
import { FriendsChildCommand } from "../FriendsChildCommand";

const args = {
Expand All @@ -29,6 +28,7 @@ export class Rating extends FriendsChildCommand<typeof args> {
arguments = args;

albumCoverService = ServiceRegistry.get(AlbumCoverService);
lilacRatingsService = ServiceRegistry.get(LilacRatingsService);

async run() {
const { senderRequestable, friends } = await this.getMentions({
Expand All @@ -42,40 +42,20 @@ export class Rating extends FriendsChildCommand<typeof args> {
senderRequestable
);

const query = gql`
query friendsRatings($user: UserInput!, $album: AlbumInput!) {
ratings(
settings: { user: $user, album: $album, pageInput: { limit: 1 } }
) {
ratings {
rating
rateYourMusicAlbum {
title
artistName
}
}
}
}
`;

const ratings: {
discordID: string;
rating: number | undefined;
album: MirrorballRateYourMusicAlbum | undefined;
}[] = await asyncMap(friends.discordIDs(), async (friendID) => {
const ratingResponse = (await this.mirrorballService.query(
this.ctx,
query,
{
user: { discordID: friendID },
album: { name: album, artist: { name: artist } },
}
)) as RatingResponse;
const { ratings } = await this.lilacRatingsService.ratings(this.ctx, {
user: { discordID: friendID },
album: { name: album, artist: { name: artist } },
});

return {
discordID: friendID,
rating: ratingResponse.ratings.ratings[0]?.rating,
album: ratingResponse.ratings.ratings[0]?.rateYourMusicAlbum,
rating: ratings[0]?.rating,
album: ratings[0]?.rateYourMusicAlbum,
};
});

Expand Down
74 changes: 0 additions & 74 deletions src/commands/Lastfm/Indexing/Index.ts

This file was deleted.

61 changes: 61 additions & 0 deletions src/commands/Lastfm/Indexing/Sync.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { Flag } from "../../../lib/context/arguments/argumentTypes/Flag";
import { LilacBaseCommand } from "../../../lib/Lilac/LilacBaseCommand";
import { ConfirmationView } from "../../../lib/ui/views/ConfirmationView";
import { SyncingProgressView } from "../../../lib/ui/views/SyncingProgressView";

const args = {
force: new Flag({
shortnames: ["f"],
longnames: ["force"],
description: "Stop your current sync and restart it",
}),
};

export default class Sync extends LilacBaseCommand<typeof args> {
idSeed = "iz*one yujin";
subcategory = "library";
description = "Fully syncs your Last.fm data to Gowon";
aliases = ["fullindex", "index"];

arguments = args;

slashCommand = true;

async run() {
this.lilacGuildsService.addUser(
this.ctx,
this.author.id,
this.requiredGuild.id
);

const embed = this.minimalEmbed()
.setDescription(
"Syncing will download all your scrobbles from Last.fm. Are you sure you want to sync?"
)
.setFooter(this.syncHelp);

const confirmationEmbed = new ConfirmationView(this.ctx, embed);

if (!(await confirmationEmbed.awaitConfirmation(this.ctx))) {
return;
}

await this.lilacUsersService.sync(
this.ctx,
{ discordID: this.author.id },
this.parsedArguments.force
);

const observable = this.lilacUsersService.syncProgress(this.ctx, {
discordID: this.author.id,
});

const syncProgressView = new SyncingProgressView(
this.ctx,
embed,
observable
);

syncProgressView.subscribeToObservable();
}
}
Loading

0 comments on commit 742f964

Please sign in to comment.