mirror of
https://gitlab.com/comunic/comunicapiv3
synced 2024-12-27 22:18:51 +00:00
Rename a table
This commit is contained in:
parent
4ecf14a32c
commit
dd00b520b5
@ -34,8 +34,9 @@ pub mod database_tables_names {
|
||||
|
||||
/// Conversations tables
|
||||
pub const CONV_LIST_TABLE: &str = "comunic_conversations_list";
|
||||
pub const CONV_USERS_TABLE: &str = "comunic_conversations_users";
|
||||
pub const CONV_MEMBERS_TABLE: &str = "comunic_conversations_members";
|
||||
pub const CONV_MESSAGES_TABLE: &str = "comunic_conversations_messages";
|
||||
pub const CONV_INFO_VIEW: &str = "comunic_conversations_info";
|
||||
|
||||
/// Posts table
|
||||
pub const POSTS_TABLE: &str = "texte";
|
||||
|
@ -2,7 +2,7 @@
|
||||
//!
|
||||
//! @author Pierre Hubert
|
||||
|
||||
use crate::constants::database_tables_names::{CONV_LIST_TABLE, CONV_MESSAGES_TABLE, CONV_USERS_TABLE};
|
||||
use crate::constants::database_tables_names::{CONV_LIST_TABLE, CONV_MESSAGES_TABLE, CONV_MEMBERS_TABLE};
|
||||
use crate::data::conversation::Conversation;
|
||||
use crate::data::conversation_message::ConversationMessage;
|
||||
use crate::data::error::{ExecError, Res, ResultBoxError};
|
||||
@ -43,7 +43,7 @@ pub fn create(conv: &NewConversation) -> ResultBoxError<u64> {
|
||||
|
||||
/// Add a member to a conversation
|
||||
pub fn add_member(conv_id: u64, user_id: &UserID, following: bool) -> ResultBoxError<()> {
|
||||
InsertQuery::new(CONV_USERS_TABLE)
|
||||
InsertQuery::new(CONV_MEMBERS_TABLE)
|
||||
.add_u64("conv_id", conv_id)
|
||||
.add_user_id("user_id", user_id)
|
||||
.add_u64("time_add", time())
|
||||
@ -56,7 +56,7 @@ pub fn add_member(conv_id: u64, user_id: &UserID, following: bool) -> ResultBoxE
|
||||
|
||||
/// Remove a member from a conversation
|
||||
pub fn remove_member(conv_id: u64, user_id: &UserID) -> ResultBoxError<()> {
|
||||
database::DeleteQuery::new(CONV_USERS_TABLE)
|
||||
database::DeleteQuery::new(CONV_MEMBERS_TABLE)
|
||||
.cond_u64("conv_id", conv_id)
|
||||
.cond_user_id("user_id", user_id)
|
||||
.exec()
|
||||
@ -68,7 +68,7 @@ pub fn get_list_user(user_id: &UserID) -> ResultBoxError<Vec<Conversation>> {
|
||||
.alias("l")
|
||||
|
||||
// Join with conversation members table
|
||||
.join(CONV_USERS_TABLE, "u", "l.id = u.conv_id")
|
||||
.join(CONV_MEMBERS_TABLE, "u", "l.id = u.conv_id")
|
||||
|
||||
// Specify selected fields
|
||||
.add_field("*")
|
||||
@ -90,7 +90,7 @@ pub fn get_single(conv_id: u64, user_id: &UserID) -> ResultBoxError<Conversation
|
||||
// Tables
|
||||
database::QueryInfo::new(CONV_LIST_TABLE)
|
||||
.alias("l")
|
||||
.join(CONV_USERS_TABLE, "u", "l.id = u.conv_id")
|
||||
.join(CONV_MEMBERS_TABLE, "u", "l.id = u.conv_id")
|
||||
|
||||
// Fields
|
||||
.add_field("*")
|
||||
@ -106,7 +106,7 @@ pub fn get_single(conv_id: u64, user_id: &UserID) -> ResultBoxError<Conversation
|
||||
|
||||
/// Get the list of members of a conversation
|
||||
pub fn get_list_members(conv_id: u64) -> ResultBoxError<Vec<UserID>> {
|
||||
database::QueryInfo::new(CONV_USERS_TABLE)
|
||||
database::QueryInfo::new(CONV_MEMBERS_TABLE)
|
||||
.cond_u64("conv_id", conv_id)
|
||||
.add_field("user_id")
|
||||
.exec(|res| res.get_user_id("user_id"))
|
||||
@ -114,7 +114,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> {
|
||||
Ok(database::QueryInfo::new(CONV_USERS_TABLE)
|
||||
Ok(database::QueryInfo::new(CONV_MEMBERS_TABLE)
|
||||
.cond_u64("conv_id", conv_id)
|
||||
.cond_user_id("user_id", user_id)
|
||||
.exec_count()? > 0)
|
||||
@ -138,7 +138,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<()> {
|
||||
database::UpdateInfo::new(CONV_USERS_TABLE)
|
||||
database::UpdateInfo::new(CONV_MEMBERS_TABLE)
|
||||
.cond_u64("conv_id", conv_id)
|
||||
.cond_user_id("user_id", user_id)
|
||||
.set_legacy_bool("following", following)
|
||||
@ -190,16 +190,16 @@ 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>> {
|
||||
database::QueryInfo::new(CONV_USERS_TABLE)
|
||||
database::QueryInfo::new(CONV_MEMBERS_TABLE)
|
||||
.alias("t1")
|
||||
|
||||
// Join
|
||||
.join(CONV_USERS_TABLE, "t2", "t1.conv_id = t2.conv_id")
|
||||
.join(CONV_MEMBERS_TABLE, "t2", "t1.conv_id = t2.conv_id")
|
||||
|
||||
// Conditions
|
||||
.cond_user_id("t1.user_id", user_1)
|
||||
.cond_user_id("t2.user_id", user_2)
|
||||
.set_custom_where(format!("(SELECT COUNT(*) FROM {} WHERE conv_id = t1.conv_id) = 2", CONV_USERS_TABLE).as_ref())
|
||||
.set_custom_where(format!("(SELECT COUNT(*) FROM {} WHERE conv_id = t1.conv_id) = 2", CONV_MEMBERS_TABLE).as_ref())
|
||||
.add_field("t1.conv_id AS conv_id")
|
||||
.exec(|f| f.get_u64("conv_id"))
|
||||
}
|
||||
@ -333,7 +333,7 @@ pub fn send_message(msg: &NewConversationMessage) -> ResultBoxError<()> {
|
||||
.exec()?;
|
||||
|
||||
// Get the list of users to notify after the update
|
||||
let list_to_notify = database::QueryInfo::new(CONV_USERS_TABLE)
|
||||
let list_to_notify = database::QueryInfo::new(CONV_MEMBERS_TABLE)
|
||||
.cond_u64("conv_id", msg.conv_id)
|
||||
.cond_legacy_bool("saw_last_message", true)
|
||||
.cond_legacy_bool("following", true)
|
||||
@ -342,7 +342,7 @@ pub fn send_message(msg: &NewConversationMessage) -> ResultBoxError<()> {
|
||||
.exec(|r| r.get_user_id("user_id"))?;
|
||||
|
||||
// Mark all the users of the conversation as unread
|
||||
database::UpdateInfo::new(CONV_USERS_TABLE)
|
||||
database::UpdateInfo::new(CONV_MEMBERS_TABLE)
|
||||
.cond_u64("conv_id", msg.conv_id)
|
||||
.cond_legacy_bool("saw_last_message", true)
|
||||
|
||||
@ -402,7 +402,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> {
|
||||
database::QueryInfo::new(CONV_USERS_TABLE)
|
||||
database::QueryInfo::new(CONV_MEMBERS_TABLE)
|
||||
.cond_user_id("user_id", user_id)
|
||||
.cond_legacy_bool("saw_last_message", false)
|
||||
.cond_legacy_bool("following", true)
|
||||
@ -411,7 +411,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>> {
|
||||
database::QueryInfo::new(CONV_USERS_TABLE)
|
||||
database::QueryInfo::new(CONV_MEMBERS_TABLE)
|
||||
.alias("users")
|
||||
.join(CONV_LIST_TABLE, "list", "users.conv_id = list.id")
|
||||
.join(CONV_MESSAGES_TABLE, "messages", "messages.conv_id = users.conv_id")
|
||||
@ -441,7 +441,7 @@ pub fn get_list_unread(user_id: &UserID) -> ResultBoxError<Vec<UnreadConversatio
|
||||
|
||||
/// Indicate that a user has seen the last messages of a conversation
|
||||
pub fn mark_user_seen(conv_id: u64, user_id: &UserID) -> ResultBoxError<()> {
|
||||
database::UpdateInfo::new(CONV_USERS_TABLE)
|
||||
database::UpdateInfo::new(CONV_MEMBERS_TABLE)
|
||||
.cond_u64("conv_id", conv_id)
|
||||
.cond_user_id("user_id", user_id)
|
||||
.cond_legacy_bool("saw_last_message", false)
|
||||
@ -471,7 +471,7 @@ pub fn delete_conversation(conv_id: u64) -> ResultBoxError<()> {
|
||||
}
|
||||
|
||||
// Delete all the members of the conversation
|
||||
database::DeleteQuery::new(CONV_USERS_TABLE)
|
||||
database::DeleteQuery::new(CONV_MEMBERS_TABLE)
|
||||
.cond_u64("conv_id", conv_id)
|
||||
.exec()?;
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user