1
0
mirror of https://gitlab.com/comunic/comunicapiv3 synced 2024-12-27 22:18:51 +00:00

Can get the latest posts

This commit is contained in:
Pierre HUBERT 2020-07-06 10:01:54 +02:00
parent fca15e15e1
commit f8413850ae
7 changed files with 110 additions and 3 deletions

View File

@ -35,7 +35,7 @@ pub fn create(r: &mut HttpRequestHandler) -> RequestResult {
/// Get the list of groups of the current user
pub fn get_list_user(r: &mut HttpRequestHandler) -> RequestResult {
let list = groups_helper::get_list_user(r.user_id()?, false)?
let list = groups_helper::get_list_user(r.user_id_ref()?, false)?
.iter()
.map(|f| f.id())
.collect::<Vec<u64>>();

View File

@ -33,5 +33,17 @@ pub fn get_list_group(r: &mut HttpRequestHandler) -> RequestResult {
.set_start_from(start_from)
.get_group(&group_id)?;
r.set_response(PostAPI::for_list(&posts, r.user_id_opt())?)
}
/// Get the latest posts of a group
pub fn get_latest(r: &mut HttpRequestHandler) -> RequestResult {
let start_from = r.post_u64_opt("startFrom", 0)?;
let include_groups = r.post_bool_opt("include_groups", false);
let posts = posts_helper::PostsQuery::new(r.user_id_opt())
.set_start_from(start_from)
.get_latest(include_groups)?;
r.set_response(PostAPI::for_list(&posts, r.user_id_opt())?)
}

View File

@ -202,6 +202,8 @@ pub fn get_routes() -> Vec<Route> {
Route::post("/posts/get_group", Box::new(posts_controller::get_list_group)),
Route::post("/posts/get_latest", Box::new(posts_controller::get_latest)),
// Virtual directory controller
Route::post("/user/findbyfolder", Box::new(virtual_directory_controller::find_user)),

View File

@ -201,6 +201,12 @@ impl QueryInfo {
self
}
/// Add a custom Group ID WHERE value
pub fn add_custom_where_argument_group_id(mut self, val: &GroupID) -> QueryInfo {
self.custom_where_ars.push(mysql::Value::UInt(val.id()));
self
}
/// Add a custom string WHERE value
pub fn add_custom_where_argument_str(mut self, val: &str) -> QueryInfo {
self.custom_where_ars.push(mysql::Value::from(val));

View File

@ -14,6 +14,8 @@ use crate::helpers::database::QueryInfo;
/// Structure used to get a friend information
pub struct GetFriendsQuery {
only_accepted: bool,
only_following: bool,
target_user: UserID,
friend_id: Option<UserID>,
}
@ -22,11 +24,23 @@ impl GetFriendsQuery {
/// Construct a new request
pub fn new(target_user: &UserID) -> GetFriendsQuery {
GetFriendsQuery {
only_accepted: false,
only_following: false,
target_user: target_user.clone(),
friend_id: None,
}
}
pub fn set_only_accepted(mut self, accepted: bool) -> GetFriendsQuery {
self.only_accepted = accepted;
self
}
pub fn set_only_following(mut self, following: bool) -> GetFriendsQuery {
self.only_following = following;
self
}
/// Get the list of friends
pub fn exec(self) -> ResultBoxError<Vec<Friend>> {
get_list(&self)
@ -58,6 +72,14 @@ fn get_list(friend_query: &GetFriendsQuery) -> ResultBoxError<Vec<Friend>> {
query = query.cond_user_id("ID_amis", friend_id);
}
if friend_query.only_accepted {
query = query.cond_legacy_bool("f.actif", true);
}
if friend_query.only_following {
query = query.cond_legacy_bool("f.abonnement", true);
}
query.exec(db_to_friend)
}

View File

@ -152,10 +152,10 @@ pub fn set_following(g: &GroupID, u: &UserID, follow: bool) -> ResultBoxError {
}
/// Get the list of groups of a user
pub fn get_list_user(user_id: UserID, only_followed: bool) -> ResultBoxError<Vec<GroupID>> {
pub fn get_list_user(user_id: &UserID, only_followed: bool) -> ResultBoxError<Vec<GroupID>> {
let mut query = database::QueryInfo::new(GROUPS_MEMBERS_TABLE)
.add_field("groups_id")
.cond_user_id("user_id", &user_id);
.cond_user_id("user_id", user_id);
if only_followed {
query = query.cond_legacy_bool("following", true);

View File

@ -78,6 +78,11 @@ impl PostsQuery {
pub fn get_group(self, group_id: &GroupID) -> ResultBoxError<Vec<Post>> {
get_group(&self, group_id)
}
/// Get the latest posts for a user
pub fn get_latest(self, include_groups_posts: bool) -> ResultBoxError<Vec<Post>> {
get_latest(&self, include_groups_posts)
}
}
/// Get the posts of `target_id`
@ -165,6 +170,66 @@ fn get_group(query: &PostsQuery, group_id: &GroupID) -> ResultBoxError<Vec<Post>
.exec(db_to_post)
}
/// Get the latest posts of a user
pub fn get_latest(query: &PostsQuery, include_group_posts: bool) -> ResultBoxError<Vec<Post>> {
let user_id = query.user_id.as_ref().ok_or(ExecError::new("Can not get latest posts of no user!"))?;
let visibility_level = PostVisibilityLevel::VISIBILITY_FRIENDS;
// Get the list of friends of the user
let friends_list = friends_helper::GetFriendsQuery::new(user_id)
.set_only_accepted(true)
.set_only_following(true)
.exec()?;
// Prepare the request
let mut db_query = database::QueryInfo::new(POSTS_TABLE);
let mut custom_where = String::new();
// ================== MEMBERSHIP CONDITION ======================
custom_where.push_str("(");
// ========== FRIENDS POSTS ===========
custom_where.push_str("(group_id = 0 AND niveau_visibilite <= ? AND (ID_personne = ?");
db_query = db_query.add_custom_where_argument_u32(visibility_level.to_db());
db_query = db_query.add_custom_where_argument_user_id(&user_id);
for f in &friends_list {
custom_where.push_str(" OR ID_personne = ?");
db_query = db_query.add_custom_where_argument_user_id(&f.friend_id);
};
custom_where.push_str("))");
// ========== /FRIENDS POSTS ==========
// =========== GROUPS POSTS ===========
if include_group_posts {
let groups = groups_helper::get_list_user(user_id, true)?;
for g in &groups {
custom_where.push_str(" OR group_id = ?");
db_query = db_query.add_custom_where_argument_group_id(g);
};
}
// ========== /GROUPS POSTS ===========
custom_where.push_str(")");
// ================== /MEMBERSHIP CONDITION =====================
// ======================= START POINT ==========================
if query.start_from > 0 {
custom_where.push_str("AND ID <= ?");
db_query = db_query.add_custom_where_argument_u64(query.start_from);
}
// ====================== /START POINT ==========================
db_query
.set_custom_where(&custom_where)
.set_order("ID DESC")
.set_limit(query.limit)
.exec(db_to_post)
}
/// Get the access level of a user over a post
pub fn get_access_level(p: &Post, user_id: &Option<UserID>) -> ResultBoxError<PostAccessLevel> {
if user_id == &p.user_id.as_option() {