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

Attachment forwarding #435

Merged
merged 6 commits into from
Apr 1, 2017
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 @@ -412,6 +413,18 @@ login({appState: JSON.parse(fs.readFileSync('appstate.json', 'utf8'))}, (err, ap

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

<a name="forwardAttachment"></a>
### api.forwardAttachment(attachmentID, userOrUsers[, callback])

Takes a userID or an array of userIDs and forward the corresponding attachment.
Copy link
Contributor

Choose a reason for hiding this comment

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

I would rephrase it this way:
"Forwards corresponding attachment to given userID or to every user from an array of userIDs"

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done


__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 @@ -82,6 +82,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);
})
.catch(function(err) {
log.error("forwardAttachment", err);
return callback(err);
});
};
};