nostr-double-ratchet 0.0.30 → 0.0.32
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/SessionManager.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { NostrPublish, NostrSubscribe, Rumor } from "./types";
|
|
2
2
|
import { Invite } from "./Invite";
|
|
3
3
|
import { StorageAdapter } from "./StorageAdapter";
|
|
4
|
+
export type OnEventCallback = (event: Rumor, from: string) => void;
|
|
4
5
|
export default class SessionManager {
|
|
5
6
|
private userRecords;
|
|
6
7
|
private nostrSubscribe;
|
|
@@ -28,7 +29,7 @@ export default class SessionManager {
|
|
|
28
29
|
listenToUser(userPubkey: string): void;
|
|
29
30
|
stopListeningToUser(userPubkey: string): void;
|
|
30
31
|
private internalSubscriptions;
|
|
31
|
-
onEvent(callback:
|
|
32
|
+
onEvent(callback: OnEventCallback): () => void;
|
|
32
33
|
close(): void;
|
|
33
34
|
/**
|
|
34
35
|
* Accept an invite as our own device, persist the session, and publish the acceptance event.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"SessionManager.d.ts","sourceRoot":"","sources":["../src/SessionManager.ts"],"names":[],"mappings":"AAAA,OAAO,EAAqB,YAAY,EAAE,cAAc,EAAE,KAAK,EAAe,MAAM,SAAS,CAAA;AAE7F,OAAO,EAAE,MAAM,EAAE,MAAM,UAAU,CAAA;AAEjC,OAAO,EAAE,cAAc,EAA0B,MAAM,kBAAkB,CAAA;AAIzE,MAAM,CAAC,OAAO,OAAO,cAAc;IAC/B,OAAO,CAAC,WAAW,CAAqC;IACxD,OAAO,CAAC,cAAc,CAAgB;IACtC,OAAO,CAAC,YAAY,CAAc;IAClC,OAAO,CAAC,cAAc,CAAY;IAClC,OAAO,CAAC,kBAAkB,CAAsC;IAChE,OAAO,CAAC,QAAQ,CAAQ;IACxB,OAAO,CAAC,MAAM,CAAC,CAAQ;IACvB,OAAO,CAAC,OAAO,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I,MAAM;IAOb,QAAQ,CAAC,oBAAoB,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM;IAQnD,SAAS,CAAC,oBAAoB,EAAE,MAAM,EAAE,KAAK,EAAE,OAAO,CAAC,KAAK,CAAC;IA4DnE,YAAY,CAAC,UAAU,EAAE,MAAM;IA8D/B,mBAAmB,CAAC,UAAU,EAAE,MAAM;IAStC,OAAO,CAAC,qBAAqB,CAAkC;IAE/D,OAAO,CAAC,QAAQ,EAAE,eAAe;IAkBjC,KAAK;IAiBL;;;OAGG;IACU,eAAe,CAAC,MAAM,EAAE,MAAM;CAgB9C"}
|
package/package.json
CHANGED
package/src/SessionManager.ts
CHANGED
|
@@ -6,6 +6,8 @@ import { StorageAdapter, InMemoryStorageAdapter } from "./StorageAdapter"
|
|
|
6
6
|
import { serializeSessionState, deserializeSessionState } from "./utils"
|
|
7
7
|
import { Session } from "./Session"
|
|
8
8
|
|
|
9
|
+
export type OnEventCallback = (event: Rumor, from: string) => void
|
|
10
|
+
|
|
9
11
|
export default class SessionManager {
|
|
10
12
|
private userRecords: Map<string, UserRecord> = new Map()
|
|
11
13
|
private nostrSubscribe: NostrSubscribe
|
|
@@ -44,6 +46,7 @@ export default class SessionManager {
|
|
|
44
46
|
* Can be awaited by callers that need deterministic readiness.
|
|
45
47
|
*/
|
|
46
48
|
public async init(): Promise<void> {
|
|
49
|
+
console.log("Initialising SessionManager")
|
|
47
50
|
if (this._initialised) return
|
|
48
51
|
|
|
49
52
|
const ourPublicKey = getPublicKey(this.ourIdentityKey)
|
|
@@ -66,6 +69,13 @@ export default class SessionManager {
|
|
|
66
69
|
}
|
|
67
70
|
this.invite = invite
|
|
68
71
|
|
|
72
|
+
// Publish our own invite
|
|
73
|
+
console.log("Publishing our own invite", invite)
|
|
74
|
+
const event = invite.getEvent()
|
|
75
|
+
this.nostrPublish(event).then((verifiedEvent) => {
|
|
76
|
+
console.log("Invite published", verifiedEvent)
|
|
77
|
+
}).catch((e) => console.error("Failed to publish our own invite", e))
|
|
78
|
+
|
|
69
79
|
// 2b. Listen for acceptances of *our* invite and create sessions
|
|
70
80
|
this.invite.listen(
|
|
71
81
|
this.ourIdentityKey,
|
|
@@ -87,7 +97,7 @@ export default class SessionManager {
|
|
|
87
97
|
this.saveSession(targetUserKey, deviceKey, session)
|
|
88
98
|
|
|
89
99
|
session.onEvent((_event: Rumor) => {
|
|
90
|
-
this.internalSubscriptions.forEach(cb => cb(_event))
|
|
100
|
+
this.internalSubscriptions.forEach(cb => cb(_event, targetUserKey))
|
|
91
101
|
})
|
|
92
102
|
} catch {/* ignore errors */}
|
|
93
103
|
}
|
|
@@ -125,7 +135,7 @@ export default class SessionManager {
|
|
|
125
135
|
this.saveSession(ourPublicKey, deviceId, session)
|
|
126
136
|
|
|
127
137
|
session.onEvent((_event: Rumor) => {
|
|
128
|
-
this.internalSubscriptions.forEach(cb => cb(_event))
|
|
138
|
+
this.internalSubscriptions.forEach(cb => cb(_event, ourPublicKey))
|
|
129
139
|
})
|
|
130
140
|
} catch (err) {
|
|
131
141
|
// eslint-disable-next-line no-console
|
|
@@ -164,7 +174,7 @@ export default class SessionManager {
|
|
|
164
174
|
this.saveSession(ownerPubKey, deviceId, session)
|
|
165
175
|
|
|
166
176
|
session.onEvent((_event: Rumor) => {
|
|
167
|
-
this.internalSubscriptions.forEach(cb => cb(_event))
|
|
177
|
+
this.internalSubscriptions.forEach(cb => cb(_event, ownerPubKey))
|
|
168
178
|
})
|
|
169
179
|
} catch {
|
|
170
180
|
// corrupted entry — ignore
|
|
@@ -201,7 +211,7 @@ export default class SessionManager {
|
|
|
201
211
|
async sendEvent(recipientIdentityKey: string, event: Partial<Rumor>) {
|
|
202
212
|
console.log("Sending event to", recipientIdentityKey, event)
|
|
203
213
|
// Immediately notify local subscribers so that UI can render sent message optimistically
|
|
204
|
-
this.internalSubscriptions.forEach(cb => cb(event as Rumor))
|
|
214
|
+
this.internalSubscriptions.forEach(cb => cb(event as Rumor, recipientIdentityKey))
|
|
205
215
|
|
|
206
216
|
const results = []
|
|
207
217
|
const publishPromises: Promise<any>[] = []
|
|
@@ -292,7 +302,7 @@ export default class SessionManager {
|
|
|
292
302
|
|
|
293
303
|
// Register all existing callbacks on the new session
|
|
294
304
|
session.onEvent((_event: Rumor) => {
|
|
295
|
-
this.internalSubscriptions.forEach(callback => callback(_event))
|
|
305
|
+
this.internalSubscriptions.forEach(callback => callback(_event, userPubkey))
|
|
296
306
|
})
|
|
297
307
|
|
|
298
308
|
const queuedMessages = this.messageQueue.get(userPubkey)
|
|
@@ -329,16 +339,16 @@ export default class SessionManager {
|
|
|
329
339
|
}
|
|
330
340
|
|
|
331
341
|
// Update onEvent to include internalSubscriptions management
|
|
332
|
-
private internalSubscriptions: Set<
|
|
342
|
+
private internalSubscriptions: Set<OnEventCallback> = new Set()
|
|
333
343
|
|
|
334
|
-
onEvent(callback:
|
|
344
|
+
onEvent(callback: OnEventCallback) {
|
|
335
345
|
this.internalSubscriptions.add(callback)
|
|
336
346
|
|
|
337
347
|
// Subscribe to existing sessions
|
|
338
|
-
for (const userRecord of this.userRecords.
|
|
348
|
+
for (const [pubkey, userRecord] of this.userRecords.entries()) {
|
|
339
349
|
for (const session of userRecord.getActiveSessions()) {
|
|
340
|
-
session.onEvent((
|
|
341
|
-
callback(
|
|
350
|
+
session.onEvent((event: Rumor) => {
|
|
351
|
+
callback(event, pubkey)
|
|
342
352
|
})
|
|
343
353
|
}
|
|
344
354
|
}
|