2023-09-11 18:09:38 +00:00
|
|
|
/** @jsx h */
|
|
|
|
import { h, render } from "https://esm.sh/preact@10.17.1";
|
|
|
|
import { PrivateKey } from "../lib/nostr-ts/key.ts";
|
|
|
|
import { InMemoryAccountContext, NostrEvent, NostrKind } from "../lib/nostr-ts/nostr.ts";
|
|
|
|
import { Database_Contextual_View } from "../database.ts";
|
2023-09-23 20:53:40 +00:00
|
|
|
import { testEventBus } from "./_setup.test.ts";
|
2023-09-11 18:09:38 +00:00
|
|
|
import { prepareEncryptedNostrEvent } from "../lib/nostr-ts/event.ts";
|
2023-09-23 21:54:13 +00:00
|
|
|
import { ConversationLists } from "./conversation-list.ts";
|
2023-09-11 18:09:38 +00:00
|
|
|
import { EventSyncer } from "./event_syncer.ts";
|
|
|
|
import { ConnectionPool } from "../lib/nostr-ts/relay.ts";
|
2023-10-01 20:59:07 +00:00
|
|
|
import { ProfileSyncer } from "../features/profile.ts";
|
2023-09-11 18:09:38 +00:00
|
|
|
import { handle_SendMessage } from "./app_update.tsx";
|
|
|
|
import { LamportTime } from "../time.ts";
|
|
|
|
import { initialModel } from "./app_model.ts";
|
|
|
|
import { relays } from "../lib/nostr-ts/relay-list.test.ts";
|
|
|
|
import { DirectMessageContainer } from "./dm.tsx";
|
|
|
|
import { fail } from "https://deno.land/std@0.176.0/testing/asserts.ts";
|
2023-09-23 20:53:40 +00:00
|
|
|
import { NewIndexedDB } from "./dexie-db.ts";
|
2023-09-11 18:09:38 +00:00
|
|
|
|
|
|
|
const ctx = InMemoryAccountContext.New(PrivateKey.Generate());
|
2023-09-23 20:53:40 +00:00
|
|
|
const indexedDB = NewIndexedDB();
|
|
|
|
if (indexedDB instanceof Error) {
|
|
|
|
fail(indexedDB.message);
|
|
|
|
}
|
|
|
|
const database = await Database_Contextual_View.New(indexedDB, ctx);
|
2023-09-12 14:51:27 +00:00
|
|
|
if (database instanceof Error) {
|
|
|
|
fail(database.message);
|
|
|
|
}
|
2023-09-11 18:09:38 +00:00
|
|
|
const lamport = new LamportTime(0);
|
|
|
|
|
|
|
|
const e = await database.addEvent(
|
2023-09-23 20:53:40 +00:00
|
|
|
await prepareEncryptedNostrEvent(ctx, {
|
|
|
|
encryptKey: ctx.publicKey,
|
|
|
|
kind: NostrKind.DIRECT_MESSAGE,
|
|
|
|
tags: [["p", ctx.publicKey.hex]],
|
|
|
|
content: "hi",
|
|
|
|
}) as NostrEvent,
|
2023-09-11 18:09:38 +00:00
|
|
|
);
|
2023-09-12 14:51:27 +00:00
|
|
|
if (!e || e instanceof Error) {
|
2023-09-11 18:09:38 +00:00
|
|
|
fail();
|
|
|
|
}
|
|
|
|
|
2023-09-23 21:33:30 +00:00
|
|
|
const allUserInfo = new ConversationLists(ctx);
|
2023-09-11 18:09:38 +00:00
|
|
|
allUserInfo.addEvents([e]);
|
2023-09-23 20:53:40 +00:00
|
|
|
allUserInfo.addEvents(database.events);
|
|
|
|
console.log(database.events);
|
2023-09-11 18:09:38 +00:00
|
|
|
const pool = new ConnectionPool();
|
|
|
|
const model = initialModel();
|
|
|
|
model.dm.currentSelectedContact = ctx.publicKey;
|
|
|
|
model.editors.set(ctx.publicKey.hex, {
|
|
|
|
files: [],
|
|
|
|
id: ctx.publicKey.hex,
|
|
|
|
tags: [["p", ctx.publicKey.hex]],
|
|
|
|
target: {
|
|
|
|
kind: NostrKind.DIRECT_MESSAGE,
|
|
|
|
receiver: {
|
|
|
|
pubkey: ctx.publicKey,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
text: "",
|
|
|
|
});
|
|
|
|
|
|
|
|
pool.addRelayURL(relays[0]);
|
|
|
|
|
|
|
|
const view = () => {
|
|
|
|
return (
|
|
|
|
<DirectMessageContainer
|
2023-10-04 15:07:40 +00:00
|
|
|
conversationLists={allUserInfo}
|
2023-09-11 18:09:38 +00:00
|
|
|
db={database}
|
|
|
|
eventSyncer={new EventSyncer(pool, database)}
|
2023-10-01 20:59:07 +00:00
|
|
|
profilesSyncer={new ProfileSyncer(database, pool)}
|
2023-09-13 18:27:08 +00:00
|
|
|
emit={testEventBus.emit}
|
2023-09-11 18:09:38 +00:00
|
|
|
rightPanelModel={{
|
|
|
|
show: true,
|
|
|
|
}}
|
|
|
|
editors={model.editors}
|
|
|
|
currentSelectedContact={model.dm.currentSelectedContact}
|
|
|
|
focusedContent={model.dm.focusedContent}
|
|
|
|
hasNewMessages={model.dm.hasNewMessages}
|
|
|
|
myAccountContext={ctx}
|
|
|
|
pool={pool}
|
|
|
|
selectedContactGroup={model.dm.selectedContactGroup}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
render(view(), document.body);
|
|
|
|
|
|
|
|
(async () => {
|
2023-09-23 20:53:40 +00:00
|
|
|
for await (const event of database.subscribe()) {
|
|
|
|
if (event == null) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
allUserInfo.addEvents([event]);
|
2023-09-11 18:09:38 +00:00
|
|
|
}
|
|
|
|
})();
|
|
|
|
|
|
|
|
for await (const e of testEventBus.onChange()) {
|
|
|
|
console.log(e);
|
|
|
|
if (e.type == "SendMessage") {
|
|
|
|
const err = await handle_SendMessage(
|
|
|
|
e,
|
|
|
|
ctx,
|
|
|
|
lamport,
|
|
|
|
pool,
|
|
|
|
model.editors,
|
|
|
|
model.social.editor,
|
|
|
|
model.social.replyEditors,
|
|
|
|
database,
|
|
|
|
);
|
|
|
|
if (err instanceof Error) {
|
|
|
|
console.error("update:SendMessage", err);
|
|
|
|
continue; // todo: global error toast
|
|
|
|
}
|
|
|
|
} else if (e.type == "UpdateMessageText") {
|
|
|
|
const event = e;
|
|
|
|
if (event.target.kind == NostrKind.DIRECT_MESSAGE) {
|
|
|
|
const editor = model.editors.get(event.id);
|
|
|
|
if (editor) {
|
|
|
|
editor.text = event.text;
|
|
|
|
} else {
|
|
|
|
console.log(event.target.receiver, event.id);
|
|
|
|
throw new Error("impossible state");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (event.id == "social") {
|
|
|
|
model.social.editor.text = event.text;
|
|
|
|
} else {
|
|
|
|
const editor = model.social.replyEditors.get(event.id);
|
|
|
|
if (editor) {
|
|
|
|
editor.text = event.text;
|
|
|
|
} else {
|
|
|
|
throw new Error("impossible state");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
render(view(), document.body);
|
|
|
|
}
|