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 * as BookLibrary from "./services/book_library";
|
||||||
import UsersCounter from '@/analytics/users_counter';
|
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 { formatBook, formatBookShort, formatAuthor, formatSequence, formatTranslator, formatDetailBook } from './format';
|
||||||
import { getCallbackArgs, getPaginatedMessage, getPrefixWithQueryCreator, getSearchArgs, registerLanguageSettingsCallback, registerPaginationCommand, registerRandomItemCallback } from './utils';
|
import { getCallbackArgs, getPaginatedMessage, getPrefixWithQueryCreator, getSearchArgs, registerLanguageSettingsCallback, registerPaginationCommand, registerRandomItemCallback } from './utils';
|
||||||
import { getRandomKeyboard, getTextPaginationData, getUpdateLogKeyboard, getUserAllowedLangsKeyboard } from './keyboard';
|
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) => {
|
bot.action(new RegExp(CallbackData.UPDATE_LOG_PREFIX), async (ctx: Context) => {
|
||||||
if (!ctx.callbackQuery || !('data' in ctx.callbackQuery)) return;
|
if (!ctx.callbackQuery || !('data' in ctx.callbackQuery)) return;
|
||||||
|
|
||||||
const userSettings = await getUserSettings(ctx.callbackQuery.from.id);
|
const allowedLangs = await getUserOrDefaultLangCodes(ctx.callbackQuery.from.id);
|
||||||
const allowedLangs = userSettings.allowed_langs.map((lang) => lang.code);
|
|
||||||
|
|
||||||
const data = ctx.callbackQuery.data.split("_");
|
const data = ctx.callbackQuery.data.split("_");
|
||||||
const page = parseInt(data[4]);
|
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 authorId = ctx.message.text.split('@')[0].split('_')[1];
|
||||||
|
|
||||||
const userSettings = await getUserSettings(ctx.message.from.id);
|
const allowedLangs = await getUserOrDefaultLangCodes(ctx.message.from.id);
|
||||||
const allowedLangs = userSettings.allowed_langs.map((lang) => lang.code);
|
|
||||||
|
|
||||||
const pMessage = await getPaginatedMessage(
|
const pMessage = await getPaginatedMessage(
|
||||||
`${CallbackData.AUTHOR_BOOKS_PREFIX}${authorId}_`, parseInt(authorId), 1,
|
`${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 translatorId = ctx.message.text.split("@")[0].split('_')[1];
|
||||||
|
|
||||||
const userSettings = await getUserSettings(ctx.message.from.id);
|
const allowedLangs = await getUserOrDefaultLangCodes(ctx.message.from.id);
|
||||||
const allowedLangs = userSettings.allowed_langs.map((lang) => lang.code);
|
|
||||||
|
|
||||||
const pMessage = await getPaginatedMessage(
|
const pMessage = await getPaginatedMessage(
|
||||||
`${CallbackData.TRANSLATOR_BOOKS_PREFIX}${translatorId}_`, parseInt(translatorId), 1,
|
`${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 sequenceId = ctx.message.text.split("@")[0].split('_')[1];
|
||||||
|
|
||||||
const userSettings = await getUserSettings(ctx.message.from.id);
|
const allowedLangs = await getUserOrDefaultLangCodes(ctx.message.from.id);
|
||||||
const allowedLangs = userSettings.allowed_langs.map((lang) => lang.code);
|
|
||||||
|
|
||||||
const pMessage = await getPaginatedMessage(
|
const pMessage = await getPaginatedMessage(
|
||||||
`${CallbackData.SEQUENCE_BOOKS_PREFIX}${sequenceId}_`, parseInt(sequenceId), 1, allowedLangs,
|
`${CallbackData.SEQUENCE_BOOKS_PREFIX}${sequenceId}_`, parseInt(sequenceId), 1, allowedLangs,
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ import moment from 'moment';
|
|||||||
import chunkText from 'chunk-text';
|
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 { 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 {
|
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>> {
|
export async function getUserAllowedLangsKeyboard(userId: number): Promise<Markup.Markup<InlineKeyboardMarkup>> {
|
||||||
const allLangs = await getLanguages();
|
const allLangs = await getLanguages();
|
||||||
const userSettings = await getUserSettings(userId);
|
const userAllowedLangsCodes = await getUserOrDefaultLangCodes(userId);
|
||||||
|
|
||||||
const userAllowedLangsCodes = userSettings !== null
|
|
||||||
? userSettings.allowed_langs.map((lang) => lang.code)
|
|
||||||
: DEFAULT_ALLOWED_LANGS_CODES;
|
|
||||||
|
|
||||||
const onEmoji = '🟢';
|
const onEmoji = '🟢';
|
||||||
const offEmoji = '🔴';
|
const offEmoji = '🔴';
|
||||||
|
|||||||
@@ -51,6 +51,16 @@ export async function getUserSettings(userId: number): Promise<UserSettings> {
|
|||||||
return _makeGetRequest<UserSettings>(`/users/${userId}`);
|
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> {
|
export async function createOrUpdateUserSettings(data: UserSettingsUpdateData): Promise<UserSettings> {
|
||||||
const response = await got.post<UserSettings>(`${env.USER_SETTINGS_URL}/users/`, {
|
const response = await got.post<UserSettings>(`${env.USER_SETTINGS_URL}/users/`, {
|
||||||
json: data,
|
json: data,
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ import env from '@/config';
|
|||||||
import { isNotModifiedMessage } from './errors_utils';
|
import { isNotModifiedMessage } from './errors_utils';
|
||||||
import { getPaginationKeyboard, getUserAllowedLangsKeyboard } from './keyboard';
|
import { getPaginationKeyboard, getUserAllowedLangsKeyboard } from './keyboard';
|
||||||
import * as BookLibrary from "./services/book_library";
|
import * as BookLibrary from "./services/book_library";
|
||||||
import { createOrUpdateUserSettings, getUserSettings } from './services/user_settings';
|
import { createOrUpdateUserSettings, getUserOrDefaultLangCodes } from './services/user_settings';
|
||||||
|
|
||||||
|
|
||||||
Sentry.init({
|
Sentry.init({
|
||||||
@@ -76,8 +76,7 @@ export function registerPaginationCommand<T, Q extends string | number>(
|
|||||||
|
|
||||||
const { query, page } = args;
|
const { query, page } = args;
|
||||||
|
|
||||||
const userSettings = await getUserSettings(ctx.callbackQuery.from.id);
|
const allowedLangs = await getUserOrDefaultLangCodes(ctx.callbackQuery.from.id);
|
||||||
const allowedLangs = userSettings.allowed_langs.map((lang) => lang.code);
|
|
||||||
|
|
||||||
const tPrefix = prefixCreator ? prefixCreator(query) : prefix;
|
const tPrefix = prefixCreator ? prefixCreator(query) : prefix;
|
||||||
|
|
||||||
@@ -106,10 +105,8 @@ export function registerRandomItemCallback<T>(
|
|||||||
bot.action(callback_data, async (ctx: Context) => {
|
bot.action(callback_data, async (ctx: Context) => {
|
||||||
if (!ctx.callbackQuery || !('data' in ctx.callbackQuery)) return;
|
if (!ctx.callbackQuery || !('data' in ctx.callbackQuery)) return;
|
||||||
|
|
||||||
const userSettings = await getUserSettings(ctx.callbackQuery.from.id);
|
|
||||||
|
|
||||||
const item = await itemGetter(
|
const item = await itemGetter(
|
||||||
userSettings.allowed_langs.map((lang) => lang.code)
|
await getUserOrDefaultLangCodes(ctx.callbackQuery.from.id),
|
||||||
);
|
);
|
||||||
|
|
||||||
const keyboard = Markup.inlineKeyboard([
|
const keyboard = Markup.inlineKeyboard([
|
||||||
@@ -135,9 +132,7 @@ export function registerLanguageSettingsCallback(
|
|||||||
bot.action(new RegExp(prefix), async (ctx: Context) => {
|
bot.action(new RegExp(prefix), async (ctx: Context) => {
|
||||||
if (!ctx.callbackQuery || !('data' in ctx.callbackQuery)) return;
|
if (!ctx.callbackQuery || !('data' in ctx.callbackQuery)) return;
|
||||||
|
|
||||||
const userSettings = await getUserSettings(ctx.callbackQuery.from.id);
|
let allowedLangsCodes = await getUserOrDefaultLangCodes(ctx.callbackQuery.from.id);
|
||||||
|
|
||||||
let allowedLangsCodes = userSettings.allowed_langs.map((item) => item.code);
|
|
||||||
|
|
||||||
const tLang = ctx.callbackQuery.data.split("_")[2];
|
const tLang = ctx.callbackQuery.data.split("_")[2];
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user