1
0
mirror of https://gitlab.com/comunic/comunicapiv3 synced 2024-11-26 07:19:22 +00:00

Can refresh active conversations

This commit is contained in:
Pierre HUBERT 2020-06-18 18:16:46 +02:00
parent bfeea42828
commit ba2e89d0be
5 changed files with 55 additions and 3 deletions

1
Cargo.lock generated
View File

@ -517,6 +517,7 @@ dependencies = [
"percent-encoding", "percent-encoding",
"rand", "rand",
"serde", "serde",
"serde_json",
"sha1", "sha1",
"yaml-rust", "yaml-rust",
] ]

View File

@ -12,6 +12,7 @@ mysql = "18.2.0"
actix-web = "2.0.0" actix-web = "2.0.0"
actix-rt = "1.1.1" actix-rt = "1.1.1"
serde = "1.0.110" serde = "1.0.110"
serde_json = "1.0.53"
futures = "0.3.5" futures = "0.3.5"
encoding_rs = "0.8.23" encoding_rs = "0.8.23"
percent-encoding = "2.1.0" percent-encoding = "2.1.0"

View File

@ -177,10 +177,37 @@ pub fn refresh_list(r: &mut HttpRequestHandler) -> RequestResult {
conversations_helper::mark_user_seen(conv_id as u64, r.user_id()?)?; conversations_helper::mark_user_seen(conv_id as u64, r.user_id()?)?;
} }
// TODO : Check for refresh of already initialized conversations
} }
// Check for refresh of already initialized conversations
if r.has_post_parameter("toRefresh") {
let v: HashMap<String, HashMap<String, serde_json::Value>> =
serde_json::from_str(r.post_string("toRefresh")?.as_str())?;
for (k, v) in v {
// Get conversation ID
if !k.starts_with("conversation-") {
return r.bad_request("Entries of 'toRefresh' must start with 'conversation-'!".to_string());
}
let conv_id = k.replace("conversation-", "").parse::<u64>()?;
// Extract last message id
if !v.contains_key("last_message_id") {
return r.bad_request(format!("Missing 'last_message_id' in conversation {}!", conv_id));
}
let last_message_id = v["last_message_id"].as_u64().unwrap_or(0);
// Check user rights
if !conversations_helper::does_user_belongs_to(r.user_id()?, conv_id)? {
return r.forbidden(format!("You do not belong to conversation {}!", conv_id));
}
let list_conv = conversations_helper::get_new_messages(conv_id, last_message_id)?;
list.insert(conv_id, list_conv);
conversations_helper::mark_user_seen(conv_id as u64, r.user_id()?)?;
}
}
r.set_response(ConversationRefreshResultAPI::new(list)) r.set_response(ConversationRefreshResultAPI::new(list))
} }

View File

@ -213,6 +213,16 @@ pub fn get_last_messages(conv_id: u64, number_of_messages: u64) -> ResultBoxErro
}) })
} }
/// Get the new messages of a conversation
pub fn get_new_messages(conv_id: u64, last_message_id: u64) -> ResultBoxError<Vec<ConversationMessage>> {
database::QueryInfo::new(CONV_MESSAGES_TABLE)
.cond_u64("conv_id", conv_id)
.set_custom_where("id > ?")
.add_custom_where_argument_u64(last_message_id)
.set_order("id")
.exec(db_to_conversation_message)
}
/// Indicate that a user has seen the last messages of a conversation /// 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) database::UpdateInfo::new(CONV_USERS_TABLE)

View File

@ -71,6 +71,9 @@ pub struct QueryInfo {
/// Custom WHERE condition /// Custom WHERE condition
pub custom_where: Option<String>, pub custom_where: Option<String>,
/// Custom WHERE values
pub custom_where_ars: Vec<mysql::Value>,
/// Limit of the query (0 = no limit) /// Limit of the query (0 = no limit)
pub limit: u64, pub limit: u64,
@ -92,6 +95,7 @@ impl QueryInfo {
joins: Vec::new(), joins: Vec::new(),
conditions: collections::HashMap::new(), conditions: collections::HashMap::new(),
custom_where: None, custom_where: None,
custom_where_ars: vec![],
limit: 0, limit: 0,
order: None, order: None,
fields: Vec::new(), fields: Vec::new(),
@ -144,6 +148,12 @@ impl QueryInfo {
self self
} }
/// Add a custom u64 WHERE value
pub fn add_custom_where_argument_u64(mut self, val: u64) -> QueryInfo {
self.custom_where_ars.push(mysql::Value::UInt(val));
self
}
/// Append a field to the list of selected fields /// Append a field to the list of selected fields
pub fn add_field(mut self, key: &str) -> QueryInfo { pub fn add_field(mut self, key: &str) -> QueryInfo {
self.fields.push(key.to_string()); self.fields.push(key.to_string());
@ -349,7 +359,7 @@ pub fn query<E, F: Fn(&RowResult) -> ProcessRowResult<E>>(info: QueryInfo, proce
for (k, v) in info.conditions { for (k, v) in info.conditions {
where_args.push(format!("{} = ?", k)); where_args.push(format!("{} = ?", k));
params.push(v) params.push(mysql::Value::from(v));
} }
let where_args = format!(" WHERE {} ", where_args.join(" AND ")); let where_args = format!(" WHERE {} ", where_args.join(" AND "));
@ -359,6 +369,9 @@ pub fn query<E, F: Fn(&RowResult) -> ProcessRowResult<E>>(info: QueryInfo, proce
// Custom WHERE clause // Custom WHERE clause
if let Some(custom_where) = info.custom_where { if let Some(custom_where) = info.custom_where {
query = query.add(format!(" AND ({})", custom_where).as_str()); query = query.add(format!(" AND ({})", custom_where).as_str());
let mut custom_args = info.custom_where_ars;
params.append(&mut custom_args);
} }
// ORDER clause // ORDER clause