Add book detail

This commit is contained in:
2022-03-06 00:21:05 +03:00
parent cf9120a5dd
commit 80050f33a9
4 changed files with 88 additions and 23 deletions

View File

@@ -12,6 +12,8 @@ export const SEQUENCE_BOOKS_PREFIX = 'bs_';
export const BOOK_ANNOTATION_PREFIX = 'a_an_'; export const BOOK_ANNOTATION_PREFIX = 'a_an_';
export const AUTHOR_ANNOTATION_PREFIX = 'b_an_'; export const AUTHOR_ANNOTATION_PREFIX = 'b_an_';
export const BOOK_INFO_PREFIX = 'b_i_';
export const RANDOM_BOOK = 'random_book'; export const RANDOM_BOOK = 'random_book';
export const RANDOM_AUTHOR = 'random_author'; export const RANDOM_AUTHOR = 'random_author';
export const RANDOM_SEQUENCE = 'random_sequence'; export const RANDOM_SEQUENCE = 'random_sequence';

View File

@@ -1,4 +1,4 @@
import { AuthorBook, TranslatorBook, Book, Author, Sequence, BookAuthor } from './services/book_library'; import { AuthorBook, TranslatorBook, Book, Author, Sequence, BookAuthor, DetailBook } from './services/book_library';
type AllBookTypes = Book | AuthorBook | TranslatorBook; type AllBookTypes = Book | AuthorBook | TranslatorBook;
@@ -19,33 +19,31 @@ export function formatBook(book: AllBookTypes, short: boolean = false): string {
response.push(`📖 ${book.title} | ${book.lang}`); response.push(`📖 ${book.title} | ${book.lang}`);
// if (book.annotation_exists) { response.push(`Информация: /b_i_${book.id}`);
// response.push(`📝 Аннотация: /b_an_${book.id}`)
// } const pushAuthorOrTranslator = (author: BookAuthor) => response.push(
`͏👤 ${author.last_name} ${author.first_name} ${author.middle_name}`
);
if (isTranslatorBook(book) && book.authors.length > 0) { if (isTranslatorBook(book) && book.authors.length > 0) {
response.push('Авторы:') response.push('Авторы:')
const pushAuthor = (author: BookAuthor) => response.push(`͏👤 ${author.last_name} ${author.first_name} ${author.middle_name}`);
if (short && book.authors.length >= 5) { if (short && book.authors.length >= 5) {
book.authors.slice(0, 5).forEach(pushAuthor); book.authors.slice(0, 5).forEach(pushAuthorOrTranslator);
response.push(" и другие."); response.push(" и другие.");
} else { } else {
book.authors.forEach(pushAuthor); book.authors.forEach(pushAuthorOrTranslator);
} }
} }
if (isAuthorBook(book) && book.translators.length > 0) { if (isAuthorBook(book) && book.translators.length > 0) {
response.push('Переводчики:'); response.push('Переводчики:');
const pushTranslator = (author: BookAuthor) => response.push(`͏👤 ${author.last_name} ${author.first_name} ${author.middle_name}`);
if (short && book.translators.length >= 5) { if (short && book.translators.length >= 5) {
book.translators.slice(0, 5).forEach(pushTranslator); book.translators.slice(0, 5).forEach(pushAuthorOrTranslator);
response.push(" и другие.") response.push(" и другие.")
} else { } else {
book.translators.forEach(pushTranslator); book.translators.forEach(pushAuthorOrTranslator);
} }
} }
@@ -54,6 +52,56 @@ export function formatBook(book: AllBookTypes, short: boolean = false): string {
return response.join('\n'); return response.join('\n');
} }
export function formatDetailBook(book: DetailBook): string {
let response: string[] = [];
const addEmptyLine = () => response.push("");
response.push(`📖 ${book.title} | ${book.lang}`);
addEmptyLine();
if (book.annotation_exists) {
response.push(`📝 Аннотация: /b_an_${book.id}`)
addEmptyLine();
}
if (book.authors.length > 0) {
response.push('Авторы:')
const pushAuthor = (author: BookAuthor) => response.push(
`͏👤 ${author.last_name} ${author.first_name} ${author.middle_name} /a_${author.id}`
);
book.authors.forEach(pushAuthor);
addEmptyLine();
}
if (book.translators.length > 0) {
response.push('Переводчики:');
const pushTranslator = (author: BookAuthor) => response.push(
`͏👤 ${author.last_name} ${author.first_name} ${author.middle_name} /t_${author.id}`
);
book.translators.forEach(pushTranslator);
addEmptyLine();
}
if (book.sequences.length > 0) {
response.push('Серии:');
const pushSequence = (sequence: Sequence) => response.push(
`📚 ${sequence.name} /s_${sequence.id}`
);
book.sequences.forEach(pushSequence);
addEmptyLine();
}
response.push("Скачать: ")
book.available_types.forEach(a_type => response.push(`📥 ${a_type}: /d_${a_type}_${book.id}`));
return response.join('\n');
}
export function formatBookShort(book: AllBookTypes): string { export function formatBookShort(book: AllBookTypes): string {
return formatBook(book, true); return formatBook(book, true);
} }
@@ -65,9 +113,9 @@ export function formatAuthor(author: Author): string {
response.push(`👤 ${author.last_name} ${author.first_name} ${author.middle_name}`); response.push(`👤 ${author.last_name} ${author.first_name} ${author.middle_name}`);
response.push(`/a_${author.id}`); response.push(`/a_${author.id}`);
// if (author.annotation_exists) { if (author.annotation_exists) {
// response.push(`📝 Аннотация: /a_an_${author.id}`); response.push(`📝 Аннотация: /a_an_${author.id}`);
// } }
return response.join('\n'); return response.join('\n');
} }
@@ -79,9 +127,9 @@ export function formatTranslator(author: Author): string {
response.push(`👤 ${author.last_name} ${author.first_name} ${author.middle_name}`); response.push(`👤 ${author.last_name} ${author.first_name} ${author.middle_name}`);
response.push(`/t_${author.id}`); response.push(`/t_${author.id}`);
// if (author.annotation_exists) { if (author.annotation_exists) {
// response.push(`📝 Аннотация: /a_an_${author.id}`); response.push(`📝 Аннотация: /a_an_${author.id}`);
// } }
return response.join('\n'); return response.join('\n');
} }

View File

@@ -14,7 +14,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, getUserSettings } from './services/user_settings';
import { formatBook, formatBookShort, formatAuthor, formatSequence, formatTranslator } 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';
import { sendFile } from './hooks/downloading'; import { sendFile } from './hooks/downloading';
@@ -102,7 +102,7 @@ export async function createApprovedBot(token: string, state: BotState): Promise
}) })
}); });
registerRandomItemCallback(bot, CallbackData.RANDOM_BOOK, BookLibrary.getRandomBook, formatBook); registerRandomItemCallback(bot, CallbackData.RANDOM_BOOK, BookLibrary.getRandomBook, formatDetailBook);
registerRandomItemCallback(bot, CallbackData.RANDOM_AUTHOR, BookLibrary.getRandomAuthor, formatAuthor); registerRandomItemCallback(bot, CallbackData.RANDOM_AUTHOR, BookLibrary.getRandomAuthor, formatAuthor);
registerRandomItemCallback(bot, CallbackData.RANDOM_SEQUENCE, BookLibrary.getRandomSequence, formatSequence); registerRandomItemCallback(bot, CallbackData.RANDOM_SEQUENCE, BookLibrary.getRandomSequence, formatSequence);
@@ -343,7 +343,7 @@ export async function createApprovedBot(token: string, state: BotState): Promise
return; return;
} }
const sequenceId = ctx.message.text.split('_')[1]; const sequenceId = ctx.message.text.split("@")[0].split('_')[1];
const userSettings = await getUserSettings(ctx.message.from.id); const userSettings = await getUserSettings(ctx.message.from.id);
const allowedLangs = userSettings.allowed_langs.map((lang) => lang.code); const allowedLangs = userSettings.allowed_langs.map((lang) => lang.code);
@@ -358,6 +358,20 @@ export async function createApprovedBot(token: string, state: BotState): Promise
}); });
}); });
bot.hears(new RegExp(`^/b_i_[\\d]+(@${me.username})*$`), async (ctx: Context) => {
if (!ctx.message || !('text' in ctx.message)) {
return;
}
const bookId = ctx.message.text.split("@")[0].split('_')[2];
const book = await BookLibrary.getBookById(parseInt(bookId));
await ctx.reply(formatDetailBook(book), {
reply_to_message_id: ctx.message.message_id,
});
});
bot.on("message", async (ctx: Context) => { bot.on("message", async (ctx: Context) => {
if (!ctx.message || !('text' in ctx.message)) { if (!ctx.message || !('text' in ctx.message)) {
return; return;

View File

@@ -58,6 +58,7 @@ export interface Source {
export interface DetailBook extends Book { export interface DetailBook extends Book {
sequences: Sequence[];
source: Source; source: Source;
remote_id: number; remote_id: number;
is_deleted: boolean; is_deleted: boolean;
@@ -199,8 +200,8 @@ export async function getSequenceBooks(sequenceId: number | string, page: number
return _makeRequest<Page<Book>>(`/api/v1/sequences/${sequenceId}/books`, searchParams); return _makeRequest<Page<Book>>(`/api/v1/sequences/${sequenceId}/books`, searchParams);
} }
export async function getRandomBook(allowedLangs: string[]): Promise<Book> { export async function getRandomBook(allowedLangs: string[]): Promise<DetailBook> {
return _makeRequest<Book>('/api/v1/books/random', getAllowedLangsSearchParams(allowedLangs)); return _makeRequest<DetailBook>('/api/v1/books/random', getAllowedLangsSearchParams(allowedLangs));
} }
export async function getRandomAuthor(allowedLangs: string[]): Promise<Author> { export async function getRandomAuthor(allowedLangs: string[]): Promise<Author> {