Skip to content

Commit 1dc63f8

Browse files
committed
models/user: Remove sync fns
1 parent 32f0ab8 commit 1dc63f8

File tree

9 files changed

+13
-60
lines changed

9 files changed

+13
-60
lines changed

src/bin/crates-admin/delete_crate.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ pub async fn run(opts: Opts) -> anyhow::Result<()> {
6161
.await
6262
.context("Failed to look up crate name from the database")?;
6363

64-
let deleted_by = User::async_find_by_login(&mut conn, &opts.deleted_by)
64+
let deleted_by = User::find_by_login(&mut conn, &opts.deleted_by)
6565
.await
6666
.context("Failed to look up `--deleted-by` user from the database")?;
6767

src/controllers/github/secret_scanning.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ async fn send_notification_email(
187187
.await
188188
.context("Failed to find user")?;
189189

190-
let Some(recipient) = user.async_email(conn).await? else {
190+
let Some(recipient) = user.email(conn).await? else {
191191
return Err(anyhow!("No address found"));
192192
};
193193

src/controllers/krate/owners.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ async fn modify_owners(
188188
));
189189

190190
if let Some(recipient) =
191-
invitee.async_verified_email(conn).await.ok().flatten()
191+
invitee.verified_email(conn).await.ok().flatten()
192192
{
193193
emails.push(OwnerInviteEmail {
194194
recipient_email_address: recipient,

src/controllers/krate/publish.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ pub async fn publish(app: AppState, req: BytesRequest) -> AppResult<Json<GoodCra
118118
.check(&req, &mut conn)
119119
.await?;
120120

121-
let verified_email_address = auth.user().async_verified_email(&mut conn).await?;
121+
let verified_email_address = auth.user().verified_email(&mut conn).await?;
122122
let verified_email_address = verified_email_address.ok_or_else(|| {
123123
bad_request(format!(
124124
"A verified email address is required to publish crates to crates.io. \

src/controllers/token.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ pub async fn new(
132132
.transpose()
133133
.map_err(|_err| bad_request("invalid endpoint scope"))?;
134134

135-
let recipient = user.async_email(&mut conn).await?;
135+
let recipient = user.email(&mut conn).await?;
136136

137137
let api_token = ApiToken::insert_with_scopes(
138138
&mut conn,

src/controllers/user/update.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ pub async fn update_user(
5050
.await?;
5151

5252
if !publish_notifications {
53-
let email_address = user.async_verified_email(&mut conn).await?;
53+
let email_address = user.verified_email(&mut conn).await?;
5454

5555
if let Some(email_address) = email_address {
5656
let email = PublishNotificationsUnsubscribeEmail {

src/models/owner.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ impl Owner {
7171
Team::create_or_update(app, conn, name, req_user).await?,
7272
))
7373
} else {
74-
User::async_find_by_login(conn, name)
74+
User::find_by_login(conn, name)
7575
.await
7676
.optional()?
7777
.map(Owner::User)

src/models/user.rs

Lines changed: 5 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use chrono::NaiveDateTime;
2+
use diesel::prelude::*;
23
use diesel_async::scoped_futures::ScopedFutureExt;
3-
use diesel_async::{AsyncConnection, AsyncPgConnection};
4+
use diesel_async::{AsyncConnection, AsyncPgConnection, RunQueryDsl};
45
use secrecy::SecretString;
56

67
use crate::app::App;
@@ -11,8 +12,6 @@ use crate::util::errors::AppResult;
1112
use crate::models::{Crate, CrateOwner, Email, NewEmail, Owner, OwnerKind, Rights};
1213
use crate::schema::{crate_owners, emails, users};
1314
use crate::sql::lower;
14-
use crate::util::diesel::prelude::*;
15-
use crate::util::diesel::Conn;
1615

1716
/// The model representing a row in the `users` database table.
1817
#[derive(Clone, Debug, PartialEq, Eq, Queryable, Identifiable, AsChangeset, Selectable)]
@@ -31,27 +30,10 @@ pub struct User {
3130

3231
impl User {
3332
pub async fn find(conn: &mut AsyncPgConnection, id: i32) -> QueryResult<User> {
34-
use diesel_async::RunQueryDsl;
35-
3633
users::table.find(id).first(conn).await
3734
}
3835

39-
pub fn find_by_login(conn: &mut impl Conn, login: &str) -> QueryResult<User> {
40-
use diesel::RunQueryDsl;
41-
42-
users::table
43-
.filter(lower(users::gh_login).eq(login.to_lowercase()))
44-
.filter(users::gh_id.ne(-1))
45-
.order(users::gh_id.desc())
46-
.first(conn)
47-
}
48-
49-
pub async fn async_find_by_login(
50-
conn: &mut AsyncPgConnection,
51-
login: &str,
52-
) -> QueryResult<User> {
53-
use diesel_async::RunQueryDsl;
54-
36+
pub async fn find_by_login(conn: &mut AsyncPgConnection, login: &str) -> QueryResult<User> {
5537
users::table
5638
.filter(lower(users::gh_login).eq(login.to_lowercase()))
5739
.filter(users::gh_id.ne(-1))
@@ -61,8 +43,6 @@ impl User {
6143
}
6244

6345
pub async fn owning(krate: &Crate, conn: &mut AsyncPgConnection) -> QueryResult<Vec<Owner>> {
64-
use diesel_async::RunQueryDsl;
65-
6646
let users = CrateOwner::by_owner_kind(OwnerKind::User)
6747
.inner_join(users::table)
6848
.select(User::as_select())
@@ -104,24 +84,10 @@ impl User {
10484

10585
/// Queries the database for the verified emails
10686
/// belonging to a given user
107-
pub fn verified_email(&self, conn: &mut impl Conn) -> QueryResult<Option<String>> {
108-
use diesel::RunQueryDsl;
109-
110-
Email::belonging_to(self)
111-
.select(emails::email)
112-
.filter(emails::verified.eq(true))
113-
.first(conn)
114-
.optional()
115-
}
116-
117-
/// Queries the database for the verified emails
118-
/// belonging to a given user
119-
pub async fn async_verified_email(
87+
pub async fn verified_email(
12088
&self,
12189
conn: &mut AsyncPgConnection,
12290
) -> QueryResult<Option<String>> {
123-
use diesel_async::RunQueryDsl;
124-
12591
Email::belonging_to(self)
12692
.select(emails::email)
12793
.filter(emails::verified.eq(true))
@@ -131,19 +97,7 @@ impl User {
13197
}
13298

13399
/// Queries for the email belonging to a particular user
134-
pub fn email(&self, conn: &mut impl Conn) -> QueryResult<Option<String>> {
135-
use diesel::RunQueryDsl;
136-
137-
Email::belonging_to(self)
138-
.select(emails::email)
139-
.first(conn)
140-
.optional()
141-
}
142-
143-
/// Queries for the email belonging to a particular user
144-
pub async fn async_email(&self, conn: &mut AsyncPgConnection) -> QueryResult<Option<String>> {
145-
use diesel_async::RunQueryDsl;
146-
100+
pub async fn email(&self, conn: &mut AsyncPgConnection) -> QueryResult<Option<String>> {
147101
Email::belonging_to(self)
148102
.select(emails::email)
149103
.first(conn)
@@ -191,7 +145,6 @@ impl<'a> NewUser<'a> {
191145
use diesel::insert_into;
192146
use diesel::pg::upsert::excluded;
193147
use diesel::sql_types::Integer;
194-
use diesel_async::RunQueryDsl;
195148

196149
conn.transaction(|conn| {
197150
async move {

src/worker/jobs/expiry_notification.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ async fn handle_expiring_token(
7575
let user = User::find(conn, token.user_id).await?;
7676

7777
debug!("Looking up email address for user {}…", user.id);
78-
let recipient = user.async_email(conn).await?;
78+
let recipient = user.email(conn).await?;
7979
if let Some(recipient) = recipient {
8080
debug!("Sending expiry notification to {}…", recipient);
8181
let email = ExpiryNotificationEmail {

0 commit comments

Comments
 (0)