1
0
mirror of https://gitlab.com/comunic/comunicapiv3 synced 2025-06-21 00:45:18 +00:00

Can get the status of a friendship

This commit is contained in:
2020-06-30 09:50:31 +02:00
parent 31ed53aacf
commit 91d12bb061
7 changed files with 102 additions and 2 deletions

View File

@ -3,9 +3,11 @@
//! @author Pierre Hubert
use crate::constants::database_tables_names::{FRIENDS_TABLE, USERS_TABLE};
use crate::data::error::{ExecError, ResultBoxError};
use crate::data::friend::Friend;
use crate::data::friendship_status::FriendshipStatus;
use crate::data::user::UserID;
use crate::helpers::database;
use crate::helpers::database::QueryInfo;
@ -59,6 +61,16 @@ fn get_list(friend_query: &GetFriendsQuery) -> ResultBoxError<Vec<Friend>> {
query.exec(db_to_friend)
}
/// Check out wheterher a user has sent a request to another user
pub fn sent_request(user_id: &UserID, target_user: &UserID) -> ResultBoxError<bool> {
database::QueryInfo::new(FRIENDS_TABLE)
.cond_user_id("ID_personne", target_user)
.cond_user_id("ID_amis", user_id)
.cond_legacy_bool("actif", false)
.exec_count()
.map(|f| f > 0)
}
/// 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)
@ -85,6 +97,42 @@ pub fn can_post_texts(user_id: &UserID, target_user: &UserID) -> ResultBoxError<
.or(Ok(false))
}
/// Check out whether a user is following another user or not
pub fn is_following(user_id: &UserID, friend_id: &UserID) -> ResultBoxError<bool> {
database::QueryInfo::new(FRIENDS_TABLE)
.cond_user_id("ID_personne", user_id)
.cond_user_id("ID_amis", friend_id)
.cond_legacy_bool("actif", true)
.cond_legacy_bool("abonnement", true)
.exec_count()
.map(|f| f > 0)
}
/// Get the status of a friendship
pub fn get_status(user_id: &UserID, friend_id: &UserID) -> ResultBoxError<FriendshipStatus> {
let mut status = FriendshipStatus {
are_friend: false,
sent_request: false,
received_request: false,
following: false,
};
status.are_friend = are_friend(user_id, friend_id)?;
match status.are_friend {
false => {
status.sent_request = sent_request(user_id, friend_id)?;
status.received_request = sent_request(friend_id, user_id)?;
}
true => {
status.following = is_following(user_id, friend_id)?;
}
}
Ok(status)
}
/// Turn a database entry into a Friend structure
fn db_to_friend(row: &database::RowResult) -> ResultBoxError<Friend> {
Ok(Friend {