@@ -11,7 +11,7 @@ type GmailMsg$payload$part = { partId?: string, body?: GmailMsg$payload$body, fi
1111type GmailMsg$payload = { partId ?: string , filename ?: string , parts ?: GmailMsg$payload$part [ ] , headers ?: GmailMsg$header [ ] , mimeType ?: string , body ?: GmailMsg$payload$body } ;
1212type GmailMsg$labelId = 'INBOX' | 'UNREAD' | 'CATEGORY_PERSONAL' | 'IMPORTANT' | 'SENT' | 'CATEGORY_UPDATES' | 'DRAFT' ;
1313type GmailThread = { historyId : string ; id : string ; snippet : string ; } ;
14- type Label = { id : string , name : "CATEGORY_SOCIAL" , messageListVisibility : " hide" , labelListVisibility : " labelHide" , type : 'system' } ;
14+ type Label = { id : string , name : string , messageListVisibility : 'show' | ' hide' , labelListVisibility : 'labelShow' | ' labelHide' , type : 'system' } ;
1515type AcctDataFile = { messages : GmailMsg [ ] ; drafts : GmailMsg [ ] , attachments : { [ id : string ] : { data : string , size : number , filename ?: string } } , labels : Label [ ] } ;
1616type ExportedMsg = { acctEmail : string , full : GmailMsg , raw : GmailMsg , attachments : { [ id : string ] : { data : string , size : number } } } ;
1717
@@ -111,7 +111,13 @@ export class GoogleData {
111111
112112 public static withInitializedData = async ( acct : string ) : Promise < GoogleData > => {
113113 if ( typeof DATA [ acct ] === 'undefined' ) {
114- const acctData : AcctDataFile = { drafts : [ ] , messages : [ ] , attachments : { } , labels : [ ] } ;
114+ const acctData : AcctDataFile = {
115+ drafts : [ ] , messages : [ ] , attachments : { } , labels :
116+ [
117+ { id : 'INBOX' , name : 'Inbox' , messageListVisibility : 'show' , labelListVisibility : 'labelShow' , type : 'system' } ,
118+ { id : 'DRAFT' , name : 'Drafts' , messageListVisibility : 'show' , labelListVisibility : 'labelShow' , type : 'system' }
119+ ]
120+ } ;
115121 const dir = GoogleData . exportedMsgsPath ;
116122 const filenames : string [ ] = await new Promise ( ( res , rej ) => readdir ( dir , ( e , f ) => e ? rej ( e ) : res ( f ) ) ) ;
117123 const filePromises = filenames . map ( f => new Promise ( ( res , rej ) => readFile ( dir + f , ( e , d ) => e ? rej ( e ) : res ( d ) ) ) ) ;
@@ -204,11 +210,11 @@ export class GoogleData {
204210 }
205211
206212 public getMessage = ( id : string ) : GmailMsg | undefined => {
207- return DATA [ this . acct ] . messages . find ( m => m . id === id ) ;
213+ return this . getMessages ( ) . find ( m => m . id === id ) ;
208214 }
209215
210216 public getMessageBySubject = ( subject : string ) : GmailMsg | undefined => {
211- return DATA [ this . acct ] . messages . find ( m => {
217+ return this . getMessages ( ) . find ( m => {
212218 if ( m . payload ?. headers ) {
213219 const subjectHeader = m . payload . headers . find ( x => x . name === 'Subject' ) ;
214220 if ( subjectHeader ) {
@@ -220,7 +226,7 @@ export class GoogleData {
220226 }
221227
222228 public getMessagesByThread = ( threadId : string ) => {
223- return DATA [ this . acct ] . messages . filter ( m => m . threadId === threadId ) ;
229+ return this . getMessages ( ) . filter ( m => m . threadId === threadId ) ;
224230 }
225231
226232 public searchMessages = ( q : string ) => {
@@ -266,26 +272,33 @@ export class GoogleData {
266272 return DATA [ this . acct ] . labels ;
267273 }
268274
269- public getThreads = ( ) => {
275+ public getThreads = ( labelIds : string [ ] ) => {
270276 const threads : GmailThread [ ] = [ ] ;
271- for ( const thread of DATA [ this . acct ] . messages . map ( m => ( { historyId : m . historyId , id : m . threadId ! , snippet : `MOCK SNIPPET: ${ GoogleData . msgSubject ( m ) } ` } ) ) ) {
277+ for ( const thread of this . getMessages ( ) .
278+ filter ( m => labelIds ? ( m . labelIds || [ ] ) . some ( l => labelIds . includes ( l ) ) : true ) .
279+ map ( m => ( { historyId : m . historyId , id : m . threadId ! , snippet : `MOCK SNIPPET: ${ GoogleData . msgSubject ( m ) } ` } ) ) ) {
272280 if ( thread . id && ! threads . map ( t => t . id ) . includes ( thread . id ) ) {
273281 threads . push ( thread ) ;
274282 }
275283 }
276284 return threads ;
277285 }
278286
287+ // returns ordinary messages and drafts
288+ private getMessages = ( ) => {
289+ return DATA [ this . acct ] . messages . concat ( DATA [ this . acct ] . drafts ) ;
290+ }
291+
279292 private searchMessagesBySubject = ( subject : string ) => {
280293 subject = subject . trim ( ) . toLowerCase ( ) ;
281- const messages = DATA [ this . acct ] . messages . filter ( m => GoogleData . msgSubject ( m ) . toLowerCase ( ) . includes ( subject ) ) ;
294+ const messages = this . getMessages ( ) . filter ( m => GoogleData . msgSubject ( m ) . toLowerCase ( ) . includes ( subject ) ) ;
282295 return messages ;
283296 }
284297
285298 private searchMessagesByPeople = ( includePeople : string [ ] , excludePeople : string [ ] ) => {
286299 includePeople = includePeople . map ( person => person . trim ( ) . toLowerCase ( ) ) ;
287300 excludePeople = excludePeople . map ( person => person . trim ( ) . toLowerCase ( ) ) ;
288- return DATA [ this . acct ] . messages . filter ( m => {
301+ return this . getMessages ( ) . filter ( m => {
289302 const msgPeople = GoogleData . msgPeople ( m ) . toLowerCase ( ) ;
290303 let shouldInclude = false ;
291304 let shouldExclude = false ;
0 commit comments