@talkjs/core 1.6.3 → 1.8.0

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.
@@ -587,6 +587,67 @@ export declare interface ConversationRef {
587
587
  markAsTyping(): Promise<void>;
588
588
  }
589
589
 
590
+ /**
591
+ * Search object returned by `TalkSession.searchConversations` functions.
592
+ * Used to search for conversations the user is in.
593
+ * @public
594
+ */
595
+ export declare interface ConversationSearch {
596
+ /**
597
+ * Sets the query string for this search and starts searching for conversations whose `subject` or `custom.search` match the query.
598
+ *
599
+ * @remarks
600
+ * This resets the search to its initial state, clearing all previous results.
601
+ * Initially loads 3 results. Call {@link MessageSearch.loadMore} to load additional results.
602
+ *
603
+ * The query matches a conversation when, for each word in the query, that word appears in the conversation's subject or in `conversation.custom.search`.
604
+ * This means one of the words in the string starts with the word from the query.
605
+ * This is case-insensitive and punctuation at the start / end of words is ignored (in both the string and the query).
606
+ *
607
+ * A conversation titled `I like to look at rainbows`, will appear in searches for `rainbows`, `rain`, `look at`, `(look, i *LIKE* rain)`, and `look look look!`.
608
+ * It will not appear in searches for `bows`, `rain-bows`, `look-at`, or `I like to look at rainbows too`.
609
+ *
610
+ * It is not possible to match both a conversation's subject and its `custom.search` field at the same time.
611
+ * A conversation called `Kittens` with `custom.search: "where to buy food"` will not appear in a search for `kitten food`.
612
+ * However, this only applies per-conversation. If there is one conversation with subject `Kittens` and a second conversation with `custom.search: "Kittens"`, then a search for `kittens` will return both conversations.
613
+ *
614
+ * @returns A promise that resolves with the complete first page of results. The promise rejects if the search encounters an error.
615
+ */
616
+ setQuery(query: string): Promise<ConversationSearchState>;
617
+ /**
618
+ * Continues searching for more conversations.
619
+ *
620
+ * @remarks
621
+ * You can only call `loadMore` when the search has a query set, and is idle.
622
+ * Calling `loadMore` before calling `setQuery`, while the `setQuery` promise is still pending, or while another `loadMore` promise is pending, has no effect.
623
+ *
624
+ * @param limit - The number of results to load. `limit` should be between 1 and 50. Default 10.
625
+ * @returns A promise that resolves once finished loading, containing all the results from this page and all previous pages. The promise rejects if the search encounters an error.
626
+ */
627
+ loadMore(limit?: number): Promise<ConversationSearchState>;
628
+ }
629
+
630
+ /**
631
+ * The state of a conversation search in the current step.
632
+ *
633
+ * @public
634
+ */
635
+ export declare interface ConversationSearchState {
636
+ /**
637
+ * The conversations that were found in the search.
638
+ */
639
+ results: ConversationSnapshot[];
640
+ /**
641
+ * Whether all results have been loaded.
642
+ *
643
+ * @remarks
644
+ * Note: When true, this means that the search has reached the oldest matching conversation and no more conversations can be loaded.
645
+ * However, it does not mean that `results` contains every matching conversation.
646
+ * Any conversations that received messages since you called `setQuery`, may be missing from `results`.
647
+ */
648
+ loadedAll: boolean;
649
+ }
650
+
590
651
  /**
591
652
  * A snapshot of a conversation's attributes at a given moment in time.
592
653
  *
@@ -1341,6 +1402,66 @@ export declare interface MessageRef {
1341
1402
  delete(): Promise<void>;
1342
1403
  }
1343
1404
 
1405
+ /**
1406
+ * Search object returned by `TalkSession.searchMessages`.
1407
+ * Used to search for messages in the current user's conversations.
1408
+ *
1409
+ * @public
1410
+ */
1411
+ export declare interface MessageSearch {
1412
+ /**
1413
+ * Sets the query string for this search and starts searching for messages matching the query.
1414
+ *
1415
+ * @remarks
1416
+ * This resets the search to its initial state, clearing all previous results.
1417
+ * Initially loads 3 results. Call {@link MessageSearch.loadMore} to load additional results.
1418
+ *
1419
+ * The query matches a message when, for each word in the query, that word appears in the message text.
1420
+ * This means one of the words in the string starts with the word from the query.
1421
+ * This is case-insensitive and punctuation at the start / end of words is ignored (in both the string and the query).
1422
+ *
1423
+ * Non-text messages (such as uploaded images) are never returned by search.
1424
+ *
1425
+ * A message saying `I like to look at rainbows`, will appear in searches for `rainbows`, `rain`, `look at`, `(look, i *LIKE* rain)`, and `look look look!`.
1426
+ * It will not appear in searches for `bows`, `rain-bows`, `look-at`, or `I like to look at rainbows too`.
1427
+ *
1428
+ * @returns A promise that resolves with the complete first page of results. The promise rejects if the search encounters an error.
1429
+ */
1430
+ setQuery(query: string): Promise<MessageSearchState>;
1431
+ /**
1432
+ * Continues searching for more messages.
1433
+ *
1434
+ * @remarks
1435
+ * You can only call `loadMore` when the search has a query set, and is idle.
1436
+ * Calling `loadMore` before calling `setQuery`, while the `setQuery` promise is still pending, or while another `loadMore` promise is pending, has no effect.
1437
+ *
1438
+ * @param limit - The number of results to load. `limit` should be between 1 and 50. Default 10.
1439
+ * @returns A promise that resolves once finished loading, containing all the results from this page and all previous pages. The promise rejects if the search encounters an error.
1440
+ */
1441
+ loadMore(limit?: number): Promise<MessageSearchState>;
1442
+ }
1443
+
1444
+ /**
1445
+ * The state of a message search in the current step.
1446
+ *
1447
+ * @public
1448
+ */
1449
+ export declare interface MessageSearchState {
1450
+ /**
1451
+ * Contains all messages found by the search. Each message is paired up with the conversation it is from.
1452
+ */
1453
+ results: SearchMessageSnapshot[];
1454
+ /**
1455
+ * Whether all results have been loaded.
1456
+ *
1457
+ * @remarks
1458
+ * Note: When true, this means that the search has reached the oldest matching message and no more messages can be loaded.
1459
+ * However, it does not mean that `results` contains every matching message.
1460
+ * Any messages sent after you called `setQuery` will be missing from `results`.
1461
+ */
1462
+ loadedAll: boolean;
1463
+ }
1464
+
1344
1465
  /**
1345
1466
  * A snapshot of a message's attributes at a given moment in time.
1346
1467
  *
@@ -1577,6 +1698,17 @@ export declare interface ParticipantRef {
1577
1698
  * @returns A promise that resolves when the operation completes. This promise will reject if client-side conversation syncing is disabled.
1578
1699
  */
1579
1700
  delete(): Promise<void>;
1701
+ /**
1702
+ * Subscribe to this participant's state.
1703
+ *
1704
+ * @remarks
1705
+ * While the subscription is active, `onSnapshot` will be called when the participant joins or leaves the conversation, or their attributes change, including attributes on their user.
1706
+ *
1707
+ * Remember to call `.unsubscribe` on the subscription once you are done with it.
1708
+ *
1709
+ * @returns A subscription to the participant
1710
+ */
1711
+ subscribe(onSnapshot?: (snapshot: ParticipantSnapshot | null) => void): SingleParticipantSubscription;
1580
1712
  }
1581
1713
 
1582
1714
  /**
@@ -1884,6 +2016,23 @@ export declare function registerPolyfills({ WebSocket }: {
1884
2016
  WebSocket: object;
1885
2017
  }): void;
1886
2018
 
2019
+ /**
2020
+ * A snapshot of search results in a given moment in time.
2021
+ * contains a message and the conversation it from.
2022
+ *
2023
+ * @public
2024
+ */
2025
+ export declare interface SearchMessageSnapshot {
2026
+ /**
2027
+ * The conversation that the message was found in.
2028
+ */
2029
+ conversation: ConversationSnapshot;
2030
+ /**
2031
+ * The message that was found in the search.
2032
+ */
2033
+ message: MessageSnapshot;
2034
+ }
2035
+
1887
2036
  /**
1888
2037
  * The version of {@link ContentBlock} that is used when sending or editing messages.
1889
2038
  *
@@ -2141,6 +2290,64 @@ export declare interface SetUserParams {
2141
2290
  pushTokens?: Record<string, true | null> | null;
2142
2291
  }
2143
2292
 
2293
+ /**
2294
+ * The state of a participant subscription when it is actively listening for changes
2295
+ *
2296
+ * @public
2297
+ */
2298
+ export declare interface SingleParticipantActiveState {
2299
+ readonly type: "active";
2300
+ /**
2301
+ * The most recently received snapshot for the participant, or `null` if they are not a participant in that conversation.
2302
+ */
2303
+ readonly latestSnapshot: ParticipantSnapshot | null;
2304
+ }
2305
+
2306
+ /**
2307
+ * A subscription to a specific participant.
2308
+ *
2309
+ * @remarks
2310
+ * Get a SingleParticipantSubscription by calling {@link ParticipantRef.subscribe}
2311
+ *
2312
+ * Remember to `.unsubscribe` the subscription once you are done with it.
2313
+ *
2314
+ * @public
2315
+ */
2316
+ export declare interface SingleParticipantSubscription {
2317
+ /**
2318
+ * The current state of the subscription
2319
+ *
2320
+ * @remarks
2321
+ * An object with the following fields:
2322
+ *
2323
+ * `type` is one of "pending", "active", "unsubscribed", or "error".
2324
+ *
2325
+ * When `type` is "active", includes `latestSnapshot: ParticipantSnapshot | null`, the current state of the participant.
2326
+ * `latestSnapshot` is `null` when the user is not a participant or the conversation does not exist.
2327
+ *
2328
+ * When `type` is "error", includes the `error` field. It is a JS `Error` object explaining what caused the subscription to be terminated.
2329
+ */
2330
+ state: PendingState | SingleParticipantActiveState | UnsubscribedState | ErrorState;
2331
+ /**
2332
+ * Resolves when the subscription starts receiving updates from the server.
2333
+ */
2334
+ readonly connected: Promise<SingleParticipantActiveState>;
2335
+ /**
2336
+ * Resolves when the subscription permanently stops receiving updates from the server.
2337
+ *
2338
+ * @remarks
2339
+ * This is either because you unsubscribed or because the subscription encountered an unrecoverable error.
2340
+ */
2341
+ readonly terminated: Promise<UnsubscribedState | ErrorState>;
2342
+ /**
2343
+ * Unsubscribe from this resource and stop receiving updates.
2344
+ *
2345
+ * @remarks
2346
+ * If the subscription is already in the "unsubscribed" or "error" state, this is a no-op.
2347
+ */
2348
+ unsubscribe(): void;
2349
+ }
2350
+
2144
2351
  export declare type Subscription = {
2145
2352
  unsubscribe: () => void;
2146
2353
  };
@@ -2297,6 +2504,92 @@ export declare interface TalkSession {
2297
2504
  * Remember to call `.unsubscribe` on the subscription once you are done with it.
2298
2505
  */
2299
2506
  subscribeConversations(onSnapshot?: (snapshot: ConversationSnapshot[], loadedAll: boolean) => void): ConversationListSubscription;
2507
+ /**
2508
+ * Creates a search for messages in all the current user's conversations.
2509
+ *
2510
+ * @remarks
2511
+ * The search feature is available on the Growth plan or higher. On the Basic plan, this method will return an error.
2512
+ *
2513
+ * Search is a two-step process. After calling `searchMessages`, call {@link MessageSearch.setQuery} to specify a search query and begin the search.
2514
+ *
2515
+ * As search can be quite slow, there are two different ways of using search:
2516
+ *
2517
+ * Option 1: Processing each search result as soon as it is received:
2518
+ *
2519
+ * ```ts
2520
+ * const search = session.searchMessages((results, status) => {
2521
+ * console.log("Found a new message. All messages found so far:", results);
2522
+ * });
2523
+ * search.setQuery("Hello");
2524
+ * ```
2525
+ *
2526
+ * Option 2: Wait until the full page is received (up to 60s delay) before processing:
2527
+ *
2528
+ * ```ts
2529
+ * const search = session.searchMessages();
2530
+ * const { results, loadedAll } = await search.setQuery("Hello");
2531
+ * console.log("Search completed, first page of messages:", results);
2532
+ * ```
2533
+ *
2534
+ * @param onResults - Callback to run whenever we receive new results or the status changes. Status `searching` means we are actively searching for more messages. `canLoadMore` means that the search is idle, but you can load older results with {@link MessageSearch.loadMore}. `loadedAll` means that there are no more messages to load.
2535
+ * @returns A search object. Call {@link MessageSearch.setQuery} to specify the string to search for, and begin receiving results.
2536
+ *
2537
+ * @example Basic UI integration (search-on-keypress)
2538
+ * ```ts
2539
+ * const search = session.searchMessages((results, status) => {
2540
+ * render(results, status);
2541
+ * });
2542
+ *
2543
+ * const searchInput = document.querySelector("#searchInput");
2544
+ * searchInput.addEventListener("change", () => {
2545
+ * search.setQuery(searchInput.value);
2546
+ * });
2547
+ * ```
2548
+ */
2549
+ searchMessages(onResults?: (results: SearchMessageSnapshot[], status: "searching" | "canLoadMore" | "loadedAll") => void): MessageSearch;
2550
+ /**
2551
+ * Creates a search for all the current user's conversations.
2552
+ *
2553
+ * @remarks
2554
+ * The search feature is available on the Growth plan or higher. On the Basic plan, this method will return an error.
2555
+ *
2556
+ * Search is a two-step process. After calling `searchConversations`, call {@link ConversationSearch.setQuery} to specify a search query and begin the search.
2557
+ *
2558
+ * As search can be quite slow, there are two different ways of using search:
2559
+ *
2560
+ * Option 1: Processing each search result as soon as it is received:
2561
+ *
2562
+ * ```ts
2563
+ * const search = session.searchConversations((results, status) => {
2564
+ * console.log("Found a new conversation. All conversations found so far:", results);
2565
+ * });
2566
+ * search.setQuery("Hello");
2567
+ * ```
2568
+ *
2569
+ * Option 2: Wait until the full page is received (up to 60s delay) before processing:
2570
+ *
2571
+ * ```ts
2572
+ * const search = session.searchConversations();
2573
+ * const { results, loadedAll } = await search.setQuery("Hello");
2574
+ * console.log("Search completed, first page of conversations:", results);
2575
+ * ```
2576
+ *
2577
+ * @param onResults - Callback to run whenever we receive new results or the status changes. Status `searching` means we are actively searching for more conversations. `canLoadMore` means that the search is idle, but you can load older results with {@link ConversationSearch.loadMore}. `loadedAll` means that there are no more conversations to load.
2578
+ * @returns A {@link ConversationSearch}. Call {@link ConversationSearch.setQuery} to specify the string to search for, and begin receiving results.
2579
+ *
2580
+ * @example Basic UI integration (search-on-keypress)
2581
+ * ```ts
2582
+ * const search = session.searchConversations((results, status) => {
2583
+ * render(results, status);
2584
+ * });
2585
+ *
2586
+ * const searchInput = document.querySelector("#searchInput");
2587
+ * searchInput.addEventListener("change", () => {
2588
+ * search.setQuery(searchInput.value);
2589
+ * });
2590
+ * ```
2591
+ */
2592
+ searchConversations(onResults?: (results: ConversationSnapshot[], status: "searching" | "canLoadMore" | "loadedAll") => void): ConversationSearch;
2300
2593
  /**
2301
2594
  * Upload a generic file without any additional metadata.
2302
2595
  *