Allow setting a channel for new users to auto-join (#9291)

Release Notes:

- Automatically add new users to the #zed channel
This commit is contained in:
Conrad Irwin 2024-03-13 11:11:31 -06:00 committed by GitHub
parent 88e33a1dbe
commit 77de5689a3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
12 changed files with 39 additions and 7 deletions

View file

@ -65,6 +65,7 @@ impl Database {
github_login: &str,
github_user_id: Option<i32>,
github_email: Option<&str>,
initial_channel_id: Option<ChannelId>,
) -> Result<()> {
self.transaction(|tx| async move {
let user = self
@ -72,6 +73,7 @@ impl Database {
github_login,
github_user_id,
github_email,
initial_channel_id,
&tx,
)
.await?;

View file

@ -74,12 +74,14 @@ impl Database {
github_login: &str,
github_user_id: Option<i32>,
github_email: Option<&str>,
initial_channel_id: Option<ChannelId>,
) -> Result<User> {
self.transaction(|tx| async move {
self.get_or_create_user_by_github_account_tx(
github_login,
github_user_id,
github_email,
initial_channel_id,
&tx,
)
.await
@ -92,6 +94,7 @@ impl Database {
github_login: &str,
github_user_id: Option<i32>,
github_email: Option<&str>,
initial_channel_id: Option<ChannelId>,
tx: &DatabaseTransaction,
) -> Result<User> {
if let Some(github_user_id) = github_user_id {
@ -124,6 +127,17 @@ impl Database {
})
.exec_with_returning(tx)
.await?;
if let Some(channel_id) = initial_channel_id {
channel_member::Entity::insert(channel_member::ActiveModel {
id: ActiveValue::NotSet,
channel_id: ActiveValue::Set(channel_id),
user_id: ActiveValue::Set(user.id),
accepted: ActiveValue::Set(true),
role: ActiveValue::Set(ChannelRole::Guest),
})
.exec(tx)
.await?;
}
Ok(user)
}
} else {

View file

@ -22,13 +22,17 @@ async fn test_contributors(db: &Arc<Database>) {
assert_eq!(db.get_contributors().await.unwrap(), Vec::<String>::new());
db.add_contributor("user1", Some(1), None).await.unwrap();
db.add_contributor("user1", Some(1), None, None)
.await
.unwrap();
assert_eq!(
db.get_contributors().await.unwrap(),
vec!["user1".to_string()]
);
db.add_contributor("user2", Some(2), None).await.unwrap();
db.add_contributor("user2", Some(2), None, None)
.await
.unwrap();
assert_eq!(
db.get_contributors().await.unwrap(),
vec!["user1".to_string(), "user2".to_string()]

View file

@ -102,7 +102,7 @@ async fn test_get_or_create_user_by_github_account(db: &Arc<Database>) {
.user_id;
let user = db
.get_or_create_user_by_github_account("the-new-login2", Some(102), None)
.get_or_create_user_by_github_account("the-new-login2", Some(102), None, None)
.await
.unwrap();
assert_eq!(user.id, user_id2);
@ -110,7 +110,7 @@ async fn test_get_or_create_user_by_github_account(db: &Arc<Database>) {
assert_eq!(user.github_user_id, Some(102));
let user = db
.get_or_create_user_by_github_account("login3", Some(103), Some("user3@example.com"))
.get_or_create_user_by_github_account("login3", Some(103), Some("user3@example.com"), None)
.await
.unwrap();
assert_eq!(&user.github_login, "login3");