mirror of
https://github.com/PR0M3TH3AN/bitvid.git
synced 2026-10-27 07:52:24 +00:00
Add session actor hook for moderation sync
This commit is contained in:
1 parent
eb4cb84909
commit
db2b08378d
2 files changed
+97
No files matched your search
@@ -508,6 +508,53 @@ class Application {
|
||||
}
|
||||
};
|
||||
|
||||
const handleSessionActorReady = async ({ pubkey, reason } = {}) => {
|
||||
if (this.pubkey) {
|
||||
return;
|
||||
}
|
||||
|
||||
const normalizedPubkey = this.normalizeHexPubkey(pubkey);
|
||||
if (!normalizedPubkey) {
|
||||
return;
|
||||
}
|
||||
|
||||
const triggerLabel = reason ? `session-actor-${reason}` : "session-actor";
|
||||
await syncSessionActorBlacklist(triggerLabel);
|
||||
|
||||
const refreshReason = "session-actor-ready";
|
||||
if (typeof this.refreshVisibleModerationUi === "function") {
|
||||
try {
|
||||
this.refreshVisibleModerationUi({ reason: refreshReason });
|
||||
} catch (error) {
|
||||
devLogger.warn(
|
||||
"[app.init()] Failed to refresh moderation UI after session actor:",
|
||||
error,
|
||||
);
|
||||
}
|
||||
} else {
|
||||
this.refreshAllVideoGrids({
|
||||
reason: refreshReason,
|
||||
forceMainReload: true,
|
||||
}).catch((error) => {
|
||||
devLogger.warn(
|
||||
"[app.init()] Failed to refresh video grids after session actor:",
|
||||
error,
|
||||
);
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
if (typeof nostrClient.onSessionActorChange === "function") {
|
||||
nostrClient.onSessionActorChange((detail) => {
|
||||
handleSessionActorReady(detail).catch((error) => {
|
||||
devLogger.warn(
|
||||
"[app.init()] Failed to process session actor change:",
|
||||
error,
|
||||
);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
await syncSessionActorBlacklist("post-refresh");
|
||||
|
||||
if (typeof accessControl.onBlacklistChange === "function") {
|
||||
|
||||
@@ -1247,6 +1247,7 @@ export class NostrClient {
|
||||
this.countUnsupportedRelays = new Set();
|
||||
this.nip46Client = null;
|
||||
this.remoteSignerListeners = new Set();
|
||||
this.sessionActorListeners = new Set();
|
||||
const storedRemoteSigner = this.getStoredNip46Metadata();
|
||||
this.remoteSignerStatus = {
|
||||
state: storedRemoteSigner.hasSession ? "stored" : "idle",
|
||||
@@ -1398,6 +1399,53 @@ export class NostrClient {
|
||||
};
|
||||
}
|
||||
|
||||
emitSessionActorChange(actor, context = {}) {
|
||||
const resolvedActor =
|
||||
actor && typeof actor === "object" ? actor : this.sessionActor;
|
||||
const pubkey =
|
||||
typeof resolvedActor?.pubkey === "string" ? resolvedActor.pubkey.trim() : "";
|
||||
if (!pubkey) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const reason =
|
||||
typeof context.reason === "string" && context.reason.trim()
|
||||
? context.reason.trim()
|
||||
: "";
|
||||
const createdAt = Number.isFinite(resolvedActor?.createdAt)
|
||||
? resolvedActor.createdAt
|
||||
: null;
|
||||
const snapshot = {
|
||||
pubkey,
|
||||
source:
|
||||
typeof resolvedActor?.source === "string" ? resolvedActor.source : "",
|
||||
persisted: resolvedActor?.persisted === true,
|
||||
createdAt,
|
||||
reason,
|
||||
};
|
||||
|
||||
for (const listener of Array.from(this.sessionActorListeners)) {
|
||||
try {
|
||||
listener(snapshot);
|
||||
} catch (error) {
|
||||
devLogger.warn("[nostr] Session actor listener threw:", error);
|
||||
}
|
||||
}
|
||||
|
||||
return snapshot;
|
||||
}
|
||||
|
||||
onSessionActorChange(listener) {
|
||||
if (typeof listener !== "function") {
|
||||
return () => {};
|
||||
}
|
||||
|
||||
this.sessionActorListeners.add(listener);
|
||||
return () => {
|
||||
this.sessionActorListeners.delete(listener);
|
||||
};
|
||||
}
|
||||
|
||||
async createNip46KeyPair(existingPrivateKey = "", existingPublicKey = "") {
|
||||
let privateKey =
|
||||
typeof existingPrivateKey === "string" && existingPrivateKey.trim()
|
||||
@@ -3167,6 +3215,7 @@ export class NostrClient {
|
||||
const restored = this.restoreSessionActorFromStorage();
|
||||
if (restored) {
|
||||
this.sessionActor = restored;
|
||||
this.emitSessionActorChange(restored, { reason: "restored" });
|
||||
return restored.pubkey;
|
||||
}
|
||||
}
|
||||
@@ -3179,6 +3228,7 @@ export class NostrClient {
|
||||
this.sessionActorCipherClosures = null;
|
||||
this.sessionActorCipherClosuresPrivateKey = null;
|
||||
this.persistSessionActor(minted);
|
||||
this.emitSessionActorChange(minted, { reason: "minted" });
|
||||
return minted.pubkey;
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user