Refactor
This commit is contained in:
@@ -15,10 +15,23 @@ export const isSuitableAsInput = (n: Note) =>
|
||||
!n.replyId &&
|
||||
(!n.mentions || n.mentions.length === 0) &&
|
||||
n.text?.length &&
|
||||
["public", "home"].includes(n.visibility) &&
|
||||
!n.cw &&
|
||||
n.text.length > 0;
|
||||
|
||||
/** randomly sample some notes from the timeline */
|
||||
export async function getNotes(localNotesCount = 5, globalNotesCount = 10) {
|
||||
export async function getNotes(
|
||||
followNotesCount: number,
|
||||
localNotesCount: number,
|
||||
globalNotesCount: number,
|
||||
) {
|
||||
// randomly sample N following notes
|
||||
const followNotes = (count: number) =>
|
||||
misskey
|
||||
.request("notes/timeline", { limit: 100 })
|
||||
.then((xs) => xs.filter(isSuitableAsInput))
|
||||
.then((xs) => sample(xs, count));
|
||||
|
||||
// randomly sample N local notes
|
||||
const localNotes = (count: number) =>
|
||||
misskey
|
||||
@@ -34,6 +47,7 @@ export async function getNotes(localNotesCount = 5, globalNotesCount = 10) {
|
||||
.then((xs) => sample(xs, count));
|
||||
|
||||
const notes = await Promise.all([
|
||||
followNotes(followNotesCount),
|
||||
localNotes(localNotesCount),
|
||||
globalNotes(globalNotesCount),
|
||||
]);
|
||||
@@ -43,10 +57,18 @@ export async function getNotes(localNotesCount = 5, globalNotesCount = 10) {
|
||||
/** fetch the whole reply tree */
|
||||
export async function expandReplyTree(
|
||||
note: Note,
|
||||
acc: Note[] = [],
|
||||
cutoff = 5,
|
||||
) {
|
||||
if (!note.reply || cutoff < 1) return [...acc, note];
|
||||
const reply = await misskey.request("notes/show", { noteId: note.reply.id });
|
||||
return await expandReplyTree(reply, [...acc, note], cutoff - 1);
|
||||
): Promise<{ last: Note; history: Note[] }> {
|
||||
let current = note;
|
||||
let count = 0;
|
||||
const history: Note[] = [];
|
||||
while (current.replyId && count < cutoff) {
|
||||
const parent = await misskey.request("notes/show", {
|
||||
noteId: current.replyId,
|
||||
});
|
||||
history.push(parent);
|
||||
current = parent;
|
||||
count++;
|
||||
}
|
||||
return { last: current, history: history.reverse() };
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user