Skip to content
This repository has been archived by the owner on Oct 19, 2024. It is now read-only.

Commit

Permalink
Merge branch 'master' of http://www.github.com/modrinth/labrinth into…
Browse files Browse the repository at this point in the history
… organizations
  • Loading branch information
thesuzerain committed Sep 30, 2023
2 parents 370d6a6 + 51777c3 commit 9b137ec
Show file tree
Hide file tree
Showing 14 changed files with 343 additions and 155 deletions.
64 changes: 32 additions & 32 deletions sqlx-data.json
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,18 @@
},
"query": "\n UPDATE mods\n SET source_url = $1\n WHERE (id = $2)\n "
},
"0461463e3e14f6c8ede5571a2905b8171e8caf4ebbd3ec844ef2cebd83980247": {
"describe": {
"columns": [],
"nullable": [],
"parameters": {
"Left": [
"Int8"
]
}
},
"query": "\n DELETE FROM reports\n WHERE user_id = $1 OR reporter = $1\n "
},
"0472045549758d8eef84592908c438d6222a26926f4b06865b84979fc92564ba": {
"describe": {
"columns": [],
Expand Down Expand Up @@ -2085,18 +2097,6 @@
},
"query": "\n SELECT mod_id FROM versions WHERE id = $1\n "
},
"4c58727309e5c79cc0505e57aeba0c977f308429f97b0ed296ab3bc0ebebb435": {
"describe": {
"columns": [],
"nullable": [],
"parameters": {
"Left": [
"Int8"
]
}
},
"query": "\n DELETE FROM reports\n WHERE user_id = $1\n "
},
"4c9e2190e2a68ffc093a69aaa1fc9384957138f57ac9cd85cbc6179613c13a08": {
"describe": {
"columns": [
Expand Down Expand Up @@ -2299,26 +2299,6 @@
},
"query": "\n UPDATE versions\n SET version_number = $1\n WHERE (id = $2)\n "
},
"5586d60c8f3d58a31e6635ffb3cb30bac389bf21b190dfd1e64a44e837f3879c": {
"describe": {
"columns": [
{
"name": "id",
"ordinal": 0,
"type_info": "Int8"
}
],
"nullable": [
false
],
"parameters": {
"Left": [
"Text"
]
}
},
"query": "\n SELECT id FROM mods\n WHERE status = $1 AND queued < NOW() - INTERVAL '40 hours'\n ORDER BY updated ASC\n "
},
"5627b3516fc7c3799154098a663b1586aac11b2dc736810f06630ee5d8a54946": {
"describe": {
"columns": [
Expand Down Expand Up @@ -6274,6 +6254,26 @@
},
"query": "\n UPDATE versions\n SET featured = $1\n WHERE (id = $2)\n "
},
"e60ea75112db37d3e73812e21b1907716e4762e06aa883af878e3be82e3f87d3": {
"describe": {
"columns": [
{
"name": "id",
"ordinal": 0,
"type_info": "Int8"
}
],
"nullable": [
false
],
"parameters": {
"Left": [
"Int8"
]
}
},
"query": "\n SELECT c.id FROM collections c\n WHERE c.user_id = $1\n "
},
"e6db02891be261e61a25716b83c1298482eb9a04f0c026532030aeb374405f13": {
"describe": {
"columns": [
Expand Down
54 changes: 54 additions & 0 deletions src/clickhouse/fetch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@ pub struct ReturnViews {
pub total_views: u64,
}

#[derive(clickhouse::Row, Serialize, Deserialize, Clone, Debug)]
pub struct ReturnDownloads {
pub time: u64,
pub id: u64,
pub total_downloads: u64,
}

// Only one of project_id or version_id should be used
// Fetches playtimes as a Vec of ReturnPlaytimes
pub async fn fetch_playtimes(
Expand Down Expand Up @@ -125,6 +132,53 @@ pub async fn fetch_views(
Ok(query.fetch_all().await?)
}

// Fetches downloads as a Vec of ReturnDownloads
pub async fn fetch_downloads(
projects: Option<Vec<ProjectId>>,
versions: Option<Vec<VersionId>>,
start_date: NaiveDate,
end_date: NaiveDate,
resolution_minutes: u32,
client: Arc<clickhouse::Client>,
) -> Result<Vec<ReturnDownloads>, ApiError> {
let project_or_version = if projects.is_some() && versions.is_none() {
"project_id"
} else if versions.is_some() {
"version_id"
} else {
return Err(ApiError::InvalidInput(
"Only one of 'project_id' or 'version_id' should be used.".to_string(),
));
};

let mut query = client
.query(&format!(
"
SELECT
toYYYYMMDDhhmmss((toStartOfInterval(recorded, toIntervalMinute(?)) AS time)),
{project_or_version},
count(id) AS total_downloads
FROM downloads
WHERE time >= toDate(?) AND time <= toDate(?)
AND {project_or_version} IN ?
GROUP BY
time,
{project_or_version}
"
))
.bind(resolution_minutes)
.bind(start_date)
.bind(end_date);

if projects.is_some() {
query = query.bind(projects.unwrap().iter().map(|x| x.0).collect::<Vec<_>>());
} else if versions.is_some() {
query = query.bind(versions.unwrap().iter().map(|x| x.0).collect::<Vec<_>>());
}

Ok(query.fetch_all().await?)
}

// Fetches countries as a Vec of ReturnCountry
pub async fn fetch_countries(
projects: Option<Vec<ProjectId>>,
Expand Down
27 changes: 26 additions & 1 deletion src/database/models/user_item.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use super::ids::{ProjectId, UserId};
use super::CollectionId;
use crate::database::models::DatabaseError;
use crate::models::ids::base62_impl::{parse_base62, to_base62};
use crate::models::users::{Badges, RecipientType, RecipientWallet};
Expand Down Expand Up @@ -320,6 +321,30 @@ impl User {
Ok(projects)
}

pub async fn get_collections<'a, E>(
user_id: UserId,
exec: E,
) -> Result<Vec<CollectionId>, sqlx::Error>
where
E: sqlx::Executor<'a, Database = sqlx::Postgres> + Copy,
{
use futures::stream::TryStreamExt;

let projects = sqlx::query!(
"
SELECT c.id FROM collections c
WHERE c.user_id = $1
",
user_id as UserId,
)
.fetch_many(exec)
.try_filter_map(|e| async { Ok(e.right().map(|m| CollectionId(m.id))) })
.try_collect::<Vec<CollectionId>>()
.await?;

Ok(projects)
}

pub async fn get_backup_codes<'a, E>(
user_id: UserId,
exec: E,
Expand Down Expand Up @@ -463,7 +488,7 @@ impl User {
sqlx::query!(
"
DELETE FROM reports
WHERE user_id = $1
WHERE user_id = $1 OR reporter = $1
",
id as UserId,
)
Expand Down
72 changes: 0 additions & 72 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ use crate::ratelimit::middleware::RateLimiter;
use crate::util::cors::default_cors;
use crate::util::env::{parse_strings_from_var, parse_var};
use actix_web::{web, App, HttpServer};
use chrono::{DateTime, Utc};
use deadpool_redis::{Config, Runtime};
use env_logger::Env;
use log::{error, info, warn};
Expand Down Expand Up @@ -181,77 +180,6 @@ async fn main() -> std::io::Result<()> {
}
});

// Reminding moderators to review projects which have been in the queue longer than 40hr
let pool_ref = pool.clone();
let redis_ref = redis_pool.clone();
let webhook_message_sent = Arc::new(Mutex::new(Vec::<(
database::models::ProjectId,
DateTime<Utc>,
)>::new()));

scheduler.run(std::time::Duration::from_secs(10 * 60), move || {
let pool_ref = pool_ref.clone();
let redis_ref = redis_ref.clone();
let webhook_message_sent_ref = webhook_message_sent.clone();
info!("Checking reviewed projects submitted more than 40hrs ago");

async move {
let do_steps = async {
use futures::TryStreamExt;

let project_ids = sqlx::query!(
"
SELECT id FROM mods
WHERE status = $1 AND queued < NOW() - INTERVAL '40 hours'
ORDER BY updated ASC
",
crate::models::projects::ProjectStatus::Processing.as_str(),
)
.fetch_many(&pool_ref)
.try_filter_map(|e| async {
Ok(e.right().map(|m| database::models::ProjectId(m.id)))
})
.try_collect::<Vec<database::models::ProjectId>>()
.await?;

let mut webhook_message_sent_ref = webhook_message_sent_ref.lock().await;

webhook_message_sent_ref.retain(|x| Utc::now() - x.1 < chrono::Duration::hours(12));

for project in project_ids {
if webhook_message_sent_ref.iter().any(|x| x.0 == project) { continue; }

if let Ok(webhook_url) =
dotenvy::var("MODERATION_DISCORD_WEBHOOK")
{
util::webhook::send_discord_webhook(
project.into(),
&pool_ref,
&redis_ref,
webhook_url,
Some("<@&783155186491195394> This project has been in the queue for over 40 hours!".to_string()),
)
.await
.ok();

webhook_message_sent_ref.push((project, Utc::now()));
}
}

Ok::<(), routes::ApiError>(())
};

if let Err(e) = do_steps.await {
warn!(
"Checking reviewed projects submitted more than 40hrs ago failed: {:?}",
e
);
}

info!("Finished checking reviewed projects submitted more than 40hrs ago");
}
});

scheduler::schedule_versions(&mut scheduler, pool.clone());

let download_queue = web::Data::new(DownloadQueue::new());
Expand Down
7 changes: 6 additions & 1 deletion src/models/collections.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ impl From<database::models::Collection> for Collection {
pub enum CollectionStatus {
Listed,
Unlisted,
Private,
Rejected,
Unknown,
}
Expand All @@ -83,6 +84,7 @@ impl CollectionStatus {
match string {
"listed" => CollectionStatus::Listed,
"unlisted" => CollectionStatus::Unlisted,
"private" => CollectionStatus::Private,
"rejected" => CollectionStatus::Rejected,
_ => CollectionStatus::Unknown,
}
Expand All @@ -91,6 +93,7 @@ impl CollectionStatus {
match self {
CollectionStatus::Listed => "listed",
CollectionStatus::Unlisted => "unlisted",
CollectionStatus::Private => "private",
CollectionStatus::Rejected => "rejected",
CollectionStatus::Unknown => "unknown",
}
Expand All @@ -100,7 +103,7 @@ impl CollectionStatus {
pub fn is_hidden(&self) -> bool {
match self {
CollectionStatus::Rejected => true,

CollectionStatus::Private => true,
CollectionStatus::Listed => false,
CollectionStatus::Unlisted => false,
CollectionStatus::Unknown => false,
Expand All @@ -110,6 +113,7 @@ impl CollectionStatus {
pub fn is_approved(&self) -> bool {
match self {
CollectionStatus::Listed => true,
CollectionStatus::Private => true,
CollectionStatus::Unlisted => true,
CollectionStatus::Rejected => false,
CollectionStatus::Unknown => false,
Expand All @@ -119,6 +123,7 @@ impl CollectionStatus {
pub fn can_be_requested(&self) -> bool {
match self {
CollectionStatus::Listed => true,
CollectionStatus::Private => true,
CollectionStatus::Unlisted => true,
CollectionStatus::Rejected => false,
CollectionStatus::Unknown => false,
Expand Down
2 changes: 2 additions & 0 deletions src/models/pack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ pub enum EnvType {
#[serde(rename_all = "kebab-case")]
pub enum PackDependency {
Forge,
Neoforge,
FabricLoader,
QuiltLoader,
Minecraft,
Expand All @@ -100,6 +101,7 @@ impl PackDependency {
pub fn as_str(&self) -> &'static str {
match self {
PackDependency::Forge => "forge",
PackDependency::Neoforge => "neoforge",
PackDependency::FabricLoader => "fabric-loader",
PackDependency::Minecraft => "minecraft",
PackDependency::QuiltLoader => "quilt-loader",
Expand Down
Loading

0 comments on commit 9b137ec

Please sign in to comment.