Add users to mailing list when using an invite link

This commit is contained in:
Joseph T. Lyons 2023-01-17 16:46:01 -05:00 committed by GitHub
parent f61b870db6
commit c3b102f5a8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 35 additions and 10 deletions

View file

@ -353,6 +353,8 @@ pub struct CreateInviteFromCodeParams {
invite_code: String, invite_code: String,
email_address: String, email_address: String,
device_id: Option<String>, device_id: Option<String>,
#[serde(default)]
added_to_mailing_list: bool,
} }
async fn create_invite_from_code( async fn create_invite_from_code(
@ -365,6 +367,7 @@ async fn create_invite_from_code(
&params.invite_code, &params.invite_code,
&params.email_address, &params.email_address,
params.device_id.as_deref(), params.device_id.as_deref(),
params.added_to_mailing_list,
) )
.await?, .await?,
)) ))

View file

@ -882,6 +882,7 @@ impl Database {
code: &str, code: &str,
email_address: &str, email_address: &str,
device_id: Option<&str>, device_id: Option<&str>,
added_to_mailing_list: bool,
) -> Result<Invite> { ) -> Result<Invite> {
self.transaction(|tx| async move { self.transaction(|tx| async move {
let existing_user = user::Entity::find() let existing_user = user::Entity::find()
@ -933,6 +934,7 @@ impl Database {
platform_windows: ActiveValue::set(false), platform_windows: ActiveValue::set(false),
platform_unknown: ActiveValue::set(true), platform_unknown: ActiveValue::set(true),
device_id: ActiveValue::set(device_id.map(|device_id| device_id.into())), device_id: ActiveValue::set(device_id.map(|device_id| device_id.into())),
added_to_mailing_list: ActiveValue::set(added_to_mailing_list),
..Default::default() ..Default::default()
}) })
.on_conflict( .on_conflict(

View file

@ -567,7 +567,12 @@ async fn test_invite_codes() {
// User 2 redeems the invite code and becomes a contact of user 1. // User 2 redeems the invite code and becomes a contact of user 1.
let user2_invite = db let user2_invite = db
.create_invite_from_code(&invite_code, "user2@example.com", Some("user-2-device-id")) .create_invite_from_code(
&invite_code,
"user2@example.com",
Some("user-2-device-id"),
true,
)
.await .await
.unwrap(); .unwrap();
let NewUserResult { let NewUserResult {
@ -617,7 +622,7 @@ async fn test_invite_codes() {
// User 3 redeems the invite code and becomes a contact of user 1. // User 3 redeems the invite code and becomes a contact of user 1.
let user3_invite = db let user3_invite = db
.create_invite_from_code(&invite_code, "user3@example.com", None) .create_invite_from_code(&invite_code, "user3@example.com", None, true)
.await .await
.unwrap(); .unwrap();
let NewUserResult { let NewUserResult {
@ -672,9 +677,14 @@ async fn test_invite_codes() {
); );
// Trying to reedem the code for the third time results in an error. // Trying to reedem the code for the third time results in an error.
db.create_invite_from_code(&invite_code, "user4@example.com", Some("user-4-device-id")) db.create_invite_from_code(
.await &invite_code,
.unwrap_err(); "user4@example.com",
Some("user-4-device-id"),
true,
)
.await
.unwrap_err();
// Invite count can be updated after the code has been created. // Invite count can be updated after the code has been created.
db.set_invite_count_for_user(user1, 2).await.unwrap(); db.set_invite_count_for_user(user1, 2).await.unwrap();
@ -684,7 +694,12 @@ async fn test_invite_codes() {
// User 4 can now redeem the invite code and becomes a contact of user 1. // User 4 can now redeem the invite code and becomes a contact of user 1.
let user4_invite = db let user4_invite = db
.create_invite_from_code(&invite_code, "user4@example.com", Some("user-4-device-id")) .create_invite_from_code(
&invite_code,
"user4@example.com",
Some("user-4-device-id"),
true,
)
.await .await
.unwrap(); .unwrap();
let user4 = db let user4 = db
@ -739,9 +754,14 @@ async fn test_invite_codes() {
); );
// An existing user cannot redeem invite codes. // An existing user cannot redeem invite codes.
db.create_invite_from_code(&invite_code, "user2@example.com", Some("user-2-device-id")) db.create_invite_from_code(
.await &invite_code,
.unwrap_err(); "user2@example.com",
Some("user-2-device-id"),
true,
)
.await
.unwrap_err();
let (_, invite_count) = db.get_invite_code_for_user(user1).await.unwrap().unwrap(); let (_, invite_count) = db.get_invite_code_for_user(user1).await.unwrap().unwrap();
assert_eq!(invite_count, 1); assert_eq!(invite_count, 1);
@ -763,7 +783,7 @@ async fn test_invite_codes() {
db.set_invite_count_for_user(user5, 5).await.unwrap(); db.set_invite_count_for_user(user5, 5).await.unwrap();
let (user5_invite_code, _) = db.get_invite_code_for_user(user5).await.unwrap().unwrap(); let (user5_invite_code, _) = db.get_invite_code_for_user(user5).await.unwrap().unwrap();
let user5_invite_to_user1 = db let user5_invite_to_user1 = db
.create_invite_from_code(&user5_invite_code, "user1@different.com", None) .create_invite_from_code(&user5_invite_code, "user1@different.com", None, true)
.await .await
.unwrap(); .unwrap();
let user1_2 = db let user1_2 = db