Skip to content
Merged
Show file tree
Hide file tree
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
25 changes: 21 additions & 4 deletions backend/src/bin/import-papers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use clap::Parser;
use flate2::read::GzDecoder;
use iqps_backend::pathutils::PaperCategory;
use iqps_backend::{db, env, qp};
use iqps_backend::{db, env, qp, slack};
use sha2::{Digest, Sha256};
use std::fs::{self, File};
use std::io::{self, BufReader, Read, Write};
Expand Down Expand Up @@ -35,8 +35,9 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {

let qps: Vec<qp::LibraryQP> =
serde_json::from_reader(reader).expect("Failed to parse JSON file");
let count = qps.len();

print!("This will add {} new papers. Continue? [Y/n] ", qps.len());
print!("This will add {} new papers. Continue? [Y/n] ", count);
io::stdout().flush().unwrap();
let mut input = String::new();
io::stdin()
Expand All @@ -58,7 +59,13 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
let timestamp = chrono::Local::now().format("%Y-%m-%d_%H-%M-%S").to_string();
let log_filename = format!("peqp_import_{}.log", timestamp);

let log_file = File::create(&log_filename).expect("Failed to create log file");
let log_path = env_vars
.log_location
.parent()
.expect("Where do you want to store that log??")
.join(&log_filename);

let log_file = File::create(&log_path).expect("Failed to create log file");

tracing_subscriber::registry()
.with(
Expand Down Expand Up @@ -131,10 +138,20 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
break;
}
}

println!("Finished uploading papers to database.");
dir.close()?;

let message = format!(
"{} papers have been imported into IQPS!",
count,
);

let _ = slack::send_slack_message(
&env_vars.slack_webhook_url,
&message,
).await;

Ok(())
}

Expand Down
15 changes: 12 additions & 3 deletions backend/src/routing/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -411,12 +411,21 @@ pub async fn upload(
});
}

let unapproved_count = state.db.get_unapproved_papers_count().await?;
let total_count = state.db.get_unapproved_papers_count().await?;
let count = upload_statuses.len();
let message = format!(
"🔔 {} uploaded to IQPS!\n\n<https://qp.metakgp.org/admin|Review> | Total Unapproved papers: *{}*",
if count == 1 {
"A new paper was".into()
} else {
format!("{} new papers were", count)
},
total_count
);

let _ = send_slack_message(
&state.env_vars.slack_webhook_url,
upload_statuses.len(),
unapproved_count,
&message,
)
.await;

Expand Down
15 changes: 2 additions & 13 deletions backend/src/slack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,15 @@
use color_eyre::eyre;
use http::StatusCode;

/// Sends a notification to the Slack channel whenever a new paper is uploaded.
/// Sends a notification to the Slack channel.
pub async fn send_slack_message(
webhook_url: &str,
count: usize,
unapproved: i64,
message: &str,
) -> Result<(), color_eyre::eyre::Error> {
if webhook_url.is_empty() {
return Ok(());
}

let message = format!(
"🔔 {} uploaded to IQPS!\n\n<https://qp.metakgp.org/admin|Review> | Total Unapproved papers: *{}*",
if count == 1 {
"A new paper was".into()
} else {
format!("{} new papers were", count)
},
unapproved
);

let client = reqwest::Client::new();
let response = client
.post(webhook_url)
Expand Down