import FcmService = require ( '@app/core/fcm/FcmService' ) ; import FcmKeyUtil = require ( '@app/core/fcm/FcmKeyUtil' ) ; import User from '@app/core/user/model/User' ; import UserService = require ( '@app/core/user/UserService' ) ; export async function attachDevice ( user , registrationId ) { if ( ! user . fcmKey ) await refreshFcmKey ( user , registrationId ) ; let keyName = FcmKeyUtil . getUserFcmKeyName ( user ) ; try { await FcmService . addDevice ( keyName , user . fcmKey , [ registrationId ] ) ; } catch ( err ) { await refreshFcmKey ( user , registrationId ) ; await FcmService . addDevice ( keyName , user . fcmKey , [ registrationId ] ) ; } await FcmService . addTopic ( registrationId ) ; } export async function detachDevice ( user , registrationId ) { if ( ! user . fcmKey ) await refreshFcmKey ( user , registrationId ) ; let keyName = FcmKeyUtil . getUserFcmKeyName ( user ) ; try { await FcmService . removeDevice ( keyName , user . fcmKey , [ registrationId ] ) ; } catch ( err ) { await refreshFcmKey ( user , registrationId ) ; await FcmService . removeDevice ( keyName , user . fcmKey , [ registrationId ] ) ; } await FcmService . removeTopicBatch ( [ registrationId ] ) ; } async function refreshFcmKey ( user , registrationId ) { let keyName = FcmKeyUtil . getUserFcmKeyName ( user ) ; var keyValue ; try { keyValue = await FcmService . getNotiKey ( keyName ) ; } catch ( err ) { keyValue = await FcmService . createNotiKey ( keyName , [ registrationId ] ) ; } if ( ! keyValue ) throw "refreshFcmKey failed" ; user . fcmKey = keyValue ; UserService . modify ( user ) ; }