mirror of
https://gitlab.com/comunic/comunicapiv3
synced 2025-06-21 17:05:16 +00:00
Upgrade UserID to a structure
This commit is contained in:
@ -25,14 +25,14 @@ pub fn login_user(email: &str, password: &str, client: &APIClient) -> ResultBoxE
|
||||
}
|
||||
|
||||
// Check if we already have a login token for this user
|
||||
if let Ok(token) = get_client_tokens(user.id, client) {
|
||||
if let Ok(token) = get_client_tokens(&user.id, client) {
|
||||
return Ok(token.token);
|
||||
}
|
||||
|
||||
|
||||
// Create new login tokens
|
||||
let new_token = UserAccessToken {
|
||||
user_id: user.id,
|
||||
user_id: user.id.clone(),
|
||||
client_id: client.id,
|
||||
token: rand_str(150),
|
||||
};
|
||||
@ -40,7 +40,7 @@ pub fn login_user(email: &str, password: &str, client: &APIClient) -> ResultBoxE
|
||||
// Save it
|
||||
database::insert(
|
||||
InsertQuery::new(USER_ACCESS_TOKENS_TABLE)
|
||||
.add_i64("user_id", new_token.user_id)
|
||||
.add_user_id("user_id", &new_token.user_id)
|
||||
.add_u32("service_id", client.id)
|
||||
.add_str("token1", &new_token.token)
|
||||
.add_str("token2", "dummy_data")
|
||||
@ -50,14 +50,14 @@ pub fn login_user(email: &str, password: &str, client: &APIClient) -> ResultBoxE
|
||||
}
|
||||
|
||||
/// Get user login tokens
|
||||
fn get_client_tokens(user_id: UserID, client: &APIClient) -> ResultBoxError<UserAccessToken> {
|
||||
fn get_client_tokens(user_id: &UserID, client: &APIClient) -> ResultBoxError<UserAccessToken> {
|
||||
database::query_row(
|
||||
QueryInfo::new(USER_ACCESS_TOKENS_TABLE)
|
||||
.cond_i64("user_id", user_id)
|
||||
.cond_user_id("user_id", user_id)
|
||||
.cond_u32("service_id", client.id),
|
||||
|res| {
|
||||
Ok(UserAccessToken {
|
||||
user_id: res.get_int64("user_id")?,
|
||||
user_id: res.get_user_id("user_id")?,
|
||||
client_id: res.get_int64("service_id")? as u32,
|
||||
token: res.get_str("token1")?,
|
||||
})
|
||||
@ -72,16 +72,16 @@ pub fn get_user_by_login_token(token: &str, client: &APIClient) -> ResultBoxErro
|
||||
.cond_u32("service_id", client.id)
|
||||
.cond("token1", token)
|
||||
.add_field("user_id"),
|
||||
|res| res.get_int64("user_id"),
|
||||
|res| res.get_user_id("user_id"),
|
||||
)
|
||||
}
|
||||
|
||||
/// Destroy a given user login tokens
|
||||
pub fn destroy_login_tokens(id: UserID, client: &APIClient) -> ResultBoxError<()> {
|
||||
pub fn destroy_login_tokens(id: &UserID, client: &APIClient) -> ResultBoxError<()> {
|
||||
|
||||
database::delete(DeleteQuery::new(USER_ACCESS_TOKENS_TABLE)
|
||||
.cond_u32("service_id", client.id)
|
||||
.cond_i64("user_id", id)
|
||||
.cond_user_id("user_id", id)
|
||||
)?;
|
||||
|
||||
Ok(())
|
||||
|
@ -6,6 +6,6 @@
|
||||
use crate::data::user::UserID;
|
||||
use crate::utils::user_data_utils::user_data_url;
|
||||
|
||||
pub fn get_url(_user_id: UserID) -> String {
|
||||
pub fn get_url(_user_id: &UserID) -> String {
|
||||
user_data_url("imgfond/0.jpg")
|
||||
}
|
@ -19,7 +19,7 @@ use crate::utils::user_data_utils::user_data_path;
|
||||
pub fn create(conv: &NewConversation) -> ResultBoxError<u64> {
|
||||
// Create the conversation in the main table
|
||||
let conv_id = InsertQuery::new(CONV_LIST_TABLE)
|
||||
.add_user_id("user_id", conv.owner_id)
|
||||
.add_user_id("user_id", &conv.owner_id)
|
||||
.add_str("name", conv.name.clone().unwrap_or(String::new()).as_str())
|
||||
.add_u64("last_active", time())
|
||||
.add_u64("creation_time", time())
|
||||
@ -34,14 +34,14 @@ pub fn create(conv: &NewConversation) -> ResultBoxError<u64> {
|
||||
follow = conv.owner_following;
|
||||
}
|
||||
|
||||
add_member(conv_id, member.clone(), follow)?;
|
||||
add_member(conv_id, member, follow)?;
|
||||
}
|
||||
|
||||
Ok(conv_id)
|
||||
}
|
||||
|
||||
/// Add a member to a conversation
|
||||
pub fn add_member(conv_id: u64, user_id: UserID, following: bool) -> ResultBoxError<()> {
|
||||
pub fn add_member(conv_id: u64, user_id: &UserID, following: bool) -> ResultBoxError<()> {
|
||||
InsertQuery::new(CONV_USERS_TABLE)
|
||||
.add_u64("conv_id", conv_id)
|
||||
.add_user_id("user_id", user_id)
|
||||
@ -54,7 +54,7 @@ pub fn add_member(conv_id: u64, user_id: UserID, following: bool) -> ResultBoxEr
|
||||
}
|
||||
|
||||
/// Remove a member from a conversation
|
||||
pub fn remove_member(conv_id: u64, user_id: UserID) -> ResultBoxError<()> {
|
||||
pub fn remove_member(conv_id: u64, user_id: &UserID) -> ResultBoxError<()> {
|
||||
database::DeleteQuery::new(CONV_USERS_TABLE)
|
||||
.cond_u64("conv_id", conv_id)
|
||||
.cond_user_id("user_id", user_id)
|
||||
@ -62,7 +62,7 @@ pub fn remove_member(conv_id: u64, user_id: UserID) -> ResultBoxError<()> {
|
||||
}
|
||||
|
||||
/// Get the list of conversations of a specific user
|
||||
pub fn get_list_user(user_id: UserID) -> ResultBoxError<Vec<Conversation>> {
|
||||
pub fn get_list_user(user_id: &UserID) -> ResultBoxError<Vec<Conversation>> {
|
||||
database::QueryInfo::new(CONV_LIST_TABLE)
|
||||
.alias("l")
|
||||
|
||||
@ -85,7 +85,7 @@ pub fn get_list_user(user_id: UserID) -> ResultBoxError<Vec<Conversation>> {
|
||||
}
|
||||
|
||||
/// Get information about a single conversation
|
||||
pub fn get_single(conv_id: u64, user_id: UserID) -> ResultBoxError<Conversation> {
|
||||
pub fn get_single(conv_id: u64, user_id: &UserID) -> ResultBoxError<Conversation> {
|
||||
// Tables
|
||||
database::QueryInfo::new(CONV_LIST_TABLE)
|
||||
.alias("l")
|
||||
@ -112,7 +112,7 @@ pub fn get_list_members(conv_id: u64) -> ResultBoxError<Vec<UserID>> {
|
||||
}
|
||||
|
||||
/// Check if a user belongs to a conversation or not
|
||||
pub fn does_user_belongs_to(user_id: UserID, conv_id: u64) -> ResultBoxError<bool> {
|
||||
pub fn does_user_belongs_to(user_id: &UserID, conv_id: u64) -> ResultBoxError<bool> {
|
||||
Ok(database::QueryInfo::new(CONV_USERS_TABLE)
|
||||
.cond_u64("conv_id", conv_id)
|
||||
.cond_user_id("user_id", user_id)
|
||||
@ -120,7 +120,7 @@ pub fn does_user_belongs_to(user_id: UserID, conv_id: u64) -> ResultBoxError<boo
|
||||
}
|
||||
|
||||
/// Check out wheter a user is the moderator of a conversation or not
|
||||
pub fn is_user_moderator(user_id: UserID, conv_id: u64) -> ResultBoxError<bool> {
|
||||
pub fn is_user_moderator(user_id: &UserID, conv_id: u64) -> ResultBoxError<bool> {
|
||||
Ok(database::QueryInfo::new(CONV_LIST_TABLE)
|
||||
.cond_u64("id", conv_id)
|
||||
.cond_user_id("user_id", user_id)
|
||||
@ -136,7 +136,7 @@ pub fn can_everyone_add_members(conv_id: u64) -> ResultBoxError<bool> {
|
||||
}
|
||||
|
||||
/// Set whether a user is following a conversation or not
|
||||
pub fn set_following(user_id: UserID, conv_id: u64, following: bool) -> ResultBoxError<()> {
|
||||
pub fn set_following(user_id: &UserID, conv_id: u64, following: bool) -> ResultBoxError<()> {
|
||||
database::UpdateInfo::new(CONV_USERS_TABLE)
|
||||
.cond_u64("conv_id", conv_id)
|
||||
.cond_user_id("user_id", user_id)
|
||||
@ -154,7 +154,7 @@ pub fn set_members(conv_id: u64, new_list: &Vec<UserID>, can_delete: bool) -> Re
|
||||
continue;
|
||||
}
|
||||
|
||||
add_member(conv_id, member.clone(), true)?;
|
||||
add_member(conv_id, member, true)?;
|
||||
}
|
||||
|
||||
// Remove a member
|
||||
@ -163,7 +163,7 @@ pub fn set_members(conv_id: u64, new_list: &Vec<UserID>, can_delete: bool) -> Re
|
||||
if new_list.contains(&member) {
|
||||
continue;
|
||||
}
|
||||
remove_member(conv_id, member)?;
|
||||
remove_member(conv_id, &member)?;
|
||||
}
|
||||
}
|
||||
|
||||
@ -188,7 +188,7 @@ pub fn set_can_everyone_add_members(conv_id: u64, allow: bool) -> ResultBoxError
|
||||
}
|
||||
|
||||
/// Search for private conversation between two users
|
||||
pub fn find_private(user_1: UserID, user_2: UserID) -> ResultBoxError<Vec<u64>> {
|
||||
pub fn find_private(user_1: &UserID, user_2: &UserID) -> ResultBoxError<Vec<u64>> {
|
||||
database::QueryInfo::new(CONV_USERS_TABLE)
|
||||
.alias("t1")
|
||||
|
||||
@ -246,7 +246,7 @@ pub fn get_older_messages(conv_id: u64, start_id: u64, limit: u64) -> ResultBoxE
|
||||
}
|
||||
|
||||
/// Get all the messages of a single user for a conversation
|
||||
pub fn get_user_messages_for_conversations(conv_id: u64, user_id: UserID) -> ResultBoxError<Vec<ConversationMessage>> {
|
||||
pub fn get_user_messages_for_conversations(conv_id: u64, user_id: &UserID) -> ResultBoxError<Vec<ConversationMessage>> {
|
||||
database::QueryInfo::new(CONV_MESSAGES_TABLE)
|
||||
.cond_u64("conv_id", conv_id)
|
||||
.cond_user_id("user_id", user_id)
|
||||
@ -274,7 +274,7 @@ pub fn send_message(msg: &NewConversationMessage) -> ResultBoxError<()> {
|
||||
// Insert the message in the database
|
||||
database::InsertQuery::new(CONV_MESSAGES_TABLE)
|
||||
.add_u64("conv_id", msg.conv_id)
|
||||
.add_user_id("user_id", msg.user_id)
|
||||
.add_user_id("user_id", &msg.user_id)
|
||||
.add_u64("time_insert", t)
|
||||
.add_str("message", msg.message.as_str())
|
||||
.add_opt_str("image_path", msg.image_path.as_ref())
|
||||
@ -292,7 +292,7 @@ pub fn send_message(msg: &NewConversationMessage) -> ResultBoxError<()> {
|
||||
.cond_legacy_bool("saw_last_message", true)
|
||||
|
||||
.custom_where("user_id != ?")
|
||||
.add_custom_where_arg_u64(msg.user_id as u64)
|
||||
.add_custom_where_arg_u64(msg.user_id.id())
|
||||
|
||||
.set_legacy_bool("saw_last_message", false)
|
||||
.exec()?;
|
||||
@ -332,7 +332,7 @@ pub fn delete_message_by_id(id: u64) -> ResultBoxError<()> {
|
||||
}
|
||||
|
||||
/// Count the number of unread conversation for a specified user
|
||||
pub fn count_unread_for_user(user_id: UserID) -> ResultBoxError<usize> {
|
||||
pub fn count_unread_for_user(user_id: &UserID) -> ResultBoxError<usize> {
|
||||
database::QueryInfo::new(CONV_USERS_TABLE)
|
||||
.cond_user_id("user_id", user_id)
|
||||
.cond_legacy_bool("saw_last_message", false)
|
||||
@ -341,7 +341,7 @@ pub fn count_unread_for_user(user_id: UserID) -> ResultBoxError<usize> {
|
||||
}
|
||||
|
||||
/// Get the list of unread conversations of a user
|
||||
pub fn get_list_unread(user_id: UserID) -> ResultBoxError<Vec<UnreadConversation>> {
|
||||
pub fn get_list_unread(user_id: &UserID) -> ResultBoxError<Vec<UnreadConversation>> {
|
||||
database::QueryInfo::new(CONV_USERS_TABLE)
|
||||
.alias("users")
|
||||
.join(CONV_LIST_TABLE, "list", "users.conv_id = list.id")
|
||||
@ -364,7 +364,7 @@ pub fn get_list_unread(user_id: UserID) -> ResultBoxError<Vec<UnreadConversation
|
||||
}
|
||||
|
||||
/// Indicate that a user has seen the last messages of a conversation
|
||||
pub fn mark_user_seen(conv_id: u64, user_id: UserID) -> ResultBoxError<()> {
|
||||
pub fn mark_user_seen(conv_id: u64, user_id: &UserID) -> ResultBoxError<()> {
|
||||
database::UpdateInfo::new(CONV_USERS_TABLE)
|
||||
.cond_u64("conv_id", conv_id)
|
||||
.cond_user_id("user_id", user_id)
|
||||
@ -374,7 +374,7 @@ pub fn mark_user_seen(conv_id: u64, user_id: UserID) -> ResultBoxError<()> {
|
||||
}
|
||||
|
||||
/// Remove a user from a conversation
|
||||
pub fn remove_user_from_conversation(user_id: UserID, conv_id: u64) -> ResultBoxError<()> {
|
||||
pub fn remove_user_from_conversation(user_id: &UserID, conv_id: u64) -> ResultBoxError<()> {
|
||||
if is_user_moderator(user_id, conv_id)? {
|
||||
delete_conversation(conv_id)
|
||||
} else {
|
||||
@ -403,7 +403,7 @@ pub fn delete_conversation(conv_id: u64) -> ResultBoxError<()> {
|
||||
}
|
||||
|
||||
/// Delete a conversation membership
|
||||
pub fn delete_member(user_id: UserID, conv_id: u64) -> ResultBoxError<()> {
|
||||
pub fn delete_member(user_id: &UserID, conv_id: u64) -> ResultBoxError<()> {
|
||||
for msg in get_user_messages_for_conversations(conv_id, user_id)? {
|
||||
delete_message(&msg)?;
|
||||
}
|
||||
@ -415,7 +415,7 @@ pub fn delete_member(user_id: UserID, conv_id: u64) -> ResultBoxError<()> {
|
||||
}
|
||||
|
||||
/// Check out whether a user is the owner of a message or not
|
||||
pub fn is_message_owner(user_id: UserID, message_id: u64) -> ResultBoxError<bool> {
|
||||
pub fn is_message_owner(user_id: &UserID, message_id: u64) -> ResultBoxError<bool> {
|
||||
database::QueryInfo::new(CONV_MESSAGES_TABLE)
|
||||
.cond_u64("id", message_id)
|
||||
.cond_user_id("user_id", user_id)
|
||||
|
@ -9,9 +9,9 @@ use crate::helpers::database;
|
||||
use crate::constants::database_tables_names::EMOJIS_TABLE;
|
||||
|
||||
/// Get the list of emojies of a user
|
||||
pub fn get_list_user(user_id: UserID) -> ResultBoxError<Vec<CustomEmoji>> {
|
||||
pub fn get_list_user(user_id: &UserID) -> ResultBoxError<Vec<CustomEmoji>> {
|
||||
database::QueryInfo::new(EMOJIS_TABLE)
|
||||
.cond_i64("user_id", user_id)
|
||||
.cond_user_id("user_id", user_id)
|
||||
.exec(db_to_custom_emoji)
|
||||
}
|
||||
|
||||
@ -19,7 +19,7 @@ pub fn get_list_user(user_id: UserID) -> ResultBoxError<Vec<CustomEmoji>> {
|
||||
fn db_to_custom_emoji(row: &database::RowResult) -> ResultBoxError<CustomEmoji> {
|
||||
Ok(CustomEmoji {
|
||||
id: row.get_u64("id")?,
|
||||
user_id: row.get_int64("user_id")?,
|
||||
user_id: row.get_user_id("user_id")?,
|
||||
shortcut: row.get_str("shortcut")?,
|
||||
path: row.get_str("path")?
|
||||
})
|
||||
|
@ -138,8 +138,8 @@ impl QueryInfo {
|
||||
self
|
||||
}
|
||||
|
||||
pub fn cond_user_id(mut self, key: &str, val: UserID) -> QueryInfo {
|
||||
self.conditions.insert(key.to_string(), val.to_string());
|
||||
pub fn cond_user_id(mut self, key: &str, val: &UserID) -> QueryInfo {
|
||||
self.conditions.insert(key.to_string(), val.id().to_string());
|
||||
self
|
||||
}
|
||||
|
||||
@ -290,7 +290,7 @@ impl<'a> RowResult<'a> {
|
||||
|
||||
/// Get the ID of a user included in the request
|
||||
pub fn get_user_id(&self, name: &str) -> ResultBoxError<UserID> {
|
||||
self.get_int64(name)
|
||||
Ok(UserID::new(self.get_u64(name)?))
|
||||
}
|
||||
|
||||
/// Get the ID of a group included in the response
|
||||
@ -514,8 +514,8 @@ impl InsertQuery {
|
||||
self
|
||||
}
|
||||
|
||||
pub fn add_user_id(mut self, key: &str, value: UserID) -> InsertQuery {
|
||||
self.values.insert(key.to_string(), Value::from(value));
|
||||
pub fn add_user_id(mut self, key: &str, value: &UserID) -> InsertQuery {
|
||||
self.values.insert(key.to_string(), Value::from(value.id()));
|
||||
self
|
||||
}
|
||||
|
||||
@ -611,8 +611,8 @@ impl DeleteQuery {
|
||||
self
|
||||
}
|
||||
|
||||
pub fn cond_user_id(mut self, key: &str, value: UserID) -> DeleteQuery {
|
||||
self.conditions.insert(key.to_string(), Value::from(value));
|
||||
pub fn cond_user_id(mut self, key: &str, value: &UserID) -> DeleteQuery {
|
||||
self.conditions.insert(key.to_string(), Value::from(value.id()));
|
||||
self
|
||||
}
|
||||
|
||||
@ -666,8 +666,8 @@ impl UpdateInfo {
|
||||
}
|
||||
|
||||
/// Filter with a user id
|
||||
pub fn cond_user_id(mut self, name: &str, val: UserID) -> UpdateInfo {
|
||||
self.cond.insert(name.to_string(), Value::Int(val));
|
||||
pub fn cond_user_id(mut self, name: &str, val: &UserID) -> UpdateInfo {
|
||||
self.cond.insert(name.to_string(), Value::UInt(val.id()));
|
||||
self
|
||||
}
|
||||
|
||||
|
@ -10,23 +10,23 @@ use crate::constants::database_tables_names::FRIENDS_TABLE;
|
||||
use crate::helpers::database::QueryInfo;
|
||||
|
||||
/// Check out whether two users are friend or not
|
||||
pub fn are_friend(user_one: UserID, user_two: UserID) -> ResultBoxError<bool> {
|
||||
pub fn are_friend(user_one: &UserID, user_two: &UserID) -> ResultBoxError<bool> {
|
||||
Ok(database::count(QueryInfo::new(FRIENDS_TABLE)
|
||||
.cond_i64("ID_personne", user_one)
|
||||
.cond_i64("ID_amis", user_two)
|
||||
.cond_user_id("ID_personne", user_one)
|
||||
.cond_user_id("ID_amis", user_two)
|
||||
.cond_i64("actif", 1))? > 0)
|
||||
}
|
||||
|
||||
/// Count the number of friends of a user
|
||||
pub fn count_friends(user_id: UserID) -> ResultBoxError<usize> {
|
||||
pub fn count_friends(user_id: &UserID) -> ResultBoxError<usize> {
|
||||
QueryInfo::new(FRIENDS_TABLE)
|
||||
.cond_i64("ID_amis", user_id)
|
||||
.cond_user_id("ID_amis", user_id)
|
||||
.cond_u32("actif", 1)
|
||||
.exec_count()
|
||||
}
|
||||
|
||||
/// Check if a user can create posts on another friend's page
|
||||
pub fn can_post_texts(user_id: UserID, target_user: UserID) -> ResultBoxError<bool> {
|
||||
pub fn can_post_texts(user_id: &UserID, target_user: &UserID) -> ResultBoxError<bool> {
|
||||
QueryInfo::new(FRIENDS_TABLE)
|
||||
.cond_user_id("ID_personne", target_user)
|
||||
.cond_user_id("ID_amis", user_id)
|
||||
|
@ -97,7 +97,7 @@ pub fn create(group: &NewGroup) -> ResultBoxError<GroupID> {
|
||||
// First, create the group
|
||||
let group_id = database::InsertQuery::new(GROUPS_LIST_TABLE)
|
||||
.add_u64("time_create", time())
|
||||
.add_user_id("userid_create", group.owner_id)
|
||||
.add_user_id("userid_create", &group.owner_id)
|
||||
.add_str("name", &group.name)
|
||||
.insert()?.ok_or(ExecError::new("Could not get group ID!"))?;
|
||||
let group_id = GroupID::new(group_id);
|
||||
@ -105,7 +105,7 @@ pub fn create(group: &NewGroup) -> ResultBoxError<GroupID> {
|
||||
// Insert first member
|
||||
insert_member(&GroupMember {
|
||||
id: 0,
|
||||
user_id: group.owner_id,
|
||||
user_id: group.owner_id.clone(),
|
||||
group_id: group_id.clone(),
|
||||
time_create: time(),
|
||||
level: GroupMembershipLevel::ADMINISTRATOR,
|
||||
@ -119,7 +119,7 @@ pub fn create(group: &NewGroup) -> ResultBoxError<GroupID> {
|
||||
pub fn insert_member(m: &GroupMember) -> ResultBoxError<()> {
|
||||
database::InsertQuery::new(GROUPS_MEMBERS_TABLE)
|
||||
.add_group_id("groups_id", &m.group_id)
|
||||
.add_user_id("user_id", m.user_id)
|
||||
.add_user_id("user_id", &m.user_id)
|
||||
.add_u64("time_create", m.time_create)
|
||||
.add_u32("level", m.level.to_db())
|
||||
.insert_drop_result()
|
||||
@ -129,7 +129,7 @@ pub fn insert_member(m: &GroupMember) -> ResultBoxError<()> {
|
||||
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);
|
||||
@ -176,7 +176,7 @@ pub fn search_group(query: &str, limit: u64) -> ResultBoxError<Vec<GroupID>> {
|
||||
pub fn get_membership(group_id: &GroupID, user_id: Option<UserID>) -> ResultBoxError<GroupMember> {
|
||||
let default_membership = GroupMember {
|
||||
id: 0,
|
||||
user_id: 0,
|
||||
user_id: UserID::new(0),
|
||||
group_id: group_id.clone(),
|
||||
time_create: 0,
|
||||
level: GroupMembershipLevel::VISITOR,
|
||||
@ -191,7 +191,7 @@ pub fn get_membership(group_id: &GroupID, user_id: Option<UserID>) -> ResultBoxE
|
||||
|
||||
Ok(database::QueryInfo::new(GROUPS_MEMBERS_TABLE)
|
||||
.cond_group_id("groups_id", group_id)
|
||||
.cond_user_id("user_id", user_id)
|
||||
.cond_user_id("user_id", &user_id)
|
||||
.query_row(db_to_group_member)
|
||||
.unwrap_or(default_membership))
|
||||
}
|
||||
@ -203,7 +203,7 @@ pub fn get_membership_level(group_id: &GroupID, user_id: Option<UserID>) -> Resu
|
||||
Some(user_id) => {
|
||||
let level = database::QueryInfo::new(GROUPS_MEMBERS_TABLE)
|
||||
.cond_group_id("groups_id", group_id)
|
||||
.cond_user_id("user_id", user_id)
|
||||
.cond_user_id("user_id", &user_id)
|
||||
.add_field("level")
|
||||
.query_row(|f| f.get_u32("level"))
|
||||
.unwrap_or(GroupMembershipLevel::VISITOR.to_db());
|
||||
|
@ -36,14 +36,14 @@ pub fn count(id: u64, kind: LikeType) -> ResultBoxError<usize> {
|
||||
}
|
||||
|
||||
/// Check if a user likes an element or not
|
||||
pub fn is_liking(user_id: UserID, id: u64, kind: LikeType) -> ResultBoxError<bool> {
|
||||
if user_id == 0 {
|
||||
pub fn is_liking(user_id: &UserID, id: u64, kind: LikeType) -> ResultBoxError<bool> {
|
||||
if !user_id.is_valid() {
|
||||
return Ok(false);
|
||||
}
|
||||
|
||||
Ok(QueryInfo::new(LIKES_TABLE)
|
||||
.cond_u64("ID_type", id)
|
||||
.cond_i64("ID_personne", user_id)
|
||||
.cond_user_id("ID_personne", user_id)
|
||||
.cond("type", kind.to_db_type().as_ref())
|
||||
.exec_count()? > 0)
|
||||
}
|
@ -10,9 +10,9 @@ use crate::helpers::friends_helper::are_friend;
|
||||
/// @author Pierre Hubert
|
||||
|
||||
/// Get & return information about a user based on its ID
|
||||
pub fn find_user_by_id(id: UserID) -> ResultBoxError<User> {
|
||||
pub fn find_user_by_id(id: &UserID) -> ResultBoxError<User> {
|
||||
exec_get_user_query(
|
||||
database::QueryInfo::new(USERS_TABLE).cond_i64("ID", id))
|
||||
database::QueryInfo::new(USERS_TABLE).cond_user_id("ID", id))
|
||||
}
|
||||
|
||||
/// Get & return information about a user based on his email
|
||||
@ -49,7 +49,7 @@ fn exec_get_user_query(query: database::QueryInfo) -> ResultBoxError<User> {
|
||||
};
|
||||
|
||||
Ok(User {
|
||||
id: res.get_int64("ID")?,
|
||||
id: res.get_user_id("ID")?,
|
||||
email: res.get_str("mail")?,
|
||||
password: res.get_str("password")?,
|
||||
first_name: res.get_str("prenom")?,
|
||||
@ -69,14 +69,14 @@ fn exec_get_user_query(query: database::QueryInfo) -> ResultBoxError<User> {
|
||||
}
|
||||
|
||||
/// Check out whether a given id maps to a user or not
|
||||
pub fn exists(id: UserID) -> ResultBoxError<bool> {
|
||||
pub fn exists(id: &UserID) -> ResultBoxError<bool> {
|
||||
Ok(database::QueryInfo::new(USERS_TABLE)
|
||||
.cond_i64("ID", id)
|
||||
.cond_user_id("ID", id)
|
||||
.exec_count()? > 0)
|
||||
}
|
||||
|
||||
/// Check if a given user can see another user's page
|
||||
pub fn can_see_user_page(user_id: UserID, target_user: UserID) -> ResultBoxError<bool> {
|
||||
pub fn can_see_user_page(user_id: &UserID, target_user: &UserID) -> ResultBoxError<bool> {
|
||||
if user_id == target_user {
|
||||
return Ok(true);
|
||||
}
|
||||
@ -89,7 +89,7 @@ pub fn can_see_user_page(user_id: UserID, target_user: UserID) -> ResultBoxError
|
||||
}
|
||||
|
||||
// The user need to be signed in
|
||||
if user_id <= 0 {
|
||||
if user_id.id() <= 0 {
|
||||
return Ok(false);
|
||||
}
|
||||
|
||||
@ -107,15 +107,15 @@ pub fn can_see_user_page(user_id: UserID, target_user: UserID) -> ResultBoxError
|
||||
}
|
||||
|
||||
/// Check out whether a user allow posts on his page or not
|
||||
pub fn allow_posts_on_his_page(user_id: UserID) -> ResultBoxError<bool> {
|
||||
pub fn allow_posts_on_his_page(user_id: &UserID) -> ResultBoxError<bool> {
|
||||
Ok(find_user_by_id(user_id)?.allow_posts_from_friends)
|
||||
}
|
||||
|
||||
/// Check out if a user can create posts on another user page
|
||||
pub fn can_create_posts(user_id: UserID, target_id: UserID) -> ResultBoxError<bool> {
|
||||
pub fn can_create_posts(user_id: &UserID, target_id: &UserID) -> ResultBoxError<bool> {
|
||||
|
||||
// Login required
|
||||
if user_id <= 0 {
|
||||
if !user_id.is_valid() {
|
||||
return Ok(false);
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user