File size: 3,074 Bytes
e192d16 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
"use strict";
const utils = require("../utils");
const log = require("npmlog");
module.exports = function (defaultFuncs, api, ctx) {
function handleUpload(image, callback) {
const uploads = [];
const form = {
images_only: "true",
"attachment[]": image
};
uploads.push(
defaultFuncs
.postFormData(
"https://upload.facebook.com/ajax/mercury/upload.php",
ctx.jar,
form,
{}
)
.then(utils.parseAndCheckLogin(ctx, defaultFuncs))
.then(function (resData) {
if (resData.error) {
throw resData;
}
return resData.payload.metadata[0];
})
);
// resolve all promises
Promise
.all(uploads)
.then(function (resData) {
callback(null, resData);
})
.catch(function (err) {
log.error("handleUpload", err);
return callback(err);
});
}
return function changeGroupImage(image, threadID, callback) {
if (
!callback &&
(utils.getType(threadID) === "Function" ||
utils.getType(threadID) === "AsyncFunction")
) {
throw { error: "please pass a threadID as a second argument." };
}
if (!utils.isReadableStream(image)) {
throw { error: "please pass a readable stream as a first argument." };
}
let resolveFunc = function () { };
let rejectFunc = function () { };
const returnPromise = new Promise(function (resolve, reject) {
resolveFunc = resolve;
rejectFunc = reject;
});
if (!callback) {
callback = function (err) {
if (err) {
return rejectFunc(err);
}
resolveFunc();
};
}
const messageAndOTID = utils.generateOfflineThreadingID();
const form = {
client: "mercury",
action_type: "ma-type:log-message",
author: "fbid:" + (ctx.i_userID || ctx.userID),
author_email: "",
ephemeral_ttl_mode: "0",
is_filtered_content: false,
is_filtered_content_account: false,
is_filtered_content_bh: false,
is_filtered_content_invalid_app: false,
is_filtered_content_quasar: false,
is_forward: false,
is_spoof_warning: false,
is_unread: false,
log_message_type: "log:thread-image",
manual_retry_cnt: "0",
message_id: messageAndOTID,
offline_threading_id: messageAndOTID,
source: "source:chat:web",
"source_tags[0]": "source:chat",
status: "0",
thread_fbid: threadID,
thread_id: "",
timestamp: Date.now(),
timestamp_absolute: "Today",
timestamp_relative: utils.generateTimestampRelative(),
timestamp_time_passed: "0"
};
handleUpload(image, function (err, payload) {
if (err) {
return callback(err);
}
form["thread_image_id"] = payload[0]["image_id"];
form["thread_id"] = threadID;
defaultFuncs
.post("https://www.facebook.com/messaging/set_thread_image/", ctx.jar, form)
.then(utils.parseAndCheckLogin(ctx, defaultFuncs))
.then(function (resData) {
// check for errors here
if (resData.error) {
throw resData;
}
return callback();
})
.catch(function (err) {
log.error("changeGroupImage", err);
return callback(err);
});
});
return returnPromise;
};
};
|