Skip to content

Commit

Permalink
irisdb-nostr 1.0.17, size method update
Browse files Browse the repository at this point in the history
  • Loading branch information
mmalmi committed Sep 10, 2024
1 parent fcc705e commit e4d39f8
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 4 deletions.
3 changes: 2 additions & 1 deletion irisdb-hooks/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"peerDependencies": {
"irisdb": "*",
"react": "*",
"irisdb-nostr": "*"
"irisdb-nostr": "*",
"@nostr-dev-kit/ndk": "*"
}
}
2 changes: 1 addition & 1 deletion irisdb-nostr/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
39 changes: 38 additions & 1 deletion irisdb-nostr/src/SocialGraph/SocialGraph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
}

/**
Expand Down Expand Up @@ -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++) {
Expand All @@ -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();
Expand All @@ -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<string> {
const users = this.usersByFollowDistance.get(distance) || new Set<UID>();
const result = new Set<string>();
for (const user of users) {
result.add(STR(user));
}
return result;
}
}
26 changes: 25 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit e4d39f8

Please sign in to comment.