File tree 2 files changed +53
-0
lines changed
packages/messenger-widget/src/hooks/messages/sizeLimit
2 files changed +53
-0
lines changed Original file line number Diff line number Diff line change
1
+ import { EncryptionEnvelop , getEnvelopSize } from '@dm3-org/dm3-lib-messaging' ;
2
+ import { log } from '@dm3-org/dm3-lib-shared' ;
3
+ import { ethers } from 'ethers' ;
4
+ import { ContactPreview } from '../../../interfaces/utils' ;
5
+ import { closeErrorModal , openErrorModal } from '../../../utils/common-utils' ;
6
+ import { fetchMessageSizeLimit } from './fetchSizeLimit' ;
7
+
8
+ export const checkIfEnvelopIsInSizeLimit = async (
9
+ mainnetProvider : ethers . providers . JsonRpcProvider ,
10
+ recipient : ContactPreview ,
11
+ encryptedEnvelop : EncryptionEnvelop ,
12
+ ) : Promise < boolean > => {
13
+ const account = recipient . contactDetails . account ;
14
+
15
+ try {
16
+ const reciversSizeLimit = await fetchMessageSizeLimit (
17
+ mainnetProvider ,
18
+ account ,
19
+ ) ;
20
+
21
+ const envelopSize = getEnvelopSize ( encryptedEnvelop ) ;
22
+
23
+ if ( envelopSize > reciversSizeLimit ) {
24
+ openErrorModal (
25
+ 'The size of the message is larger than limit '
26
+ . concat ( reciversSizeLimit . toString ( ) , ' bytes. ' )
27
+ . concat ( 'Please reduce the message size.' ) ,
28
+ false ,
29
+ closeErrorModal ,
30
+ ) ;
31
+
32
+ return false ;
33
+ }
34
+ return true ;
35
+ } catch ( error ) {
36
+ log ( error , 'message size limit' ) ;
37
+ return true ;
38
+ }
39
+ } ;
Original file line number Diff line number Diff line change
1
+ import { getDeliveryServiceProperties } from '@dm3-org/dm3-lib-delivery-api' ;
2
+ import { Account } from '@dm3-org/dm3-lib-profile' ;
3
+ import { ethers } from 'ethers' ;
4
+
5
+ export const fetchMessageSizeLimit = async (
6
+ mainnetProvider : ethers . providers . StaticJsonRpcProvider ,
7
+ account : Account ,
8
+ ) => {
9
+ const details = await getDeliveryServiceProperties (
10
+ mainnetProvider as ethers . providers . JsonRpcProvider ,
11
+ account as Account ,
12
+ ) ;
13
+ return details . sizeLimit ;
14
+ } ;
You can’t perform that action at this time.
0 commit comments