diff --git a/functions/integrations/avatar.js b/functions/integrations/avatar.js index 15a4247..adf7750 100644 --- a/functions/integrations/avatar.js +++ b/functions/integrations/avatar.js @@ -1,10 +1,12 @@ +const functions = require( 'firebase-functions' ) +const { integration }= functions.config() const { db, dataFromSnap } = require( '../modules/firebase' ) const Web3 = require( 'web3' ) const web3 = new Web3() const { getStorage } = require( 'firebase-admin/storage' ) -module.exports = async function( req, res ) { +exports.setAvatar = async function( req, res ) { const chain = process.env.NODE_ENV == 'development' ? '0x4' : '0x1' // const chain = '0x1' @@ -69,8 +71,6 @@ module.exports = async function( req, res ) { await cacheFile.save( JSON.stringify( cachedJson ) ) await cacheFile.makePublic() - console.log( 'New data: ', cachedJson ) - return res.json( { success: true, url: cacheFile.publicUrl() @@ -85,4 +85,68 @@ module.exports = async function( req, res ) { } +} + +exports.resetAvatar = async function( req, res ) { + + const chain = process.env.NODE_ENV == 'development' ? '0x4' : '0x1' + const network = 'mainnet' + // const chain = '0x1' + + try { + + // Get request data + const { address, secret } = req.body + if( !address || !secret || secret != integration.secret ) throw new Error( `Malformed request` ) + + // Check if validator was already assigned + await db.collection( `${ network }Validators` ).doc( address ).delete() + + // Update the static overview JSON + const storage = getStorage() + const bucket = storage.bucket() + const cacheFile = bucket.file( `integrations/${ network }Avatars.json` ) + + // Load existing json + let jsonstring = '{}' + const [ fileExists ] = await cacheFile.exists() + if( fileExists ) { + // Read old json + const [ oldJson ] = await cacheFile.download() + jsonstring = oldJson + } + const cachedJson = JSON.parse( jsonstring ) + + // Get items that have not been updated + const tenSecondsAgo = Date.now() - ( 10 * 1000 ) + const shouldBeUpdated = await db.collection( `${ network }Validators` ).where( 'updated', '>', cachedJson.updated || tenSecondsAgo ).get().then( dataFromSnap ) + + // Update items that should be updated ( including current update ) + shouldBeUpdated.map( doc => { + if( !cachedJson.images ) cachedJson.images = {} + if( !cachedJson.ids ) cachedJson.ids = {} + cachedJson.images[ doc.uid ] = doc.src + cachedJson.ids[ doc.uid ] = doc.tokenId + } ) + + // Save new data to file + cachedJson.updated = Date.now() + cachedJson.trail = shouldBeUpdated.length + await cacheFile.save( JSON.stringify( cachedJson ) ) + await cacheFile.makePublic() + + return res.json( { + success: true, + url: cacheFile.publicUrl() + } ) + + } catch( e ) { + + console.error( 'avatar deletion integration error: ', e ) + return res.json( { + error: e.message + } ) + + } + } \ No newline at end of file diff --git a/functions/modules/mainnet.js b/functions/modules/mainnet.js index cdcac9b..7d8d189 100644 --- a/functions/modules/mainnet.js +++ b/functions/modules/mainnet.js @@ -1,7 +1,7 @@ const app = require( './express' )() const { getTotalSupply } = require( './contract' ) const { safelyReturnRocketeer, web2domain } = require( './rocketeer' ) -const setAvatarOfValidtor = require( '../integrations/avatar' ) +const { setAvatar, resetAvatar } = require( '../integrations/avatar' ) // /////////////////////////////// @@ -33,7 +33,8 @@ app.get( '/api/rocketeer/:id', async ( req, res ) => { } ) -app.post( '/api/integrations/avatar/', setAvatarOfValidtor ) +app.post( '/api/integrations/avatar/', setAvatar ) +app.delete( '/api/integrations/avatar/', resetAvatar ) // ///////////////////////////////