Skip to content

Fix #467 - Author ordering #571

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 7, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 13 additions & 23 deletions src/version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use time::Duration;
use time::Timespec;
use url;

use {Model, Crate, User};
use {Model, Crate};
use app::RequestApp;
use db::RequestTransaction;
use dependency::{Dependency, EncodableDependency, Kind};
Expand All @@ -33,9 +33,8 @@ pub struct Version {
pub yanked: bool,
}

pub enum Author {
User(User),
Name(String),
pub struct Author {
pub name: String
}

#[derive(RustcEncodable, RustcDecodable)]
Expand Down Expand Up @@ -167,23 +166,17 @@ impl Version {

pub fn authors(&self, conn: &GenericConnection) -> CargoResult<Vec<Author>> {
let stmt = conn.prepare("SELECT * FROM version_authors
WHERE version_id = $1")?;
WHERE version_id = $1
ORDER BY name ASC")?;
let rows = stmt.query(&[&self.id])?;
rows.into_iter().map(|row| {
let user_id: Option<i32> = row.get("user_id");
let name: String = row.get("name");
Ok(match user_id {
Some(id) => Author::User(User::find(conn, id)?),
None => Author::Name(name),
})
}).collect()
Ok(rows.into_iter().map(|row| {
Author { name: row.get("name") }
}).collect())
}

pub fn add_author(&self,
conn: &GenericConnection,
name: &str) -> CargoResult<()> {
println!("add author: {}", name);
// TODO: at least try to link `name` to a pre-existing user
conn.execute("INSERT INTO version_authors (version_id, name)
VALUES ($1, $2)", &[&self.id, &name])?;
Ok(())
Expand Down Expand Up @@ -328,19 +321,16 @@ pub fn downloads(req: &mut Request) -> CargoResult<Response> {
pub fn authors(req: &mut Request) -> CargoResult<Response> {
let (version, _) = version_and_crate(req)?;
let tx = req.tx()?;
let (mut users, mut names) = (Vec::new(), Vec::new());
for author in version.authors(tx)?.into_iter() {
match author {
Author::User(u) => users.push(u.encodable()),
Author::Name(n) => names.push(n),
}
}
let names = version.authors(tx)?.into_iter().map(|a| a.name).collect();

// It was imagined that we wold associate authors with users.
// This was never implemented. This complicated return struct
// is all that is left, hear for backwards compatibility.
#[derive(RustcEncodable)]
struct R { users: Vec<::user::EncodableUser>, meta: Meta }
#[derive(RustcEncodable)]
struct Meta { names: Vec<String> }
Ok(req.json(&R{ users: users, meta: Meta { names: names } }))
Ok(req.json(&R{ users: vec![], meta: Meta { names: names } }))
}

/// Handles the `DELETE /crates/:crate_id/:version/yank` route.
Expand Down