Skip to content

Commit 63a8908

Browse files
committed
models/owners: Move Owner::find_or_create_by_login() fn into corresponding controller
This fn performs way to much non-database stuff to be in the `models` module...
1 parent 5d0ae76 commit 63a8908

File tree

2 files changed

+27
-30
lines changed

2 files changed

+27
-30
lines changed

src/controllers/krate/owners.rs

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,32 @@ async fn modify_owners(
281281
Ok(json!({ "msg": comma_sep_msg, "ok": true }))
282282
}
283283

284+
/// Finds the owner by name. Always recreates teams to get the most
285+
/// up-to-date GitHub ID. Fails out if the user isn't found in the
286+
/// database, the team isn't found on GitHub, or if the user isn't a member
287+
/// of the team on GitHub.
288+
///
289+
/// May be a user's GH login or a full team name. This is case
290+
/// sensitive.
291+
pub async fn find_or_create_owner(
292+
app: &App,
293+
conn: &mut AsyncPgConnection,
294+
req_user: &User,
295+
name: &str,
296+
) -> AppResult<Owner> {
297+
if name.contains(':') {
298+
Ok(Owner::Team(
299+
Team::create_or_update(app, conn, name, req_user).await?,
300+
))
301+
} else {
302+
User::find_by_login(conn, name)
303+
.await
304+
.optional()?
305+
.map(Owner::User)
306+
.ok_or_else(|| bad_request(format_args!("could not find user with login `{name}`")))
307+
}
308+
}
309+
284310
/// Invite `login` as an owner of this crate, returning the created
285311
/// [`NewOwnerInvite`].
286312
async fn add_owner(
@@ -292,7 +318,7 @@ async fn add_owner(
292318
) -> Result<NewOwnerInvite, OwnerAddError> {
293319
use diesel::insert_into;
294320

295-
let owner = Owner::find_or_create_by_login(app, conn, req_user, login).await?;
321+
let owner = find_or_create_owner(app, conn, req_user, login).await?;
296322
match owner {
297323
// Users are invited and must accept before being added
298324
Owner::User(user) => {

src/models/owner.rs

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
1-
use crate::app::App;
2-
use crate::util::errors::{bad_request, AppResult};
31
use diesel::pg::Pg;
42
use diesel::prelude::*;
5-
use diesel_async::AsyncPgConnection;
63

74
use crate::models::{Crate, CrateOwnerInvitation, Team, User};
85
use crate::schema::crate_owners;
@@ -63,32 +60,6 @@ pub enum Owner {
6360
}
6461

6562
impl Owner {
66-
/// Finds the owner by name. Always recreates teams to get the most
67-
/// up-to-date GitHub ID. Fails out if the user isn't found in the
68-
/// database, the team isn't found on GitHub, or if the user isn't a member
69-
/// of the team on GitHub.
70-
///
71-
/// May be a user's GH login or a full team name. This is case
72-
/// sensitive.
73-
pub async fn find_or_create_by_login(
74-
app: &App,
75-
conn: &mut AsyncPgConnection,
76-
req_user: &User,
77-
name: &str,
78-
) -> AppResult<Owner> {
79-
if name.contains(':') {
80-
Ok(Owner::Team(
81-
Team::create_or_update(app, conn, name, req_user).await?,
82-
))
83-
} else {
84-
User::find_by_login(conn, name)
85-
.await
86-
.optional()?
87-
.map(Owner::User)
88-
.ok_or_else(|| bad_request(format_args!("could not find user with login `{name}`")))
89-
}
90-
}
91-
9263
pub fn kind(&self) -> i32 {
9364
match self {
9465
Owner::User(_) => OwnerKind::User as i32,

0 commit comments

Comments
 (0)