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

View File

@ -110,9 +110,30 @@ ComunicWeb.components.friends.bar = {
//Show each friend //Show each friend
for(i in friendsList){ 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 //Extract friend ID
var friendID = friendsList[i].ID_friend; var friendID = friendshipInfos.ID_friend;
//Create a row //Create a row
var friendRow = createElem("tr", listFriendsElem); var friendRow = createElem("tr", listFriendsElem);
@ -120,20 +141,23 @@ ComunicWeb.components.friends.bar = {
//Add user avatar //Add user avatar
var imageRow = createElem("td", friendRow); var imageRow = createElem("td", friendRow);
var imageElem = createElem("img", imageRow); var imageElem = createElem("img", imageRow);
imageElem.src = usersInfos["user-"+friendID].accountImage; imageElem.src = userInfos.accountImage;
imageElem.className = "account-image"; imageElem.className = "account-image";
//Add user name //Add user name
var nameRow = createElem("td", friendRow); 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 //Add user login status
var statusRow = createElem("td", friendRow); var statusRow = createElem("td", friendRow);
statusRow.className = "statusRow"; statusRow.className = "statusRow";
//Check if the user was accepted or not
if(friendshipInfos.accepted == "1"){
//Check if user is online or not //Check if user is online or not
var currentTime = ComunicWeb.common.date.time(); var currentTime = ComunicWeb.common.date.time();
var timeDifference = currentTime - friendsList[i].time_last_activity; var timeDifference = currentTime - friendshipInfos.time_last_activity;
if(timeDifference < 30){ if(timeDifference < 30){
//User is logged in //User is logged in
@ -147,14 +171,30 @@ ComunicWeb.components.friends.bar = {
logoutTime.innerHTML = ComunicWeb.common.date.diffToStr(timeDifference); 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 //Insert space
$(listFriendsElem.parentNode.parentNode).slimScroll({ var space = createElem("span", statusRow);
height: '100%;' 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 //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;
},
}; };