1- import { AuthenticationCreds , AuthenticationState , BufferJSON , initAuthCreds , proto , SignalDataTypeMap } from 'baileys' ;
1+ import { AuthenticationState , BufferJSON , initAuthCreds , WAProto as proto } from 'baileys' ;
2+ import fs from 'fs/promises' ;
3+ import path from 'path' ;
24
35import { configService , Database } from '../config/env.config' ;
46import { Logger } from '../config/logger.config' ;
7+ import { INSTANCE_DIR } from '../config/path.config' ;
58import { dbserver } from '../libs/db.connect' ;
69
10+ const fixFileName = ( file ) => {
11+ if ( ! file ) {
12+ return undefined ;
13+ }
14+ const replacedSlash = file . replace ( / \/ / g, '__' ) ;
15+ const replacedColon = replacedSlash . replace ( / : / g, '-' ) ;
16+ return replacedColon ;
17+ } ;
18+
19+ async function fileExists ( file ) {
20+ try {
21+ const stat = await fs . stat ( file ) ;
22+ if ( stat . isFile ( ) ) return true ;
23+ } catch ( error ) {
24+ return ;
25+ }
26+ }
27+
728export async function useMultiFileAuthStateDb (
829 coll : string ,
930) : Promise < { state : AuthenticationState ; saveCreds : ( ) => Promise < void > } > {
10- const logger = new Logger ( useMultiFileAuthStateDb . name ) ;
11-
1231 const client = dbserver . getClient ( ) ;
1332
33+ const logger = new Logger ( useMultiFileAuthStateDb . name ) ;
34+
1435 const collection = client
1536 . db ( configService . get < Database > ( 'DATABASE' ) . CONNECTION . DB_PREFIX_NAME + '-instances' )
1637 . collection ( coll ) ;
1738
18- const writeData = async ( data : any , key : string ) : Promise < any > => {
39+ const sessionId = coll ;
40+
41+ const localFolder = path . join ( INSTANCE_DIR , sessionId ) ;
42+ const localFile = ( key : string ) => path . join ( localFolder , fixFileName ( key ) + '.json' ) ;
43+ await fs . mkdir ( localFolder , { recursive : true } ) ;
44+
45+ async function writeData ( data : any , key : string ) : Promise < any > {
1946 try {
47+ const dataString = JSON . stringify ( data , BufferJSON . replacer ) ;
48+
49+ if ( key != 'creds' ) {
50+ await fs . writeFile ( localFile ( key ) , dataString ) ;
51+ return ;
52+ }
2053 await client . connect ( ) ;
2154 let msgParsed = JSON . parse ( JSON . stringify ( data , BufferJSON . replacer ) ) ;
2255 if ( Array . isArray ( msgParsed ) ) {
@@ -30,42 +63,59 @@ export async function useMultiFileAuthStateDb(
3063 } ) ;
3164 } catch ( error ) {
3265 logger . error ( error ) ;
66+ return ;
3367 }
34- } ;
68+ }
3569
36- const readData = async ( key : string ) : Promise < any > = > {
70+ async function readData ( key : string ) : Promise < any > {
3771 try {
38- await client . connect ( ) ;
39- let data = ( await collection . findOne ( { _id : key } ) ) as any ;
40- if ( data ?. content_array ) {
41- data = data . content_array ;
72+ if ( key != 'creds' ) {
73+ if ( ! ( await fileExists ( localFile ( key ) ) ) ) return null ;
74+ const rawData = await fs . readFile ( localFile ( key ) , { encoding : 'utf-8' } ) ;
75+
76+ const parsedData = JSON . parse ( rawData , BufferJSON . reviver ) ;
77+ return parsedData ;
78+ } else {
79+ await client . connect ( ) ;
80+ let data = ( await collection . findOne ( { _id : key } ) ) as any ;
81+ if ( data ?. content_array ) {
82+ data = data . content_array ;
83+ }
84+ const creds = JSON . stringify ( data ) ;
85+ return JSON . parse ( creds , BufferJSON . reviver ) ;
4286 }
43- const creds = JSON . stringify ( data ) ;
44- return JSON . parse ( creds , BufferJSON . reviver ) ;
4587 } catch ( error ) {
4688 logger . error ( error ) ;
89+ return null ;
4790 }
48- } ;
91+ }
4992
50- const removeData = async ( key : string ) = > {
93+ async function removeData ( key : string ) : Promise < any > {
5194 try {
52- await client . connect ( ) ;
53- return await collection . deleteOne ( { _id : key } ) ;
95+ if ( key != 'creds' ) {
96+ await fs . unlink ( localFile ( key ) ) ;
97+ } else {
98+ await client . connect ( ) ;
99+ return await collection . deleteOne ( { _id : key } ) ;
100+ }
54101 } catch ( error ) {
55102 logger . error ( error ) ;
103+ return ;
56104 }
57- } ;
105+ }
58106
59- const creds : AuthenticationCreds = ( await readData ( 'creds' ) ) || initAuthCreds ( ) ;
107+ let creds = await readData ( 'creds' ) ;
108+ if ( ! creds ) {
109+ creds = initAuthCreds ( ) ;
110+ await writeData ( creds , 'creds' ) ;
111+ }
60112
61113 return {
62114 state : {
63115 creds,
64116 keys : {
65- get : async ( type , ids : string [ ] ) => {
66- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
67- // @ts -ignore
68- const data : { [ _ : string ] : SignalDataTypeMap [ type ] } = { } ;
117+ get : async ( type , ids ) => {
118+ const data = { } ;
69119 await Promise . all (
70120 ids . map ( async ( id ) => {
71121 let value = await readData ( `${ type } -${ id } ` ) ;
@@ -76,25 +126,24 @@ export async function useMultiFileAuthStateDb(
76126 data [ id ] = value ;
77127 } ) ,
78128 ) ;
79-
80129 return data ;
81130 } ,
82- set : async ( data : any ) => {
83- const tasks : Promise < void > [ ] = [ ] ;
131+ set : async ( data ) => {
132+ const tasks = [ ] ;
84133 for ( const category in data ) {
85134 for ( const id in data [ category ] ) {
86135 const value = data [ category ] [ id ] ;
87136 const key = `${ category } -${ id } ` ;
137+
88138 tasks . push ( value ? writeData ( value , key ) : removeData ( key ) ) ;
89139 }
90140 }
91-
92141 await Promise . all ( tasks ) ;
93142 } ,
94143 } ,
95144 } ,
96- saveCreds : async ( ) => {
97- return await writeData ( creds , 'creds' ) ;
145+ saveCreds : ( ) => {
146+ return writeData ( creds , 'creds' ) ;
98147 } ,
99148 } ;
100149}
0 commit comments