Added accept / reject friendship requests

This commit is contained in:
Pierre 2017-06-04 16:02:51 +02:00
parent c27b2759b2
commit 29e1f3d68a
4 changed files with 133 additions and 40 deletions

View File

@ -98,6 +98,6 @@
} }
#friendsList .statusRow { #friendsList .statusRow {
width: 48px; width: 58px;
text-align: center; text-align: center;
} }

View File

@ -61,7 +61,8 @@ ComunicWeb.common.api.makeAPIrequest = function(apiURI, params, requireLoginToke
} }
//We can do the next step //We can do the next step
nextAction(result); if(nextAction)
nextAction(result);
} }
} }

View File

@ -110,42 +110,8 @@ ComunicWeb.components.friends.bar = {
//Show each friend //Show each friend
for(i in friendsList){ for(i in friendsList){
//Show informations about the friend
//Extract friend ID ComunicWeb.components.friends.bar.showFriendInfos(usersInfos["user-"+friendsList[i].ID_friend], friendsList[i], listFriendsElem);
var friendID = friendsList[i].ID_friend;
//Create a row
var friendRow = createElem("tr", listFriendsElem);
//Add user avatar
var imageRow = createElem("td", friendRow);
var imageElem = createElem("img", imageRow);
imageElem.src = usersInfos["user-"+friendID].accountImage;
imageElem.className = "account-image";
//Add user name
var nameRow = createElem("td", friendRow);
nameRow.innerHTML = usersInfos["user-"+friendID].firstName + " " + usersInfos["user-"+friendID].lastName;
//Add user login status
var statusRow = createElem("td", friendRow);
statusRow.className = "statusRow";
//Check if user is online or not
var currentTime = ComunicWeb.common.date.time();
var timeDifference = currentTime - friendsList[i].time_last_activity;
if(timeDifference < 30){
//User is logged in
var iconsStats = createElem("i", statusRow);
iconsStats.className = "fa fa-fw fa-circle";
iconsStats.style.color = "green";
}
else {
//User isn't logged in
var logoutTime = createElem("small", statusRow);
logoutTime.innerHTML = ComunicWeb.common.date.diffToStr(timeDifference);
}
} }
//Enable slimscroll //Enable slimscroll
@ -157,6 +123,80 @@ ComunicWeb.components.friends.bar = {
}); });
}, },
/**
* Show a friend informations
*
* @param {Object} userInfos Informations about the user
* @param {Object} friendshipInfos Informations about the friendship
* @param {HTMLElement} listFriendsElem The target for the friends list
* @return {Boolean} True for a success
*/
showFriendInfos: function(userInfos, friendshipInfos, listFriendsElem){
//Extract friend ID
var friendID = friendshipInfos.ID_friend;
//Create a row
var friendRow = createElem("tr", listFriendsElem);
//Add user avatar
var imageRow = createElem("td", friendRow);
var imageElem = createElem("img", imageRow);
imageElem.src = userInfos.accountImage;
imageElem.className = "account-image";
//Add user name
var nameRow = createElem("td", friendRow);
nameRow.innerHTML = userInfos.firstName + " " + userInfos.lastName;
//Add user login status
var statusRow = createElem("td", friendRow);
statusRow.className = "statusRow";
//Check if the user was accepted or not
if(friendshipInfos.accepted == "1"){
//Check if user is online or not
var currentTime = ComunicWeb.common.date.time();
var timeDifference = currentTime - friendshipInfos.time_last_activity;
if(timeDifference < 30){
//User is logged in
var iconsStats = createElem("i", statusRow);
iconsStats.className = "fa fa-fw fa-circle";
iconsStats.style.color = "green";
}
else {
//User isn't logged in
var logoutTime = createElem("small", statusRow);
logoutTime.innerHTML = ComunicWeb.common.date.diffToStr(timeDifference);
}
}
else {
//We offer user to accept invitation
var acceptButton = createElem("button", statusRow);
acceptButton.className = "btn btn-xs btn-success";
acceptButton.innerHTML = "<i class='fa fa-check'></i>";
acceptButton.onclick = function(){
ComunicWeb.components.friends.bar.processFriendShipRequest(friendID, true, statusRow)
};
//Insert space
var space = createElem("span", statusRow);
space.innerHTML = "&nbsp";
//But he can also refuse it
var refuseButton = createElem("button", statusRow);
refuseButton.className = "btn btn-xs btn-danger";
refuseButton.innerHTML = "<i class='fa fa-times'></i>";
refuseButton.onclick = function(){
ComunicWeb.components.friends.bar.processFriendShipRequest(friendID, false, statusRow)
};
}
//Sucess
return true;
},
/** /**
* Toogle show / hide friends bar * Toogle show / hide friends bar
* *
@ -180,6 +220,31 @@ ComunicWeb.components.friends.bar = {
//Success //Success
return true; return true;
} },
/**
* Accept / Deny a friendship request
*
* @param {Integer} friendID The ID of the friend to accept / rejet
* @param {Boolean} accepted True if friendship is accepted, false else
* @param {HTMLElement} statusRow The parent node of friendship area
* @return {Boolean} True for a success
*/
processFriendShipRequest: function(friendID, accepted, statusRow){
//Log action
ComunicWeb.debug.logMessage("Process friendship request "+friendID);
//Change statusRow style
if(accepted)
statusRow.innerHTML = "Accepted";
else
statusRow.innerHTML = "Refused";
//Perform an API request
ComunicWeb.components.friends.list.respondRequest(friendID, accepted);
},
} }

View File

@ -61,5 +61,32 @@ ComunicWeb.components.friends.list = {
get: function(){ get: function(){
//Return the list //Return the list
return this.__list; return this.__list;
} },
/**
* Respond a friendship request
*
* @param {Integer} friendID The friend ID to respond
* @param {Boolean} accept Specify if the request was accepted or not
* @param {Function} afterResponse Specify an action to do next
* @return {Boolean} True for a success
*/
respondRequest: function(friendID, accept, afterResponse){
//Prepare the API request
var apiURI = "friends/respondRequest"
var params = {
"friendID": friendID,
};
if(accept)
params.accept = "true";
else
params.accept = "false";
//Process request
ComunicWeb.common.api.makeAPIrequest(apiURI, params, true, afterResponse);
//Success
return true;
},
}; };