summaryrefslogtreecommitdiffstats
path: root/src/review/candidates.rs
blob: b2200131506dcd735edca793d9d69cec7279fa47 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
use crate::db::{self, candidates as sql, Db, Pkgbase, ReleaseRow};
use crate::error::Result;

pub use crate::db::candidates::{
    all as all_candidates, by_id as candidate_by_id, mark_viewed, open_reviews_for, pending as pending_candidates,
    reopen_rejected, set_diff_digest, NewCandidate,
};

/// The risk columns are review's to write, from `intake::record` and nowhere
/// else. A fixture that needs a particular score says so through this.
#[cfg(feature = "test-support")]
pub fn set_risk_for_tests(db: &Db, id: i64, level: &str, signals: &[String]) -> Result<()> {
    db::candidates::set_risk(db, id, level, signals)
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Upsert {
    Created { id: i64, kind: String },
    Collapsed { id: i64, revision: i64 },
    Superseded { old: i64, new: i64 },
    PreviouslyRejected,
    Unchanged,
}

pub(crate) fn upsert_candidate(db: &Db, pb: &Pkgbase, accepted: Option<&ReleaseRow>, nc: &NewCandidate) -> Result<Upsert> {
    let kind = if accepted.is_some() { db::candidate_kind::NEW_VERSION } else { db::candidate_kind::FIRST_INTRODUCTION };
    let base = accepted.map(|a| a.id);
    let pkgbase_id = pb.id;
    let nc = nc.clone();

    db.writer.tx(move |tx| {
        if let Some(open) = sql::open_in(tx, pkgbase_id)? {
            if open.identity_hash == nc.identity_hash {
                return Ok(Upsert::Unchanged);
            }
            if !open.viewed() {
                let revision = sql::collapse_in(tx, &open, &nc)?;
                return Ok(Upsert::Collapsed { id: open.id, revision });
            }
            sql::supersede_in(tx, open.id)?;
            let new_id = sql::insert_in(tx, pkgbase_id, base, kind, &nc)?;
            return Ok(Upsert::Superseded { old: open.id, new: new_id });
        }

        if sql::rejected_before_in(tx, pkgbase_id, &nc.identity_hash)? {
            return Ok(Upsert::PreviouslyRejected);
        }
        if sql::recorded_before_in(tx, pkgbase_id, &nc.identity_hash)? {
            return Ok(Upsert::Unchanged);
        }
        let id = sql::insert_in(tx, pkgbase_id, base, kind, &nc)?;
        Ok(Upsert::Created { id, kind: kind.to_string() })
    })
}