//native
import {
getState,
setState,
} from "windmill-client@1";
const MAX_LOOKBACK = 100;
/**
* @param mentions Case **insensitive** phrases that should be searched for.
*/
export async function main(mentions: string[]) {
let lastState = await getState();
let maxItem = await getMaxItem();
if (!lastState) {
lastState = maxItem - MAX_LOOKBACK;
}
maxItem = Math.min(maxItem, lastState + MAX_LOOKBACK);
const items = [];
for (let i = lastState; i < maxItem; i++) {
const item = await getItem(i);
if (
mentions.find((mention) => {
if (!item.text) return false;
const m = mention.trim().toLowerCase();
return item.text.toLowerCase().includes(m);
})
) {
items.push(item);
}
}
await setState(maxItem);
return items;
}
export async function getMaxItem() {
const res = await fetch("https://hacker-news.firebaseio.com/v0/maxitem.json");
return Number(await res.text());
}
export async function getItem(id: number) {
const res = await fetch(
`https://hacker-news.firebaseio.com/v0/item/${id}.json`,
);
return res.json();
}
Submitted by hugo989 6 days ago