Last active
March 19, 2021 07:45
-
-
Save gb103/7aba2400fd229233efdc6b172c35e4ed to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const functions = require('firebase-functions'); | |
const admin = require('firebase-admin'); | |
const nodemailer = require('nodemailer'); | |
const cors = require('cors')({origin: true}); | |
const rp = require('request-promise'); | |
const jsonDiff = require('json-diff'); | |
//const jsondiffpatch = require('jsondiffpatch').create(); | |
//const deepDiff = require('deep-diff').diff; | |
admin.initializeApp(functions.config().firebase); | |
/** | |
* Here we're using Gmail to send | |
*/ | |
let transporter = nodemailer.createTransport({ | |
service: 'gmail', | |
auth: { | |
user: '[email protected]', | |
pass: 'xxxxxx'//put above email password | |
} | |
}); | |
exports.sendMail = functions.remoteConfig.onUpdate(versionMetadata => { | |
// getting dest email by query string | |
const dest = '[email protected]'; | |
return admin.credential.applicationDefault().getAccessToken() | |
.then(accessTokenObj => { | |
return accessTokenObj.access_token; | |
}) | |
.then(accessToken => { | |
const currentVersion = versionMetadata.versionNumber; | |
const templatePromises = []; | |
templatePromises.push(getTemplate(currentVersion, accessToken)); | |
templatePromises.push(getTemplate(currentVersion - 1, accessToken)); | |
return Promise.all(templatePromises); | |
}) | |
.then(results => { | |
const currentTemplate = results[0]; | |
const previousTemplate = results[1]; | |
const diff = jsonDiff.diffString(previousTemplate, currentTemplate); | |
const logicalDiff = parseDiff(diff); | |
//const diff = jsondiffpatch.diff(previousTemplate, currentTemplate); | |
//const diff = deepDiff(previousTemplate, currentTemplate); | |
const mailOptions = { | |
from: '[email protected]', // Something like: Jane Doe <[email protected]> | |
to: dest, | |
subject: '[Firebase] Remote Config Added, Updated or Deleted', // email subject | |
text: 'change is ' + diff , | |
html: `<p style="font-size: 16px;">Hey, below RemoteConfig have been changed, <br />${diff}<br /><br /> ${logicalDiff}</p> | |
` // email content in HTML | |
}; | |
console.log(diff); | |
// returning result | |
return transporter.sendMail(mailOptions, (erro, info) => { | |
if(erro){ | |
return console.log(erro.toString());//res.send(erro.toString()); | |
} | |
return console.log('Sent');//res.send('Sended'); | |
}); | |
//return null; | |
}).catch(error => { | |
console.error(error); | |
return null; | |
}); | |
}); | |
function getTemplate(version, accessToken) { | |
const options = { | |
uri: 'https://firebaseremoteconfig.googleapis.com/v1/projects/<your-own-firebase-project-id>/remoteConfig', | |
qs: { | |
versionNumber: version | |
}, | |
headers: { | |
Authorization: 'Bearer ' + accessToken | |
}, | |
json: true // Automatically parses the JSON string in the response | |
}; | |
return rp(options).then(resp => { | |
return Promise.resolve(resp); | |
}).catch(err => { | |
console.error(err); | |
return Promise.resolve(null); | |
}); | |
} | |
function getIndicesOf(searchStr, str, caseSensitive) { | |
var searchStrLen = searchStr.length; | |
if (searchStrLen == 0) { | |
return []; | |
} | |
var startIndex = 0, index, indices = []; | |
if (!caseSensitive) { | |
str = str.toLowerCase(); | |
searchStr = searchStr.toLowerCase(); | |
} | |
while ((index = str.indexOf(searchStr, startIndex)) > -1) { | |
indices.push(index); | |
startIndex = index + searchStrLen; | |
} | |
return indices; | |
} | |
function parseDiff(str) { | |
const | |
process = string => { | |
const lines = string.match(/\[3\dm.*?\[39m/g).map(s => s.slice(4, -4)); | |
let colon = lines[0].indexOf(':'), | |
i = 0, | |
key = ''; | |
if (lines[0][0] === '-' && lines[1][0] === '+' && lines[0].slice(1, colon) === lines[1].slice(1, colon)) { | |
return [ | |
'UPDATE A KEY', | |
`key name : ${lines[0].slice(1, colon).trim()}`, | |
`old value: ${JSON.parse(JSON.stringify(lines[i].slice(colon + 1)))}`, | |
`new value: ${JSON.parse(JSON.stringify(lines[i].slice(colon + 1)))}` | |
]; | |
} | |
while (lines[i].slice(colon + 1).trim() === '{') { | |
key += (key && '.') + lines[i].slice(1, colon).trim(); | |
colon = lines[++i].indexOf(':'); | |
} | |
key += (key && '.') + lines[i].slice(1, colon).trim(); | |
return [ | |
lines[0][0] === '-' ? 'DELETE A KEY' : 'ADD A KEY', | |
`key name: ${key}`, | |
`value : ${JSON.parse(JSON.stringify(lines[i].slice(colon + 1)))}` | |
]; | |
}, | |
data = [ | |
str | |
]; | |
return data.map(process); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment