firebase functions stopped working after iam - firebase

Im trying to do something absurderlly simple but this whole IAM, Realtime Database, cloud functions misconfiguration are making me waste hours of work in something simple as a hello world.
I have an already populated database (over 300 items):
then i have the following function deployed to firebase cloud:
const actions = [];
const igDatabase = require('firebase-admin').initializeApp({
serviceAccountId: 'actionkeeper#igibo-b0b27.iam.gserviceaccount.com'
}).database("https://igibo-b0b27.firebaseio.com/");
let lastMapRefresh = 0;
let lastUpdateFirebase = 0;
function refreshActions(afterRefresh) {
console.log("refreshing actions");
igDatabase.ref('actions/').orderByChild('timestamp').startAt(lastMapRefresh).once('value').then(function(data) {
if (data != null && data.exists()) {
let bef = actions.length;
actions.length = 0;
actions.push(data.val());
lastMapRefresh = new Date().getTime();
afterRefresh();
}
console.log("actions refreshed before: " + bef + " now: " + actions.length);
}).catch(function(error) {
console.error("Error: " + JSON.stringify(error));
});
}
exports.decrementAction = (req, res) => {
refreshActions(function() {});
}
this function is simple reading a branch on database and populating an array... the purpose of the functio is more complex but im building it and testing slowly... and even this simple method is not working
the firebase rules for that node is:
{
"rules":{
"actions":{
".indexOn":[
"timestamp"
],
".read":"auth != null",
"$act":{
"countdown":{
".write":"auth != null && data.val() - newData.val() == 1 && newData.val() >= 0"
}
}
}
}
}
so ANYBODY logged can read
in the google IAM console i have
so the service account is supposed to have admin powers to database...
but running this function ALWAYS RETURN NULL data
why?

Your code doesn't appear to actually send a respond to the client. Here's your function:
exports.decrementAction = (req, res) => {
refreshActions(function() {});
}
Not once does it ever use res to send a response. It's always going to time out after the default 60s, stuck waiting for you to call res.send() or something similar.
I suggest reviewing the documentation on HTTP triggers to see how to send a response. I imagine your (currently empty) callback function needs to do this, based on what refreshFunctions delivers to it.

Related

Am I doing Firestore Transactions correct?

I've followed the Firestore documentation with relation to transactions, and I think I have it all sorted correctly, but in testing I am noticing issues with my documents not getting updated properly sometimes. It is possible that multiple versions of the document could be submitted to the function in a very short interval, but I am only interested in only ever keeping the most recent version.
My general logic is this:
New/Updated document is sent to cloud function
Check if document already exists in Firestore, and if not, add it.
If it does exist, check that it is "newer" than the instance in firestore, if it is, update it.
Otherwise, don't do anything.
Here is the code from my function that attempts to accomplish this...I would love some feedback if this is correct/best way to do this:
const ocsFlight = req.body;
const procFlight = processOcsFlightEvent(ocsFlight);
try {
const ocsFlightRef = db.collection(collection).doc(procFlight.fltId);
const originalFlight = await ocsFlightRef.get();
if (!originalFlight.exists) {
const response = await ocsFlightRef.set(procFlight);
console.log("Record Added: ", JSON.stringify(procFlight));
res.status(201).json(response); // 201 - Created
return;
}
await db.runTransaction(async (t) => {
const doc = await t.get(ocsFlightRef);
const flightDoc = doc.data();
if (flightDoc.recordModified <= procFlight.recordModified) {
t.update(ocsFlightRef, procFlight);
console.log("Record Updated: ", JSON.stringify(procFlight));
res.status(200).json("Record Updated");
return;
}
console.log("Record isn't newer, nothing changed.");
console.log("Record:", JSON.stringify("Same Flight:", JSON.stringify(procFlight)));
res.status(200).json("Record isn't newer, nothing done.");
return;
});
} catch (error) {
console.log("Error:", JSON.stringify(error));
res.status(500).json(error.message);
}
The Bugs
First, you are trusting the value of req.body to be of the correct shape. If you don't already have type assertions that mirror your security rules for /collection/someFlightId in processOcsFlightEvent, you should add them. This is important because any database operations from the Admin SDKs will bypass your security rules.
The next bug is sending a response to your function inside the transaction. Once you send a response back the client, your function is marked inactive - resources are severely throttled and any network requests may not complete or crash. As a transaction may be retried a handful of times if a database collision is detected, you should make sure to only respond to the client once the transaction has properly completed.
You use set to write the new flight to Firestore, this can lead to trouble when working with transactions as a set operation will cancel all pending transactions at that location. If two function instances are fighting over the same flight ID, this will lead to the problem where the wrong data can be written to the database.
In your current code, you return the result of the ocsFlightRef.set() operation to the client as the body of the HTTP 201 Created response. As the result of the DocumentReference#set() is a WriteResult object, you'll need to properly serialize it if you want to return it to the client and even then, I don't think it will be useful as you don't seem to use it for the other response types. Instead, a HTTP 201 Created response normally includes where the resource was written to as the Location header with no body, but here we'll pass the path in the body. If you start using multiple database instances, including the relevant database may also be useful.
Fixing
The correct way to achieve the desired result would be to do the entire read->check->write process inside of a transaction and only once the transaction has completed, then respond to the client.
So we can send the appropriate response to the client, we can use the return value of the transaction to pass data out of it. We'll pass the type of the change we made ("created" | "updated" | "aborted") and the recordModified value of what was stored in the database. We'll return these along with the resource's path and an appropriate message.
In the case of an error, we'll return a message to show the user as message and the error's Firebase error code (if available) or general message as the error property.
// if not using express to wrangle requests, assert the correct method
if (req.method !== "POST") {
console.log(`Denied ${req.method} request`);
res.status(405) // 405 - Method Not Allowed
.set("Allow", "POST")
.end();
return;
}
const ocsFlight = req.body;
try {
// process AND type check `ocsFlight`
const procFlight = processOcsFlightEvent(ocsFlight);
const ocsFlightRef = db.collection(collection).doc(procFlight.fltId);
const { changeType, recordModified } = await db.runTransaction(async (t) => {
const flightDoc = await t.get(ocsFlightRef);
if (!flightDoc.exists) {
t.set(ocsFlightRef, procFlight);
return {
changeType: "created",
recordModified: procFlight.recordModified
};
}
// only parse the field we need rather than everything
const storedRecordModified = flightDoc.get('recordModified');
if (storedRecordModified <= procFlight.recordModified) {
t.update(ocsFlightRef, procFlight);
return {
changeType: "updated",
recordModified: procFlight.recordModified
};
}
return {
changeType: "aborted",
recordModified: storedRecordModified
};
});
switch (changeType) {
case "updated":
console.log("Record updated: ", JSON.stringify(procFlight));
res.status(200).json({ // 200 - OK
path: ocsFlightRef.path,
message: "Updated",
recordModified,
changeType
});
return;
case "created":
console.log("Record added: ", JSON.stringify(procFlight));
res.status(201).json({ // 201 - Created
path: ocsFlightRef.path,
message: "Created",
recordModified,
changeType
});
return;
case "aborted":
console.log("Outdated record discarded: ", JSON.stringify(procFlight));
res.status(200).json({ // 200 - OK
path: ocsFlightRef.path,
message: "Record isn't newer, nothing done.",
recordModified,
changeType
});
return;
default:
throw new Error("Unexpected value for 'changeType': " + changeType);
}
} catch (error) {
console.log("Error:", JSON.stringify(error));
res.status(500) // 500 - Internal Server Error
.json({
message: "Something went wrong",
// if available, prefer a Firebase error code
error: error.code || error.message
});
}
References
Cloud Firestore Transactions
Cloud Firestore Node SDK Reference
HTTP Event Cloud Functions

fetching data keeps stop my react-native tab until fetching gets complete

I am retrieving data from cloud firestor in componentDidMount() method, but when fetching starts and during fetching data from cloud firestor I am not able to change tab, it takes 8 seconds for less than 100 items docs.
I am using react-navigation and cloud firestor in react-native app.
Even from cache it takes 8 seconds and I couldnt change tabs during fetching data from cloud firestor, but after fetching I am able to change tabs.
getMessages(){
db.collection("users/" + this.state.username + "/msgs").orderBy("date", "asc").get().then(snapshot=>{
this.docs = snapshot.docs;
for (let i = this.docs.length - 1; i >= 0; i--) {
this.prtcpnts = this.state.currentuser === this.docs[i].data().user.username ? this.state.currentuser + this.docs[i].data().otheruser : this.state.currentuser + this.docs[i].data().user.username;
if (this.state[this.prtcpnts] === undefined){
this.setState({
[this.prtcpnts]: [this.docs[i].data()]
});
}else{
this.setState(preState => ({ [this.prtcpnts]: [...preState[this.prtcpnts], this.docs[i].data()] }));
}
}
});
}
I want to fetch smothly without stopping my app, I mean I should be able to change tab even during fetching data from cloud firestor.
I solved my problem by refactoring to the following code.
if (this.outState[this.prtcpnts] === undefined){
this.outState[this.prtcpnts] = [this.docs[i].data()];
}else{
this.outState[this.prtcpnts] = [...this.outState[this.prtcpnts], this.docs[i].data()]
}
if (i === 0) {
this.setState({ ...this.state, ...this.outState })
}

Firebase: Get notified just after the firebase-function trigger promise completed

In my app I pushed some object to my firebase-database and immediately after that (after the then-promise fully filled) I fetch the object (with the returned key) from the database (with the on-value method).
In addition, I make some changes on the pushed object using the firebase-functions.
How can I receive the object (in the app) just after the changes and not before? (like other ordinary backend services)
I hope this helps you, I have not tested this piece of code but it should help you in the right direction.
Also dont use this exact code in production, there is plenty room for improvement, this is just an example code.
exports.testFunction = functions.https.onRequest((req, res) => {
if (req && req.body) {
if (
req.body.hasOwnProperty('name') &&
req.body.hasOwnProperty('age')
) {
const person = {
name: req.body['name'],
age: req.body['age']
}
// Make some changes to the person object
person['hobby'] = 'Programmer';
// Add object to FireStore
admin
.firestore()
.collection('/persons')
.add(person)
.then((success) => {
// Return the added & changed person
res.status(201).send(JSON.stringify(person));
})
.catch((error) => {
// Error
console.error('Something broke', error)
res.status(500).send();
});
}
else {
// Error
res.status(500).send({err: 'Missing property'});
}
}
else {
// Error
res.status(500).send({err: 'Missing something'});
}
});

How can I delete a post from a supergroup in telegram with telegram-cli?

we have a group in telegram and we have a rule says no one must leave a message in group between 23 to 7 am , I wanna delete messages comes to group between these times automatically . could anyone tell me how I can do that with telegram cli or any other telegram client?
Use new version of telegram-cli. It's not fully open source, but you can download a binary from its site. Also you can find some examples there.
I hope the following snippet in JavaScript will help you to achieve your goal.
var spawn = require('child_process').spawn;
var readline = require('readline');
// delay between restarts of the client in case of failure
const RESTARTING_DELAY = 1000;
// the main object for a process of telegram-cli
var tg;
function launchTelegram() {
tg = spawn('./telegram-cli', ['--json', '-DCR'],
{ stdio: ['ipc', 'pipe', process.stderr] });
readline.createInterface({ input: tg.stdout }).on('line', function(data) {
try {
var obj = JSON.parse(data);
} catch (err) {
if (err.name == 'SyntaxError') {
// sometimes client sends not only json, plain text process is not
// necessary, just output for easy debugging
console.log(data.toString());
} else {
throw err;
}
}
if (obj) {
processUpdate(obj);
}
});
tg.on('close', function(code) {
// sometimes telegram-cli fails due to bugs, then try to restart it
// skipping problematic messages
setTimeout(function(tg) {
tg.kill(); // the program terminates by sending double SIGINT
tg.kill();
tg.on('close', launchTelegram); // start again for updates
// as soon as it is finished
}, RESTARTING_DELAY, spawn('./telegram-cli', { stdio: 'inherit' }));
});
}
function processUpdate(upd) {
var currentHour = Date.now().getHours();
if (23 <= currentHour && currentHour < 7 &&
upd.ID='UpdateNewMessage' && upd.message_.can_be_deleted_) {
// if the message meets certain criteria, send a command to telegram-cli to
// delete it
tg.send({
'ID': 'DeleteMessages',
'chat_id_': upd.message_.chat_id_,
'message_ids_': [ upd.message_.id_ ]
});
}
}
launchTelegram(); // just launch these gizmos
We activate JSON mode passing --json key. telegram-cli appends underscore to all fields in objects. See all available methods in full schema.

Querying Firebase data with AngularFire2 in Ionic 2

I am using AngularFire2 with ionic 2 and storing the data in webSqlStorage.
When I complete the the first GET it saves it to storage. What I want to do is if the user has a connection then check the data from Firebase, if it has not changed, then get the local data, else get the updated data.
Currently, I have a simple check to see if there the storage is not null which then gets the local data, but this will not work for production.
let loader = this.loadingCtrl.create({
content: 'Getting Programs...'
});
loader.present().then(() => {
this.storage.get('programs').then((data) => {
if (data != null) {
loader.setContent("Getting Local Data...");
this.programs = data;
} else {
this.yApi.getPrograms().then(data => {
this.programs = data;
this.storage.set('programs', data);
},err => {
// Probaly offline with no local data
console.log("Err is to human");
});
}
}).then(() => {
loader.dismiss();
});
});
Just wondering if there is a way to write something like
if (data != null || this.af.checkUpdated('/programs')) { ...
or something.
I used an additional database value like program_version in my projects. If firebase program_version is greater than the local storage, than update. On every program update your program_version will be increased or set a timestamp.

Resources