@@ -7,6 +7,7 @@ import throttle from 'lodash/throttle';
77import { lookup } from 'mime-types' ;
88import {
99 Channel as ChannelClass ,
10+ ChannelResponse ,
1011 ChannelState ,
1112 Channel as ChannelType ,
1213 DeleteMessageOptions ,
@@ -102,7 +103,6 @@ import {
102103 isImagePickerAvailable ,
103104 NativeHandlers ,
104105} from '../../native' ;
105- import * as dbApi from '../../store/apis' ;
106106import { ChannelUnreadState , FileTypes } from '../../types/types' ;
107107import { addReactionToLocalState } from '../../utils/addReactionToLocalState' ;
108108import { compressedImageURI } from '../../utils/compressImage' ;
@@ -1300,9 +1300,13 @@ const ChannelWithContext = (props: PropsWithChildren<ChannelPropsWithContext>) =
13001300 attachment . image_url = uploadResponse . file ;
13011301 delete attachment . originalFile ;
13021302
1303- await dbApi . updateMessage ( {
1304- message : { ...updatedMessage , cid : channel . cid } ,
1305- } ) ;
1303+ client . offlineDb ?. executeQuerySafely (
1304+ ( db ) =>
1305+ db . updateMessage ( {
1306+ message : { ...updatedMessage , cid : channel . cid } ,
1307+ } ) ,
1308+ { method : 'upsertAppSettings' } ,
1309+ ) ;
13061310 }
13071311
13081312 if ( attachment . type !== FileTypes . Image && file ?. uri ) {
@@ -1321,9 +1325,13 @@ const ChannelWithContext = (props: PropsWithChildren<ChannelPropsWithContext>) =
13211325 }
13221326
13231327 delete attachment . originalFile ;
1324- await dbApi . updateMessage ( {
1325- message : { ...updatedMessage , cid : channel . cid } ,
1326- } ) ;
1328+ client . offlineDb ?. executeQuerySafely (
1329+ ( db ) =>
1330+ db . updateMessage ( {
1331+ message : { ...updatedMessage , cid : channel . cid } ,
1332+ } ) ,
1333+ { method : 'upsertAppSettings' } ,
1334+ ) ;
13271335 }
13281336 }
13291337 }
@@ -1344,7 +1352,7 @@ const ChannelWithContext = (props: PropsWithChildren<ChannelPropsWithContext>) =
13441352 retrying ?: boolean ;
13451353 } ) => {
13461354 let failedMessageUpdated = false ;
1347- const handleFailedMessage = async ( ) => {
1355+ const handleFailedMessage = ( ) => {
13481356 if ( ! failedMessageUpdated ) {
13491357 const updatedMessage = {
13501358 ...localMessage ,
@@ -1355,11 +1363,13 @@ const ChannelWithContext = (props: PropsWithChildren<ChannelPropsWithContext>) =
13551363 threadInstance ?. upsertReplyLocally ?.( { message : updatedMessage } ) ;
13561364 optimisticallyUpdatedNewMessages . delete ( localMessage . id ) ;
13571365
1358- if ( enableOfflineSupport ) {
1359- await dbApi . updateMessage ( {
1360- message : updatedMessage ,
1361- } ) ;
1362- }
1366+ client . offlineDb ?. executeQuerySafely (
1367+ ( db ) =>
1368+ db . updateMessage ( {
1369+ message : updatedMessage ,
1370+ } ) ,
1371+ { method : 'upsertAppSettings' } ,
1372+ ) ;
13631373
13641374 failedMessageUpdated = true ;
13651375 }
@@ -1397,11 +1407,14 @@ const ChannelWithContext = (props: PropsWithChildren<ChannelPropsWithContext>) =
13971407 status : MessageStatusTypes . RECEIVED ,
13981408 } ;
13991409
1400- if ( enableOfflineSupport ) {
1401- await dbApi . updateMessage ( {
1402- message : { ...newMessageResponse , cid : channel . cid } ,
1403- } ) ;
1404- }
1410+ client . offlineDb ?. executeQuerySafely (
1411+ ( db ) =>
1412+ db . updateMessage ( {
1413+ message : { ...newMessageResponse , cid : channel . cid } ,
1414+ } ) ,
1415+ { method : 'upsertAppSettings' } ,
1416+ ) ;
1417+
14051418 if ( retrying ) {
14061419 replaceMessage ( localMessage , newMessageResponse ) ;
14071420 } else {
@@ -1425,15 +1438,22 @@ const ChannelWithContext = (props: PropsWithChildren<ChannelPropsWithContext>) =
14251438 threadInstance ?. upsertReplyLocally ?.( { message : localMessage } ) ;
14261439 optimisticallyUpdatedNewMessages . add ( localMessage . id ) ;
14271440
1428- if ( enableOfflineSupport ) {
1429- // While sending a message, we add the message to local db with failed status, so that
1430- // if app gets closed before message gets sent and next time user opens the app
1431- // then user can see that message in failed state and can retry.
1432- // If succesfull, it will be updated with received status.
1433- await dbApi . upsertMessages ( {
1434- messages : [ { ...localMessage , cid : channel . cid , status : MessageStatusTypes . FAILED } ] ,
1435- } ) ;
1436- }
1441+ // While sending a message, we add the message to local db with failed status, so that
1442+ // if app gets closed before message gets sent and next time user opens the app
1443+ // then user can see that message in failed state and can retry.
1444+ // If succesfull, it will be updated with received status.
1445+ client . offlineDb ?. executeQuerySafely (
1446+ async ( db ) => {
1447+ if ( channel . data ) {
1448+ await db . upsertChannelData ( { channel : channel . data as unknown as ChannelResponse } ) ;
1449+ }
1450+ await db . upsertMessages ( {
1451+ // @ts -ignore
1452+ messages : [ { ...localMessage , cid : channel . cid , status : MessageStatusTypes . FAILED } ] ,
1453+ } ) ;
1454+ } ,
1455+ { method : 'upsertAppSettings' } ,
1456+ ) ;
14371457
14381458 await sendMessageRequest ( { localMessage, message, options } ) ;
14391459 } ,
0 commit comments