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 Dec 1, 2019
1 parent dbeb66c commit 10ed5ca
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 3 deletions.
24 changes: 22 additions & 2 deletions src/graphQLUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -593,7 +593,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
};
17 changes: 16 additions & 1 deletion 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 @@ -142,6 +143,7 @@ module.exports = function(defaultFuncs, api, ctx) {
}

var atLeastOne = false;
var pendingForceFetch = [];
if (resData.ms) {
msgsRecv += resData.ms.length;
resData.ms
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 @@ -407,9 +413,18 @@ module.exports = function(defaultFuncs, api, ctx) {
}
return {
resData: resData,
atLeastOne: atLeastOne,
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
Expand Down

0 comments on commit 10ed5ca

Please sign in to comment.