blowater/features/social.ts

46 lines
1.5 KiB
TypeScript
Raw Normal View History

2023-06-30 14:05:57 +00:00
import { ChatMessage } from "../UI/message.ts";
2023-07-11 09:49:58 +00:00
import { Database_Contextual_View } from "../database.ts";
2023-08-28 17:58:05 +00:00
import { NostrKind } from "../lib/nostr-ts/nostr.ts";
2023-06-30 14:05:57 +00:00
2023-07-15 05:58:03 +00:00
import { computeThreads } from "../nostr.ts";
2023-06-30 14:05:57 +00:00
import { MessageThread } from "../UI/dm.tsx";
import { UserInfo } from "../UI/contact-list.ts";
export function getSocialPosts(
db: Database_Contextual_View,
allUsersInfo: Map<string, UserInfo>,
) {
2023-06-30 14:05:57 +00:00
const t = Date.now();
const events = [];
for (const e of db.events) {
if (e.kind == NostrKind.TEXT_NOTE) {
events.push(e);
}
}
2023-07-15 05:58:03 +00:00
console.log("getSocialPosts:filterEvents", Date.now() - t);
2023-07-13 10:17:52 +00:00
const threads = computeThreads(events);
2023-07-15 05:58:03 +00:00
console.log("getSocialPosts:computeThreads", Date.now() - t);
2023-06-30 14:05:57 +00:00
const msgs: MessageThread[] = new Array(threads.length);
2023-07-08 09:12:43 +00:00
for (let i = 0; i < threads.length; i++) {
const thread = threads[i];
2023-06-30 14:05:57 +00:00
const messages: ChatMessage[] = [];
2023-07-08 09:12:43 +00:00
for (let j = 0; j < thread.length; j++) {
const event = thread[j];
2023-06-30 14:05:57 +00:00
messages[j] = {
event: event,
content: event.content,
created_at: new Date(event.created_at * 1000),
type: "text",
2023-07-13 10:17:52 +00:00
lamport: event.parsedTags.lamport_timestamp,
2023-06-30 14:05:57 +00:00
};
}
msgs[i] = {
root: messages[0],
replies: messages.slice(1),
};
}
2023-07-08 09:12:43 +00:00
console.log("getSocialPosts:end", Date.now() - t);
2023-06-30 14:05:57 +00:00
return msgs;
}