feat(room): auto-add new project members to all rooms
Some checks are pending
CI / Rust Lint & Check (push) Waiting to run
CI / Rust Tests (push) Waiting to run
CI / Frontend Lint & Type Check (push) Waiting to run
CI / Frontend Build (push) Blocked by required conditions

This commit is contained in:
ZhenYi 2026-04-19 22:27:57 +08:00
parent b8e5cbbb69
commit e612043e5f
3 changed files with 47 additions and 0 deletions

View File

@ -369,6 +369,9 @@ impl AppService {
joined_at: Set(Utc::now()),
};
member.insert(&txn).await?;
// Auto-add user to all rooms in the project
let _ = Self::add_user_to_all_project_rooms(&txn, project.id, user_uid).await;
}
let log = project_audit_log::ActiveModel {
project: Set(project.id),

View File

@ -309,6 +309,9 @@ impl AppService {
joined_at: Set(Utc::now()),
};
member.insert(&txn).await?;
// Auto-add user to all rooms in the project
let _ = Self::add_user_to_all_project_rooms(&txn, project.id, request_user).await;
} else {
active_request.status = Set("rejected".to_string());
active_request.processed_by = Set(Some(user_uid));

View File

@ -2,8 +2,10 @@ use crate::AppService;
use crate::error::AppError;
use chrono::Utc;
use models::projects::{MemberRole, project_audit_log, project_members};
use models::rooms::{room, room_member, RoomMemberRole};
use models::users::user;
use sea_orm::*;
use sea_orm::sea_query::OnConflict;
use serde::{Deserialize, Serialize};
use session::Session;
use utoipa::ToSchema;
@ -315,4 +317,43 @@ impl AppService {
Ok(())
}
/// Add a user to all rooms in a project (used automatically when a user joins a project).
pub async fn add_user_to_all_project_rooms(
db: &impl ConnectionTrait,
project_id: Uuid,
user_id: Uuid,
) -> Result<(), AppError> {
let rooms: Vec<room::Model> = room::Entity::find()
.filter(room::Column::Project.eq(project_id))
.all(db)
.await?;
if rooms.is_empty() {
return Ok(());
}
let now = Utc::now();
let values: Vec<room_member::ActiveModel> = rooms
.iter()
.map(|r| room_member::ActiveModel {
room: Set(r.id),
user: Set(user_id),
role: Set(RoomMemberRole::Member),
first_msg_in: Set(None),
joined_at: Set(Some(now)),
last_read_seq: Set(None),
do_not_disturb: Set(false),
dnd_start_hour: Set(None),
dnd_end_hour: Set(None),
})
.collect();
room_member::Entity::insert_many(values)
.on_conflict(OnConflict::new().do_nothing().to_owned())
.exec(db)
.await?;
Ok(())
}
}