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),
users: result.users.map(buildApiUser).filter(Boolean),
results: processInlineBotResult(String(result.queryId), result.results),
cacheTime: result.cacheTime,
};
}

View File

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

View File

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

View File

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

View File

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