Commit 1d942d41 authored by Killian Kemps's avatar Killian Kemps

Manage each conversation separately on front-end

parent e41ab84a
......@@ -21,13 +21,42 @@ function getUserInfo(api, userID) {
exports.getUserInfo = getUserInfo;
exports.getThreadInfo = function(api, threadID) {
exports.getThreadInfo = function(currentUserID, api, threadID) {
return new Promise(function (resolve, reject) {
if (fbThreads[threadID]) resolve(fbThreads[threadID])
api.getThreadInfo(threadID, function(err, ret) {
if(err) return console.error(err);
fbThreads[threadID] = ret.name;
resolve(fbThreads[threadID]);
var userInfoPromises = [];
if (ret.isCanonicalUser || ret.name === '') {
for (var z = 0; z < ret.participantIDs.length; z++) {
// Don't put current user in thread's names and images
if (ret.participantIDs[z] !== currentUserID){
userInfoPromises.push(getUserInfo(api, ret.participantIDs[z]));
}
}
}
// Get all user infos and then update the thread list
Promise.all(userInfoPromises).then(function (userInfoArr) {
if (ret.name === '') {
for (var y = 0; y < userInfoArr.length; y++) {
if (ret.participantIDs.indexOf(userInfoArr[y].id) > -1){
if (ret.name !== '') {
ret.name += ', ' + userInfoArr[y].name;
}
else {
ret.name = userInfoArr[y].name;
}
}
}
}
fbThreads[threadID] = {
name: ret.name,
}
resolve(fbThreads[threadID]);
});
});
});
}
......@@ -40,10 +69,10 @@ exports.getThreadList = function(currentUserID, api, nbThreads) {
// Populate threads with no name and no image
for (var i = 0; i < arr.length; i++) {
if (arr[i].isCanonicalUser || arr[i].name === '') {
for (var z = 0; z < arr[i].participants.length; z++) {
for (var z = 0; z < arr[i].participantIDs.length; z++) {
// Don't put current user in thread's names and images
if (arr[i].participants[z] !== currentUserID){
userInfoPromises.push(getUserInfo(api, arr[i].participants[z]));
if (arr[i].participantIDs[z] !== currentUserID){
userInfoPromises.push(getUserInfo(api, arr[i].participantIDs[z]));
}
}
}
......@@ -53,7 +82,7 @@ exports.getThreadList = function(currentUserID, api, nbThreads) {
for (var i = 0; i < arr.length; i++) {
if (arr[i].name === '') {
for (var y = 0; y < userInfoArr.length; y++) {
if (arr[i].participants.indexOf(userInfoArr[y].id) > -1){
if (arr[i].participantIDs.indexOf(userInfoArr[y].id) > -1){
arr[i].imageSrc = userInfoArr[y].img;
if (arr[i].name !== '') {
arr[i].name += ', ' + userInfoArr[y].name;
......
......@@ -51,8 +51,7 @@ function loginFacebookAppstate (email) {
users[email] = {
ID: 0,
fbID: 0,
fbApi: api,
lastThreadID: 0
fbApi: api
};
resolve(users[email]);
});
......@@ -74,8 +73,7 @@ function loginFacebookCredentials (email, password) {
users[email] = {
ID:0,
fbID: 0,
fbApi: api,
lastThreadID: 0
fbApi: api
};
resolve(users[email]);
});
......@@ -114,14 +112,13 @@ function startFacebook(decoded, users, socket) {
currentUser.fbApi.listen(function callback(err, message) {
if (err) return console.error(err);
console.log(message);
currentUser.lastThreadID = message.threadID;
var allInfos = [];
console.log(Object.keys(facebookMessengerService));
allInfos.push(facebookMessengerService.getUserInfo(currentUser.fbApi, message.senderID));
allInfos.push(facebookMessengerService.getThreadInfo(currentUser.fbApi, message.threadID));
allInfos.push(facebookMessengerService.getThreadInfo(currentUser.ID, currentUser.fbApi, message.threadID));
Promise.all(allInfos).then(function(data) {
console.log(data);
messageToSend = '[thread: ' + data[1] + '] ' + data[0].name + ': ' + message.body;
messageToSend = '[thread: ' + data[1].name + '] ' + data[0].name + ': ' + message.body;
socket.emit('chat message', messageToSend);
if (Array.isArray(chatHistory[currentUser.ID])) {
chatHistory[currentUser.ID].push(messageToSend);
......@@ -217,6 +214,7 @@ io.on('connection', function(socket){
socket.on('chat message', function(payload){
const token = payload.token;
const msg = payload.msg;
const threadID = payload.conversationID;
jwt.verify(token, app.get('superSecret'), function(err, decoded) {
if (err) {
const message = 'Failed to authenticate token.';
......@@ -225,21 +223,15 @@ io.on('connection', function(socket){
var currentUser = users[decoded.email];
if (currentUser) {
if (currentUser.lastThreadID != 0) {
facebookMessengerService.getThreadInfo(
currentUser.ID,
currentUser.fbApi,
currentUser.lastThreadID
threadID
).then(function(data) {
info = 'You were going to send to ' + data + ' (' + currentUser.lastThreadID + ')';
info = 'You were going to send to ' + data.name + ' (' + threadID + ')';
console.log(info);
socket.emit('chat message', info);
});
}
else {
info = 'No FB thread to send to';
console.log(info);
socket.emit('chat message', info);
}
socket.emit('chat message', msg);
}
else {
......
......@@ -26,11 +26,16 @@ export default {
availability: 'Available',
};
},
created: function () {
const msg = 'You are speaking in ' + this.$route.params.conversationName;
this.messages.push(msg);
},
methods: {
sendMsg: function sendMsg() {
const payload = {
token: localStorage.getItem('qowala-token'),
msg: this.messageInput
msg: this.messageInput,
conversationID: this.$route.params.conversationID
};
this.$socket.emit('chat message', payload);
this.messageInput = '';
......
......@@ -2,7 +2,7 @@
<div>
<ul id="conversations">
<li v-for="conversation in conversations">
<router-link to="/conversation">
<router-link :to="{ name: 'conversation', params: { conversationID: conversation.threadID , conversationName: conversation.name}}">
{{ conversation.name }}
<img v-bind:src="conversation.imageSrc" width="30px"/>
{{ conversation.snippet }}
......
......@@ -24,7 +24,8 @@ const routes = [
component: Login
},
{
path: '/conversation',
path: '/conversation/:conversationID/:conversationName',
name: 'conversation',
component: Conversation,
meta: { requiresAuth: true }
}
......
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