2019-11-16 20:35:50 -05:00
/* eslint-disable no-await-in-loop */
import axios from 'axios' ;
2019-12-24 20:40:48 -05:00
import { inspect } from 'util' ;
2019-11-16 20:35:50 -05:00
import { Client } from '..' ;
2020-01-03 10:55:23 -05:00
let interval : NodeJS.Timeout ;
2019-11-16 20:35:50 -05:00
export default function checkSS ( client : Client ) {
2020-01-03 10:55:23 -05:00
interval = setInterval ( async ( ) = > {
2019-11-16 20:46:55 -05:00
try {
const accounts = await client . db . Account . find ( ) ;
2020-01-03 10:34:31 -05:00
for ( const { userID , homepath , hash } of accounts ) {
2019-11-16 20:46:55 -05:00
try {
2020-01-03 10:34:31 -05:00
const Authorization = client . util . getAcctHash ( homepath ) ;
2019-12-24 20:59:03 -05:00
if ( hash === null ) throw new Error ( 'Unable to locate auth file, homepath is probably incorrect' ) ;
2019-11-16 20:46:55 -05:00
await axios ( {
method : 'get' ,
url : 'https://api.securesign.org/account/details' ,
2020-01-03 10:34:31 -05:00
headers : { Authorization } ,
2019-11-16 20:46:55 -05:00
} ) ;
2020-01-03 10:34:31 -05:00
if ( ! hash ) {
await client . db . Account . updateOne ( { userID } , { $set : { hash : true } } ) ;
client . getDMChannel ( userID ) . then ( ( channel ) = > channel . createMessage ( 'Your SecureSign account has been automatically initialized via the SecureSign CLI.' ) ) . catch ( ) ;
}
2019-11-16 20:46:55 -05:00
} catch ( error ) {
2020-01-03 10:34:31 -05:00
if ( ! hash ) return ;
2019-11-16 20:46:55 -05:00
const { status } = error . response ;
if ( status === 400 || status === 401 || status === 403 || status === 404 ) {
2019-11-19 09:00:50 -05:00
await client . db . Account . updateOne ( { userID } , { $set : { hash : false } } ) ;
2020-01-03 10:34:31 -05:00
client . getDMChannel ( userID ) . then ( ( channel ) = > channel . createMessage ( 'Your SecureSign password has been reset - please reinitialize your SecureSign account. Run `=securesign init` for more information' ) ) . catch ( ) ;
2019-11-16 20:46:55 -05:00
}
2019-11-16 20:35:50 -05:00
}
}
2019-11-16 20:46:55 -05:00
} catch ( error ) {
client . util . handleError ( error ) ;
2019-11-16 20:35:50 -05:00
}
} , 60000 ) ;
2020-01-03 10:55:23 -05:00
return interval ;
}
export function clear() {
clearTimeout ( interval ) ;
2019-11-16 20:35:50 -05:00
}