Skip to content

Commit 6e8bdda

Browse files
authored
fix: scan CHQ for 100 most recent messages on load
1 parent e22ef00 commit 6e8bdda

File tree

3 files changed

+56
-4
lines changed

3 files changed

+56
-4
lines changed

src/chat.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,10 @@ function parseChatMessage(content: Document): void {
136136
updateKeywordLists(regexText, action);
137137
}
138138

139-
export function newChatEventOccurred({ event_type, user_id, content }: ChatParsedEvent): void {
139+
export function newChatEventOccurred(
140+
{ event_type, user_id, content }: ChatParsedEvent,
141+
updateGithub = true
142+
): void {
140143
if ((user_id !== smokeyId && user_id !== metasmokeId) || event_type !== 1) return;
141144

142145
parseChatMessage(content);
@@ -154,7 +157,8 @@ export function newChatEventOccurred({ event_type, user_id, content }: ChatParse
154157
.filter(({ id }) => id !== prId);
155158
}
156159

157-
// don't wait for that to finish for the function to return
160+
if (!updateGithub) return;
161+
158162
getUpdatedPrInfo(message)
159163
.then(info => {
160164
Domains.pullRequests = (info || [])

src/github.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export const githubUrls = {
2727
};
2828

2929
function makeRegexESCompatible(keyword: string): RegExp[] {
30-
const shortenerPathRegex = /\(\?-i:(\w+)\)\(\?#[a-zA-Z.]+\)/;
30+
const shortenerPathRegex = /\(\?-i:(\w+)\)\(\?#\s*[a-zA-Z.]+\)/;
3131

3232
const path = shortenerPathRegex.exec(keyword)?.[1];
3333
if (!path) return [];

src/index.ts

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,18 @@ export interface Toastr {
2626
error: (message: string) => void;
2727
}
2828

29+
interface ChatEvent {
30+
event_type: number;
31+
content?: string;
32+
user_id: number;
33+
}
34+
35+
interface ChatEvents {
36+
events: ChatEvent[];
37+
time: number;
38+
sync: number;
39+
}
40+
2941
declare const CHAT: ChatObject;
3042
declare const toastr: Toastr;
3143
declare const fire: {
@@ -422,17 +434,53 @@ void (async function(): Promise<void> {
422434
await new Promise(resolve => setTimeout(resolve, 0));
423435
await Domains.fetchAllDomainInformation();
424436

437+
const domParser = new DOMParser();
438+
425439
CHAT.addEventHandlerHook(event => {
426440
const eventToPass = Object.assign({
427441
...event,
428442
// because we can't use DOMParser with tests,
429443
// newChatEventOccurred has to accept a Document argument for content
430-
content: new DOMParser().parseFromString(event.content, 'text/html')
444+
content: domParser.parseFromString(event.content, 'text/html')
431445
}) as ChatParsedEvent;
432446

433447
newChatEventOccurred(eventToPass);
434448
});
435449

450+
// to fix caching issues, fetch the most recent 100 messages
451+
// and run newChatEventOccurred on them
452+
try {
453+
const fkey = document.querySelector<HTMLInputElement>('#fkey')?.value;
454+
const formData = new FormData();
455+
formData.append('since', '0');
456+
formData.append('mode', 'Messages');
457+
formData.append('msgCount', '100');
458+
formData.append('fkey', fkey || '');
459+
460+
const request = await fetch(
461+
'https://chat.stackexchange.com/chats/11540/events',
462+
{
463+
method: 'POST',
464+
body: formData
465+
}
466+
);
467+
const response = await request.json() as ChatEvents;
468+
469+
response.events
470+
.filter(({ event_type, content }) => event_type === 1 && content)
471+
.forEach(event => {
472+
const parsed = Object.assign({
473+
...event,
474+
content: domParser.parseFromString(event.content as string, 'text/html')
475+
}) as ChatParsedEvent;
476+
477+
// do NOT fetch PR info from the GitHub API
478+
newChatEventOccurred(parsed, false);
479+
});
480+
} catch (error) {
481+
console.error(error);
482+
}
483+
436484
window.addEventListener('fire-popup-open', () => {
437485
void addHtmlToFirePopup();
438486
});

0 commit comments

Comments
 (0)