New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

nostr-social-graph

Package Overview
Dependencies
Maintainers
0
Versions
15
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

nostr-social-graph - npm Package Compare versions

Comparing version 1.0.6 to 1.0.8

106

dist/nostr-social-graph.es.js

@@ -1,9 +0,9 @@

var u = Object.defineProperty;
var y = (c, t, e) => t in c ? u(c, t, { enumerable: !0, configurable: !0, writable: !0, value: e }) : c[t] = e;
var l = (c, t, e) => y(c, typeof t != "symbol" ? t + "" : t, e);
class a {
var B = Object.defineProperty;
var g = (c, t, e) => t in c ? B(c, t, { enumerable: !0, configurable: !0, writable: !0, value: e }) : c[t] = e;
var h = (c, t, e) => g(c, typeof t != "symbol" ? t + "" : t, e);
class w {
constructor(t) {
l(this, "strToUniqueId", /* @__PURE__ */ new Map());
l(this, "uniqueIdToStr", /* @__PURE__ */ new Map());
l(this, "currentUniqueId", 0);
h(this, "strToUniqueId", /* @__PURE__ */ new Map());
h(this, "uniqueIdToStr", /* @__PURE__ */ new Map());
h(this, "currentUniqueId", 0);
if (t)

@@ -36,15 +36,16 @@ for (const [e, s] of t)

}
const w = /^[0-9a-fA-F]{64}$/;
class g {
const y = /^[0-9a-fA-F]{64}$/;
class m {
constructor(t, e) {
l(this, "root");
l(this, "followDistanceByUser", /* @__PURE__ */ new Map());
l(this, "usersByFollowDistance", /* @__PURE__ */ new Map());
l(this, "followedByUser", /* @__PURE__ */ new Map());
l(this, "followersByUser", /* @__PURE__ */ new Map());
l(this, "followListCreatedAt", /* @__PURE__ */ new Map());
l(this, "mutedByUser", /* @__PURE__ */ new Map());
l(this, "muteListCreatedAt", /* @__PURE__ */ new Map());
l(this, "ids", new a());
this.ids = new a(e && e.uniqueIds), this.root = this.id(t), this.followDistanceByUser.set(this.root, 0), this.usersByFollowDistance.set(0, /* @__PURE__ */ new Set([this.root])), e && this.deserialize(e.followLists);
h(this, "root");
h(this, "followDistanceByUser", /* @__PURE__ */ new Map());
h(this, "usersByFollowDistance", /* @__PURE__ */ new Map());
h(this, "followedByUser", /* @__PURE__ */ new Map());
h(this, "followersByUser", /* @__PURE__ */ new Map());
h(this, "followListCreatedAt", /* @__PURE__ */ new Map());
h(this, "mutedByUser", /* @__PURE__ */ new Map());
h(this, "userMutedBy", /* @__PURE__ */ new Map());
h(this, "muteListCreatedAt", /* @__PURE__ */ new Map());
h(this, "ids", new w());
this.ids = new w(e && e.uniqueIds), this.root = this.id(t), this.followDistanceByUser.set(this.root, 0), this.usersByFollowDistance.set(0, /* @__PURE__ */ new Set([this.root])), e && this.deserialize(e.followLists);
}

@@ -96,3 +97,3 @@ id(t) {

if (n[0] === "p") {
if (!w.test(n[1]))
if (!y.test(n[1]))
continue;

@@ -109,3 +110,3 @@ const d = this.id(n[1]);

handleMuteList(t, e, s) {
var n, d;
var n, d, f, a;
const o = this.muteListCreatedAt.get(e);

@@ -116,14 +117,14 @@ if (o && s <= o)

const i = /* @__PURE__ */ new Set();
for (const h of t.tags)
if (h[0] === "p") {
if (!w.test(h[1]))
for (const l of t.tags)
if (l[0] === "p") {
if (!y.test(l[1]))
continue;
const f = this.id(h[1]);
f !== e && i.add(f);
const u = this.id(l[1]);
u !== e && i.add(u);
}
const r = this.mutedByUser.get(e) || /* @__PURE__ */ new Set();
for (const h of r)
i.has(h) || (n = this.mutedByUser.get(e)) == null || n.delete(h);
for (const h of i)
this.mutedByUser.has(e) || this.mutedByUser.set(e, /* @__PURE__ */ new Set()), (d = this.mutedByUser.get(e)) == null || d.add(h);
for (const l of r)
i.has(l) || ((n = this.mutedByUser.get(e)) == null || n.delete(l), (d = this.userMutedBy.get(l)) == null || d.delete(e));
for (const l of i)
this.mutedByUser.has(e) || this.mutedByUser.set(e, /* @__PURE__ */ new Set()), (f = this.mutedByUser.get(e)) == null || f.add(l), this.userMutedBy.has(l) || this.userMutedBy.set(l, /* @__PURE__ */ new Set()), (a = this.userMutedBy.get(l)) == null || a.add(e);
}

@@ -262,14 +263,29 @@ isFollowing(t, e) {

console.log("size before merge", this.size()), console.time("merge graph");
for (const e of t) {
const s = this.getFollowListCreatedAt(e), o = t.getFollowListCreatedAt(e);
if (!s || o && s < o) {
const i = t.getFollowedByUser(e);
for (const r of i)
this.followedByUser.has(this.id(r)) || this.privateAddFollower(this.id(r), this.id(e));
for (const r of this.followedByUser.get(this.id(e)) || /* @__PURE__ */ new Set())
i.has(this.str(r)) || this.privateRemoveFollower(r, this.id(e));
}
}
for (const e of t)
this.mergeUserLists(
e,
this.followListCreatedAt,
t.followListCreatedAt,
this.followedByUser,
t.followedByUser
), this.mergeUserLists(
e,
this.muteListCreatedAt,
t.muteListCreatedAt,
this.mutedByUser,
t.mutedByUser
);
this.recalculateFollowDistances(), console.timeEnd("merge graph"), console.log("size after merge", this.size());
}
mergeUserLists(t, e, s, o, i) {
const r = this.id(t), n = e.get(r), d = s.get(r);
if (!n || d && n < d) {
const f = i.get(r) || /* @__PURE__ */ new Set(), a = o.get(r) || /* @__PURE__ */ new Set();
for (const l of f)
a.has(l) || (o.has(r) || o.set(r, /* @__PURE__ */ new Set()), o.get(r).add(l));
for (const l of a)
f.has(l) || o.get(r).delete(l);
e.set(r, d ?? 0);
}
}
*userIterator(t) {

@@ -294,6 +310,12 @@ const e = Array.from(this.usersByFollowDistance.keys()).sort((s, o) => s - o);

}
getUserMutedBy(t) {
const e = this.id(t), s = /* @__PURE__ */ new Set();
for (const o of this.userMutedBy.get(e) || [])
s.add(this.str(o));
return s;
}
}
export {
g as SocialGraph,
a as UniqueIds
m as SocialGraph,
w as UniqueIds
};

@@ -1,1 +0,1 @@

(function(d,l){typeof exports=="object"&&typeof module<"u"?l(exports):typeof define=="function"&&define.amd?define(["exports"],l):(d=typeof globalThis<"u"?globalThis:d||self,l(d["nostr-social-graph"]={}))})(this,function(d){"use strict";var B=Object.defineProperty;var g=(d,l,a)=>l in d?B(d,l,{enumerable:!0,configurable:!0,writable:!0,value:a}):d[l]=a;var h=(d,l,a)=>g(d,typeof l!="symbol"?l+"":l,a);class l{constructor(t){h(this,"strToUniqueId",new Map);h(this,"uniqueIdToStr",new Map);h(this,"currentUniqueId",0);if(t)for(const[e,s]of t)this.strToUniqueId.set(e,s),this.uniqueIdToStr.set(s,e),this.currentUniqueId=Math.max(this.currentUniqueId,s+1)}id(t){const e=this.strToUniqueId.get(t);if(e!==void 0)return e;const s=this.currentUniqueId++;return this.strToUniqueId.set(t,s),this.uniqueIdToStr.set(s,t),s}str(t){const e=this.uniqueIdToStr.get(t);if(!e)throw new Error("pub: invalid id "+t);return e}has(t){return this.strToUniqueId.has(t)}serialize(){return Array.from(this.strToUniqueId.entries())}*[Symbol.iterator](){yield*this.uniqueIdToStr.entries()}}const a=/^[0-9a-fA-F]{64}$/;class w{constructor(t,e){h(this,"root");h(this,"followDistanceByUser",new Map);h(this,"usersByFollowDistance",new Map);h(this,"followedByUser",new Map);h(this,"followersByUser",new Map);h(this,"followListCreatedAt",new Map);h(this,"mutedByUser",new Map);h(this,"muteListCreatedAt",new Map);h(this,"ids",new l);this.ids=new l(e&&e.uniqueIds),this.root=this.id(t),this.followDistanceByUser.set(this.root,0),this.usersByFollowDistance.set(0,new Set([this.root])),e&&this.deserialize(e.followLists)}id(t){return this.ids.id(t)}str(t){return this.ids.str(t)}getRoot(){return this.str(this.root)}setRoot(t){const e=this.id(t);e!==this.root&&(this.root=e,this.recalculateFollowDistances())}recalculateFollowDistances(){this.followDistanceByUser.clear(),this.usersByFollowDistance.clear(),this.followDistanceByUser.set(this.root,0),this.usersByFollowDistance.set(0,new Set([this.root]));const t=[this.root];for(;t.length>0;){const e=t.shift(),s=this.followDistanceByUser.get(e),o=this.followedByUser.get(e)||new Set;for(const i of o)if(!this.followDistanceByUser.has(i)){const r=s+1;this.followDistanceByUser.set(i,r),this.usersByFollowDistance.has(r)||this.usersByFollowDistance.set(r,new Set),this.usersByFollowDistance.get(r).add(i),t.push(i)}}}handleEvent(t){const e=(Array.isArray(t)?t:[t]).filter(s=>[3,1e4].includes(s.kind));for(const s of e){const o=s.created_at;if(o>Math.floor(Date.now()/1e3)+10*60){console.debug("event.created_at more than 10 minutes in the future",s);continue}const i=this.id(s.pubkey);s.kind===3?this.handleFollowList(s,i,o):s.kind===1e4&&this.handleMuteList(s,i,o)}}handleFollowList(t,e,s){const o=this.followListCreatedAt.get(e);if(o&&s<=o)return;this.followListCreatedAt.set(e,s);const i=new Set;for(const n of t.tags)if(n[0]==="p"){if(!a.test(n[1]))continue;const f=this.id(n[1]);f!==e&&i.add(f)}const r=this.followedByUser.get(e)||new Set;for(const n of r)i.has(n)||this.privateRemoveFollower(n,e);for(const n of i)this.privateAddFollower(n,e)}handleMuteList(t,e,s){var n,f;const o=this.muteListCreatedAt.get(e);if(o&&s<=o)return;this.muteListCreatedAt.set(e,s);const i=new Set;for(const c of t.tags)if(c[0]==="p"){if(!a.test(c[1]))continue;const u=this.id(c[1]);u!==e&&i.add(u)}const r=this.mutedByUser.get(e)||new Set;for(const c of r)i.has(c)||(n=this.mutedByUser.get(e))==null||n.delete(c);for(const c of i)this.mutedByUser.has(e)||this.mutedByUser.set(e,new Set),(f=this.mutedByUser.get(e))==null||f.add(c)}isFollowing(t,e){var i;const s=this.id(e),o=this.id(t);return!!((i=this.followedByUser.get(o))!=null&&i.has(s))}getFollowDistance(t){const e=this.followDistanceByUser.get(this.id(t));return e===void 0?1e3:e}addUserByFollowDistance(t,e){var s,o;this.usersByFollowDistance.has(t)||this.usersByFollowDistance.set(t,new Set),(s=this.usersByFollowDistance.get(t))==null||s.add(e);for(const i of this.usersByFollowDistance.keys())i>t&&((o=this.usersByFollowDistance.get(i))==null||o.delete(e))}privateAddFollower(t,e){var s,o;if(typeof t!="number"||typeof e!="number")throw new Error("Invalid user id");if(this.followersByUser.has(t)||this.followersByUser.set(t,new Set),(s=this.followersByUser.get(t))==null||s.add(e),this.followedByUser.has(e)||this.followedByUser.set(e,new Set),t!==this.root){let i;if(e===this.root)i=1,this.addUserByFollowDistance(i,t),this.followDistanceByUser.set(t,i);else{const r=this.followDistanceByUser.get(t),n=this.followDistanceByUser.get(e);i=n&&n+1,(r===void 0||i&&i<r)&&(this.followDistanceByUser.set(t,i),this.addUserByFollowDistance(i,t))}}(o=this.followedByUser.get(e))==null||o.add(t)}addFollower(t,e){this.privateAddFollower(this.id(e),this.id(t))}removeFollower(t,e){this.privateRemoveFollower(this.id(e),this.id(t))}privateRemoveFollower(t,e){var o,i;if((o=this.followersByUser.get(t))==null||o.delete(e),(i=this.followedByUser.get(e))==null||i.delete(t),t===this.root)return;let s=1/0;for(const r of this.followersByUser.get(t)||[]){const n=this.followDistanceByUser.get(r);n!==void 0&&n+1<s&&(s=n+1)}s===1/0?this.followDistanceByUser.delete(t):this.followDistanceByUser.set(t,s)}followerCount(t){var s;const e=this.id(t);return((s=this.followersByUser.get(e))==null?void 0:s.size)??0}followedByFriendsCount(t){var o;let e=0;const s=this.id(t);for(const i of this.followersByUser.get(s)??[])(o=this.followedByUser.get(this.root))!=null&&o.has(i)&&e++;return e}size(){let t=0,e=0;const s={};for(const o of this.followedByUser.values())t+=o.size;for(const o of this.mutedByUser.values())e+=o.size;for(const[o,i]of this.usersByFollowDistance.entries())s[o]=i.size;return{users:this.followDistanceByUser.size,follows:t,mutes:e,sizeByDistance:s}}followedByFriends(t){var o;const e=this.id(t),s=new Set;for(const i of this.followersByUser.get(e)??[])(o=this.followedByUser.get(this.root))!=null&&o.has(i)&&s.add(this.str(i));return s}getFollowedByUser(t,e=!1){const s=this.id(t),o=new Set;for(const i of this.followedByUser.get(s)||[])o.add(this.str(i));return e&&o.add(t),o}getFollowersByUser(t){const e=this.id(t),s=new Set;for(const o of this.followersByUser.get(e)||[])s.add(this.str(o));return s}serialize(t){const e=[],s=[];for(const[o,i]of this.followedByUser){const r=this.followListCreatedAt.get(o);if(!r)continue;if(e.push([o,[...i.values()],r]),t&&e.length>=t)return{followLists:e,uniqueIds:this.ids.serialize()};const n=this.muteListCreatedAt.get(o),f=this.mutedByUser.get(o);n&&f&&s.push([o,[...f.values()],n])}return{followLists:e,uniqueIds:this.ids.serialize(),muteLists:s}}deserialize(t,e){var o;const s=(o=t[0])==null?void 0:o[0];for(const[i,r,n]of t){for(const f of r)this.privateAddFollower(f,i);this.followListCreatedAt.set(i,n??0)}if(e)for(const[i,r,n]of e)this.mutedByUser.set(i,new Set(r)),this.muteListCreatedAt.set(i,n??0);s!==this.root&&this.recalculateFollowDistances()}getUsersByFollowDistance(t){const e=this.usersByFollowDistance.get(t)||new Set,s=new Set;for(const o of e)s.add(this.str(o));return s}getFollowListCreatedAt(t){return this.followListCreatedAt.get(this.id(t))}merge(t){console.log("size before merge",this.size()),console.time("merge graph");for(const e of t){const s=this.getFollowListCreatedAt(e),o=t.getFollowListCreatedAt(e);if(!s||o&&s<o){const i=t.getFollowedByUser(e);for(const r of i)this.followedByUser.has(this.id(r))||this.privateAddFollower(this.id(r),this.id(e));for(const r of this.followedByUser.get(this.id(e))||new Set)i.has(this.str(r))||this.privateRemoveFollower(r,this.id(e))}}this.recalculateFollowDistances(),console.timeEnd("merge graph"),console.log("size after merge",this.size())}*userIterator(t){const e=Array.from(this.usersByFollowDistance.keys()).sort((s,o)=>s-o);for(const s of e){if(t!==void 0&&s>t)break;const o=this.usersByFollowDistance.get(s)||new Set;for(const i of o)yield this.str(i)}}[Symbol.iterator](){return this.userIterator()}getMutedByUser(t){const e=this.id(t),s=new Set;for(const o of this.mutedByUser.get(e)||[])s.add(this.str(o));return s}}d.SocialGraph=w,d.UniqueIds=l,Object.defineProperty(d,Symbol.toStringTag,{value:"Module"})});
(function(c,d){typeof exports=="object"&&typeof module<"u"?d(exports):typeof define=="function"&&define.amd?define(["exports"],d):(c=typeof globalThis<"u"?globalThis:c||self,d(c["nostr-social-graph"]={}))})(this,function(c){"use strict";var U=Object.defineProperty;var m=(c,d,a)=>d in c?U(c,d,{enumerable:!0,configurable:!0,writable:!0,value:a}):c[d]=a;var f=(c,d,a)=>m(c,typeof d!="symbol"?d+"":d,a);class d{constructor(t){f(this,"strToUniqueId",new Map);f(this,"uniqueIdToStr",new Map);f(this,"currentUniqueId",0);if(t)for(const[e,s]of t)this.strToUniqueId.set(e,s),this.uniqueIdToStr.set(s,e),this.currentUniqueId=Math.max(this.currentUniqueId,s+1)}id(t){const e=this.strToUniqueId.get(t);if(e!==void 0)return e;const s=this.currentUniqueId++;return this.strToUniqueId.set(t,s),this.uniqueIdToStr.set(s,t),s}str(t){const e=this.uniqueIdToStr.get(t);if(!e)throw new Error("pub: invalid id "+t);return e}has(t){return this.strToUniqueId.has(t)}serialize(){return Array.from(this.strToUniqueId.entries())}*[Symbol.iterator](){yield*this.uniqueIdToStr.entries()}}const a=/^[0-9a-fA-F]{64}$/;class B{constructor(t,e){f(this,"root");f(this,"followDistanceByUser",new Map);f(this,"usersByFollowDistance",new Map);f(this,"followedByUser",new Map);f(this,"followersByUser",new Map);f(this,"followListCreatedAt",new Map);f(this,"mutedByUser",new Map);f(this,"userMutedBy",new Map);f(this,"muteListCreatedAt",new Map);f(this,"ids",new d);this.ids=new d(e&&e.uniqueIds),this.root=this.id(t),this.followDistanceByUser.set(this.root,0),this.usersByFollowDistance.set(0,new Set([this.root])),e&&this.deserialize(e.followLists)}id(t){return this.ids.id(t)}str(t){return this.ids.str(t)}getRoot(){return this.str(this.root)}setRoot(t){const e=this.id(t);e!==this.root&&(this.root=e,this.recalculateFollowDistances())}recalculateFollowDistances(){this.followDistanceByUser.clear(),this.usersByFollowDistance.clear(),this.followDistanceByUser.set(this.root,0),this.usersByFollowDistance.set(0,new Set([this.root]));const t=[this.root];for(;t.length>0;){const e=t.shift(),s=this.followDistanceByUser.get(e),o=this.followedByUser.get(e)||new Set;for(const i of o)if(!this.followDistanceByUser.has(i)){const r=s+1;this.followDistanceByUser.set(i,r),this.usersByFollowDistance.has(r)||this.usersByFollowDistance.set(r,new Set),this.usersByFollowDistance.get(r).add(i),t.push(i)}}}handleEvent(t){const e=(Array.isArray(t)?t:[t]).filter(s=>[3,1e4].includes(s.kind));for(const s of e){const o=s.created_at;if(o>Math.floor(Date.now()/1e3)+10*60){console.debug("event.created_at more than 10 minutes in the future",s);continue}const i=this.id(s.pubkey);s.kind===3?this.handleFollowList(s,i,o):s.kind===1e4&&this.handleMuteList(s,i,o)}}handleFollowList(t,e,s){const o=this.followListCreatedAt.get(e);if(o&&s<=o)return;this.followListCreatedAt.set(e,s);const i=new Set;for(const n of t.tags)if(n[0]==="p"){if(!a.test(n[1]))continue;const h=this.id(n[1]);h!==e&&i.add(h)}const r=this.followedByUser.get(e)||new Set;for(const n of r)i.has(n)||this.privateRemoveFollower(n,e);for(const n of i)this.privateAddFollower(n,e)}handleMuteList(t,e,s){var n,h,u,w;const o=this.muteListCreatedAt.get(e);if(o&&s<=o)return;this.muteListCreatedAt.set(e,s);const i=new Set;for(const l of t.tags)if(l[0]==="p"){if(!a.test(l[1]))continue;const y=this.id(l[1]);y!==e&&i.add(y)}const r=this.mutedByUser.get(e)||new Set;for(const l of r)i.has(l)||((n=this.mutedByUser.get(e))==null||n.delete(l),(h=this.userMutedBy.get(l))==null||h.delete(e));for(const l of i)this.mutedByUser.has(e)||this.mutedByUser.set(e,new Set),(u=this.mutedByUser.get(e))==null||u.add(l),this.userMutedBy.has(l)||this.userMutedBy.set(l,new Set),(w=this.userMutedBy.get(l))==null||w.add(e)}isFollowing(t,e){var i;const s=this.id(e),o=this.id(t);return!!((i=this.followedByUser.get(o))!=null&&i.has(s))}getFollowDistance(t){const e=this.followDistanceByUser.get(this.id(t));return e===void 0?1e3:e}addUserByFollowDistance(t,e){var s,o;this.usersByFollowDistance.has(t)||this.usersByFollowDistance.set(t,new Set),(s=this.usersByFollowDistance.get(t))==null||s.add(e);for(const i of this.usersByFollowDistance.keys())i>t&&((o=this.usersByFollowDistance.get(i))==null||o.delete(e))}privateAddFollower(t,e){var s,o;if(typeof t!="number"||typeof e!="number")throw new Error("Invalid user id");if(this.followersByUser.has(t)||this.followersByUser.set(t,new Set),(s=this.followersByUser.get(t))==null||s.add(e),this.followedByUser.has(e)||this.followedByUser.set(e,new Set),t!==this.root){let i;if(e===this.root)i=1,this.addUserByFollowDistance(i,t),this.followDistanceByUser.set(t,i);else{const r=this.followDistanceByUser.get(t),n=this.followDistanceByUser.get(e);i=n&&n+1,(r===void 0||i&&i<r)&&(this.followDistanceByUser.set(t,i),this.addUserByFollowDistance(i,t))}}(o=this.followedByUser.get(e))==null||o.add(t)}addFollower(t,e){this.privateAddFollower(this.id(e),this.id(t))}removeFollower(t,e){this.privateRemoveFollower(this.id(e),this.id(t))}privateRemoveFollower(t,e){var o,i;if((o=this.followersByUser.get(t))==null||o.delete(e),(i=this.followedByUser.get(e))==null||i.delete(t),t===this.root)return;let s=1/0;for(const r of this.followersByUser.get(t)||[]){const n=this.followDistanceByUser.get(r);n!==void 0&&n+1<s&&(s=n+1)}s===1/0?this.followDistanceByUser.delete(t):this.followDistanceByUser.set(t,s)}followerCount(t){var s;const e=this.id(t);return((s=this.followersByUser.get(e))==null?void 0:s.size)??0}followedByFriendsCount(t){var o;let e=0;const s=this.id(t);for(const i of this.followersByUser.get(s)??[])(o=this.followedByUser.get(this.root))!=null&&o.has(i)&&e++;return e}size(){let t=0,e=0;const s={};for(const o of this.followedByUser.values())t+=o.size;for(const o of this.mutedByUser.values())e+=o.size;for(const[o,i]of this.usersByFollowDistance.entries())s[o]=i.size;return{users:this.followDistanceByUser.size,follows:t,mutes:e,sizeByDistance:s}}followedByFriends(t){var o;const e=this.id(t),s=new Set;for(const i of this.followersByUser.get(e)??[])(o=this.followedByUser.get(this.root))!=null&&o.has(i)&&s.add(this.str(i));return s}getFollowedByUser(t,e=!1){const s=this.id(t),o=new Set;for(const i of this.followedByUser.get(s)||[])o.add(this.str(i));return e&&o.add(t),o}getFollowersByUser(t){const e=this.id(t),s=new Set;for(const o of this.followersByUser.get(e)||[])s.add(this.str(o));return s}serialize(t){const e=[],s=[];for(const[o,i]of this.followedByUser){const r=this.followListCreatedAt.get(o);if(!r)continue;if(e.push([o,[...i.values()],r]),t&&e.length>=t)return{followLists:e,uniqueIds:this.ids.serialize()};const n=this.muteListCreatedAt.get(o),h=this.mutedByUser.get(o);n&&h&&s.push([o,[...h.values()],n])}return{followLists:e,uniqueIds:this.ids.serialize(),muteLists:s}}deserialize(t,e){var o;const s=(o=t[0])==null?void 0:o[0];for(const[i,r,n]of t){for(const h of r)this.privateAddFollower(h,i);this.followListCreatedAt.set(i,n??0)}if(e)for(const[i,r,n]of e)this.mutedByUser.set(i,new Set(r)),this.muteListCreatedAt.set(i,n??0);s!==this.root&&this.recalculateFollowDistances()}getUsersByFollowDistance(t){const e=this.usersByFollowDistance.get(t)||new Set,s=new Set;for(const o of e)s.add(this.str(o));return s}getFollowListCreatedAt(t){return this.followListCreatedAt.get(this.id(t))}merge(t){console.log("size before merge",this.size()),console.time("merge graph");for(const e of t)this.mergeUserLists(e,this.followListCreatedAt,t.followListCreatedAt,this.followedByUser,t.followedByUser),this.mergeUserLists(e,this.muteListCreatedAt,t.muteListCreatedAt,this.mutedByUser,t.mutedByUser);this.recalculateFollowDistances(),console.timeEnd("merge graph"),console.log("size after merge",this.size())}mergeUserLists(t,e,s,o,i){const r=this.id(t),n=e.get(r),h=s.get(r);if(!n||h&&n<h){const u=i.get(r)||new Set,w=o.get(r)||new Set;for(const l of u)w.has(l)||(o.has(r)||o.set(r,new Set),o.get(r).add(l));for(const l of w)u.has(l)||o.get(r).delete(l);e.set(r,h??0)}}*userIterator(t){const e=Array.from(this.usersByFollowDistance.keys()).sort((s,o)=>s-o);for(const s of e){if(t!==void 0&&s>t)break;const o=this.usersByFollowDistance.get(s)||new Set;for(const i of o)yield this.str(i)}}[Symbol.iterator](){return this.userIterator()}getMutedByUser(t){const e=this.id(t),s=new Set;for(const o of this.mutedByUser.get(e)||[])s.add(this.str(o));return s}getUserMutedBy(t){const e=this.id(t),s=new Set;for(const o of this.userMutedBy.get(e)||[])s.add(this.str(o));return s}}c.SocialGraph=B,c.UniqueIds=d,Object.defineProperty(c,Symbol.toStringTag,{value:"Module"})});

@@ -17,2 +17,3 @@ import { SerializedUniqueIds } from './UniqueIds';

private mutedByUser;
private userMutedBy;
private muteListCreatedAt;

@@ -54,6 +55,8 @@ private ids;

merge(other: SocialGraph): void;
private mergeUserLists;
userIterator(upToDistance?: number): Generator<string>;
[Symbol.iterator](): Generator<string>;
getMutedByUser(user: string): Set<string>;
getUserMutedBy(user: string): Set<string>;
}
//# sourceMappingURL=SocialGraph.d.ts.map
{
"name": "nostr-social-graph",
"version": "1.0.6",
"version": "1.0.8",
"type": "module",

@@ -5,0 +5,0 @@ "packageManager": "yarn@1.22.22",

@@ -20,2 +20,3 @@ import { SerializedUniqueIds, UniqueIds } from './UniqueIds';

private mutedByUser = new Map<number, Set<number>>();
private userMutedBy = new Map<number, Set<number>>();
private muteListCreatedAt = new Map<number, number>()

@@ -156,2 +157,3 @@ private ids = new UniqueIds();

this.mutedByUser.get(author)?.delete(user);
this.userMutedBy.get(user)?.delete(author);
}

@@ -165,2 +167,7 @@ }

this.mutedByUser.get(author)?.add(user);
if (!this.userMutedBy.has(user)) {
this.userMutedBy.set(user, new Set<number>());
}
this.userMutedBy.get(user)?.add(author);
}

@@ -389,24 +396,58 @@ }

merge(other: SocialGraph) {
console.log('size before merge', this.size())
console.time('merge graph')
console.log('size before merge', this.size());
console.time('merge graph');
for (const user of other) {
const ourCreatedAt = this.getFollowListCreatedAt(user)
const theirCreatedAt = other.getFollowListCreatedAt(user)
if (!ourCreatedAt || (theirCreatedAt && ourCreatedAt < theirCreatedAt)) {
const newFollows = other.getFollowedByUser(user)
for (const follow of newFollows) {
if (!this.followedByUser.has(this.id(follow))) {
this.privateAddFollower(this.id(follow), this.id(user))
this.mergeUserLists(
user,
this.followListCreatedAt,
other.followListCreatedAt,
this.followedByUser,
other.followedByUser
);
this.mergeUserLists(
user,
this.muteListCreatedAt,
other.muteListCreatedAt,
this.mutedByUser,
other.mutedByUser
);
}
this.recalculateFollowDistances();
console.timeEnd('merge graph');
console.log('size after merge', this.size());
}
private mergeUserLists(
user: string,
ourCreatedAtMap: Map<number, number>,
theirCreatedAtMap: Map<number, number>,
ourUserMap: Map<number, Set<number>>,
theirUserMap: Map<number, Set<number>>
) {
const userId = this.id(user);
const ourCreatedAt = ourCreatedAtMap.get(userId);
const theirCreatedAt = theirCreatedAtMap.get(userId);
if (!ourCreatedAt || (theirCreatedAt && ourCreatedAt < theirCreatedAt)) {
const newUsers = theirUserMap.get(userId) || new Set<number>();
const currentUsers = ourUserMap.get(userId) || new Set<number>();
for (const newUser of newUsers) {
if (!currentUsers.has(newUser)) {
if (!ourUserMap.has(userId)) {
ourUserMap.set(userId, new Set<number>());
}
ourUserMap.get(userId)!.add(newUser);
}
for (const follow of this.followedByUser.get(this.id(user)) || new Set()) {
if (!newFollows.has(this.str(follow))) {
this.privateRemoveFollower(follow, this.id(user))
}
}
for (const currentUser of currentUsers) {
if (!newUsers.has(currentUser)) {
ourUserMap.get(userId)!.delete(currentUser);
}
}
ourCreatedAtMap.set(userId, theirCreatedAt ?? 0);
}
this.recalculateFollowDistances()
console.timeEnd('merge graph')
console.log('size after merge', this.size())
}

@@ -439,2 +480,11 @@

}
getUserMutedBy(user: string): Set<string> {
const userId = this.id(user);
const set = new Set<string>();
for (const id of this.userMutedBy.get(userId) || []) {
set.add(this.str(id));
}
return set;
}
}

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc