Skip to content

Commit e133da0

Browse files
committed
Split owner and team into separate models
1 parent 482020c commit e133da0

File tree

6 files changed

+163
-153
lines changed

6 files changed

+163
-153
lines changed

src/github.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,3 +93,12 @@ pub fn token(token: String) -> Token {
9393
token_type: String::new(),
9494
}
9595
}
96+
97+
pub fn team_url(login: &str) -> String {
98+
let mut login_pieces = login.split(':');
99+
login_pieces.next();
100+
format!(
101+
"https://github.com/{}",
102+
login_pieces.next().expect("org failed"),
103+
)
104+
}

src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@ pub mod download;
7373
pub mod git;
7474
pub mod github;
7575
pub mod http;
76-
pub mod owner;
7776
pub mod render;
7877
pub mod schema;
7978
pub mod token;

src/models/mod.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@ pub use download::VersionDownload;
66
pub use self::follow::Follow;
77
pub use self::keyword::{CrateKeyword, Keyword};
88
pub use self::krate::{Crate, CrateDownload, NewCrate};
9-
pub use owner::{CrateOwner, NewTeam, Owner, OwnerKind, Team};
9+
pub use self::owner::{CrateOwner, Owner, OwnerKind};
1010
pub use self::rights::Rights;
11+
pub use self::team::{NewTeam, Team};
1112
pub use user::{Email, NewUser, User};
1213
pub use token::ApiToken;
1314
pub use version::{NewVersion, Version};
@@ -19,4 +20,6 @@ mod category;
1920
mod follow;
2021
mod keyword;
2122
pub mod krate;
22-
mod rights;
23+
mod owner;
24+
mod rights;
25+
mod team;

src/models/owner.rs

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
use diesel::prelude::*;
2+
3+
use app::App;
4+
use github;
5+
use util::{human, CargoResult};
6+
7+
use models::{Crate, Team, User};
8+
use schema::{crate_owners, users};
9+
use views::EncodableOwner;
10+
11+
#[derive(Insertable, Associations, Identifiable, Debug, Clone, Copy)]
12+
#[belongs_to(Crate)]
13+
#[belongs_to(User, foreign_key = "owner_id")]
14+
#[belongs_to(Team, foreign_key = "owner_id")]
15+
#[table_name = "crate_owners"]
16+
#[primary_key(crate_id, owner_id, owner_kind)]
17+
pub struct CrateOwner {
18+
pub crate_id: i32,
19+
pub owner_id: i32,
20+
pub created_by: i32,
21+
pub owner_kind: i32,
22+
}
23+
24+
#[derive(Debug, Clone, Copy)]
25+
#[repr(u32)]
26+
pub enum OwnerKind {
27+
User = 0,
28+
Team = 1,
29+
}
30+
31+
/// Unifies the notion of a User or a Team.
32+
#[derive(Debug)]
33+
pub enum Owner {
34+
User(User),
35+
Team(Team),
36+
}
37+
38+
impl Owner {
39+
/// Finds the owner by name. Always recreates teams to get the most
40+
/// up-to-date GitHub ID. Fails out if the user isn't found in the
41+
/// database, the team isn't found on GitHub, or if the user isn't a member
42+
/// of the team on GitHub.
43+
/// May be a user's GH login or a full team name. This is case
44+
/// sensitive.
45+
pub fn find_or_create_by_login(
46+
app: &App,
47+
conn: &PgConnection,
48+
req_user: &User,
49+
name: &str,
50+
) -> CargoResult<Owner> {
51+
if name.contains(':') {
52+
Ok(Owner::Team(Team::create_or_update(
53+
app,
54+
conn,
55+
name,
56+
req_user,
57+
)?))
58+
} else {
59+
users::table
60+
.filter(users::gh_login.eq(name))
61+
.first(conn)
62+
.map(Owner::User)
63+
.map_err(|_| human(&format_args!("could not find user with login `{}`", name)))
64+
}
65+
}
66+
67+
pub fn kind(&self) -> i32 {
68+
match *self {
69+
Owner::User(_) => OwnerKind::User as i32,
70+
Owner::Team(_) => OwnerKind::Team as i32,
71+
}
72+
}
73+
74+
pub fn login(&self) -> &str {
75+
match *self {
76+
Owner::User(ref user) => &user.gh_login,
77+
Owner::Team(ref team) => &team.login,
78+
}
79+
}
80+
81+
pub fn id(&self) -> i32 {
82+
match *self {
83+
Owner::User(ref user) => user.id,
84+
Owner::Team(ref team) => team.id,
85+
}
86+
}
87+
88+
pub fn encodable(self) -> EncodableOwner {
89+
match self {
90+
Owner::User(User {
91+
id,
92+
name,
93+
gh_login,
94+
gh_avatar,
95+
..
96+
}) => {
97+
let url = format!("https://github.com/{}", gh_login);
98+
EncodableOwner {
99+
id: id,
100+
login: gh_login,
101+
avatar: gh_avatar,
102+
url: Some(url),
103+
name: name,
104+
kind: String::from("user"),
105+
}
106+
}
107+
Owner::Team(Team {
108+
id,
109+
name,
110+
login,
111+
avatar,
112+
..
113+
}) => {
114+
let url = github::team_url(&login);
115+
EncodableOwner {
116+
id: id,
117+
login: login,
118+
url: Some(url),
119+
avatar: avatar,
120+
name: name,
121+
kind: String::from("team"),
122+
}
123+
}
124+
}
125+
}
126+
}

src/owner.rs renamed to src/models/team.rs

Lines changed: 4 additions & 149 deletions
Original file line numberDiff line numberDiff line change
@@ -4,35 +4,9 @@ use app::App;
44
use github;
55
use util::{human, CargoResult};
66

7-
use models::{Crate, User};
8-
use schema::{crate_owners, teams, users};
9-
10-
#[derive(Insertable, Associations, Identifiable, Debug, Clone, Copy)]
11-
#[belongs_to(Crate)]
12-
#[belongs_to(User, foreign_key = "owner_id")]
13-
#[belongs_to(Team, foreign_key = "owner_id")]
14-
#[table_name = "crate_owners"]
15-
#[primary_key(crate_id, owner_id, owner_kind)]
16-
pub struct CrateOwner {
17-
pub crate_id: i32,
18-
pub owner_id: i32,
19-
pub created_by: i32,
20-
pub owner_kind: i32,
21-
}
22-
23-
#[derive(Debug, Clone, Copy)]
24-
#[repr(u32)]
25-
pub enum OwnerKind {
26-
User = 0,
27-
Team = 1,
28-
}
29-
30-
/// Unifies the notion of a User or a Team.
31-
#[derive(Debug)]
32-
pub enum Owner {
33-
User(User),
34-
Team(Team),
35-
}
7+
use models::{Crate, CrateOwner, Owner, OwnerKind, User};
8+
use schema::{crate_owners, teams};
9+
use views::EncodableTeam;
3610

3711
/// For now, just a Github Team. Can be upgraded to other teams
3812
/// later if desirable.
@@ -53,25 +27,6 @@ pub struct Team {
5327
pub avatar: Option<String>,
5428
}
5529

56-
#[derive(Serialize, Debug)]
57-
pub struct EncodableTeam {
58-
pub id: i32,
59-
pub login: String,
60-
pub name: Option<String>,
61-
pub avatar: Option<String>,
62-
pub url: Option<String>,
63-
}
64-
65-
#[derive(Serialize, Deserialize, Debug)]
66-
pub struct EncodableOwner {
67-
pub id: i32,
68-
pub login: String,
69-
pub kind: String,
70-
pub url: Option<String>,
71-
pub name: Option<String>,
72-
pub avatar: Option<String>,
73-
}
74-
7530
#[derive(Insertable, AsChangeset, Debug)]
7631
#[table_name = "teams"]
7732
pub struct NewTeam<'a> {
@@ -240,7 +195,7 @@ impl Team {
240195
avatar,
241196
..
242197
} = self;
243-
let url = Team::github_url(&login);
198+
let url = github::team_url(&login);
244199

245200
EncodableTeam {
246201
id: id,
@@ -250,16 +205,6 @@ impl Team {
250205
url: Some(url),
251206
}
252207
}
253-
254-
fn github_url(login: &str) -> String {
255-
let mut login_pieces = login.split(':');
256-
login_pieces.next();
257-
258-
format!(
259-
"https://github.com/{}",
260-
login_pieces.next().expect("org failed"),
261-
)
262-
}
263208
}
264209

265210
fn team_with_gh_id_contains_user(app: &App, github_id: i32, user: &User) -> CargoResult<bool> {
@@ -286,93 +231,3 @@ fn team_with_gh_id_contains_user(app: &App, github_id: i32, user: &User) -> Carg
286231
// some feedback, but it's not obvious how that should work.
287232
Ok(membership.state == "active")
288233
}
289-
290-
impl Owner {
291-
/// Finds the owner by name. Always recreates teams to get the most
292-
/// up-to-date GitHub ID. Fails out if the user isn't found in the
293-
/// database, the team isn't found on GitHub, or if the user isn't a member
294-
/// of the team on GitHub.
295-
/// May be a user's GH login or a full team name. This is case
296-
/// sensitive.
297-
pub fn find_or_create_by_login(
298-
app: &App,
299-
conn: &PgConnection,
300-
req_user: &User,
301-
name: &str,
302-
) -> CargoResult<Owner> {
303-
if name.contains(':') {
304-
Ok(Owner::Team(Team::create_or_update(
305-
app,
306-
conn,
307-
name,
308-
req_user,
309-
)?))
310-
} else {
311-
users::table
312-
.filter(users::gh_login.eq(name))
313-
.first(conn)
314-
.map(Owner::User)
315-
.map_err(|_| human(&format_args!("could not find user with login `{}`", name)))
316-
}
317-
}
318-
319-
pub fn kind(&self) -> i32 {
320-
match *self {
321-
Owner::User(_) => OwnerKind::User as i32,
322-
Owner::Team(_) => OwnerKind::Team as i32,
323-
}
324-
}
325-
326-
pub fn login(&self) -> &str {
327-
match *self {
328-
Owner::User(ref user) => &user.gh_login,
329-
Owner::Team(ref team) => &team.login,
330-
}
331-
}
332-
333-
pub fn id(&self) -> i32 {
334-
match *self {
335-
Owner::User(ref user) => user.id,
336-
Owner::Team(ref team) => team.id,
337-
}
338-
}
339-
340-
pub fn encodable(self) -> EncodableOwner {
341-
match self {
342-
Owner::User(User {
343-
id,
344-
name,
345-
gh_login,
346-
gh_avatar,
347-
..
348-
}) => {
349-
let url = format!("https://github.com/{}", gh_login);
350-
EncodableOwner {
351-
id: id,
352-
login: gh_login,
353-
avatar: gh_avatar,
354-
url: Some(url),
355-
name: name,
356-
kind: String::from("user"),
357-
}
358-
}
359-
Owner::Team(Team {
360-
id,
361-
name,
362-
login,
363-
avatar,
364-
..
365-
}) => {
366-
let url = Team::github_url(&login);
367-
EncodableOwner {
368-
id: id,
369-
login: login,
370-
url: Some(url),
371-
avatar: avatar,
372-
name: name,
373-
kind: String::from("team"),
374-
}
375-
}
376-
}
377-
}
378-
}

src/views/mod.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,25 @@ pub struct EncodableCrateLinks {
7979
pub reverse_dependencies: String,
8080
}
8181

82-
pub use owner::{EncodableOwner, EncodableTeam};
82+
#[derive(Serialize, Deserialize, Debug)]
83+
pub struct EncodableOwner {
84+
pub id: i32,
85+
pub login: String,
86+
pub kind: String,
87+
pub url: Option<String>,
88+
pub name: Option<String>,
89+
pub avatar: Option<String>,
90+
}
91+
92+
#[derive(Serialize, Debug)]
93+
pub struct EncodableTeam {
94+
pub id: i32,
95+
pub login: String,
96+
pub name: Option<String>,
97+
pub avatar: Option<String>,
98+
pub url: Option<String>,
99+
}
100+
83101
pub use token::EncodableApiTokenWithToken;
84102
pub use user::{EncodablePrivateUser, EncodablePublicUser};
85103
pub use version::{EncodableVersion, EncodableVersionLinks};

0 commit comments

Comments
 (0)