Skip to content

Commit

Permalink
Migrate to a shared redis cluster
Browse files Browse the repository at this point in the history
  • Loading branch information
lwschan committed Dec 20, 2022
1 parent b4caf9e commit 833bb36
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 18 deletions.
30 changes: 15 additions & 15 deletions src/WhatsPlayingBot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import redisClient from './common/redisClient';
import LastFmClient from './LastFmClient/LastFmClient';
import telegramMessage from './common/telegramMessage';
import environment from './common/environment';
import { getUserIdRedisKey } from './common/utils';

class WhatsPlayingBot extends TelegramBot {
private lastFmClient: LastFmClient;
Expand Down Expand Up @@ -81,7 +82,7 @@ class WhatsPlayingBot extends TelegramBot {

private async handleSetUser(message: TelegramBot.Message): Promise<void> {
const chatId = telegramMessage.getChatId(message);
const telegramUsername = telegramMessage.getUsername(message);
const userId = telegramMessage.getUserId(message);
const lastFmUsername = message.text?.split(' ')[1];

const allowedChat = this.isChatIdValid(chatId);
Expand All @@ -98,7 +99,7 @@ class WhatsPlayingBot extends TelegramBot {
return;
}

if (telegramUsername == null) {
if (userId == null) {
this.sendMessage(chatId, 'Please set a valid Telegram username.');

return;
Expand All @@ -114,7 +115,8 @@ class WhatsPlayingBot extends TelegramBot {

const replyName = telegramMessage.getReplyName(message);

await this.redisClient.set(telegramUsername, lastFmUsername);
const userRedisKey = getUserIdRedisKey(userId);
await this.redisClient.set(userRedisKey, lastFmUsername);

const replyMessage = `Last FM username ${lastFmUsername} is set for ${replyName}.`;

Expand All @@ -123,8 +125,6 @@ class WhatsPlayingBot extends TelegramBot {

private async handleDeleteUser(message: TelegramBot.Message): Promise<void> {
const chatId = telegramMessage.getChatId(message);
const telegramUsername = telegramMessage.getUsername(message);

const allowedChat = this.isChatIdValid(chatId);

if (!allowedChat) {
Expand All @@ -133,14 +133,15 @@ class WhatsPlayingBot extends TelegramBot {
return;
}

if (telegramUsername == null) {
this.sendMessage(chatId, 'Please set a valid Telegram username.');
const userId = telegramMessage.getUserId(message);
if (userId == null) {
this.sendMessage(chatId, 'Error, could not retrieve your Telegram user id.');

return;
}

const lastFmUsername = await this.redisClient.get(telegramUsername);

const userRedisKey = getUserIdRedisKey(userId);
const lastFmUsername = await this.redisClient.get(userRedisKey);
const replyName = telegramMessage.getReplyName(message);

if (lastFmUsername == null) {
Expand All @@ -149,30 +150,29 @@ class WhatsPlayingBot extends TelegramBot {
return;
}

this.redisClient.del(telegramUsername);
this.redisClient.del(userRedisKey);

this.sendMessage(chatId, `Successfully deleted Last FM username for ${replyName}.`);
}

private async handleNowPlaying(message: TelegramBot.Message): Promise<void> {
const chatId = telegramMessage.getChatId(message);
const telegramUsername = telegramMessage.getUsername(message);

const allowedChat = this.isChatIdValid(chatId);

if (!allowedChat) {
console.log(`Chat not allowed for ${chatId}.`);

return;
}

if (telegramUsername == null) {
const userId = telegramMessage.getUserId(message);
if (userId == null) {
this.sendMessage(chatId, 'Please set a valid Telegram username.');

return;
}

const lastFmUsername = await this.redisClient.get(telegramUsername);
const userRedisKey = getUserIdRedisKey(userId);
const lastFmUsername = await this.redisClient.get(userRedisKey);

if (lastFmUsername == null) {
this.sendMessage(chatId, 'Please set a valid Last FM username.');
Expand Down
6 changes: 3 additions & 3 deletions src/common/telegramMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ const getChatId = (message: TelegramBot.Message): number => {
return message.chat.id;
};

const getUsername = (message: TelegramBot.Message): string => {
return message.from?.username || '';
const getUserId = (message: TelegramBot.Message): number | undefined => {
return message.from?.id;
};

export default { getReplyName, getChatId, getUsername };
export default { getReplyName, getChatId, getUserId };
3 changes: 3 additions & 0 deletions src/common/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const getUserIdRedisKey = (userId: number): string => `whats-playing-bot-user-id-${userId}`;

export { getUserIdRedisKey };

0 comments on commit 833bb36

Please sign in to comment.