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

Add message request support #301

Merged
merged 6 commits into from
Jul 21, 2016
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion DOCS.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
* [`api.deleteThread`](#deleteThread)
* [`api.getUserID`](#getUserID)
* [`api.getUserInfo`](#getUserInfo)
* [`api.handleMessageRequest`](#handleMessageRequest)
* [`api.listen`](#listen)
* [`api.logout`](#logout)
* [`api.markAsRead`](#markAsRead)
Expand Down Expand Up @@ -360,14 +361,15 @@ __Arguments__
---------------------------------------

<a name="getThreadList" />
### api.getThreadList(start, end, callback)
### api.getThreadList(start, end, pending, callback)

Will return information about threads.

__Arguments__

* `start`: Start index in the list of recently used threads.
* `end`: End index.
* `pending`: Optional boolean, set to true if you want the list of message requests; set to false or remove the argument if you want the inbox threads
* `callback(err, arr)`: A callback called when the query is done (either with an error or with an confirmation object). `arr` is an array of thread object containing the following properties: `threadID`, <del>`participants`</del>, `participantIDs`, `formerParticipants`, `name`, `snippet`, `snippetHasAttachment`, `snippetAttachments`, `snippetSender`, `unreadCount`, `messageCount`, `imageSrc`, `timestamp`, `serverTimestamp`, `muteSettings`, `isCanonicalUser`, `isCanonical`, `canonicalFbid`, `isSubscribed`, `rootMessageThreadingID`, `folder`, `isArchived`, `recipientsLoadable`, `hasEmailParticipant`, `readOnly`, `canReply`, `composerEnabled`, `blockedParticipants`, `lastMessageID`.

---------------------------------------
Expand Down Expand Up @@ -456,6 +458,19 @@ login({email: "FB_EMAIL", password: "FB_PASSWORD"}, function callback (err, api)

---------------------------------------

<a name="handleMessageRequest" />
### api.handleMessageRequest(threadID, accept, [callback])

Accept or ignore message request(s) with thread id `threadID`.

__Arguments__

* `threadID`: A threadID or array of threadIDs corresponding to the target thread(s). Can be numbers or strings.
* `accept`: Boolean indicating the new status to assign to the message request(s); true for inbox, false to others.
* `callback(err)`: A callback called when the query is done (with an error or with null).

---------------------------------------

<a name="listen" />
### api.listen(callback)

Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ login({email: "FB_EMAIL", password: "FB_PASSWORD"}, function callback (err, api)
* [`api.getThreadList`](DOCS.md#getThreadList)
* [`api.getUserID`](DOCS.md#getUserID)
* [`api.getUserInfo`](DOCS.md#getUserInfo)
* [`api.handleMessageRequest`](DOCS.md#handleMessageRequest)
* [`api.listen`](DOCS.md#listen)
* [`api.logout`](DOCS.md#logout)
* [`api.markAsRead`](DOCS.md#markAsRead)
Expand Down
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ function buildAPI(globalOptions, html, jar) {
'getThreadList',
'getUserID',
'getUserInfo',
'handleMessageRequest',
'listen',
'logout',
'markAsRead',
Expand Down
95 changes: 52 additions & 43 deletions src/getThreadInfo.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,49 +4,58 @@ var utils = require("../utils");
var log = require("npmlog");

module.exports = function(defaultFuncs, api, ctx) {
return function getThreadInfo(threadID, callback) {
if(!callback) callback = function() {};

var form = {
'client' : 'mercury'
};

api.getUserInfo(threadID, function(err, userRes) {
if(err) {
return callback(err);
}
var key = (Object.keys(userRes).length > 0) ? "user_ids" : "thread_fbids";
form['threads['+key+'][0]'] = threadID;

if(ctx.globalOptions.pageId) form.request_user_id = ctx.globalOptions.pageId;

defaultFuncs.post("https://www.facebook.com/ajax/mercury/thread_info.php", ctx.jar, form)
.then(utils.parseAndCheckLogin(ctx.jar, defaultFuncs))
.then(function(resData) {
if (resData.error) {
throw resData;
} else if (!resData.payload){
throw {error: "Could not retrieve thread Info."};
}

var threadData = resData.payload.threads[0];
var userData = userRes[threadID];
var info = {
participantIDs: threadData.participants.map(id => id.split(':').pop()),
name: userData != null && userData.name != null ? userData.name : threadData.name,
snippet: threadData.snippet,
messageCount: threadData.message_count,
emoji: threadData.custom_like_icon,
nicknames: threadData.custom_nickname,
color: threadData.custom_color,
};
callback(null, info);

}).catch(function(err) {
log.error("Error in getThreadInfo", err);
return callback(err);
return function getThreadInfo(threadID, callback) {
Copy link
Collaborator

@bsansouci bsansouci Jul 21, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you re-indent this back to 2 spaces

if (!callback) callback = function() {};

var form = {
'client': 'mercury'
};

api.getUserInfo(threadID, function(err, userRes) {
if (err) {
return callback(err);
}
var key = (Object.keys(userRes).length > 0) ? "user_ids" : "thread_fbids";
form['threads[' + key + '][0]'] = threadID;

if (ctx.globalOptions.pageId) form.request_user_id = ctx.globalOptions.pageId;

defaultFuncs.post("https://www.facebook.com/ajax/mercury/thread_info.php", ctx.jar, form)
.then(utils.parseAndCheckLogin(ctx.jar, defaultFuncs))
.then(function(resData) {
if (resData.error) {
throw resData;
} else if (!resData.payload) {
throw {
error: "Could not retrieve thread Info."
};
}

var threadData = resData.payload.threads[0];
var userData = userRes[threadID];

if (threadData == null) {
throw {
error: "ThreadData is null"
};
}

var info = {
participantIDs: threadData.participants.map(id => id.split(':').pop()),
name: userData != null && userData.name != null ? userData.name : threadData.name,
snippet: threadData.snippet,
messageCount: threadData.message_count,
emoji: threadData.custom_like_icon,
nicknames: threadData.custom_nickname,
color: threadData.custom_color,
};
callback(null, info);

}).catch(function(err) {
log.error("Error in getThreadInfo", err);
return callback(err);
});
});
});

};
};
};
74 changes: 44 additions & 30 deletions src/getThreadList.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,40 +4,54 @@ var utils = require("../utils");
var log = require("npmlog");

module.exports = function(defaultFuncs, api, ctx) {
return function getThreadList(start, end, callback) {
if(!callback && utils.getType(end) !== 'Number') {
throw {error: "please pass an number as a second argument."};
}
return function getThreadList(start, end, pending, callback) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

re-indent to 2 spaces

if (utils.getType(callback) === 'Undefined') {
if (utils.getType(end) !== 'Number') {
throw {
error: "please pass a number as a second argument."
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please capital letter.

};
} else if (utils.getType(pending) === 'Function') {
callback = pending;
pending = false; //default to inbox
} else if (utils.getType(pending) !== 'Boolean') {
throw {
error: "please pass a Boolean as a third argument."
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please capital letter.

};
} else {
throw {
error: "getThreadList: need callback"
};
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here you can add

else if (utils.getType(pending) != "Boolean") {
  throw {
    error: "Please pass a boolean as a third argument."
  };
}


if(!callback) {
throw {error: "getThreadList: need callback"};
}
if (end <= start) end = start + 20;

if (end <= start) end = start + 20;
var messageBox = pending ? "pending" : "inbox";

var form = {
'client' : 'mercury',
'inbox[offset]' : start,
'inbox[limit]' : end - start,
};
var form = {
'client': 'mercury'
};

if(ctx.globalOptions.pageID) {
form.request_user_id = ctx.globalOptions.pageID;
}
form[messageBox + '[offset]'] = start;
form[messageBox + '[limit]'] = end - start;

defaultFuncs
.post("https://www.facebook.com/ajax/mercury/threadlist_info.php", ctx.jar, form)
.then(utils.parseAndCheckLogin(ctx.jar, defaultFuncs))
.then(function(resData) {
if (resData.error) {
throw resData;
if (ctx.globalOptions.pageID) {
form.request_user_id = ctx.globalOptions.pageID;
}
log.verbose("Response in getThreadList: " + JSON.stringify(resData.payload.threads));
return callback(null, (resData.payload.threads || []).map(utils.formatThread));
})
.catch(function(err) {
log.error("Error in getThreadList", err);
return callback(err);
});
};

defaultFuncs
.post("https://www.facebook.com/ajax/mercury/threadlist_info.php", ctx.jar, form)
.then(utils.parseAndCheckLogin(ctx.jar, defaultFuncs))
.then(function(resData) {
if (resData.error) {
throw resData;
}
log.verbose("Response in getThreadList: " + JSON.stringify(resData.payload.threads));
return callback(null, (resData.payload.threads || []).map(utils.formatThread));
})
.catch(function(err) {
log.error("Error in getThreadList", err);
return callback(err);
});
};
};
47 changes: 47 additions & 0 deletions src/handleMessageRequest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
"use strict";

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

module.exports = function(defaultFuncs, api, ctx) {
return function handleMessageRequest(threadID, accept, callback) {
if (utils.getType(accept) !== 'Boolean') {
throw {
error: "please pass a boolean as a second argument."
};
}

if (!callback) {
callback = function() {};
}

var form = {
client: 'mercury'
};

if (utils.getType(threadID) !== "Array") {
threadID = [threadID];
}

var messageBox = accept ? "inbox" : "other";

for (var i = 0; i < threadID.length; i++) {
form[messageBox + '[' + i + ']'] = threadID[i];
}

defaultFuncs
.post("https://www.facebook.com/ajax/mercury/move_thread.php", ctx.jar, form)
.then(utils.parseAndCheckLogin(ctx.jar, defaultFuncs))
.then(function(resData) {
if (resData.error) {
throw resData;
}

return callback();
})
.catch(function(err) {
log.error("Error in handleMessageRequest", err);
return callback(err);
});
};
};