|
| 1 | +use conduit::{Request, Response}; |
| 2 | +use diesel::prelude::*; |
| 3 | +use time::Timespec; |
| 4 | + |
| 5 | +use db::RequestTransaction; |
| 6 | +use schema::{crate_owner_invitations, users, crates}; |
| 7 | +use user::RequestUser; |
| 8 | +use util::errors::CargoResult; |
| 9 | +use util::RequestUtils; |
| 10 | + |
| 11 | +/// The model representing a row in the `crate_owner_invitations` database table. |
| 12 | +#[derive(Clone, Copy, Debug, PartialEq, Eq, Identifiable, Queryable)] |
| 13 | +#[primary_key(invited_user_id, crate_id)] |
| 14 | +pub struct CrateOwnerInvitation { |
| 15 | + pub invited_user_id: i32, |
| 16 | + pub invited_by_user_id: i32, |
| 17 | + pub crate_id: i32, |
| 18 | + pub created_at: Timespec, |
| 19 | +} |
| 20 | + |
| 21 | +#[derive(Insertable, Clone, Copy, Debug)] |
| 22 | +#[table_name = "crate_owner_invitations"] |
| 23 | +pub struct NewCrateOwnerInvitation { |
| 24 | + pub invited_user_id: i32, |
| 25 | + pub invited_by_user_id: i32, |
| 26 | + pub crate_id: i32, |
| 27 | +} |
| 28 | + |
| 29 | +impl CrateOwnerInvitation { |
| 30 | + pub fn invited_by_username(&self, conn: &PgConnection) -> String { |
| 31 | + users::table |
| 32 | + .find(self.invited_by_user_id) |
| 33 | + .select(users::gh_login) |
| 34 | + .first(&*conn) |
| 35 | + .unwrap_or_else(|_| String::from("(unknown username)")) |
| 36 | + } |
| 37 | + |
| 38 | + pub fn crate_name(&self, conn: &PgConnection) -> String { |
| 39 | + crates::table |
| 40 | + .find(self.crate_id) |
| 41 | + .select(crates::name) |
| 42 | + .first(&*conn) |
| 43 | + .unwrap_or_else(|_| String::from("(unknown crate name)")) |
| 44 | + } |
| 45 | + |
| 46 | + pub fn encodable(self, conn: &PgConnection) -> EncodableCrateOwnerInvitation { |
| 47 | + EncodableCrateOwnerInvitation { |
| 48 | + invited_by_username: self.invited_by_username(conn), |
| 49 | + crate_name: self.crate_name(conn), |
| 50 | + crate_id: self.crate_id, |
| 51 | + created_at: ::encode_time(self.created_at), |
| 52 | + } |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +/// The serialization format for the `CrateOwnerInvitation` model. |
| 57 | +#[derive(Deserialize, Serialize, Debug)] |
| 58 | +pub struct EncodableCrateOwnerInvitation { |
| 59 | + pub invited_by_username: String, |
| 60 | + pub crate_name: String, |
| 61 | + pub crate_id: i32, |
| 62 | + pub created_at: String, |
| 63 | +} |
| 64 | + |
| 65 | +/// Handles the `GET /me/crate_owner_invitations` route. |
| 66 | +pub fn list(req: &mut Request) -> CargoResult<Response> { |
| 67 | + let conn = &*req.db_conn()?; |
| 68 | + let user_id = req.user()?.id; |
| 69 | + |
| 70 | + let invitations = crate_owner_invitations::table |
| 71 | + .filter(crate_owner_invitations::invited_user_id.eq(user_id)) |
| 72 | + .load::<CrateOwnerInvitation>(&*conn)? |
| 73 | + .into_iter() |
| 74 | + .map(|i| i.encodable(conn)) |
| 75 | + .collect(); |
| 76 | + |
| 77 | + #[derive(Serialize)] |
| 78 | + struct R { |
| 79 | + invitations: Vec<EncodableCrateOwnerInvitation>, |
| 80 | + } |
| 81 | + Ok(req.json(&R { invitations })) |
| 82 | +} |
0 commit comments