mirror of
https://github.com/flibusta-apps/book_bot.git
synced 2025-12-06 15:35:35 +01:00
Fix allowed langs getting
This commit is contained in:
@@ -13,7 +13,7 @@ import * as CallbackData from "./callback_data";
|
||||
|
||||
import * as BookLibrary from "./services/book_library";
|
||||
import UsersCounter from '@/analytics/users_counter';
|
||||
import { createOrUpdateUserSettings, getUserSettings } from './services/user_settings';
|
||||
import { createOrUpdateUserSettings, getUserOrDefaultLangCodes } from './services/user_settings';
|
||||
import { formatBook, formatBookShort, formatAuthor, formatSequence, formatTranslator, formatDetailBook } from './format';
|
||||
import { getCallbackArgs, getPaginatedMessage, getPrefixWithQueryCreator, getSearchArgs, registerLanguageSettingsCallback, registerPaginationCommand, registerRandomItemCallback } from './utils';
|
||||
import { getRandomKeyboard, getTextPaginationData, getUpdateLogKeyboard, getUserAllowedLangsKeyboard } from './keyboard';
|
||||
@@ -115,8 +115,7 @@ export async function createApprovedBot(token: string, state: BotState): Promise
|
||||
bot.action(new RegExp(CallbackData.UPDATE_LOG_PREFIX), async (ctx: Context) => {
|
||||
if (!ctx.callbackQuery || !('data' in ctx.callbackQuery)) return;
|
||||
|
||||
const userSettings = await getUserSettings(ctx.callbackQuery.from.id);
|
||||
const allowedLangs = userSettings.allowed_langs.map((lang) => lang.code);
|
||||
const allowedLangs = await getUserOrDefaultLangCodes(ctx.callbackQuery.from.id);
|
||||
|
||||
const data = ctx.callbackQuery.data.split("_");
|
||||
const page = parseInt(data[4]);
|
||||
@@ -305,8 +304,7 @@ export async function createApprovedBot(token: string, state: BotState): Promise
|
||||
|
||||
const authorId = ctx.message.text.split('@')[0].split('_')[1];
|
||||
|
||||
const userSettings = await getUserSettings(ctx.message.from.id);
|
||||
const allowedLangs = userSettings.allowed_langs.map((lang) => lang.code);
|
||||
const allowedLangs = await getUserOrDefaultLangCodes(ctx.message.from.id);
|
||||
|
||||
const pMessage = await getPaginatedMessage(
|
||||
`${CallbackData.AUTHOR_BOOKS_PREFIX}${authorId}_`, parseInt(authorId), 1,
|
||||
@@ -325,8 +323,7 @@ export async function createApprovedBot(token: string, state: BotState): Promise
|
||||
|
||||
const translatorId = ctx.message.text.split("@")[0].split('_')[1];
|
||||
|
||||
const userSettings = await getUserSettings(ctx.message.from.id);
|
||||
const allowedLangs = userSettings.allowed_langs.map((lang) => lang.code);
|
||||
const allowedLangs = await getUserOrDefaultLangCodes(ctx.message.from.id);
|
||||
|
||||
const pMessage = await getPaginatedMessage(
|
||||
`${CallbackData.TRANSLATOR_BOOKS_PREFIX}${translatorId}_`, parseInt(translatorId), 1,
|
||||
@@ -345,8 +342,7 @@ export async function createApprovedBot(token: string, state: BotState): Promise
|
||||
|
||||
const sequenceId = ctx.message.text.split("@")[0].split('_')[1];
|
||||
|
||||
const userSettings = await getUserSettings(ctx.message.from.id);
|
||||
const allowedLangs = userSettings.allowed_langs.map((lang) => lang.code);
|
||||
const allowedLangs = await getUserOrDefaultLangCodes(ctx.message.from.id);
|
||||
|
||||
const pMessage = await getPaginatedMessage(
|
||||
`${CallbackData.SEQUENCE_BOOKS_PREFIX}${sequenceId}_`, parseInt(sequenceId), 1, allowedLangs,
|
||||
|
||||
@@ -4,7 +4,7 @@ import moment from 'moment';
|
||||
import chunkText from 'chunk-text';
|
||||
|
||||
import { RANDOM_BOOK, RANDOM_AUTHOR, RANDOM_SEQUENCE, ENABLE_LANG_PREFIX, DISABLE_LANG_PREFIX, UPDATE_LOG_PREFIX } from './callback_data';
|
||||
import { getUserSettings, getLanguages } from './services/user_settings';
|
||||
import { getLanguages, getUserOrDefaultLangCodes } from './services/user_settings';
|
||||
|
||||
|
||||
function getButtonLabel(delta: number, direction: 'left' | 'right'): string {
|
||||
@@ -94,16 +94,9 @@ export function getUpdateLogKeyboard(): Markup.Markup<InlineKeyboardMarkup> {
|
||||
]);
|
||||
}
|
||||
|
||||
|
||||
const DEFAULT_ALLOWED_LANGS_CODES = ['ru', 'be', 'uk'];
|
||||
|
||||
export async function getUserAllowedLangsKeyboard(userId: number): Promise<Markup.Markup<InlineKeyboardMarkup>> {
|
||||
const allLangs = await getLanguages();
|
||||
const userSettings = await getUserSettings(userId);
|
||||
|
||||
const userAllowedLangsCodes = userSettings !== null
|
||||
? userSettings.allowed_langs.map((lang) => lang.code)
|
||||
: DEFAULT_ALLOWED_LANGS_CODES;
|
||||
const userAllowedLangsCodes = await getUserOrDefaultLangCodes(userId);
|
||||
|
||||
const onEmoji = '🟢';
|
||||
const offEmoji = '🔴';
|
||||
|
||||
@@ -51,6 +51,16 @@ export async function getUserSettings(userId: number): Promise<UserSettings> {
|
||||
return _makeGetRequest<UserSettings>(`/users/${userId}`);
|
||||
}
|
||||
|
||||
|
||||
export async function getUserOrDefaultLangCodes(userId: number): Promise<string[]> {
|
||||
try {
|
||||
return (await getUserSettings(userId)).allowed_langs.map((lang) => lang.code);
|
||||
} catch {
|
||||
return ["ru", "be", "uk"];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
export async function createOrUpdateUserSettings(data: UserSettingsUpdateData): Promise<UserSettings> {
|
||||
const response = await got.post<UserSettings>(`${env.USER_SETTINGS_URL}/users/`, {
|
||||
json: data,
|
||||
|
||||
@@ -9,7 +9,7 @@ import env from '@/config';
|
||||
import { isNotModifiedMessage } from './errors_utils';
|
||||
import { getPaginationKeyboard, getUserAllowedLangsKeyboard } from './keyboard';
|
||||
import * as BookLibrary from "./services/book_library";
|
||||
import { createOrUpdateUserSettings, getUserSettings } from './services/user_settings';
|
||||
import { createOrUpdateUserSettings, getUserOrDefaultLangCodes } from './services/user_settings';
|
||||
|
||||
|
||||
Sentry.init({
|
||||
@@ -76,8 +76,7 @@ export function registerPaginationCommand<T, Q extends string | number>(
|
||||
|
||||
const { query, page } = args;
|
||||
|
||||
const userSettings = await getUserSettings(ctx.callbackQuery.from.id);
|
||||
const allowedLangs = userSettings.allowed_langs.map((lang) => lang.code);
|
||||
const allowedLangs = await getUserOrDefaultLangCodes(ctx.callbackQuery.from.id);
|
||||
|
||||
const tPrefix = prefixCreator ? prefixCreator(query) : prefix;
|
||||
|
||||
@@ -106,10 +105,8 @@ export function registerRandomItemCallback<T>(
|
||||
bot.action(callback_data, async (ctx: Context) => {
|
||||
if (!ctx.callbackQuery || !('data' in ctx.callbackQuery)) return;
|
||||
|
||||
const userSettings = await getUserSettings(ctx.callbackQuery.from.id);
|
||||
|
||||
const item = await itemGetter(
|
||||
userSettings.allowed_langs.map((lang) => lang.code)
|
||||
await getUserOrDefaultLangCodes(ctx.callbackQuery.from.id),
|
||||
);
|
||||
|
||||
const keyboard = Markup.inlineKeyboard([
|
||||
@@ -135,9 +132,7 @@ export function registerLanguageSettingsCallback(
|
||||
bot.action(new RegExp(prefix), async (ctx: Context) => {
|
||||
if (!ctx.callbackQuery || !('data' in ctx.callbackQuery)) return;
|
||||
|
||||
const userSettings = await getUserSettings(ctx.callbackQuery.from.id);
|
||||
|
||||
let allowedLangsCodes = userSettings.allowed_langs.map((item) => item.code);
|
||||
let allowedLangsCodes = await getUserOrDefaultLangCodes(ctx.callbackQuery.from.id);
|
||||
|
||||
const tLang = ctx.callbackQuery.data.split("_")[2];
|
||||
|
||||
|
||||
Reference in New Issue
Block a user