Commit 84335019 authored by Killian Kemps's avatar Killian Kemps

Merge branch 'features/sender-image-metadata' into 'master'

Add sender image in each conversation's message

See merge request !9
parents 25b64f04 59913ce3
......@@ -107,3 +107,47 @@ exports.getThreadList = function(currentUserID, api, nbThreads) {
});
});
}
exports.getThreadHistory = function(api, userID, threadID) {
return new Promise(function (resolve, reject) {
api.getThreadHistory(threadID, 0, 10, '', function (err, data) {
if(err) return console.error(err);
// XXX Temporary fix to remove events from messages
// Permament fix is related to this issue: https://github.com/Schmavery/facebook-chat-api/issues/313
const filtered_data = data.filter(function(msg) {
return msg.body || msg.attachments.length > 0;
});
var userInfoPromises = [];
// Add custom properties
for (var i = 0; i < filtered_data.length; i++) {
const msg = filtered_data[i];
// Change user's id fbid:12356 to 123456
senderID = msg.senderID.slice(5);
userInfoPromises.push(getUserInfo(api, senderID));
}
// Get all user infos and then update the thread list
Promise.all(userInfoPromises).then(function (userInfoArr) {
var enhanced_data = [];
// Check that both array have same length
if (userInfoArr.length === filtered_data.length) {
for (var i = 0; i < filtered_data.length; i++) {
const msg = filtered_data[i];
const userData = userInfoArr[i];
msg.isSenderUser = senderID === userID;
msg.senderImage = userData.img;
enhanced_data.push(msg);
}
resolve(enhanced_data);
}
else {
console.error('userInfoArr and filtered_data have different length');
reject(enhanced_data);
}
});
});
});
}
......@@ -128,6 +128,7 @@ function startFacebook(decoded, users, socket) {
attachments: message.attachments,
senderID: message.senderID,
senderName: data[0].name,
senderImage: data[0].img,
isSenderUser: message.senderID === currentUser.ID.toString()
}
socket.emit('chat message', msgToSend);
......@@ -180,18 +181,8 @@ function getFBThreadHistory(decoded, users, socket, threadID) {
if (currentUser) {
(function() {
return new Promise(function (resolve, reject) {
currentUser.fbApi.getThreadHistory(threadID, 0, 10, '', function (err, data) {
if(err) return console.error(err);
// XXX Temporary fix to remove events from messages
// Permament fix is related to this issue: https://github.com/Schmavery/facebook-chat-api/issues/313
const filtered_data = data.filter(function(msg) {
return msg.body || msg.attachments.length > 0;
});
// Add custom property
const enhanced_data = filtered_data.map(function(msg) {
msg.isSenderUser = msg.senderID === 'fbid:' + currentUser.ID;
return msg;
});
facebookMessengerService.getThreadHistory(currentUser.fbApi, currentUser.ID, threadID)
.then(function(enhanced_data) {
socket.emit('return/threadHistory', enhanced_data);
resolve(enhanced_data);
});
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment