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 {
width: 48px;
width: 58px;
text-align: center;
}

View File

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

View File

@ -110,9 +110,30 @@ ComunicWeb.components.friends.bar = {
//Show each friend
for(i in friendsList){
//Show informations about the friend
ComunicWeb.components.friends.bar.showFriendInfos(usersInfos["user-"+friendsList[i].ID_friend], friendsList[i], listFriendsElem);
}
//Enable slimscroll
$(listFriendsElem.parentNode.parentNode).slimScroll({
height: '100%;'
});
});
});
},
/**
* 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 = friendsList[i].ID_friend;
var friendID = friendshipInfos.ID_friend;
//Create a row
var friendRow = createElem("tr", listFriendsElem);
@ -120,20 +141,23 @@ ComunicWeb.components.friends.bar = {
//Add user avatar
var imageRow = createElem("td", friendRow);
var imageElem = createElem("img", imageRow);
imageElem.src = usersInfos["user-"+friendID].accountImage;
imageElem.src = userInfos.accountImage;
imageElem.className = "account-image";
//Add user name
var nameRow = createElem("td", friendRow);
nameRow.innerHTML = usersInfos["user-"+friendID].firstName + " " + usersInfos["user-"+friendID].lastName;
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 - friendsList[i].time_last_activity;
var timeDifference = currentTime - friendshipInfos.time_last_activity;
if(timeDifference < 30){
//User is logged in
@ -147,14 +171,30 @@ ComunicWeb.components.friends.bar = {
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)
};
//Enable slimscroll
$(listFriendsElem.parentNode.parentNode).slimScroll({
height: '100%;'
});
//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;
},
/**
@ -180,6 +220,31 @@ ComunicWeb.components.friends.bar = {
//Success
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(){
//Return the 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;
},
};