mirror of
https://github.com/PR0M3TH3AN/bitvid.git
synced 2026-11-05 04:12:25 +00:00
Include whitelist in trusted seed bootstrap
This commit is contained in:
1 parent
c3c87b810f
commit
1fc4624596
3 files changed
+134
-6
No files matched your search
@@ -31,6 +31,27 @@ function dedupeNpubs(values) {
|
||||
);
|
||||
}
|
||||
|
||||
function areSetsEqual(first, second) {
|
||||
if (first === second) {
|
||||
return true;
|
||||
}
|
||||
|
||||
const a = first instanceof Set ? first : new Set();
|
||||
const b = second instanceof Set ? second : new Set();
|
||||
|
||||
if (a.size !== b.size) {
|
||||
return false;
|
||||
}
|
||||
|
||||
for (const value of a) {
|
||||
if (!b.has(value)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
function isValidNpub(npub) {
|
||||
if (typeof npub !== "string") {
|
||||
return false;
|
||||
@@ -59,6 +80,7 @@ class AccessControl {
|
||||
this._isRefreshing = false;
|
||||
this._refreshPromise = Promise.resolve();
|
||||
this._hydratedFromCache = false;
|
||||
this._whitelistListeners = new Set();
|
||||
|
||||
this._hydrateFromCache();
|
||||
}
|
||||
@@ -78,11 +100,15 @@ class AccessControl {
|
||||
const whitelist = Array.isArray(state?.whitelist) ? state.whitelist : [];
|
||||
const blacklist = Array.isArray(state?.blacklist) ? state.blacklist : [];
|
||||
|
||||
const previousWhitelist =
|
||||
this.whitelist instanceof Set ? new Set(this.whitelist) : new Set();
|
||||
|
||||
this.editors = new Set(
|
||||
dedupeNpubs([...ADMIN_EDITORS_NPUBS, ...editors])
|
||||
);
|
||||
const normalizedWhitelist = dedupeNpubs(whitelist);
|
||||
this.whitelist = new Set(normalizedWhitelist);
|
||||
const whitelistChanged = !areSetsEqual(previousWhitelist, this.whitelist);
|
||||
|
||||
const blacklistDedupe = dedupeNpubs(blacklist);
|
||||
const whitelistSet = new Set(normalizedWhitelist.map(normalizeNpub));
|
||||
@@ -112,6 +138,28 @@ class AccessControl {
|
||||
this.hasLoaded = true;
|
||||
this._hydratedFromCache = false;
|
||||
}
|
||||
|
||||
if (whitelistChanged) {
|
||||
this._emitWhitelistChange(Array.from(this.whitelist));
|
||||
}
|
||||
}
|
||||
|
||||
_emitWhitelistChange(whitelistValues) {
|
||||
if (!this._whitelistListeners.size) {
|
||||
return;
|
||||
}
|
||||
|
||||
const snapshot = Array.isArray(whitelistValues)
|
||||
? [...whitelistValues]
|
||||
: this.getWhitelist();
|
||||
|
||||
for (const listener of Array.from(this._whitelistListeners)) {
|
||||
try {
|
||||
listener(snapshot);
|
||||
} catch (error) {
|
||||
userLogger.error("accessControl whitelist listener failed", error);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
_performRefresh() {
|
||||
@@ -209,6 +257,18 @@ class AccessControl {
|
||||
return Array.from(this.editors);
|
||||
}
|
||||
|
||||
onWhitelistChange(listener) {
|
||||
if (typeof listener !== "function") {
|
||||
return () => {};
|
||||
}
|
||||
|
||||
this._whitelistListeners.add(listener);
|
||||
|
||||
return () => {
|
||||
this._whitelistListeners.delete(listener);
|
||||
};
|
||||
}
|
||||
|
||||
async addModerator(requestorNpub, moderatorNpub) {
|
||||
if (!this.isSuperAdmin(requestorNpub)) {
|
||||
return { ok: false, error: "forbidden" };
|
||||
|
||||
Reference in new issue
Block a user