From 10ed5ca98790e2aff7a3c8380d0d450de9c2e949 Mon Sep 17 00:00:00 2001 From: Ivan Kolesnikov Date: Mon, 26 Mar 2018 17:39:08 -0700 Subject: [PATCH] Handle messages with ForcedFetch class 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 --- src/graphQLUtils.js | 24 ++++++++++++++++++++++-- src/listen.js | 17 ++++++++++++++++- 2 files changed, 38 insertions(+), 3 deletions(-) diff --git a/src/graphQLUtils.js b/src/graphQLUtils.js index 7adf881a..b6ee7d88 100644 --- a/src/graphQLUtils.js +++ b/src/graphQLUtils.js @@ -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 +}; diff --git a/src/listen.js b/src/listen.js index 081581cb..305d7612 100644 --- a/src/listen.js +++ b/src/listen.js @@ -1,6 +1,7 @@ "use strict"; var utils = require("../utils"); +var graphQLUtils = require("./graphQLUtils"); var log = require("npmlog"); var msgsRecv = 0; @@ -142,6 +143,7 @@ module.exports = function(defaultFuncs, api, ctx) { } var atLeastOne = false; + var pendingForceFetch = []; if (resData.ms) { msgsRecv += resData.ms.length; resData.ms @@ -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" && @@ -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