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

Commit

Permalink
Handle messages with ForcedFetch class
Browse files Browse the repository at this point in the history
Recently FB changes the API and it seems now a complex message
(e.g. a message with buttons) is not returned directly and has to be loaded
via /api/graphqlbatch
  • Loading branch information
ivankolesnikov committed Mar 27, 2018
1 parent 02f7d70 commit a342b85
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 30 deletions.
24 changes: 22 additions & 2 deletions src/graphQLUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -595,7 +595,27 @@ function graphQLBatch(ctx, defaultFuncs, query) {
});
}

function loadMessage(ctx, defaultFuncs, threadID, messageId) {
var query = {
// This doc_id was valid on March 26th 2018.
doc_id: "1801329719924418",
query_params: {
thread_and_message_id: {
thread_id: threadID,
message_id: messageId
}
}
};

return graphQLBatch(ctx, defaultFuncs, query)
.then(function(resData) {
// TODO@ Figure out how to get the correct thread type
return formatMessageGraphQLResponse(threadID, undefined, resData.message);
})
}

module.exports = {
formatMessageGraphQLResponse,
graphQLBatch
}
graphQLBatch,
loadMessage
};
75 changes: 47 additions & 28 deletions src/listen.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"use strict";

var utils = require("../utils");
var graphQLUtils = require("./graphQLUtils");
var log = require("npmlog");

var msgsRecv = 0;
Expand Down Expand Up @@ -141,9 +142,10 @@ module.exports = function(defaultFuncs, api, ctx) {
return;
}

var atLeastOne = false;
var pendingForceFetch = [];
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 @@ -234,6 +236,10 @@ module.exports = function(defaultFuncs, api, ctx) {
});
break;
case "delta":
if (v.delta.class === "ForcedFetch") {
pendingForceFetch.push(v.delta);
return;
}
if (
ctx.globalOptions.pageID ||
(v.delta.class !== "NewMessage" &&
Expand Down Expand Up @@ -394,32 +400,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 +411,46 @@ module.exports = function(defaultFuncs, api, ctx) {
if (currentlyRunning) {
currentlyRunning = setTimeout(listen, Math.random() * 200 + 50);
}
return;
return {
resData: resData,
atLeastOne: atLeastOne || pendingForceFetch.length > 0,
pendingForceFetch: pendingForceFetch,
};
})
.then(function(res) {
var pending = (res && res.pendingForceFetch) || [];
var promises = pending.map(e =>
graphQLUtils.loadMessage(ctx, defaultFuncs, e.threadKey.otherUserFbId, e.messageId)
.then(fmtMsg => globalCallback(null, fmtMsg)));
return Promise.all(promises)
.then(() => res)
})
.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 a342b85

Please sign in to comment.