@massalabs/gossip-sdk 0.0.2-dev.20260220142001 → 0.0.2-dev.20260223065033

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.
Files changed (73) hide show
  1. package/README.md +26 -3
  2. package/dist/core/SdkEventEmitter.d.ts +2 -0
  3. package/dist/core/SdkEventEmitter.js +2 -0
  4. package/dist/{db.d.ts → db/db.d.ts} +14 -3
  5. package/dist/{db.js → db/db.js} +17 -8
  6. package/dist/db/exec-utils.d.ts +19 -0
  7. package/dist/db/exec-utils.js +48 -0
  8. package/dist/db/generated-ddl.d.ts +1 -0
  9. package/dist/db/generated-ddl.js +31 -0
  10. package/dist/db/index.d.ts +3 -0
  11. package/dist/db/index.js +3 -0
  12. package/dist/db/queries/activeSeekers.d.ts +1 -0
  13. package/dist/{queries → db/queries}/activeSeekers.js +0 -5
  14. package/dist/{queries → db/queries}/messages.d.ts +2 -2
  15. package/dist/{queries → db/queries}/messages.js +3 -3
  16. package/dist/{queries → db/queries}/userProfile.d.ts +1 -1
  17. package/dist/{queries → db/queries}/userProfile.js +1 -1
  18. package/dist/db/schema/_helpers.d.ts +26 -0
  19. package/dist/db/schema/_helpers.js +15 -0
  20. package/dist/db/schema/activeSeekers.d.ts +43 -0
  21. package/dist/db/schema/activeSeekers.js +6 -0
  22. package/dist/db/schema/announcementCursors.d.ts +45 -0
  23. package/dist/db/schema/announcementCursors.js +5 -0
  24. package/dist/db/schema/contacts.d.ts +170 -0
  25. package/dist/db/schema/contacts.js +16 -0
  26. package/dist/db/schema/discussions.d.ts +336 -0
  27. package/dist/db/schema/discussions.js +31 -0
  28. package/dist/db/schema/index.d.ts +8 -0
  29. package/dist/db/schema/index.js +8 -0
  30. package/dist/db/schema/messages.d.ts +309 -0
  31. package/dist/db/schema/messages.js +29 -0
  32. package/dist/db/schema/pendingAnnouncements.d.ts +79 -0
  33. package/dist/db/schema/pendingAnnouncements.js +11 -0
  34. package/dist/db/schema/pendingEncryptedMessages.d.ts +79 -0
  35. package/dist/db/schema/pendingEncryptedMessages.js +11 -0
  36. package/dist/db/schema/userProfile.d.ts +208 -0
  37. package/dist/db/schema/userProfile.js +20 -0
  38. package/dist/{sqlite-worker.js → db/sqlite-worker.js} +2 -30
  39. package/dist/db/sqlite.d.ts +77 -0
  40. package/dist/db/sqlite.js +254 -0
  41. package/dist/gossip.d.ts +1 -41
  42. package/dist/gossip.js +8 -10
  43. package/dist/index.d.ts +3 -3
  44. package/dist/index.js +3 -3
  45. package/dist/services/announcement.js +1 -1
  46. package/dist/services/discussion.js +1 -1
  47. package/dist/services/message.d.ts +1 -1
  48. package/dist/services/message.js +2 -3
  49. package/dist/services/refresh.js +1 -1
  50. package/dist/utils/contacts.d.ts +11 -0
  51. package/dist/utils/contacts.js +30 -2
  52. package/dist/utils/discussions.d.ts +1 -1
  53. package/dist/utils/discussions.js +1 -1
  54. package/dist/utils/validation.js +1 -1
  55. package/package.json +2 -1
  56. package/dist/contacts.d.ts +0 -120
  57. package/dist/contacts.js +0 -160
  58. package/dist/queries/activeSeekers.d.ts +0 -2
  59. package/dist/schema.d.ts +0 -1280
  60. package/dist/schema.js +0 -164
  61. package/dist/sqlite.d.ts +0 -79
  62. package/dist/sqlite.js +0 -448
  63. /package/dist/{queries → db/queries}/announcementCursors.d.ts +0 -0
  64. /package/dist/{queries → db/queries}/announcementCursors.js +0 -0
  65. /package/dist/{queries → db/queries}/contacts.d.ts +0 -0
  66. /package/dist/{queries → db/queries}/contacts.js +0 -0
  67. /package/dist/{queries → db/queries}/discussions.d.ts +0 -0
  68. /package/dist/{queries → db/queries}/discussions.js +0 -0
  69. /package/dist/{queries → db/queries}/index.d.ts +0 -0
  70. /package/dist/{queries → db/queries}/index.js +0 -0
  71. /package/dist/{queries → db/queries}/pendingAnnouncements.d.ts +0 -0
  72. /package/dist/{queries → db/queries}/pendingAnnouncements.js +0 -0
  73. /package/dist/{sqlite-worker.d.ts → db/sqlite-worker.d.ts} +0 -0
package/dist/gossip.js CHANGED
@@ -38,7 +38,6 @@
38
38
  * await gossipSdk.closeSession();
39
39
  * ```
40
40
  */
41
- import { MessageStatus, } from './db';
42
41
  import { toDiscussion, toSortedDiscussions } from './utils/discussions';
43
42
  import { createMessageProtocol } from './api/messageProtocol';
44
43
  import { createAuthProtocol } from './api/authProtocol';
@@ -55,9 +54,8 @@ import { AuthService } from './services/auth';
55
54
  import { validateUserIdFormat, validateUsernameFormat, } from './utils/validation';
56
55
  import { QueueManager } from './utils/queue';
57
56
  import { encodeUserId, decodeUserId } from './utils/userId';
58
- import { initDb } from './sqlite';
59
- import { getMessageById as queryGetMessageById, getMessagesByOwnerAndContact, getMessagesByStatus, updateMessageById, getDiscussionsByOwner, getDiscussionByOwnerAndContact, } from './queries';
60
- import { getContacts, getContact, addContact, updateContactName, deleteContact, } from './contacts';
57
+ import { initDb, getMessageById, getMessagesByOwnerAndContact, getMessagesByStatus, updateMessageById, getDiscussionByOwnerAndContact, getDiscussionsByOwner, getContactsByOwner, getContactByOwnerAndUser, MessageStatus, } from './db';
58
+ import { addContact, updateContactName, deleteContact } from './utils/contacts';
61
59
  import { SdkEventEmitter, SdkEventType, } from './core/SdkEventEmitter';
62
60
  import { SdkPolling } from './core/SdkPolling';
63
61
  export { SdkEventType };
@@ -243,7 +241,7 @@ class GossipSdk {
243
241
  this._announcement.setRefreshService(this._refresh);
244
242
  // Reset any messages stuck in SENDING status to WAITING_SESSION
245
243
  // This handles app crash/close during message send
246
- await this.resetStuckSendingMessages();
244
+ await this.resetStuckSendingMessages(session.userIdEncoded);
247
245
  // Update SDK state to reflect the newly opened session.
248
246
  this.state = {
249
247
  status: SdkStatus.SESSION_OPEN,
@@ -268,7 +266,7 @@ class GossipSdk {
268
266
  createServiceAPIWrappers(session) {
269
267
  this._messagesAPI = {
270
268
  get: async (id) => {
271
- const row = await queryGetMessageById(id);
269
+ const row = await getMessageById(id);
272
270
  return row ? rowToMessage(row) : undefined;
273
271
  },
274
272
  getMessages: async (contactUserId) => {
@@ -304,8 +302,8 @@ class GossipSdk {
304
302
  skipHistorical: () => this._announcement.skipHistoricalAnnouncements(),
305
303
  };
306
304
  this._contactsAPI = {
307
- list: ownerUserId => getContacts(ownerUserId),
308
- get: (ownerUserId, contactUserId) => getContact(ownerUserId, contactUserId),
305
+ list: ownerUserId => getContactsByOwner(ownerUserId),
306
+ get: async (ownerUserId, contactUserId) => (await getContactByOwnerAndUser(ownerUserId, contactUserId)) ?? null,
309
307
  add: (ownerUserId, userId, name, publicKeys) => addContact(ownerUserId, userId, name, publicKeys),
310
308
  updateName: (ownerUserId, contactUserId, newName) => updateContactName(ownerUserId, contactUserId, newName),
311
309
  delete: (ownerUserId, contactUserId) => deleteContact(ownerUserId, contactUserId, session),
@@ -551,9 +549,9 @@ class GossipSdk {
551
549
  *
552
550
  * We also clear encryptedMessage and seeker since they may be stale.
553
551
  */
554
- async resetStuckSendingMessages() {
552
+ async resetStuckSendingMessages(ownerUserId) {
555
553
  try {
556
- const stuck = await getMessagesByStatus(MessageStatus.SENDING);
554
+ const stuck = await getMessagesByStatus(ownerUserId, MessageStatus.SENDING);
557
555
  for (const m of stuck) {
558
556
  await updateMessageById(m.id, {
559
557
  status: MessageStatus.WAITING_SESSION,
package/dist/index.d.ts CHANGED
@@ -21,9 +21,9 @@
21
21
  */
22
22
  export * from './api';
23
23
  export * from './crypto';
24
- export * from './db';
25
24
  export * from './gossip';
26
- export * from './queries';
27
25
  export * from './utils';
28
26
  export * from './wasm';
29
- export { initDb, closeSqlite, getSqliteDb, clearAllTables, clearConversationTables, isSqliteOpen, } from './sqlite';
27
+ export * from './db/db';
28
+ export * from './db/queries';
29
+ export * from './db/sqlite';
package/dist/index.js CHANGED
@@ -21,9 +21,9 @@
21
21
  */
22
22
  export * from './api';
23
23
  export * from './crypto';
24
- export * from './db';
25
24
  export * from './gossip';
26
- export * from './queries';
27
25
  export * from './utils';
28
26
  export * from './wasm';
29
- export { initDb, closeSqlite, getSqliteDb, clearAllTables, clearConversationTables, isSqliteOpen, } from './sqlite';
27
+ export * from './db/db';
28
+ export * from './db/queries';
29
+ export * from './db/sqlite';
@@ -12,7 +12,7 @@ import { Logger } from '../utils/logs';
12
12
  import { defaultSdkConfig } from '../config/sdk';
13
13
  import { decodeAnnouncementPayload } from '../utils/announcementPayload';
14
14
  import { SdkEventType } from '../core/SdkEventEmitter';
15
- import { getContactByOwnerAndUser, insertContact, getContactNamesByPrefix, getDiscussionByOwnerAndContact, getDiscussionById, insertDiscussion, updateDiscussionById, insertMessage, getAnnouncementMessagesByContact, resetSendQueueMessages, getAllPendingAnnouncements, deletePendingAnnouncementsByIds, getAnnouncementCursor, upsertAnnouncementCursor, } from '../queries';
15
+ import { getContactByOwnerAndUser, insertContact, getContactNamesByPrefix, getDiscussionByOwnerAndContact, getDiscussionById, insertDiscussion, updateDiscussionById, insertMessage, getAnnouncementMessagesByContact, resetSendQueueMessages, getAllPendingAnnouncements, deletePendingAnnouncementsByIds, getAnnouncementCursor, upsertAnnouncementCursor, } from '../db';
16
16
  const logger = new Logger('AnnouncementService');
17
17
  export const EstablishSessionError = 'Session manager failed to establish outgoing session';
18
18
  export class AnnouncementService {
@@ -9,7 +9,7 @@ import { encodeAnnouncementPayload, } from '../utils/announcementPayload';
9
9
  import { UserPublicKeys } from '../wasm/bindings';
10
10
  import { Logger } from '../utils/logs';
11
11
  import { SdkEventType } from '../core/SdkEventEmitter';
12
- import { getContactByOwnerAndUser, getDiscussionByOwnerAndContact, getDiscussionById, insertDiscussion, updateDiscussionById, deleteDiscussionById, insertMessage, resetSendQueueMessages, } from '../queries';
12
+ import { getContactByOwnerAndUser, getDiscussionByOwnerAndContact, getDiscussionById, insertDiscussion, updateDiscussionById, deleteDiscussionById, insertMessage, resetSendQueueMessages, } from '../db';
13
13
  const logger = new Logger('DiscussionService');
14
14
  /**
15
15
  * Service for managing discussions between users.
@@ -5,7 +5,7 @@
5
5
  * This service works both in host app contexts and SDK/automation context.
6
6
  */
7
7
  import { type Message } from '../db';
8
- import { type MessageRow } from '../queries';
8
+ import { type MessageRow } from '../db';
9
9
  import { IMessageProtocol } from '../api/messageProtocol';
10
10
  import { SessionModule } from '../wasm';
11
11
  import { Result } from '../utils/type';
@@ -5,9 +5,7 @@
5
5
  * This service works both in host app contexts and SDK/automation context.
6
6
  */
7
7
  import { MessageDirection, MessageStatus, MessageType, MESSAGE_ID_SIZE, } from '../db';
8
- import { getMessageById, getMessageByOwnerAndSeeker, findMessageByMessageId, insertMessage, updateMessageById, deleteDeliveredKeepAliveMessages, getOutgoingSentMessagesByOwner, getWaitingMessageCount as getWaitingCount, getSendQueueMessages, } from '../queries';
9
- import { getDiscussionByOwnerAndContact, updateDiscussionById, incrementUnreadCount, decrementUnreadCount, } from '../queries';
10
- import { replaceActiveSeekers } from '../queries';
8
+ import { getMessageById, getMessageByOwnerAndSeeker, findMessageByMessageId, insertMessage, updateMessageById, deleteDeliveredKeepAliveMessages, getOutgoingSentMessagesByOwner, getWaitingMessageCount as getWaitingCount, getSendQueueMessages, getDiscussionByOwnerAndContact, updateDiscussionById, incrementUnreadCount, decrementUnreadCount, replaceActiveSeekers, } from '../db';
11
9
  import { decodeUserId, encodeUserId } from '../utils/userId';
12
10
  import { SessionStatus } from '../wasm/bindings';
13
11
  import { serializeRegularMessage, serializeReplyMessage, serializeForwardMessage, serializeKeepAliveMessage, deserializeMessage, } from '../utils/messageSerialization';
@@ -204,6 +202,7 @@ export class MessageService {
204
202
  }
205
203
  try {
206
204
  await replaceActiveSeekers(seekers);
205
+ this.eventEmitter.emit(SdkEventType.SEEKERS_UPDATED, seekers);
207
206
  }
208
207
  catch (error) {
209
208
  log.error('failed to update active seekers', error);
@@ -5,7 +5,7 @@
5
5
  * keep-alive messages and broken sessions.
6
6
  */
7
7
  import { MessageType, MessageDirection, MessageStatus } from '../db';
8
- import { getDiscussionsByOwner } from '../queries';
8
+ import { getDiscussionsByOwner } from '../db';
9
9
  import { toSortedDiscussions } from '../utils/discussions';
10
10
  import { SessionStatus } from '../wasm/bindings';
11
11
  import { decodeUserId, encodeUserId } from '../utils/userId';
@@ -4,6 +4,13 @@
4
4
  * Functions for managing contacts including updating names and deleting contacts.
5
5
  */
6
6
  import type { SessionModule } from '../wasm/session';
7
+ import type { UserPublicKeys } from '../wasm/bindings';
8
+ import { type Contact } from '../db';
9
+ export type AddContactResult = {
10
+ success: boolean;
11
+ error?: string;
12
+ contact?: Contact;
13
+ };
7
14
  export type UpdateContactNameResult = {
8
15
  success: true;
9
16
  trimmedName: string;
@@ -37,3 +44,7 @@ export declare function updateContactName(ownerUserId: string, contactUserId: st
37
44
  * @returns Result with success status
38
45
  */
39
46
  export declare function deleteContact(ownerUserId: string, contactUserId: string, session: SessionModule): Promise<DeleteContactResult>;
47
+ /**
48
+ * Add a new contact.
49
+ */
50
+ export declare function addContact(ownerUserId: string, userId: string, name: string, publicKeys: UserPublicKeys): Promise<AddContactResult>;
@@ -4,8 +4,7 @@
4
4
  * Functions for managing contacts including updating names and deleting contacts.
5
5
  */
6
6
  import { decodeUserId } from './userId';
7
- import { getContactsByOwner, getContactByOwnerAndUser, updateContactByOwnerAndUser, deleteContactByOwnerAndUser, deleteDiscussionsByOwnerAndContact, deleteMessagesByOwnerAndContact, } from '../queries';
8
- import { withTransaction } from '../sqlite';
7
+ import { getContactsByOwner, getContactByOwnerAndUser, insertContact as queryInsertContact, updateContactByOwnerAndUser, deleteContactByOwnerAndUser, deleteDiscussionsByOwnerAndContact, deleteMessagesByOwnerAndContact, withTransaction, } from '../db';
9
8
  /**
10
9
  * Update the name of a contact
11
10
  *
@@ -98,3 +97,32 @@ export async function deleteContact(ownerUserId, contactUserId, session) {
98
97
  };
99
98
  }
100
99
  }
100
+ /**
101
+ * Add a new contact.
102
+ */
103
+ export async function addContact(ownerUserId, userId, name, publicKeys) {
104
+ try {
105
+ const existing = await getContactByOwnerAndUser(ownerUserId, userId);
106
+ if (existing) {
107
+ return { success: false, error: 'Contact already exists' };
108
+ }
109
+ await queryInsertContact({
110
+ ownerUserId,
111
+ userId,
112
+ name,
113
+ publicKeys: publicKeys.to_bytes(),
114
+ isOnline: false,
115
+ lastSeen: new Date(),
116
+ createdAt: new Date(),
117
+ });
118
+ const newContact = await getContactByOwnerAndUser(ownerUserId, userId);
119
+ return { success: true, contact: newContact ?? undefined };
120
+ }
121
+ catch (error) {
122
+ console.error('Error adding contact:', error);
123
+ return {
124
+ success: false,
125
+ error: error instanceof Error ? error.message : 'Unknown error',
126
+ };
127
+ }
128
+ }
@@ -4,7 +4,7 @@
4
4
  * Functions for managing discussion metadata.
5
5
  */
6
6
  import { type Discussion } from '../db';
7
- import type { DiscussionRow } from '../queries';
7
+ import type { DiscussionRow } from '../db';
8
8
  /** Convert a Drizzle discussion row to a domain Discussion. */
9
9
  export declare function toDiscussion(row: DiscussionRow): Discussion;
10
10
  /** Convert discussion rows to sorted Discussion[]. Most recent activity first. */
@@ -4,7 +4,7 @@
4
4
  * Functions for managing discussion metadata.
5
5
  */
6
6
  import { rowToDiscussion } from '../db';
7
- import { getDiscussionById, updateDiscussionById } from '../queries';
7
+ import { getDiscussionById, updateDiscussionById } from '../db';
8
8
  /** Convert a Drizzle discussion row to a domain Discussion. */
9
9
  export function toDiscussion(row) {
10
10
  return rowToDiscussion(row);
@@ -4,7 +4,7 @@
4
4
  * Functions for validating user input like usernames, passwords, and user IDs.
5
5
  */
6
6
  import { isValidUserId } from './userId';
7
- import { getUserProfileByUsernameLower } from '../queries';
7
+ import { getUserProfileByUsernameLower } from '../db';
8
8
  /**
9
9
  * Validate a password meets requirements
10
10
  *
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@massalabs/gossip-sdk",
3
- "version": "0.0.2-dev.20260220142001",
3
+ "version": "0.0.2-dev.20260223065033",
4
4
  "description": "Gossip SDK for automation, chatbot, and integration use cases",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -27,6 +27,7 @@
27
27
  "test:run": "vitest run",
28
28
  "test:coverage": "vitest run --coverage",
29
29
  "test:e2e": "vitest run test/e2e/",
30
+ "db:generate": "drizzle-kit generate && node --experimental-strip-types scripts/embed-migrations.ts",
30
31
  "lint": "eslint .",
31
32
  "lint:fix": "eslint . --fix"
32
33
  },
@@ -1,120 +0,0 @@
1
- /**
2
- * Contact Management SDK
3
- *
4
- * Functions for managing contacts including CRUD operations.
5
- *
6
- * @example
7
- * ```typescript
8
- * import { getContacts, addContact, deleteContact } from '@massalabs/gossip-sdk';
9
- *
10
- * // Get all contacts
11
- * const contacts = await getContacts(userId);
12
- *
13
- * // Add a new contact
14
- * const result = await addContact(userId, contactUserId, 'Alice', publicKeys);
15
- *
16
- * // Delete a contact
17
- * await deleteContact(userId, contactUserId);
18
- * ```
19
- */
20
- import { type Contact } from './db';
21
- import type { UpdateContactNameResult, DeleteContactResult } from './utils/contacts';
22
- import type { UserPublicKeys } from './wasm/bindings';
23
- import type { SessionModule } from './wasm/session';
24
- export type { UpdateContactNameResult, DeleteContactResult };
25
- /**
26
- * Get all contacts for an owner.
27
- *
28
- * @param ownerUserId - The user ID of the contact owner
29
- * @returns Array of contacts
30
- *
31
- * @example
32
- * ```typescript
33
- * const contacts = await getContacts(myUserId);
34
- * contacts.forEach(c => console.log(c.name, c.userId));
35
- * ```
36
- */
37
- export declare function getContacts(ownerUserId: string): Promise<Contact[]>;
38
- /**
39
- * Get a specific contact by owner and contact user IDs.
40
- *
41
- * @param ownerUserId - The user ID of the contact owner
42
- * @param contactUserId - The user ID of the contact
43
- * @returns Contact or null if not found
44
- *
45
- * @example
46
- * ```typescript
47
- * const contact = await getContact(myUserId, theirUserId);
48
- * if (contact) {
49
- * console.log('Found contact:', contact.name);
50
- * }
51
- * ```
52
- */
53
- export declare function getContact(ownerUserId: string, contactUserId: string): Promise<Contact | null>;
54
- /**
55
- * Add a new contact.
56
- *
57
- * @param ownerUserId - The user ID of the contact owner
58
- * @param userId - The user ID of the contact (Bech32-encoded)
59
- * @param name - Display name for the contact
60
- * @param publicKeys - The contact's public keys
61
- * @returns Result with success status and optional contact
62
- *
63
- * @example
64
- * ```typescript
65
- * const result = await addContact(
66
- * myUserId,
67
- * 'gossip1abc...',
68
- * 'Alice',
69
- * alicePublicKeys
70
- * );
71
- * if (result.success) {
72
- * console.log('Contact added:', result.contact?.name);
73
- * } else if (result.error === 'Contact already exists') {
74
- * console.log('Contact already exists');
75
- * }
76
- * ```
77
- */
78
- export declare function addContact(ownerUserId: string, userId: string, name: string, publicKeys: UserPublicKeys): Promise<{
79
- success: boolean;
80
- error?: string;
81
- contact?: Contact;
82
- }>;
83
- /**
84
- * Update contact name.
85
- *
86
- * @param ownerUserId - The user ID of the contact owner
87
- * @param contactUserId - The user ID of the contact
88
- * @param newName - New name for the contact
89
- * @returns Result with success status and trimmed name
90
- *
91
- * @example
92
- * ```typescript
93
- * const result = await updateContactName(myUserId, theirUserId, 'Alice Smith');
94
- * if (result.ok) {
95
- * console.log('Updated to:', result.trimmedName);
96
- * } else {
97
- * console.error('Failed:', result.message);
98
- * }
99
- * ```
100
- */
101
- export declare function updateContactName(ownerUserId: string, contactUserId: string, newName: string): Promise<UpdateContactNameResult>;
102
- /**
103
- * Delete a contact and all associated discussions and messages.
104
- *
105
- * @param ownerUserId - The user ID of the contact owner
106
- * @param contactUserId - The user ID of the contact to delete
107
- * @param session - Session module for peer management
108
- * @returns Result with success status
109
- *
110
- * @example
111
- * ```typescript
112
- * const result = await deleteContact(myUserId, theirUserId, session);
113
- * if (result.ok) {
114
- * console.log('Contact deleted');
115
- * } else {
116
- * console.error('Failed:', result.message);
117
- * }
118
- * ```
119
- */
120
- export declare function deleteContact(ownerUserId: string, contactUserId: string, session: SessionModule): Promise<DeleteContactResult>;
package/dist/contacts.js DELETED
@@ -1,160 +0,0 @@
1
- /**
2
- * Contact Management SDK
3
- *
4
- * Functions for managing contacts including CRUD operations.
5
- *
6
- * @example
7
- * ```typescript
8
- * import { getContacts, addContact, deleteContact } from '@massalabs/gossip-sdk';
9
- *
10
- * // Get all contacts
11
- * const contacts = await getContacts(userId);
12
- *
13
- * // Add a new contact
14
- * const result = await addContact(userId, contactUserId, 'Alice', publicKeys);
15
- *
16
- * // Delete a contact
17
- * await deleteContact(userId, contactUserId);
18
- * ```
19
- */
20
- import { updateContactName as updateContactNameUtil, deleteContact as deleteContactUtil, } from './utils/contacts';
21
- import { getContactsByOwner, getContactByOwnerAndUser, insertContact as queryInsertContact, } from './queries';
22
- /**
23
- * Get all contacts for an owner.
24
- *
25
- * @param ownerUserId - The user ID of the contact owner
26
- * @returns Array of contacts
27
- *
28
- * @example
29
- * ```typescript
30
- * const contacts = await getContacts(myUserId);
31
- * contacts.forEach(c => console.log(c.name, c.userId));
32
- * ```
33
- */
34
- export async function getContacts(ownerUserId) {
35
- try {
36
- return await getContactsByOwner(ownerUserId);
37
- }
38
- catch (error) {
39
- console.error('Error getting contacts:', error);
40
- return [];
41
- }
42
- }
43
- /**
44
- * Get a specific contact by owner and contact user IDs.
45
- *
46
- * @param ownerUserId - The user ID of the contact owner
47
- * @param contactUserId - The user ID of the contact
48
- * @returns Contact or null if not found
49
- *
50
- * @example
51
- * ```typescript
52
- * const contact = await getContact(myUserId, theirUserId);
53
- * if (contact) {
54
- * console.log('Found contact:', contact.name);
55
- * }
56
- * ```
57
- */
58
- export async function getContact(ownerUserId, contactUserId) {
59
- try {
60
- const contact = await getContactByOwnerAndUser(ownerUserId, contactUserId);
61
- return contact ?? null;
62
- }
63
- catch (error) {
64
- console.error('Error getting contact:', error);
65
- return null;
66
- }
67
- }
68
- /**
69
- * Add a new contact.
70
- *
71
- * @param ownerUserId - The user ID of the contact owner
72
- * @param userId - The user ID of the contact (Bech32-encoded)
73
- * @param name - Display name for the contact
74
- * @param publicKeys - The contact's public keys
75
- * @returns Result with success status and optional contact
76
- *
77
- * @example
78
- * ```typescript
79
- * const result = await addContact(
80
- * myUserId,
81
- * 'gossip1abc...',
82
- * 'Alice',
83
- * alicePublicKeys
84
- * );
85
- * if (result.success) {
86
- * console.log('Contact added:', result.contact?.name);
87
- * } else if (result.error === 'Contact already exists') {
88
- * console.log('Contact already exists');
89
- * }
90
- * ```
91
- */
92
- export async function addContact(ownerUserId, userId, name, publicKeys) {
93
- try {
94
- // Check if contact already exists
95
- const existing = await getContactByOwnerAndUser(ownerUserId, userId);
96
- if (existing) {
97
- return { success: false, error: 'Contact already exists' };
98
- }
99
- await queryInsertContact({
100
- ownerUserId,
101
- userId,
102
- name,
103
- publicKeys: publicKeys.to_bytes(),
104
- isOnline: false,
105
- lastSeen: new Date(),
106
- createdAt: new Date(),
107
- });
108
- const newContact = await getContactByOwnerAndUser(ownerUserId, userId);
109
- return { success: true, contact: newContact ?? undefined };
110
- }
111
- catch (error) {
112
- console.error('Error adding contact:', error);
113
- return {
114
- success: false,
115
- error: error instanceof Error ? error.message : 'Unknown error',
116
- };
117
- }
118
- }
119
- /**
120
- * Update contact name.
121
- *
122
- * @param ownerUserId - The user ID of the contact owner
123
- * @param contactUserId - The user ID of the contact
124
- * @param newName - New name for the contact
125
- * @returns Result with success status and trimmed name
126
- *
127
- * @example
128
- * ```typescript
129
- * const result = await updateContactName(myUserId, theirUserId, 'Alice Smith');
130
- * if (result.ok) {
131
- * console.log('Updated to:', result.trimmedName);
132
- * } else {
133
- * console.error('Failed:', result.message);
134
- * }
135
- * ```
136
- */
137
- export async function updateContactName(ownerUserId, contactUserId, newName) {
138
- return await updateContactNameUtil(ownerUserId, contactUserId, newName);
139
- }
140
- /**
141
- * Delete a contact and all associated discussions and messages.
142
- *
143
- * @param ownerUserId - The user ID of the contact owner
144
- * @param contactUserId - The user ID of the contact to delete
145
- * @param session - Session module for peer management
146
- * @returns Result with success status
147
- *
148
- * @example
149
- * ```typescript
150
- * const result = await deleteContact(myUserId, theirUserId, session);
151
- * if (result.ok) {
152
- * console.log('Contact deleted');
153
- * } else {
154
- * console.error('Failed:', result.message);
155
- * }
156
- * ```
157
- */
158
- export async function deleteContact(ownerUserId, contactUserId, session) {
159
- return await deleteContactUtil(ownerUserId, contactUserId, session);
160
- }
@@ -1,2 +0,0 @@
1
- export declare function setOnSeekersUpdated(cb: ((seekers: Uint8Array[]) => void) | null): void;
2
- export declare function replaceActiveSeekers(seekers: Uint8Array[]): Promise<void>;