@@ -10,6 +10,7 @@ import { v4 as uuidv4 } from 'uuid'
1010import ChatUser from '../types/ChatUser'
1111import Message , { Mention , Reaction } from '../types/Message'
1212import MessageWithUser from '../types/MessageWithUser'
13+ import type { RetentionConfig } from '../types/Retention'
1314import Room from '../types/Room'
1415import { ChatStore , CreateSlice } from '../useChat'
1516
@@ -18,15 +19,15 @@ export interface MessageSlice {
1819 messageObserversByRoom : Record < string , StoreObserver | null >
1920 messageSubscriptionsByRoom : Record < string , SyncSubscription | null >
2021 messagesLoading : boolean
21- messagesPublisher : ( room : Room , retentionDays ?: number ) => Promise < void >
22+ messagesPublisher : ( room : Room , retention ?: RetentionConfig ) => Promise < void >
2223 /**
2324 * Subscribe to messages for a specific room on-demand.
2425 * Used for generated rooms (comment rooms) that need dynamic subscriptions.
2526 */
2627 subscribeToRoomMessages : (
2728 roomId : string ,
2829 messagesId : string ,
29- retentionDays ?: number ,
30+ retention ?: RetentionConfig ,
3031 ) => Promise < void >
3132 /**
3233 * Unsubscribe from messages for a specific room.
@@ -94,7 +95,7 @@ export const createMessageSlice: CreateSlice<MessageSlice> = (
9495 ditto,
9596 userId,
9697 userCollectionKey,
97- retentionDays : globalRetentionDays ,
98+ retention : globalRetention ,
9899 notificationHandler,
99100 } ,
100101) => {
@@ -407,9 +408,9 @@ export const createMessageSlice: CreateSlice<MessageSlice> = (
407408 * 5. Triggers notifications for new messages from other users
408409 *
409410 * @param room - Room to subscribe to messages for
410- * @param retentionDays - Optional override for message retention period
411+ * @param retention - Optional override for message retention configuration
411412 */
412- async messagesPublisher ( room : Room , retentionDays ?: number ) {
413+ async messagesPublisher ( room : Room , retention ?: RetentionConfig ) {
413414 if ( ! ditto ) {
414415 return
415416 }
@@ -419,22 +420,40 @@ export const createMessageSlice: CreateSlice<MessageSlice> = (
419420 return
420421 }
421422
422- const effectiveRetentionDays =
423- retentionDays ??
424- room . retentionDays ??
425- globalRetentionDays ??
426- DEFAULT_RETENTION_DAYS
427-
428- const retentionDate = new Date (
429- Date . now ( ) - effectiveRetentionDays * 24 * 60 * 60 * 1000 ,
430- )
431- const query = `SELECT * FROM COLLECTION ${ room . messagesId } (thumbnailImageToken ATTACHMENT, largeImageToken ATTACHMENT, fileAttachmentToken ATTACHMENT)
432- WHERE roomId = :roomId AND createdOn >= :date AND isArchived = false
433- ORDER BY createdOn ASC`
434-
435- const args = {
436- roomId : room . _id ,
437- date : retentionDate . toISOString ( ) ,
423+ // Check if retention should be indefinite (priority: param > room > global)
424+ const retainIndefinitely =
425+ retention ?. retainIndefinitely ??
426+ room . retention ?. retainIndefinitely ??
427+ globalRetention ?. retainIndefinitely ??
428+ false
429+
430+ let query : string
431+ let args : Record < string , string >
432+
433+ if ( retainIndefinitely ) {
434+ query = `SELECT * FROM COLLECTION ${ room . messagesId } (thumbnailImageToken ATTACHMENT, largeImageToken ATTACHMENT, fileAttachmentToken ATTACHMENT)
435+ WHERE roomId = :roomId AND isArchived = false
436+ ORDER BY createdOn ASC`
437+ args = {
438+ roomId : room . _id ,
439+ }
440+ } else {
441+ const effectiveRetentionDays =
442+ retention ?. days ??
443+ room . retention ?. days ??
444+ globalRetention ?. days ??
445+ DEFAULT_RETENTION_DAYS
446+
447+ const retentionDate = new Date (
448+ Date . now ( ) - effectiveRetentionDays * 24 * 60 * 60 * 1000 ,
449+ )
450+ query = `SELECT * FROM COLLECTION ${ room . messagesId } (thumbnailImageToken ATTACHMENT, largeImageToken ATTACHMENT, fileAttachmentToken ATTACHMENT)
451+ WHERE roomId = :roomId AND createdOn >= :date AND isArchived = false
452+ ORDER BY createdOn ASC`
453+ args = {
454+ roomId : room . _id ,
455+ date : retentionDate . toISOString ( ) ,
456+ }
438457 }
439458
440459 try {
@@ -501,12 +520,12 @@ export const createMessageSlice: CreateSlice<MessageSlice> = (
501520 *
502521 * @param roomId - Room ID to subscribe to
503522 * @param messagesId - Collection ID for messages ("messages" or "dm_messages")
504- * @param retentionDays - Optional message retention override
523+ * @param retention - Optional message retention configuration override
505524 */
506525 async subscribeToRoomMessages (
507526 roomId : string ,
508527 messagesId : string ,
509- retentionDays ?: number ,
528+ retention ?: RetentionConfig ,
510529 ) {
511530 if ( ! ditto ) {
512531 return
@@ -517,20 +536,38 @@ export const createMessageSlice: CreateSlice<MessageSlice> = (
517536 return
518537 }
519538
520- const effectiveRetentionDays =
521- retentionDays ?? globalRetentionDays ?? DEFAULT_RETENTION_DAYS
522-
523- const retentionDate = new Date (
524- Date . now ( ) - effectiveRetentionDays * 24 * 60 * 60 * 1000 ,
525- )
526-
527- const query = `SELECT * FROM COLLECTION ${ messagesId } (thumbnailImageToken ATTACHMENT, largeImageToken ATTACHMENT, fileAttachmentToken ATTACHMENT)
528- WHERE roomId = :roomId AND createdOn >= :date AND isArchived = false
529- ORDER BY createdOn ASC`
539+ // Check if retention should be indefinite (priority: param > global)
540+ const retainIndefinitely =
541+ retention ?. retainIndefinitely ??
542+ globalRetention ?. retainIndefinitely ??
543+ false
544+
545+ let query : string
546+ let args : Record < string , string >
547+
548+ if ( retainIndefinitely ) {
549+ // Build query without date filter for indefinite retention
550+ query = `SELECT * FROM COLLECTION ${ messagesId } (thumbnailImageToken ATTACHMENT, largeImageToken ATTACHMENT, fileAttachmentToken ATTACHMENT)
551+ WHERE roomId = :roomId AND isArchived = false
552+ ORDER BY createdOn ASC`
553+ args = {
554+ roomId,
555+ }
556+ } else {
557+ // Extract days with priority: param > global > default (30)
558+ const effectiveRetentionDays =
559+ retention ?. days ?? globalRetention ?. days ?? DEFAULT_RETENTION_DAYS
530560
531- const args = {
532- roomId,
533- date : retentionDate . toISOString ( ) ,
561+ const retentionDate = new Date (
562+ Date . now ( ) - effectiveRetentionDays * 24 * 60 * 60 * 1000 ,
563+ )
564+ query = `SELECT * FROM COLLECTION ${ messagesId } (thumbnailImageToken ATTACHMENT, largeImageToken ATTACHMENT, fileAttachmentToken ATTACHMENT)
565+ WHERE roomId = :roomId AND createdOn >= :date AND isArchived = false
566+ ORDER BY createdOn ASC`
567+ args = {
568+ roomId,
569+ date : retentionDate . toISOString ( ) ,
570+ }
534571 }
535572
536573 try {
0 commit comments