import { BrowserMsg } from '../../browser/browser-msg.js' ; import { Env } from '../../browser/env.js' ; import { GmailRes } from '../../../common/api/email-provider/gmail/gmail-parser.js' ; import { RawStore , AbstractStore } from './abstract-store.js' ; import { Dict , Value } from '../../core/common.js' ; import { storageLocalSet , storageLocalGet , storageLocalRemove } from '../../browser/chrome.js' ; import { Catch } from '../catch.js' ; export type StoredAdminCode = { date : number ; codes : string [ ] ; } ; export type LocalDraft = GmailRes . GmailDraftGet & { timestamp : number ; acctEmail : string ; } ; export type GlobalStoreDict = { version ? : number | null ; account_emails ? : string ; settings_seen ? : boolean ; hide_pass_phrases ? : boolean ; dev_outlook_allow ? : boolean ; install_mobile_app_notification_dismissed ? : boolean ; key_info_store_fingerprints_added ? : boolean ; contact_store_x509_fingerprints_and_longids_updated ? : boolean ; contact_store_opgp_revoked_flags_updated ? : boolean ; contact_store_searchable_extended ? : boolean ; local_drafts ? : Dict < LocalDraft > ; } ; export type GlobalIndex = 'version' | 'account_emails' | 'settings_seen' | 'hide_pass_phrases' | 'dev_outlook_allow' | 'install_mobile_app_notification_dismissed' | 'key_info_store_fingerprints_added' | 'contact_store_x509_fingerprints_and_longids_updated' | 'contact_store_opgp_revoked_flags_updated' | 'contact_store_searchable_extended' | 'local_drafts' ; /**
 * Locally stored data that is not associated with any email account
 */ export class GlobalStore extends AbstractStore { private static globalStorageScope : 'global' = 'global' ; public static set = async ( values ) : Promise < void > => { if ( Env . isContentScript ( ) ) { return await BrowserMsg . send . bg . await . storeGlobalSet ( { values } ) ; } const storageUpdate = { } ; for ( const key of Object . keys ( values ) ) { const index = GlobalStore . singleScopeRawIndex ( GlobalStore . globalStorageScope , key ) ; storageUpdate [ index ] = values [ key as GlobalIndex ] ; } await storageLocalSet ( storageUpdate ) ; } ; public static get = async ( keys ) : Promise < GlobalStoreDict > => { if ( Env . isContentScript ( ) ) { return await BrowserMsg . send . bg . await . storeGlobalGet ( { keys } ) ; } const storageObj = await storageLocalGet ( GlobalStore . singleScopeRawIndexArr ( GlobalStore . globalStorageScope , keys ) ) as RawStore ; return GlobalStore . buildSingleAccountStoreFromRawResults ( GlobalStore . globalStorageScope , storageObj ) as GlobalStore ; } ; public static remove = async ( keys ) => { await storageLocalRemove ( GlobalStore . singleScopeRawIndexArr ( GlobalStore . globalStorageScope , keys ) ) ; } ; public static acctEmailsGet = async ( ) : Promise < string [ ] > => { const storage = await GlobalStore . get ( [ 'account_emails' ] ) ; const acctEmails = [ ] ; if ( typeof storage . account_emails !== 'undefined' ) { for ( const acctEmail of JSON . parse ( storage . account_emails ) as string [ ] ) { if ( ! acctEmails . includes ( acctEmail . toLowerCase ( ) ) ) { acctEmails . push ( acctEmail . toLowerCase ( ) ) ; } } } return acctEmails ; } ; public static acctEmailsAdd = async ( acctEmail ) : Promise < void > => { if ( ! acctEmail ) { throw new Error ( `attempting to save empty acctEmail: ${ acctEmail }` ) ; } if ( acctEmail . match ( /[A-Z]/ ) ) { Catch . report ( `attempting to save acctEmail that wasn't lowercased: ${ acctEmail }` ) ; acctEmail = acctEmail . toLowerCase ( ) ; } const acctEmails = await GlobalStore . acctEmailsGet ( ) ; if ( ! acctEmails . includes ( acctEmail ) && acctEmail ) { acctEmails . push ( acctEmail ) ; await GlobalStore . set ( { account_emails : JSON . stringify ( acctEmails ) } ) ; BrowserMsg . send . bg . updateUninstallUrl ( ) ; } } ; public static acctEmailsRemove = async ( acctEmail ) : Promise < void > => { const acctEmails = await GlobalStore . acctEmailsGet ( ) ; await GlobalStore . set ( { account_emails : JSON . stringify ( Value . arr . withoutVal ( acctEmails , acctEmail ) ) } ) ; BrowserMsg . send . bg . updateUninstallUrl ( ) ; } ; }