81 lines
2.3 KiB
TypeScript
81 lines
2.3 KiB
TypeScript
import { api } from "misskey-js";
|
||
import type { Note } from "misskey-js/entities.js";
|
||
import { sample } from "./util";
|
||
|
||
export const misskey = new api.APIClient({
|
||
origin: Bun.env["MISSKEY_ORIGIN"] || "https://misskey.cannorin.net",
|
||
credential: Bun.env["MISSKEY_CREDENTIAL"],
|
||
});
|
||
|
||
export const me = await misskey.request("i", {});
|
||
|
||
/** check if a note is suitable as an input */
|
||
export const isSuitableAsInput = (n: Note) =>
|
||
!n.user.isBot &&
|
||
!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(
|
||
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
|
||
.request("notes/local-timeline", { limit: 100 })
|
||
.then((xs) => xs.filter(isSuitableAsInput))
|
||
.then((xs) => sample(xs, count));
|
||
|
||
// randomly sample N global notes
|
||
const globalNotes = (count: number) =>
|
||
misskey
|
||
.request("notes/global-timeline", { limit: 100 })
|
||
.then((xs) => xs.filter(isSuitableAsInput))
|
||
.then((xs) => sample(xs, count));
|
||
|
||
const notes = await Promise.all([
|
||
followNotes(followNotesCount),
|
||
localNotes(localNotesCount),
|
||
globalNotes(globalNotesCount),
|
||
]);
|
||
return sample(notes.flat());
|
||
}
|
||
|
||
/** fetch the whole reply tree */
|
||
export async function expandReplyTree(
|
||
note: Note,
|
||
cutoff = 5,
|
||
): 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() };
|
||
}
|
||
|
||
export const sanitizeText = (text: string) =>
|
||
text
|
||
.replaceAll(/(\r\n|\r|\n)\s+/g, "\n\n") // remove extra newlines
|
||
.replaceAll("@", "@") // remove mentions
|
||
.replaceAll("#", "#"); // remove hashtags
|