blowater/features/social.ts

50 lines
1.9 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-06-30 14:05:57 +00:00
import { NostrKind } from "https://raw.githubusercontent.com/BlowaterNostr/nostr.ts/main/nostr.ts";
import { PublicKey } from "https://raw.githubusercontent.com/BlowaterNostr/nostr.ts/main/key.ts";
import { computeThreads, getTags, ParsedTag_Nostr_Event } 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 = db.filterEvents((e) => e.kind == NostrKind.TEXT_NOTE);
2023-07-13 10:17:52 +00:00
const threads = computeThreads(events);
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
let userInfo = allUsersInfo.get(event.pubkey);
const pubkey = PublicKey.FromHex(event.pubkey);
if (pubkey instanceof Error) {
throw new Error("impossible");
}
messages[j] = {
event: event,
author: {
pubkey: pubkey,
name: userInfo?.profile?.content.name,
picture: userInfo?.profile?.content.picture,
},
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;
}