mirror of
https://gitlab.com/comunic/comunicapiv3
synced 2025-06-20 16:35:17 +00:00
Implement presence mechanismes
This commit is contained in:
@ -350,6 +350,16 @@ impl<'a> RowResult<'a> {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_u16(&self, name: &str) -> ResultBoxError<u16> {
|
||||
let value = self.row.get_opt(self.find_col(name)?);
|
||||
|
||||
match value {
|
||||
None => Err(ExecError::boxed_string(
|
||||
format!("Could not extract integer field {} !", name))),
|
||||
Some(s) => Ok(s?)
|
||||
}
|
||||
}
|
||||
|
||||
/// Find an integer included in the request
|
||||
pub fn get_usize(&self, name: &str) -> Result<usize, Box<dyn Error>> {
|
||||
let value = self.row.get_opt(self.find_col(name)?);
|
||||
@ -661,6 +671,11 @@ impl InsertQuery {
|
||||
self
|
||||
}
|
||||
|
||||
pub fn add_u16(mut self, key: &str, value: u16) -> InsertQuery {
|
||||
self.values.insert(key.to_string(), Value::from(value));
|
||||
self
|
||||
}
|
||||
|
||||
pub fn add_user_id(mut self, key: &str, value: &UserID) -> InsertQuery {
|
||||
self.values.insert(key.to_string(), Value::from(value.id()));
|
||||
self
|
||||
|
62
src/helpers/forez_presence_helper.rs
Normal file
62
src/helpers/forez_presence_helper.rs
Normal file
@ -0,0 +1,62 @@
|
||||
//! # Forez Presence helper
|
||||
//!
|
||||
//! @author Pierre Hubert
|
||||
|
||||
use crate::constants::database_tables_names::FOREZ_PRESENCE_TABLE;
|
||||
use crate::data::error::Res;
|
||||
use crate::data::group_id::GroupID;
|
||||
use crate::data::presence::Presence;
|
||||
use crate::data::user::UserID;
|
||||
use crate::helpers::database;
|
||||
|
||||
/// Get the list of presence contained in the database
|
||||
pub fn get_list(group_id: &GroupID) -> Res<Vec<Presence>> {
|
||||
database::QueryInfo::new(FOREZ_PRESENCE_TABLE)
|
||||
.cond_group_id("group_id", group_id)
|
||||
.exec(db_to_presence)
|
||||
}
|
||||
|
||||
/// Get the list of presences of a specific user
|
||||
pub fn get_user_presences(group_id: &GroupID, user_id: &UserID) -> Res<Vec<Presence>> {
|
||||
database::QueryInfo::new(FOREZ_PRESENCE_TABLE)
|
||||
.cond_user_id("user_id", user_id)
|
||||
.cond_group_id("group_id", group_id)
|
||||
.exec(db_to_presence)
|
||||
}
|
||||
|
||||
/// Update the presences of a user
|
||||
pub fn update(group_id: &GroupID, user_id: &UserID, list: Vec<Presence>) -> Res {
|
||||
let previous_presences = get_user_presences(group_id, user_id)?;
|
||||
for presence in &list {
|
||||
if !previous_presences.contains(presence) {
|
||||
database::InsertQuery::new(FOREZ_PRESENCE_TABLE)
|
||||
.add_user_id("user_id", user_id)
|
||||
.add_group_id("group_id", group_id)
|
||||
.add_u32("year", presence.year)
|
||||
.add_u16("month", presence.month)
|
||||
.add_u16("day", presence.day)
|
||||
.insert()?;
|
||||
}
|
||||
}
|
||||
|
||||
for prev in &previous_presences {
|
||||
if !list.contains(prev) {
|
||||
database::DeleteQuery::new(FOREZ_PRESENCE_TABLE)
|
||||
.cond_u64("id", prev.id)
|
||||
.exec()?;
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Turn a database entry into a presence entry
|
||||
fn db_to_presence(row: &database::RowResult) -> Res<Presence> {
|
||||
Ok(Presence {
|
||||
id: row.get_u64("id")?,
|
||||
user_id: row.get_user_id("user_id")?,
|
||||
year: row.get_u32("year")?,
|
||||
month: row.get_u16("month")?,
|
||||
day: row.get_u16("day")?,
|
||||
})
|
||||
}
|
@ -20,4 +20,5 @@ pub mod events_helper;
|
||||
pub mod calls_helper;
|
||||
pub mod push_notifications_helper;
|
||||
pub mod independent_push_notifications_service_helper;
|
||||
pub mod firebase_notifications_helper;
|
||||
pub mod firebase_notifications_helper;
|
||||
pub mod forez_presence_helper;
|
Reference in New Issue
Block a user