From e4d39f8bc132c4ae73983d65a37494d845304f6c Mon Sep 17 00:00:00 2001 From: Martti Malmi Date: Tue, 10 Sep 2024 12:08:46 +0300 Subject: [PATCH] irisdb-nostr 1.0.17, size method update --- irisdb-hooks/package.json | 3 +- irisdb-nostr/package.json | 2 +- irisdb-nostr/src/SocialGraph/SocialGraph.ts | 39 ++++++++++++++++++++- package-lock.json | 26 +++++++++++++- 4 files changed, 66 insertions(+), 4 deletions(-) diff --git a/irisdb-hooks/package.json b/irisdb-hooks/package.json index 073ec90..0b663ad 100644 --- a/irisdb-hooks/package.json +++ b/irisdb-hooks/package.json @@ -28,6 +28,7 @@ "peerDependencies": { "irisdb": "*", "react": "*", - "irisdb-nostr": "*" + "irisdb-nostr": "*", + "@nostr-dev-kit/ndk": "*" } } diff --git a/irisdb-nostr/package.json b/irisdb-nostr/package.json index b865dc0..d35551a 100644 --- a/irisdb-nostr/package.json +++ b/irisdb-nostr/package.json @@ -1,6 +1,6 @@ { "name": "irisdb-nostr", - "version": "1.0.16", + "version": "1.0.17", "type": "module", "description": "Nostr adapter for IrisDB", "main": "dist/irisdb-nostr.umd.js", diff --git a/irisdb-nostr/src/SocialGraph/SocialGraph.ts b/irisdb-nostr/src/SocialGraph/SocialGraph.ts index 54b778f..c53848d 100644 --- a/irisdb-nostr/src/SocialGraph/SocialGraph.ts +++ b/irisdb-nostr/src/SocialGraph/SocialGraph.ts @@ -250,7 +250,22 @@ export class SocialGraph { } size() { - return this.followDistanceByUser.size; + let follows = 0; + const sizeByDistance: { [distance: number]: number } = {}; + + for (const followedSet of this.followedByUser.values()) { + follows += followedSet.size; + } + + for (const [distance, users] of this.usersByFollowDistance.entries()) { + sizeByDistance[distance] = users.size; + } + + return { + users: this.followDistanceByUser.size, + follows, + sizeByDistance, + }; } /** @@ -298,6 +313,11 @@ export class SocialGraph { return set; } + /** + * Serialize the social graph to a JSON string. + * @param maxSize Optional maximum number of follow relationships to include in the serialized output. + * @returns A JSON string representing the serialized social graph. + */ serialize(maxSize?: number): string { const pairs: [number, number][] = []; for (let distance = 0; distance <= Math.max(...this.usersByFollowDistance.keys()); distance++) { @@ -315,6 +335,10 @@ export class SocialGraph { return JSON.stringify(pairs); } + /** + * Deserialize a JSON string to reconstruct the social graph. + * @param serialized A JSON string representing the serialized social graph. + */ deserialize(serialized: string): void { this.followDistanceByUser.clear(); this.usersByFollowDistance.clear(); @@ -330,4 +354,17 @@ export class SocialGraph { this.addFollower(followedUser, follower); } } + + /** + * Get the users at a specified follow distance. + * @param distance + */ + getUsersByFollowDistance(distance: number): Set { + const users = this.usersByFollowDistance.get(distance) || new Set(); + const result = new Set(); + for (const user of users) { + result.add(STR(user)); + } + return result; + } } diff --git a/package-lock.json b/package-lock.json index 37e7aec..35b022d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -39,13 +39,14 @@ "version": "1.0.12", "license": "MIT", "peerDependencies": { + "@nostr-dev-kit/ndk": "*", "irisdb": "*", "irisdb-nostr": "*", "react": "*" } }, "irisdb-nostr": { - "version": "1.0.16", + "version": "1.0.17", "license": "MIT", "dependencies": { "@noble/hashes": "^1.5.0", @@ -1208,6 +1209,29 @@ "node": ">= 8" } }, + "node_modules/@nostr-dev-kit/ndk": { + "version": "2.10.0", + "resolved": "https://registry.npmjs.org/@nostr-dev-kit/ndk/-/ndk-2.10.0.tgz", + "integrity": "sha512-TqCAAo6ylORraAXrzRkCGFN2xTMiFbdER8Y8CtUT0HwOpFG/Wn+PBNeDeDmqkl/6LaPdeyXmVwCWj2KcUjIwYA==", + "peer": true, + "dependencies": { + "@noble/curves": "^1.4.0", + "@noble/hashes": "^1.3.1", + "@noble/secp256k1": "^2.0.0", + "@scure/base": "^1.1.1", + "debug": "^4.3.4", + "light-bolt11-decoder": "^3.0.0", + "node-fetch": "^3.3.1", + "nostr-tools": "^2.7.1", + "tseep": "^1.1.1", + "typescript-lru-cache": "^2.0.0", + "utf8-buffer": "^1.0.0", + "websocket-polyfill": "^0.0.3" + }, + "engines": { + "node": ">=16" + } + }, "node_modules/@pkgr/core": { "version": "0.1.1", "resolved": "https://registry.npmjs.org/@pkgr/core/-/core-0.1.1.tgz",