nostr-double-ratchet 0.0.37 → 0.0.48

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/src/Session.ts CHANGED
@@ -9,8 +9,11 @@ import {
9
9
  MESSAGE_EVENT_KIND,
10
10
  Rumor,
11
11
  CHAT_MESSAGE_KIND,
12
+ REACTION_KIND,
13
+ RECEIPT_KIND,
14
+ TYPING_KIND,
12
15
  } from "./types";
13
- import { kdf, deepCopyState } from "./utils";
16
+ import { kdf, deepCopyState, createReactionPayload } from "./utils";
14
17
 
15
18
  const MAX_SKIP = 1000;
16
19
 
@@ -93,7 +96,7 @@ export class Session {
93
96
  previousSendingChainMessageCount: 0,
94
97
  skippedKeys: {},
95
98
  };
96
-
99
+
97
100
  const session = new Session(nostrSubscribe, state);
98
101
  if (name) session.name = name;
99
102
  return session;
@@ -113,6 +116,46 @@ export class Session {
113
116
  });
114
117
  }
115
118
 
119
+ /**
120
+ * Sends a reaction to a message through the encrypted session.
121
+ * @param messageId The ID of the message being reacted to
122
+ * @param emoji The emoji or reaction content (e.g., "👍", "❤️", "+1")
123
+ * @returns A verified Nostr event containing the encrypted reaction. You need to publish this event to the Nostr network.
124
+ * @throws Error if we are not the initiator and trying to send the first message
125
+ */
126
+ sendReaction(messageId: string, emoji: string): {event: VerifiedEvent, innerEvent: Rumor} {
127
+ return this.sendEvent({
128
+ content: createReactionPayload(messageId, emoji),
129
+ kind: REACTION_KIND,
130
+ tags: [["e", messageId]]
131
+ });
132
+ }
133
+
134
+ /**
135
+ * Sends a typing indicator through the encrypted session.
136
+ * @returns A verified Nostr event containing the encrypted typing indicator. You need to publish this event to the Nostr network.
137
+ */
138
+ sendTyping(): {event: VerifiedEvent, innerEvent: Rumor} {
139
+ return this.sendEvent({
140
+ content: 'typing',
141
+ kind: TYPING_KIND,
142
+ });
143
+ }
144
+
145
+ /**
146
+ * Sends a delivery/read receipt through the encrypted session.
147
+ * @param receiptType Either "delivered" or "seen"
148
+ * @param messageIds The IDs of the messages being acknowledged
149
+ * @returns A verified Nostr event containing the encrypted receipt. You need to publish this event to the Nostr network.
150
+ */
151
+ sendReceipt(receiptType: 'delivered' | 'seen', messageIds: string[]): {event: VerifiedEvent, innerEvent: Rumor} {
152
+ return this.sendEvent({
153
+ content: receiptType,
154
+ kind: RECEIPT_KIND,
155
+ tags: messageIds.map(id => ["e", id]),
156
+ });
157
+ }
158
+
116
159
  /**
117
160
  * Send a partial Nostr event through the encrypted session.
118
161
  * In addition to chat messages, it could be files, webrtc negotiation or many other types of messages.
@@ -393,7 +436,7 @@ export class Session {
393
436
  this.nostrUnsubscribe = this.nostrSubscribe(
394
437
  {authors: [this.state.theirCurrentNostrPublicKey], kinds: [MESSAGE_EVENT_KIND]},
395
438
  (e) => this.handleNostrEvent(e)
396
- );
439
+ );
397
440
  }
398
441
 
399
442
  const skippedAuthors = Object.keys(this.state.skippedKeys);
@@ -401,7 +444,7 @@ export class Session {
401
444
  this.skippedSubscription = this.nostrSubscribe(
402
445
  {authors: skippedAuthors, kinds: [MESSAGE_EVENT_KIND]},
403
446
  (e) => this.handleNostrEvent(e)
404
- );
447
+ );
405
448
  }
406
449
  }
407
450
  }