Refactoring
This commit is contained in:
45
index.ts
45
index.ts
@@ -41,42 +41,37 @@ const sleep = (msec: number) =>
|
|||||||
// #region misskey
|
// #region misskey
|
||||||
const me = await misskey.request("i", {});
|
const me = await misskey.request("i", {});
|
||||||
|
|
||||||
|
/** check if a note is suitable as an input */
|
||||||
const isSuitableAsInput = (n: Note) =>
|
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 */
|
/** randomly sample some notes from the timeline */
|
||||||
async function getNotes() {
|
async function getNotes() {
|
||||||
|
|
||||||
// randomly sample N local notes
|
// randomly sample N local notes
|
||||||
async function getLocalNotes(count: number) {
|
const localNotes = (count: number) =>
|
||||||
const timeline = await misskey.request("notes/local-timeline", {
|
misskey.request("notes/local-timeline", { limit: 100 })
|
||||||
limit: 100,
|
.then((xs) => xs.filter(isSuitableAsInput))
|
||||||
});
|
.then((xs) => sample(xs, count));
|
||||||
// exclude bot notes
|
|
||||||
return sample(timeline.filter(isSuitableAsInput), count);
|
|
||||||
}
|
|
||||||
|
|
||||||
// randomly sample N global notes
|
// randomly sample N global notes
|
||||||
async function getGlobalNotes(count: number) {
|
const globalNotes = (count: number) =>
|
||||||
const timeline = await misskey.request("notes/global-timeline", {
|
misskey.request("notes/global-timeline", { limit: 100 })
|
||||||
limit: 100,
|
.then((xs) => xs.filter(isSuitableAsInput))
|
||||||
});
|
.then((xs) => sample(xs, count));
|
||||||
// exclude bot notes and replies
|
|
||||||
return sample(timeline.filter(isSuitableAsInput), count);
|
|
||||||
}
|
|
||||||
|
|
||||||
// randomly sample N notes of mine
|
// randomly sample N notes of mine
|
||||||
async function getMyNotes(count: number) {
|
const myNotes = (count: number) =>
|
||||||
const notes = await misskey.request("users/notes", {
|
misskey.request("users/notes", { userId: me.id, limit: 100 })
|
||||||
userId: me.id,
|
.then((xs) => sample(xs, count));
|
||||||
limit: 100,
|
|
||||||
});
|
|
||||||
return sample(notes, count);
|
|
||||||
}
|
|
||||||
|
|
||||||
const notes = await Promise.all([
|
const notes = await Promise.all([
|
||||||
getLocalNotes(5),
|
localNotes(5),
|
||||||
getGlobalNotes(10),
|
globalNotes(10),
|
||||||
getMyNotes(2),
|
myNotes(2),
|
||||||
]);
|
]);
|
||||||
return sample(notes.flat());
|
return sample(notes.flat());
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user