mirror of
https://github.com/stronk-dev/RandomChad.git
synced 2025-07-05 18:35:10 +02:00
Deletion endpoint
This commit is contained in:
parent
548d475ddd
commit
115a510999
@ -1,10 +1,12 @@
|
|||||||
|
const functions = require( 'firebase-functions' )
|
||||||
|
const { integration }= functions.config()
|
||||||
const { db, dataFromSnap } = require( '../modules/firebase' )
|
const { db, dataFromSnap } = require( '../modules/firebase' )
|
||||||
const Web3 = require( 'web3' )
|
const Web3 = require( 'web3' )
|
||||||
const web3 = new Web3()
|
const web3 = new Web3()
|
||||||
const { getStorage } = require( 'firebase-admin/storage' )
|
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 = process.env.NODE_ENV == 'development' ? '0x4' : '0x1'
|
||||||
// const chain = '0x1'
|
// const chain = '0x1'
|
||||||
@ -69,8 +71,6 @@ module.exports = async function( req, res ) {
|
|||||||
await cacheFile.save( JSON.stringify( cachedJson ) )
|
await cacheFile.save( JSON.stringify( cachedJson ) )
|
||||||
await cacheFile.makePublic()
|
await cacheFile.makePublic()
|
||||||
|
|
||||||
console.log( 'New data: ', cachedJson )
|
|
||||||
|
|
||||||
return res.json( {
|
return res.json( {
|
||||||
success: true,
|
success: true,
|
||||||
url: cacheFile.publicUrl()
|
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
|
||||||
|
} )
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
@ -1,7 +1,7 @@
|
|||||||
const app = require( './express' )()
|
const app = require( './express' )()
|
||||||
const { getTotalSupply } = require( './contract' )
|
const { getTotalSupply } = require( './contract' )
|
||||||
const { safelyReturnRocketeer, web2domain } = require( './rocketeer' )
|
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 )
|
||||||
|
|
||||||
|
|
||||||
// ///////////////////////////////
|
// ///////////////////////////////
|
||||||
|
Loading…
x
Reference in New Issue
Block a user