Inline Bots: Fix last result caching (#2374)

This commit is contained in:
Alexander Zinchuk 2023-01-28 02:16:10 +01:00
parent e1f4d12c45
commit 8d4e447981
5 changed files with 26 additions and 2 deletions

View File

@ -104,6 +104,7 @@ export async function fetchInlineBotResults({
switchPm: buildBotSwitchPm(result.switchPm), switchPm: buildBotSwitchPm(result.switchPm),
users: result.users.map(buildApiUser).filter(Boolean), users: result.users.map(buildApiUser).filter(Boolean),
results: processInlineBotResult(String(result.queryId), result.results), results: processInlineBotResult(String(result.queryId), result.results),
cacheTime: result.cacheTime,
}; };
} }

View File

@ -23,7 +23,7 @@ export default function useInlineBotTooltip(
html: string, html: string,
inlineBots?: Record<string, false | InlineBotSettings>, inlineBots?: Record<string, false | InlineBotSettings>,
) { ) {
const { queryInlineBot, resetInlineBot } = getActions(); const { queryInlineBot, resetInlineBot, resetAllInlineBots } = getActions();
const [isOpen, markIsOpen, unmarkIsOpen] = useFlag(); const [isOpen, markIsOpen, unmarkIsOpen] = useFlag();
const { const {
@ -47,6 +47,12 @@ export default function useInlineBotTooltip(
} }
}, [prevQuery, query, unmarkIsOpen]); }, [prevQuery, query, unmarkIsOpen]);
useEffect(() => {
if (!isOpen && !username) {
resetAllInlineBots();
}
}, [isOpen, resetAllInlineBots, username]);
useEffect(() => { useEffect(() => {
if (isAllowed && usernameLowered && chatId) { if (isAllowed && usernameLowered && chatId) {
queryInlineBot({ chatId, username: usernameLowered, query: query! }); queryInlineBot({ chatId, username: usernameLowered, query: query! });

View File

@ -267,6 +267,7 @@ addActionHandler('queryInlineBot', async (global, actions, payload): Promise<voi
switchPm: undefined, switchPm: undefined,
canLoadMore: true, canLoadMore: true,
results: [], results: [],
cacheTime: 0,
}; };
global = replaceInlineBotSettings(global, username, inlineBotData, tabId); global = replaceInlineBotSettings(global, username, inlineBotData, tabId);
@ -350,7 +351,7 @@ addActionHandler('sendInlineBotResult', (global, actions, payload): ActionReturn
}); });
addActionHandler('resetInlineBot', (global, actions, payload): ActionReturnType => { addActionHandler('resetInlineBot', (global, actions, payload): ActionReturnType => {
const { username, tabId = getCurrentTabId() } = payload; const { username, force, tabId = getCurrentTabId() } = payload;
let inlineBotData = selectTabState(global, tabId).inlineBots.byUsername[username]; let inlineBotData = selectTabState(global, tabId).inlineBots.byUsername[username];
@ -358,6 +359,8 @@ addActionHandler('resetInlineBot', (global, actions, payload): ActionReturnType
return; return;
} }
if (!force && Date.now() < inlineBotData.cacheTime) return;
inlineBotData = { inlineBotData = {
id: inlineBotData.id, id: inlineBotData.id,
query: '', query: '',
@ -365,12 +368,22 @@ addActionHandler('resetInlineBot', (global, actions, payload): ActionReturnType
switchPm: undefined, switchPm: undefined,
canLoadMore: true, canLoadMore: true,
results: [], results: [],
cacheTime: 0,
}; };
global = replaceInlineBotSettings(global, username, inlineBotData, tabId); global = replaceInlineBotSettings(global, username, inlineBotData, tabId);
setGlobal(global); setGlobal(global);
}); });
addActionHandler('resetAllInlineBots', (global, actions, payload): ActionReturnType => {
const { tabId = getCurrentTabId() } = payload || {};
const inlineBots = selectTabState(global, tabId).inlineBots.byUsername;
Object.keys(inlineBots).forEach((username) => {
actions.resetInlineBot({ username, tabId });
});
});
addActionHandler('startBot', async (global, actions, payload): Promise<void> => { addActionHandler('startBot', async (global, actions, payload): Promise<void> => {
const { botId, param } = payload; const { botId, param } = payload;
@ -900,6 +913,7 @@ async function searchInlineBot<T extends GlobalState>(global: T, {
global = replaceInlineBotSettings(global, username, { global = replaceInlineBotSettings(global, username, {
...newInlineBotData, ...newInlineBotData,
help: result.help, help: result.help,
cacheTime: Date.now() + result.cacheTime * 1000,
...(newResults.length && { isGallery: result.isGallery }), ...(newResults.length && { isGallery: result.isGallery }),
...(result.switchPm && { switchPm: result.switchPm }), ...(result.switchPm && { switchPm: result.switchPm }),
canLoadMore: result.results.length > 0 && Boolean(result.nextOffset), canLoadMore: result.results.length > 0 && Boolean(result.nextOffset),

View File

@ -1893,7 +1893,9 @@ export interface ActionPayloads {
} & WithTabId; } & WithTabId;
resetInlineBot: { resetInlineBot: {
username: string; username: string;
force?: boolean;
} & WithTabId; } & WithTabId;
resetAllInlineBots: WithTabId | undefined;
startBot: { startBot: {
botId: string; botId: string;
param?: string; param?: string;

View File

@ -391,4 +391,5 @@ export type InlineBotSettings = {
results?: (ApiBotInlineResult | ApiBotInlineMediaResult)[]; results?: (ApiBotInlineResult | ApiBotInlineMediaResult)[];
isGallery?: boolean; isGallery?: boolean;
switchPm?: ApiBotInlineSwitchPm; switchPm?: ApiBotInlineSwitchPm;
cacheTime: number;
}; };