Skip to content
This repository has been archived by the owner on Jul 9, 2022. It is now read-only.

Commit

Permalink
Extract sending deliveryReceipt into a separate promise
Browse files Browse the repository at this point in the history
  • Loading branch information
ivankolesnikov committed Dec 1, 2019
1 parent 3ca83be commit dbeb66c
Showing 1 changed file with 32 additions and 28 deletions.
60 changes: 32 additions & 28 deletions src/listen.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,9 @@ module.exports = function(defaultFuncs, api, ctx) {
return;
}

var atLeastOne = false;
if (resData.ms) {
msgsRecv += resData.ms.length;
var atLeastOne = false;
resData.ms
.sort(function(a, b) {
return a.timestamp - b.timestamp;
Expand Down Expand Up @@ -394,32 +394,6 @@ module.exports = function(defaultFuncs, api, ctx) {
break;
}
});

if (atLeastOne) {
// Send deliveryReceipt notification to the server
var formDeliveryReceipt = {};

resData.ms
.filter(function(v) {
return (
v.message &&
v.message.mid &&
v.message.sender_fbid.toString() !== ctx.userID
);
})
.forEach(function(val, i) {
formDeliveryReceipt["[" + i + "]"] = val.message.mid;
});

// If there's at least one, we do the post request
if (formDeliveryReceipt["[0]"]) {
defaultFuncs.post(
"https://www.facebook.com/ajax/mercury/delivery_receipts.php",
ctx.jar,
formDeliveryReceipt
);
}
}
}

if (resData.seq) {
Expand All @@ -431,7 +405,37 @@ module.exports = function(defaultFuncs, api, ctx) {
if (currentlyRunning) {
currentlyRunning = setTimeout(listen, Math.random() * 200 + 50);
}
return;
return {
resData: resData,
atLeastOne: atLeastOne,
};
})
.then(function(res) {
if (res && res.atLeastOne) {
// Send deliveryReceipt notification to the server
var formDeliveryReceipt = {};

res.resData.ms
.filter(function(v) {
return (
v.message &&
v.message.mid &&
v.message.sender_fbid.toString() !== ctx.userID
);
})
.forEach(function(val, i) {
formDeliveryReceipt["[" + i + "]"] = val.message.mid;
});

// If there's at least one, we do the post request
if (formDeliveryReceipt["[0]"]) {
defaultFuncs.post(
"https://www.facebook.com/ajax/mercury/delivery_receipts.php",
ctx.jar,
formDeliveryReceipt
);
}
}
})
.catch(function(err) {
if (err.code === "ETIMEDOUT") {
Expand Down

0 comments on commit dbeb66c

Please sign in to comment.