Fix profile syncer (#204)

This commit is contained in:
BlowaterNostr 2023-10-01 20:59:07 +00:00 committed by GitHub
parent de889d8317
commit b8616ed8bf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 31 additions and 28 deletions

View File

@ -23,7 +23,7 @@ import { EventSyncer } from "./event_syncer.ts";
import { defaultRelays, RelayConfig } from "./relay-config.ts";
import { DexieDatabase } from "./dexie-db.ts";
import { About } from "./about.tsx";
import { ProfilesSyncer } from "../features/profile.ts";
import { ProfileSyncer } from "../features/profile.ts";
import { Popover, PopOverInputChannel } from "./components/popover.tsx";
import { Channel } from "https://raw.githubusercontent.com/BlowaterNostr/csp/master/csp.ts";
import { GroupChatController } from "../group-chat.ts";
@ -87,7 +87,7 @@ export async function Start(database: DexieDatabase) {
}
export class App {
readonly profileSyncer: ProfilesSyncer;
readonly profileSyncer: ProfileSyncer;
readonly eventSyncer: EventSyncer;
public readonly conversationLists: ConversationLists;
public readonly relayConfig: RelayConfig;
@ -112,7 +112,7 @@ export class App {
this.relayConfig.add(url);
}
}
this.profileSyncer = new ProfilesSyncer(this.database, pool);
this.profileSyncer = new ProfileSyncer(this.database, pool);
this.profileSyncer.add(ctx.publicKey.hex);
}

View File

@ -1,6 +1,6 @@
/** @jsx h */
import { h } from "https://esm.sh/preact@10.17.1";
import { getProfileEvent, getProfilesByName, ProfilesSyncer, saveProfile } from "../features/profile.ts";
import { getProfileEvent, getProfilesByName, ProfileSyncer, saveProfile } from "../features/profile.ts";
import { App } from "./app.tsx";
import {
@ -498,7 +498,7 @@ export async function* Database_Update(
ctx: NostrAccountContext,
database: Database_Contextual_View,
model: Model,
profileSyncer: ProfilesSyncer,
profileSyncer: ProfileSyncer,
lamport: LamportTime,
convoLists: ConversationLists,
emit: emitFunc<SelectConversation>,

View File

@ -143,7 +143,6 @@ export class ConversationLists implements ConversationListRetriever {
newestEventReceivedByMe: undefined,
newestEventSendByMe: undefined,
profile: undefined,
// events: [event],
};
if (whoAm_I_TalkingTo == this.ctx.publicKey.hex) {
// talking to myself

View File

@ -8,7 +8,7 @@ import { prepareEncryptedNostrEvent } from "../lib/nostr-ts/event.ts";
import { ConversationLists } from "./conversation-list.ts";
import { EventSyncer } from "./event_syncer.ts";
import { ConnectionPool } from "../lib/nostr-ts/relay.ts";
import { ProfilesSyncer } from "../features/profile.ts";
import { ProfileSyncer } from "../features/profile.ts";
import { handle_SendMessage } from "./app_update.tsx";
import { LamportTime } from "../time.ts";
import { initialModel } from "./app_model.ts";
@ -68,7 +68,7 @@ const view = () => {
allUserInfo={allUserInfo}
db={database}
eventSyncer={new EventSyncer(pool, database)}
profilesSyncer={new ProfilesSyncer(database, pool)}
profilesSyncer={new ProfileSyncer(database, pool)}
emit={testEventBus.emit}
rightPanelModel={{
show: true,

View File

@ -11,7 +11,7 @@ import { DM_EditorModel } from "./editor.tsx";
import { getConversationMessages, UI_Interaction_Event } from "./app_update.tsx";
import { NostrAccountContext, NostrKind } from "../lib/nostr-ts/nostr.ts";
import { ConnectionPool } from "../lib/nostr-ts/relay.ts";
import { getProfileEvent, ProfilesSyncer } from "../features/profile.ts";
import { getProfileEvent, ProfileSyncer } from "../features/profile.ts";
import { ChatMessage } from "./message.ts";
import { DM_Model } from "./dm.ts";
import { getFocusedContent } from "./app.tsx";
@ -26,7 +26,7 @@ type DirectMessageContainerProps = {
emit: emitFunc<UI_Interaction_Event>;
db: Database_Contextual_View;
allUserInfo: ConversationLists;
profilesSyncer: ProfilesSyncer;
profilesSyncer: ProfileSyncer;
eventSyncer: EventSyncer;
} & DM_Model;

View File

@ -10,7 +10,7 @@ import { getSocialPosts } from "../features/social.ts";
import { ConversationLists } from "./conversation-list.ts";
import { EventSyncer } from "./event_syncer.ts";
import { ConnectionPool } from "../lib/nostr-ts/relay.ts";
import { ProfilesSyncer } from "../features/profile.ts";
import { ProfileSyncer } from "../features/profile.ts";
import { handle_SendMessage } from "./app_update.tsx";
import { LamportTime } from "../time.ts";
import { initialModel } from "./app_model.ts";
@ -41,7 +41,7 @@ const view = () => {
eventSyncer={new EventSyncer(pool, database)}
focusedContent={undefined}
myPublicKey={ctx.publicKey}
profilesSyncer={new ProfilesSyncer(database, pool)}
profilesSyncer={new ProfileSyncer(database, pool)}
emit={testEventBus.emit}
messages={threads}
rightPanelModel={{

View File

@ -20,7 +20,7 @@ import {
Text_Note_Event,
UnpinContact,
} from "../nostr.ts";
import { ProfileData, ProfilesSyncer } from "../features/profile.ts";
import { ProfileData, ProfileSyncer } from "../features/profile.ts";
import { MessageThread } from "./dm.tsx";
import { UserDetail } from "./user-detail.tsx";
import { MessageThreadPanel } from "./message-thread-panel.tsx";
@ -85,7 +85,7 @@ interface DirectMessagePanelProps {
emit: emitFunc<
EditorEvent | DirectMessagePanelUpdate | PinContact | UnpinContact
>;
profilesSyncer: ProfilesSyncer;
profilesSyncer: ProfileSyncer;
eventSyncer: EventSyncer;
allUserInfo: Map<string, ConversationSummary>;
}
@ -201,7 +201,7 @@ interface MessageListProps {
threads: MessageThread[];
db: Database_Contextual_View;
emit: emitFunc<DirectMessagePanelUpdate>;
profilesSyncer: ProfilesSyncer;
profilesSyncer: ProfileSyncer;
eventSyncer: EventSyncer;
allUserInfo: Map<string, ConversationSummary>;
}
@ -334,7 +334,7 @@ function MessageBoxGroup(props: {
db: Database_Contextual_View;
allUserInfo: Map<string, ConversationSummary>;
emit: emitFunc<DirectMessagePanelUpdate | ViewUserDetail>;
profilesSyncer: ProfilesSyncer;
profilesSyncer: ProfileSyncer;
eventSyncer: EventSyncer;
}) {
// const t = Date.now();
@ -555,7 +555,7 @@ export function NameAndTime(
export function ParseMessageContent(
message: ChatMessage,
allUserInfo: Map<string, ConversationSummary>,
profilesSyncer: ProfilesSyncer,
profilesSyncer: ProfileSyncer,
eventSyncer: EventSyncer,
emit: emitFunc<ViewUserDetail | ViewThread | ViewNoteThread>,
) {

View File

@ -17,7 +17,7 @@ import { Database_Contextual_View } from "../database.ts";
import { ConversationSummary, getConversationSummaryFromPublicKey } from "./conversation-list.ts";
import { EventSyncer } from "./event_syncer.ts";
import { Avatar } from "./components/avatar.tsx";
import { ProfilesSyncer } from "../features/profile.ts";
import { ProfileSyncer } from "../features/profile.ts";
import { DirectedMessage_Event, Text_Note_Event } from "../nostr.ts";
import { ButtonGroup } from "./components/button-group.tsx";
import { AboutIcon } from "./icons/about-icon.tsx";
@ -29,7 +29,7 @@ interface MessageThreadProps {
myPublicKey: PublicKey;
db: Database_Contextual_View;
editorModel: EditorModel;
profilesSyncer: ProfilesSyncer;
profilesSyncer: ProfileSyncer;
eventSyncer: EventSyncer;
allUserInfo: Map<string, ConversationSummary>;
}
@ -73,7 +73,7 @@ function MessageThreadList(props: {
myPublicKey: PublicKey;
messages: ChatMessage[];
db: Database_Contextual_View;
profilesSyncer: ProfilesSyncer;
profilesSyncer: ProfileSyncer;
eventSyncer: EventSyncer;
emit: emitFunc<ViewUserDetail | ViewThread | DirectMessagePanelUpdate>;
allUserInfo: Map<string, ConversationSummary>;
@ -110,7 +110,7 @@ function MessageThreadBoxGroup(props: {
messages: ChatMessage[];
myPublicKey: PublicKey;
db: Database_Contextual_View;
profilesSyncer: ProfilesSyncer;
profilesSyncer: ProfileSyncer;
eventSyncer: EventSyncer;
emit: emitFunc<ViewUserDetail | ViewThread | DirectMessagePanelUpdate>;
allUserInfo: Map<string, ConversationSummary>;

View File

@ -214,7 +214,6 @@ export function whoIamTalkingTo(event: NostrEvent, myPublicKey: PublicKey) {
`This is not a valid DM, id ${event.id}, kind ${event.kind}`,
);
} else {
console.log(event);
return Error(`Multiple tag p: ${event}`);
}
}

View File

@ -4,9 +4,11 @@ import { PublicKey } from "../lib/nostr-ts/key.ts";
import { groupBy, NostrAccountContext, NostrKind } from "../lib/nostr-ts/nostr.ts";
import { Parsed_Event, Profile_Nostr_Event } from "../nostr.ts";
import { prepareNormalNostrEvent } from "../lib/nostr-ts/event.ts";
import { semaphore } from "https://raw.githubusercontent.com/BlowaterNostr/csp/master/csp.ts";
export class ProfilesSyncer {
export class ProfileSyncer {
readonly userSet = new Set<string>();
private readonly lock = semaphore(1);
constructor(
private readonly database: Database_Contextual_View,
@ -22,13 +24,16 @@ export class ProfilesSyncer {
if (this.userSet.size == size) {
return;
}
await this.pool.closeSub(ProfilesSyncer.name);
const resp = await this.pool.newSub(ProfilesSyncer.name, {
const resp = await this.lock(async () => {
await this.pool.closeSub(ProfileSyncer.name);
const resp = await this.pool.newSub(ProfileSyncer.name, {
authors: Array.from(this.userSet),
kinds: [NostrKind.META_DATA],
});
return resp;
});
if (resp instanceof Error) {
console.error(resp.message);
console.log(resp);
return;
}
for await (let { res: nostrMessage, url: relayUrl } of resp.chan) {