move social threads to model

fix imports
This commit is contained in:
BlowaterNostr 2023-07-13 13:52:52 +00:00
parent d677d5a464
commit 341229b80b
5 changed files with 35 additions and 26 deletions

View File

@ -48,6 +48,7 @@ import { getRelayURLs } from "./setting.ts";
import { DexieDatabase } from "./dexie-db.ts";
import { DividerClass } from "./components/tw.ts";
import { About } from "./about.tsx";
import { computeThreads } from "../nostr.ts";
export async function Start(database: DexieDatabase) {
const model = initialModel();
@ -168,6 +169,8 @@ export class App {
}
initApp = async (accountContext: NostrAccountContext) => {
const events = this.database.filterEvents((e) => e.kind == NostrKind.TEXT_NOTE);
console.log("App.initApp");
const profilesSyncer = await initProfileSyncer(this.relayPool, accountContext, this.database);
if (profilesSyncer instanceof Error) {
@ -176,14 +179,15 @@ export class App {
this.profileSyncer = profilesSyncer;
const allUsersInfo = getAllUsersInformation(this.database, this.myAccountContext);
this.model.allUsersInfo = getAllUsersInformation(this.database, this.myAccountContext);
console.log("App allUsersInfo");
this.model.social.threads = getSocialPosts(this.database, this.model.allUsersInfo);
/* my profile */
this.model.myProfile = allUsersInfo.get(accountContext.publicKey.hex)?.profile?.content;
this.model.myProfile = this.model.allUsersInfo.get(accountContext.publicKey.hex)?.profile?.content;
/* contacts */
for (const contact of allUsersInfo.values()) {
for (const contact of this.model.allUsersInfo.values()) {
const editor = this.model.editors.get(contact.pubkey.hex);
if (editor == null) {
const pubkey = PublicKey.FromHex(contact.pubkey.hex);
@ -202,7 +206,7 @@ export class App {
}
await profilesSyncer.add(
...Array.from(allUsersInfo.keys()),
...Array.from(this.model.allUsersInfo.keys()),
);
console.log("user set", profilesSyncer.userSet);
@ -255,13 +259,9 @@ export function AppComponent(props: {
let socialPostsPanel: VNode | undefined;
if (model.navigationModel.activeNav == "Social") {
const allUserInfo = getAllUsersInformation(app.database, myAccountCtx);
// console.log("AppComponent:getSocialPosts before", Date.now() - t);
const socialPosts = getSocialPosts(app.database, allUserInfo);
// console.log("AppComponent:getSocialPosts after", Date.now() - t, Date.now());
let focusedContentGetter = () => {
// console.log("AppComponent:getFocusedContent before", Date.now() - t);
let _ = getFocusedContent(model.social.focusedContent, allUserInfo, socialPosts);
let _ = getFocusedContent(model.social.focusedContent, model.allUsersInfo, model.social.threads);
// console.log("AppComponent:getFocusedContent", Date.now() - t);
if (_?.type === "MessageThread") {
let editor = model.social.replyEditors.get(_.data.root.event.id);
@ -286,9 +286,7 @@ export function AppComponent(props: {
}
return _;
};
console.log("AppComponent:focusedContentGetter", Date.now() - t);
let focusedContent = focusedContentGetter();
console.log("AppComponent:socialPosts", Date.now() - t);
socialPostsPanel = (
<div
class={tw`flex-1 overflow-hidden bg-[#313338]`}
@ -297,7 +295,7 @@ export function AppComponent(props: {
focusedContent={focusedContent}
editorModel={model.social.editor}
myPublicKey={myAccountCtx.publicKey}
messages={socialPosts}
messages={model.social.threads}
rightPanelModel={model.rightPanelModel}
db={app.database}
eventEmitter={app.eventBus}
@ -343,7 +341,6 @@ export function AppComponent(props: {
model.navigationModel.activeNav == "DM" ||
model.navigationModel.activeNav == "About"
) {
const allUserInfo = getAllUsersInformation(app.database, myAccountCtx);
if (model.navigationModel.activeNav == "DM") {
dmVNode = (
<div
@ -357,7 +354,7 @@ export function AppComponent(props: {
myAccountContext: myAccountCtx,
db: app.database,
pool: app.relayPool,
allUserInfo: allUserInfo,
allUserInfo: model.allUsersInfo,
profilesSyncer: app.profileSyncer,
eventSyncer: app.eventSyncer,
})}
@ -427,7 +424,7 @@ export function AppComponent(props: {
</div>
);
console.debug("App:end", Date.now() - t);
console.debug("AppComponent:end", Date.now() - t);
return final;
}

View File

@ -9,6 +9,9 @@ import { ProfileData } from "../features/profile.ts";
import { RightPanelModel } from "./message-panel.tsx";
import { DM_Container_Model } from "./dm.ts";
import { App } from "./app.tsx";
import { ParsedTag_Nostr_Event } from "../nostr.ts";
import { MessageThread } from "./dm.tsx";
import { UserInfo } from "./contact-list.ts";
export type Model = {
app: App | undefined; // app is only available after sign-in
@ -21,9 +24,11 @@ export type Model = {
key: string;
value: string;
};
allUsersInfo: Map<string, UserInfo>;
// social
social: {
threads: MessageThread[];
editor: Social_EditorModel;
replyEditors: Map<string, Social_EditorModel>;
focusedContent: NostrEvent /* thread root event */ | PublicKey /* focused user profile */ | undefined;
@ -52,12 +57,14 @@ export function initialModel(): Model {
hasNewMessages: new Set(),
currentSelectedContact: undefined,
},
allUsersInfo: new Map(),
editors: editors,
newProfileField: {
key: "",
value: "",
},
social: {
threads: [],
editor: new_Social_EditorModel(),
replyEditors: new Map<string, Social_EditorModel>(),
focusedContent: undefined,

View File

@ -30,6 +30,7 @@ import { SignInEvent, signInWithExtension, signInWithPrivateKey } from "./signIn
import { computeThreads, getTags, PinContact, UnpinContact } from "../nostr.ts";
import { MessageThread } from "./dm.tsx";
import { DexieDatabase } from "./dexie-db.ts";
import { getSocialPosts } from "../features/social.ts";
export type UI_Interaction_Event =
| RemoveRelayButtonClicked
@ -131,6 +132,7 @@ export async function* UI_Interaction_Update(
}
// All events below are only valid after signning in
//
// Relay
//
if (event.type == "AddRelayButtonClicked") {
@ -190,7 +192,7 @@ export async function* UI_Interaction_Update(
};
const group = getGroupOf(
event.pubkey,
getAllUsersInformation(model.app.database, model.app.myAccountContext),
model.allUsersInfo,
);
model.dm.selectedContactGroup = group;
updateConversation(model.app.model, event.pubkey);
@ -457,6 +459,7 @@ export async function* Database_Update(
}
for (let e of changes_events) {
model.allUsersInfo = getAllUsersInformation(database, ctx);
const t = getTags(e).lamport_timestamp;
if (t) {
lamport.set(t);
@ -466,8 +469,7 @@ export async function* Database_Update(
await profileSyncer.add(key.hex);
}
if (e.kind == NostrKind.META_DATA || e.kind == NostrKind.DIRECT_MESSAGE) {
const contacts = getAllUsersInformation(database, ctx);
for (const contact of contacts.values()) {
for (const contact of model.allUsersInfo.values()) {
const editor = model.editors.get(contact.pubkey.hex);
if (editor == null) { // a stranger sends a message
const pubkey = PublicKey.FromHex(contact.pubkey.hex);
@ -539,6 +541,9 @@ export async function* Database_Update(
}
}
}
} else if (e.kind == NostrKind.TEXT_NOTE) {
const events = database.filterEvents((e) => e.kind == NostrKind.TEXT_NOTE);
model.social.threads = getSocialPosts(database, model.allUsersInfo);
}
}
yield model;

View File

@ -1,7 +1,8 @@
/** @jsx h */
import { h, render } from "https://esm.sh/preact@10.11.3";
import { EventBus } from "../event-bus.ts";
import { Search, SearchUpdate } from "./search.tsx";
import { Search } from "./search.tsx";
import { SearchUpdate } from "./search_model.ts";
const eventBus = new EventBus<SearchUpdate>();

View File

@ -3,19 +3,18 @@ import { Database_Contextual_View } from "../database.ts";
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 } from "../nostr.ts";
import { computeThreads, getTags, ParsedTag_Nostr_Event } from "../nostr.ts";
import { MessageThread } from "../UI/dm.tsx";
import { UserInfo } from "../UI/contact-list.ts";
export function getSocialPosts(db: Database_Contextual_View, allUsersInfo: Map<string, UserInfo>) {
export function getSocialPosts(
db: Database_Contextual_View,
allUsersInfo: Map<string, UserInfo>,
) {
const t = Date.now();
const events = db.filterEvents((e) => {
return e.kind == NostrKind.TEXT_NOTE;
});
const events = db.filterEvents((e) => e.kind == NostrKind.TEXT_NOTE);
const threads = computeThreads(events);
console.log("getSocialPosts:threads", Date.now() - t);
const msgs: MessageThread[] = new Array(threads.length);
for (let i = 0; i < threads.length; i++) {
const thread = threads[i];