1
0
mirror of https://gitlab.com/comunic/comunicapiv3 synced 2024-12-28 14:38:52 +00:00

Can respond to a friendship request

This commit is contained in:
Pierre HUBERT 2020-06-30 14:05:36 +02:00
parent 13f86fd8c1
commit e59541720d
4 changed files with 48 additions and 0 deletions

View File

@ -101,3 +101,19 @@ pub fn cancel_request(r: &mut HttpRequestHandler) -> RequestResult {
r.success("Friendship request removed!")
}
/// Respond to a friendship request
pub fn respond_request(r: &mut HttpRequestHandler) -> RequestResult {
let friend_id = r.post_user_id("friendID")?;
let accept = r.post_bool("accept")?;
if !friends_helper::sent_request(&friend_id, r.user_id_ref()?)? {
r.forbidden("No friendship request was sent by this user!".to_string())?;
}
friends_helper::respond_request(r.user_id_ref()?, &friend_id, accept)?;
// TODO : create a notification
r.set_response("Response to the friendship request successfully saved!")
}

View File

@ -101,6 +101,8 @@ pub fn get_routes() -> Vec<Route> {
Route::post("/friends/removeRequest", Box::new(friends_controller::cancel_request)),
Route::post("/friends/respondRequest", Box::new(friends_controller::respond_request)),
// Conversations controller
Route::post("/conversations/create", Box::new(conversations_controller::create)),

View File

@ -384,6 +384,14 @@ impl HttpRequestHandler {
self.user_id_opt().unwrap_or(UserID::invalid())
}
/// Get user ID as a reference
pub fn user_id_ref(&self) -> ResultBoxError<&UserID> {
match self.curr_user_id.as_ref() {
Some(s) => Ok(s),
None => Err(ExecError::boxed_new("Could not get required user ID!"))
}
}
/// Get an email included in the request
pub fn post_email(&mut self, name: &str) -> ResultBoxError<String> {
let mail = self.post_string(name)?;

View File

@ -89,6 +89,28 @@ pub fn sent_request(user_id: &UserID, target_user: &UserID) -> ResultBoxError<bo
.map(|f| f > 0)
}
/// Respond to a friendship request
pub fn respond_request(user_id: &UserID, friend_id: &UserID, accept: bool) -> ResultBoxError {
// Delete the request
remove_request(friend_id, user_id)?;
if accept {
database::InsertQuery::new(FRIENDS_TABLE)
.add_user_id("ID_personne", user_id)
.add_user_id("ID_amis", friend_id)
.add_legacy_bool("actif", true)
.insert_drop_result()?;
database::InsertQuery::new(FRIENDS_TABLE)
.add_user_id("ID_personne", friend_id)
.add_user_id("ID_amis", user_id)
.add_legacy_bool("actif", true)
.insert_drop_result()?;
}
Ok(())
}
/// Check out whether two users are friend or not
pub fn are_friend(user_one: &UserID, user_two: &UserID) -> ResultBoxError<bool> {
Ok(database::count(QueryInfo::new(FRIENDS_TABLE)