diff --git a/index.ts b/index.ts index 303afff..343e316 100644 --- a/index.ts +++ b/index.ts @@ -41,42 +41,37 @@ const sleep = (msec: number) => // #region misskey const me = await misskey.request("i", {}); +/** check if a note is suitable as an input */ const isSuitableAsInput = (n: Note) => - !n.user.isBot && !n.replyId && (!n.mentions || n.mentions.length === 0) && n.text?.length && n.text.length > 0 + !n.user.isBot + && !n.replyId + && (!n.mentions || n.mentions.length === 0) + && n.text?.length && n.text.length > 0 /** randomly sample some notes from the timeline */ async function getNotes() { + // randomly sample N local notes - async function getLocalNotes(count: number) { - const timeline = await misskey.request("notes/local-timeline", { - limit: 100, - }); - // exclude bot notes - return sample(timeline.filter(isSuitableAsInput), count); - } + const localNotes = (count: number) => + misskey.request("notes/local-timeline", { limit: 100 }) + .then((xs) => xs.filter(isSuitableAsInput)) + .then((xs) => sample(xs, count)); // randomly sample N global notes - async function getGlobalNotes(count: number) { - const timeline = await misskey.request("notes/global-timeline", { - limit: 100, - }); - // exclude bot notes and replies - return sample(timeline.filter(isSuitableAsInput), count); - } + const globalNotes = (count: number) => + misskey.request("notes/global-timeline", { limit: 100 }) + .then((xs) => xs.filter(isSuitableAsInput)) + .then((xs) => sample(xs, count)); // randomly sample N notes of mine - async function getMyNotes(count: number) { - const notes = await misskey.request("users/notes", { - userId: me.id, - limit: 100, - }); - return sample(notes, count); - } + const myNotes = (count: number) => + misskey.request("users/notes", { userId: me.id, limit: 100 }) + .then((xs) => sample(xs, count)); const notes = await Promise.all([ - getLocalNotes(5), - getGlobalNotes(10), - getMyNotes(2), + localNotes(5), + globalNotes(10), + myNotes(2), ]); return sample(notes.flat()); }