delete channel function, add/update user

This commit is contained in:
jb-alvarado 2024-06-20 17:08:20 +02:00
parent 113f659037
commit f1ea2f4f43
3 changed files with 78 additions and 27 deletions

View File

@ -318,7 +318,22 @@ async fn update_user(
fields.push_str(", ");
}
fields.push_str(format!("mail = '{mail}'").as_str());
fields.push_str(&format!("mail = '{mail}'"));
}
if !data.channel_ids.is_empty() {
if !fields.is_empty() {
fields.push_str(", ");
}
fields.push_str(&format!(
"channel_ids = '{}'",
data.channel_ids
.iter()
.map(|i| i.to_string())
.collect::<Vec<String>>()
.join(",")
));
}
if !data.password.is_empty() {
@ -338,7 +353,7 @@ async fn update_user(
.await?
.unwrap();
fields.push_str(format!("password = '{password_hash}'").as_str());
fields.push_str(&format!("password = '{password_hash}'"));
}
if handles::update_user(&pool.into_inner(), *id, fields)
@ -438,7 +453,10 @@ async fn get_channel(
/// curl -X GET http://127.0.0.1:8787/api/channels -H "Authorization: Bearer <TOKEN>"
/// ```
#[get("/channels")]
#[protect(any("Role::GlobalAdmin"), ty = "Role")]
#[protect(
any("Role::GlobalAdmin", "Role::ChannelAdmin", "Role::User"),
ty = "Role"
)]
async fn get_all_channels(
pool: web::Data<Pool<Sqlite>>,
user: web::ReqData<UserMeta>,
@ -521,8 +539,18 @@ async fn add_channel(
async fn remove_channel(
pool: web::Data<Pool<Sqlite>>,
id: web::Path<i32>,
controllers: web::Data<Mutex<ChannelController>>,
queue: web::Data<Mutex<Vec<Arc<Mutex<MailQueue>>>>>,
) -> Result<impl Responder, ServiceError> {
if delete_channel(&pool.into_inner(), *id).await.is_ok() {
if delete_channel(
&pool.into_inner(),
*id,
controllers.into_inner(),
queue.into_inner(),
)
.await
.is_ok()
{
return Ok("Delete Channel Success");
}
@ -730,6 +758,10 @@ async fn update_playout_config(
}
let mut config = manager.config.lock().unwrap();
let (filler_path, _, _) = norm_abs_path(
&config.global.storage_path,
&data.storage.filler.to_string_lossy().to_string(),
)?;
config.general.stop_threshold = data.general.stop_threshold;
config.mail.subject.clone_from(&data.mail.subject);
@ -795,7 +827,7 @@ async fn update_playout_config(
.clone_from(&data.playlist.day_start);
config.playlist.length.clone_from(&data.playlist.length);
config.playlist.infinit = data.playlist.infinit;
config.storage.filler.clone_from(&data.storage.filler);
config.storage.filler.clone_from(&filler_path);
config
.storage
.extensions

View File

@ -63,9 +63,7 @@ pub struct User {
pub username: String,
#[serde(skip_serializing, default = "empty_string")]
pub password: String,
#[serde(skip_serializing)]
pub role_id: Option<i32>,
#[serde(skip_serializing, skip_deserializing)]
// #[serde_as(as = "StringWithSeparator::<CommaSeparator, i32>")]
pub channel_ids: Vec<i32>,
#[serde(skip_serializing_if = "Option::is_none")]

View File

@ -11,6 +11,29 @@ use crate::db::{handles, models::Channel};
use crate::player::controller::{ChannelController, ChannelManager};
use crate::utils::{config::PlayoutConfig, errors::ServiceError};
async fn map_global_admins(conn: &Pool<Sqlite>) -> Result<(), ServiceError> {
let channels = handles::select_related_channels(conn, None).await?;
let admins = handles::select_global_admins(conn).await?;
for admin in admins {
if let Err(e) = handles::update_user_channel(
conn,
admin.id,
channels
.iter()
.map(|c| c.id.to_string())
.collect::<Vec<String>>()
.join(","),
)
.await
{
error!("Update global admin: {e}");
};
}
Ok(())
}
pub async fn create_channel(
conn: &Pool<Sqlite>,
controllers: Arc<Mutex<ChannelController>>,
@ -37,34 +60,32 @@ pub async fn create_channel(
mqs.push(m_queue.clone());
}
if let Ok(channels) = handles::select_related_channels(conn, None).await {
if let Ok(admins) = handles::select_global_admins(conn).await {
for admin in admins {
if let Err(e) = handles::update_user_channel(
conn,
admin.id,
channels
.iter()
.map(|c| c.id.to_string())
.collect::<Vec<String>>()
.join(","),
)
.await
{
error!("Update global admin: {e}");
};
}
}
}
map_global_admins(conn).await?;
Ok(channel)
}
pub async fn delete_channel(conn: &Pool<Sqlite>, id: i32) -> Result<(), ServiceError> {
pub async fn delete_channel(
conn: &Pool<Sqlite>,
id: i32,
controllers: Arc<Mutex<ChannelController>>,
queue: Arc<Mutex<Vec<Arc<Mutex<MailQueue>>>>>,
) -> Result<(), ServiceError> {
let channel = handles::select_channel(conn, &id).await?;
// TODO: Remove Channel controller
handles::delete_channel(conn, &channel.id).await?;
controllers
.lock()
.map_err(|e| io::Error::new(io::ErrorKind::Other, e.to_string()))?
.remove(id);
if let Ok(mut mqs) = queue.lock() {
mqs.retain(|q| q.lock().unwrap().id != id);
}
map_global_admins(conn).await?;
Ok(())
}