Skip to content

Commit

Permalink
problem: can't change pubkey
Browse files Browse the repository at this point in the history
  • Loading branch information
gsovereignty committed Apr 30, 2024
1 parent 49b1944 commit 1308b4b
Show file tree
Hide file tree
Showing 12 changed files with 96 additions and 903 deletions.
9 changes: 4 additions & 5 deletions src/lib/components/LoginButton.svelte
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<script lang="ts">
import { currentPubkey } from "@/stores/user";
import { NDKNip07Signer } from "@nostr-dev-kit/ndk";
import { Person } from "radix-icons-svelte";
import { onMount } from "svelte";
Expand All @@ -20,10 +19,10 @@
const user = await signer.blockUntilReady();
if (user) {
currentPubkey.update(existing=>{
existing = user.pubkey
return existing
})
// currentPubkey.update(existing=>{
// existing = user.pubkey
// return existing
// })
// $ndk.signer = signer;
// $ndk = $ndk
localStorage.setItem("signed-in", "true");
Expand Down
24 changes: 19 additions & 5 deletions src/lib/snort_workers/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export let viewed: Writable<Set<string>> = writable(new Set());

let worker: Worker;

export async function Init() {
export async function Init(pubkey?:string) {
let end = execTime("14 init worker")
worker = new WorkerVite()
end()
Expand All @@ -23,22 +23,36 @@ export async function Init() {
});
};;
let end2 = execTime("21 worker.postMessage")
worker.postMessage(new Command("start")); //todo add pubkey
let cmd = new Command("start")
cmd.pubkey = pubkey
worker.postMessage(cmd);
end2()
}

export function UpdatePubkey(pubkey:string) {

if (worker) {
worker.terminate()
}
Init(pubkey)
// let end = execTime("31 set pubkey")
// if (worker) {
// let cmd = new Command("set_master_pubkey")
// cmd.pubkey = pubkey
// worker.postMessage(cmd)
// } else {
// console.log("no worker started")
// }
//end()
}

export function PushEvent(e: NostrEvent[]) {
let end = execTime("34 pushevent")
if (worker) {
let cmd = new Command("push_event")
cmd.event = e
worker.postMessage(new Command("ping"))
//worker.postMessage(new Command("ping"))
worker.postMessage(cmd)
worker.postMessage(new Command("ping"))
//worker.postMessage(new Command("ping"))
} else {
console.log("no worker started")
}
Expand Down
53 changes: 41 additions & 12 deletions src/lib/snort_workers/master_worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,21 @@ workerDataStore.subscribe((data) => {
if (!re) {
throw new Error('missing event, this should not happen, bug!');
}
// let replies = data.replies.get(re.id);
// let weFollow = false;
// if (replies) {
// for (let r of replies) {
// let event = data.events.get(r);
// if (event) {
// if (data.ourFollows.has(event.pubkey)) {
// weFollow = true;
// }
// }
// }
// }
// if (weFollow) {
// roots.push(re!);
// }
roots.push(re!);
} else {
inBloom.push(r);
Expand All @@ -64,10 +79,12 @@ workerDataStore.subscribe((data) => {
}
return 0;
});
fed.rootsByReplies = Array.from(_rootsByReply, (r)=>{return r.id!})
fed.rootsByReplies = Array.from(_rootsByReply, (r) => {
return r.id!;
});
let _roots = roots.toSorted((a, b) => {
let a_rakeHits = data.rake.hitcount(a.content)
let b_rakeHits = data.rake.hitcount(b.content)
let a_rakeHits = data.rake.hitcount(a.content);
let b_rakeHits = data.rake.hitcount(b.content);
if (a_rakeHits && b_rakeHits) {
return b_rakeHits - a_rakeHits;
}
Expand All @@ -78,15 +95,17 @@ workerDataStore.subscribe((data) => {
return -1;
}
return 0;
})//.filter((x)=>{return data.rake.hitcount(x.content) > 200});
fed.rootsByKeyword = Array.from(_roots, (r)=>{return r.id!})
}); //.filter((x)=>{return data.rake.hitcount(x.content) > 200});
fed.rootsByKeyword = Array.from(_roots, (r) => {
return r.id!;
});
fed.replies = data.replies;
fed.events = data.events;
fed._bloomString = JSON.stringify([].slice.call(data.ourBloom.buckets));
let testbloom = new BloomFilter(JSON.parse(fed._bloomString), 32);
//console.log(data.ourBloom.test(inBloom[0]), testbloom.test(inBloom[0]))
fed.ourBloom = data.ourBloom; //new BloomFilter(JSON.parse(fed._bloomString), 32)
fed.keywords = workerData.rake.words
fed.keywords = workerData.rake.words;
postMessage(fed);
end();
});
Expand All @@ -106,7 +125,7 @@ lengthOfFollows.subscribe((x) => {
.kinds([1, 7]);
rb.withOptions({ leaveOpen: true });
if (q_subToFollows) {
q_subToFollows.cancel();
//q_subToFollows.cancel();
}
q_subToFollows = sys.Query(rb);
q_subToFollows.on('event', (evs): void => {
Expand All @@ -131,6 +150,10 @@ onmessage = (m: MessageEvent<Command>) => {
if (m.data.command == 'start') {
start(m.data.pubkey);
}
if (m.data.command == 'set_master_pubkey') {
console.log(135, m.data.pubkey);
start(m.data.pubkey);
}
if (m.data.command == 'push_event') {
//console.log(96);
let map = new Map<string, NostrEvent>();
Expand All @@ -152,11 +175,17 @@ async function start(pubkey?: string, pubkeys?: string[]) {
connect();
return new Promise((resolve, reject) => {
if (pubkey) {
workerData.setOurPubkey(pubkey);
if (workerData._ourPubkey != pubkey) {
workerData = new WorkerData()
workerDataStore.set(workerData)
}
workerDataStore.update((c) => {
c.setOurPubkey(pubkey!);
return c;
});
} else {
pubkey = workerData.ourPubkey();
}

(async () => {
let end = execTime('125 async start');
const rb = new RequestBuilder('fetch-initial-data');
Expand Down Expand Up @@ -212,10 +241,10 @@ async function start(pubkey?: string, pubkeys?: string[]) {
}
event = workerData.latestReplaceable.get(pubkey)?.get('18100');
if (event) {
let b = bloomFromKind18100(event)
let b = bloomFromKind18100(event);
if (b) {
workerData.ourBloom = b
dirty = true
workerData.ourBloom = b;
dirty = true;
}
}
}
Expand Down
25 changes: 22 additions & 3 deletions src/lib/snort_workers/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import { BloomFilter } from 'bloomfilter';
import type { NostrEvent } from 'nostr-tools';

export class Command {
command: 'start' | 'sub_to_pubkeys' | 'fetch_events' | 'push_event' | 'ping';
command: 'start' | 'sub_to_pubkeys' | 'fetch_events' | 'push_event' | 'ping' | 'set_master_pubkey';
pubkey?: string;
pubkeys?: string[];
events?: string[];
event?: NostrEvent[];
constructor(command: 'start' | 'sub_to_pubkeys' | 'fetch_events' | 'push_event' | 'ping') {
constructor(command: 'start' | 'sub_to_pubkeys' | 'fetch_events' | 'push_event' | 'ping' | 'set_master_pubkey') {
this.command = command;
}
}
Expand Down Expand Up @@ -53,12 +53,15 @@ export class WorkerData {
ourPubkey(): string {
return this._ourPubkey
? this._ourPubkey
: 'd91191e30e00444b942c0e82cad470b32af171764c2275bee0bd99377efd4075';
: 'd926c9849295e7d75a7eba75428e633ce66fd06f6c88ced88edc0950fa761484'//'d91191e30e00444b942c0e82cad470b32af171764c2275bee0bd99377efd4075';
}
setOurPubkey(pubkey: string) {
if (pubkey.length != 64) {
throw new Error('invalid pubkey');
}
// if (this._ourPubkey != pubkey) {
// this.reset(pubkey)
// }
this._ourPubkey = pubkey;
}
setOurFollows(follows: Set<string> | string[]): void {
Expand All @@ -73,6 +76,22 @@ export class WorkerData {
let json = JSON.stringify(a)
return json
}

reset(pubkey:string):void {
this.bloomSize = 0;
this.ourBloom = new BloomFilter(
64 * 256, // bits to allocate.
32 // number of hashes
);
this.ourFollows = new Set()
this.roots = new Set()
this._ourPubkey = pubkey
this.rake.processedThisSession = new Set()
this.rake.words = new Map()
this.roots = new Set()
this.replies = new Map()
this.missingEvents = new Set();
}
latestReplaceable: Map<string, Map<string, NostrEvent>>;
constructor(pubkey?: string) {
this.rake = new RakeWords();
Expand Down
2 changes: 1 addition & 1 deletion src/lib/snort_workers/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ export function updateRepliesInPlace(current: FrontendData | WorkerData) {
let printed = 0;
let printedID = new Set<string>();
for (let [id, e] of current.events) {
if (e.kind == 18100) {console.log(e)}
if (e.kind == 18100) {console.log(117, e)}
if (current instanceof WorkerData) {
current.missingEvents.delete(id);
}
Expand Down
2 changes: 1 addition & 1 deletion src/lib/stores/user.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { NostrEvent } from "nostr-tools";
import { writable, type Writable } from "svelte/store";

export let currentPubkey = writable(undefined)
//export let currentPubkey = writable(undefined)

export let kind0 = writable(new Map<string, NostrEvent>)
9 changes: 8 additions & 1 deletion src/lib/views/messages/Messages.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import MessageInput from '@/components/MessageInput.svelte';
import Input from '@/components/ui/input/input.svelte';
import { currentUser, ndk } from '@/ndk/ndk';
import { PushEvent, FrontendDataStore as feds, viewed } from '@/snort_workers/main';
import { PushEvent, UpdatePubkey, FrontendDataStore as feds, viewed } from '@/snort_workers/main';
import { updateRepliesInPlace } from '@/snort_workers/utils';
import { stableShortList } from '@/stores/shortlist';
import { NDKEvent } from '@nostr-dev-kit/ndk';
Expand Down Expand Up @@ -166,6 +166,13 @@
}
let eventID: string;
currentUser.subscribe(c=>{
if (c && c.pubkey) {
UpdatePubkey(c.pubkey)
}
})
</script>

<div class=" hidden">{$shortListLength}</div>
Expand Down
Loading

0 comments on commit 1308b4b

Please sign in to comment.