mirror of
https://github.com/flibusta-apps/book_bot.git
synced 2025-12-06 15:35:35 +01:00
Clear cache on fail
This commit is contained in:
@@ -11,7 +11,7 @@ import * as Messages from "./messages";
|
|||||||
import * as CallbackData from "./callback_data";
|
import * as CallbackData from "./callback_data";
|
||||||
|
|
||||||
import * as BookLibrary from "./services/book_library";
|
import * as BookLibrary from "./services/book_library";
|
||||||
import { CachedMessage, getBookCache } from './services/book_cache';
|
import { CachedMessage, clearBookCache, getBookCache } from './services/book_cache';
|
||||||
import { getBookCacheBuffer } from './services/book_cache_buffer';
|
import { getBookCacheBuffer } from './services/book_cache_buffer';
|
||||||
import { download } from './services/downloader';
|
import { download } from './services/downloader';
|
||||||
import { createOrUpdateUserSettings, getUserSettings } from './services/user_settings';
|
import { createOrUpdateUserSettings, getUserSettings } from './services/user_settings';
|
||||||
@@ -150,14 +150,9 @@ export async function createApprovedBot(token: string, state: BotState): Promise
|
|||||||
}
|
}
|
||||||
|
|
||||||
const [_, format, id] = ctx.message.text.split('_');
|
const [_, format, id] = ctx.message.text.split('_');
|
||||||
|
const chatId = ctx.message.chat.id;
|
||||||
let cache: CachedMessage;
|
|
||||||
|
if (state.cache === Cache.NO_CACHE) {
|
||||||
if (state.cache === Cache.ORIGINAL) {
|
|
||||||
cache = await getBookCache(parseInt(id), format);
|
|
||||||
} else if (state.cache === Cache.BUFFER) {
|
|
||||||
cache = await getBookCacheBuffer(parseInt(id), format);
|
|
||||||
} else {
|
|
||||||
const book = await BookLibrary.getBookById(parseInt(id));
|
const book = await BookLibrary.getBookById(parseInt(id));
|
||||||
const data = await download(book.source.id, book.remote_id, format);
|
const data = await download(book.source.id, book.remote_id, format);
|
||||||
ctx.telegram.sendDocument(ctx.message.chat.id, data, {
|
ctx.telegram.sendDocument(ctx.message.chat.id, data, {
|
||||||
@@ -166,9 +161,30 @@ export async function createApprovedBot(token: string, state: BotState): Promise
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx.telegram.copyMessage(ctx.message.chat.id, cache.chat_id, cache.message_id, {
|
let cache: CachedMessage | null = null;
|
||||||
allow_sending_without_reply: true,
|
|
||||||
})
|
const getCachedMessage = async () => {
|
||||||
|
if (state.cache === Cache.ORIGINAL) {
|
||||||
|
return getBookCache(parseInt(id), format);
|
||||||
|
}
|
||||||
|
|
||||||
|
return getBookCacheBuffer(parseInt(id), format);
|
||||||
|
};
|
||||||
|
|
||||||
|
const sendCached = async () => {
|
||||||
|
cache = await getCachedMessage();
|
||||||
|
await ctx.telegram.copyMessage(chatId, cache.chat_id, cache.message_id, {
|
||||||
|
allow_sending_without_reply: true,
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
try {
|
||||||
|
await sendCached();
|
||||||
|
} catch (e) {
|
||||||
|
await clearBookCache(parseInt(id), format);
|
||||||
|
await sendCached();
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
bot.hears(/^\/b_info_[\d]+$/gm, async (ctx: Context) => {
|
bot.hears(/^\/b_info_[\d]+$/gm, async (ctx: Context) => {
|
||||||
@@ -279,6 +295,7 @@ export async function createApprovedBot(token: string, state: BotState): Promise
|
|||||||
});
|
});
|
||||||
|
|
||||||
bot.catch((err) => {
|
bot.catch((err) => {
|
||||||
|
console.log(err);
|
||||||
Sentry.captureException(err);
|
Sentry.captureException(err);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -31,7 +31,23 @@ async function _makeRequest<T>(url: string, searchParams?: string | Record<strin
|
|||||||
return response.body;
|
return response.body;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function _makeDeleteRequest<T>(url: string, searchParams?: string | Record<string, string | number | boolean | null | undefined> | URLSearchParams | undefined): Promise<T> {
|
||||||
|
const response = await got.delete<T>(`${env.CACHE_SERVER_URL}${url}`, {
|
||||||
|
searchParams,
|
||||||
|
headers: {
|
||||||
|
'Authorization': env.CACHE_SERVER_API_KEY,
|
||||||
|
},
|
||||||
|
responseType: 'json',
|
||||||
|
});
|
||||||
|
|
||||||
|
return response.body;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
export async function getBookCache(bookId: number, fileType: string): Promise<CachedMessage> {
|
export async function getBookCache(bookId: number, fileType: string): Promise<CachedMessage> {
|
||||||
return (await _makeRequest<BookCache>(`/api/v1/${bookId}/${fileType}`)).data;
|
return (await _makeRequest<BookCache>(`/api/v1/${bookId}/${fileType}`)).data;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export async function clearBookCache(bookId: number, fileType: string): Promise<CachedMessage> {
|
||||||
|
return (await _makeDeleteRequest<BookCache>(`/api/v1/${bookId}/${fileType}`)).data;
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user