Skip to content

Commit

Permalink
Merge pull request Schmavery#435 from GAMELASTER/feature-forward
Browse files Browse the repository at this point in the history
Attachment forwarding
  • Loading branch information
bsansouci committed Apr 1, 2017
2 parents 16141b0 + 237eee9 commit 5b8e412
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 0 deletions.
13 changes: 13 additions & 0 deletions DOCS.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
* [`api.createPoll`](#createPoll)
* [`api.deleteMessage`](#deleteMessage)
* [`api.deleteThread`](#deleteThread)
* [`api.forwardAttachment`](#forwardAttachment)
* [`api.getAppState`](#getAppState)
* [`api.getCurrentUserID`](#getCurrentUserID)
* [`api.getFriendsList`](#getFriendsList)
Expand Down Expand Up @@ -413,6 +414,18 @@ login({appState: JSON.parse(fs.readFileSync('appstate.json', 'utf8'))}, (err, ap
---------------------------------------
<a name="forwardAttachment"></a>
### api.forwardAttachment(attachmentID, userOrUsers[, callback])
Forwards corresponding attachment to given userID or to every user from an array of userIDs
__Arguments__
* `attachmentID`: The ID field in the attachment object. Not all attachment have IDs: recorded audio and arbitrary files don't for example.
* `userOrUsers`: A userID string or usersID string array
* `callback(err)`: A callback called when the query is done (either with an error or null).
---------------------------------------
<a name="getAppState"></a>
### api.getAppState()
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ Result:
* [`api.createPoll`](DOCS.md#createPoll)
* [`api.deleteMessage`](DOCS.md#deleteMessage)
* [`api.deleteThread`](DOCS.md#deleteThread)
* [`api.forwardAttachment`](DOCS.md#forwardAttachment)
* [`api.getAppState`](DOCS.md#getAppState)
* [`api.getCurrentUserID`](DOCS.md#getCurrentUserID)
* [`api.getFriendsList`](DOCS.md#getFriendsList)
Expand Down
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ function buildAPI(globalOptions, html, jar) {
'createPoll',
'deleteMessage',
'deleteThread',
'forwardAttachment',
'getCurrentUserID',
'getFriendsList',
'getThreadHistory',
Expand Down
43 changes: 43 additions & 0 deletions src/forwardAttachment.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
"use strict";

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

module.exports = function(defaultFuncs, api, ctx) {
return function forwardAttachment(attachmentID, userOrUsers, callback) {
if (!callback) {
callback = function() {};
}

var form = {
attachment_id: attachmentID,
};

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

var timestamp = Math.floor(Date.now() / 1000);

for (var i = 0; i < userOrUsers.length; i++) {
//That's good, the key of the array is really timestmap in seconds + index
//Probably time when the attachment will be sent?
form['recipient_map[' + (timestamp + i) + ']'] = userOrUsers[i];
}

defaultFuncs
.post("https://www.messenger.com/mercury/attachments/forward/", ctx.jar, form)
.then(utils.parseAndCheckLogin(ctx.jar, defaultFuncs))
.then(function(resData) {
if (resData.error) {
throw resData;
}

return callback(null);
***REMOVED***
.catch(function(err) {
log.error("forwardAttachment", err);
return callback(err);
***REMOVED***;
};
};

0 comments on commit 5b8e412

Please sign in to comment.