feat: filter imported and native forges

This commit is contained in:
Aravinth Manivannan 2023-02-26 23:38:36 +05:30
parent 3f29d420c3
commit 3fb7b27ab3
Signed by: realaravinth
GPG key ID: AD9F0F08E855ED88
5 changed files with 52 additions and 14 deletions

View file

@ -191,7 +191,12 @@ pub trait SCDatabase: std::marker::Send + std::marker::Sync + CloneSPDatabase {
async fn forge_type_exists(&self, forge_type: &ForgeImplementation) -> DBResult<bool>; async fn forge_type_exists(&self, forge_type: &ForgeImplementation) -> DBResult<bool>;
/// Get all forges /// Get all forges
async fn get_all_forges(&self, offset: u32, limit: u32) -> DBResult<Vec<Forge>>; async fn get_all_forges(
&self,
with_imports: bool,
offset: u32,
limit: u32,
) -> DBResult<Vec<Forge>>;
/// add new user to database /// add new user to database
async fn add_user(&self, u: &AddUser) -> DBResult<()>; async fn add_user(&self, u: &AddUser) -> DBResult<()>;

View file

@ -35,7 +35,7 @@ pub async fn adding_forge_works<'a, T: SCDatabase>(
{ {
let forge = db.get_forge(&create_forge_msg.url).await.unwrap(); let forge = db.get_forge(&create_forge_msg.url).await.unwrap();
let forges = db.get_all_forges(0, 10).await.unwrap(); let forges = db.get_all_forges(true, 0, 10).await.unwrap();
assert_eq!(forges.len(), 1); assert_eq!(forges.len(), 1);
assert_eq!( assert_eq!(

View file

@ -307,8 +307,14 @@ impl SCDatabase for Database {
} }
/// Get all forges /// Get all forges
async fn get_all_forges(&self, offset: u32, limit: u32) -> DBResult<Vec<Forge>> { async fn get_all_forges(
let mut inter_forges = sqlx::query_as!( &self,
with_imports: bool,
offset: u32,
limit: u32,
) -> DBResult<Vec<Forge>> {
let mut inter_forges = if with_imports {
sqlx::query_as!(
InnerForge, InnerForge,
"SELECT "SELECT
hostname, hostname,
@ -330,7 +336,34 @@ impl SCDatabase for Database {
) )
.fetch_all(&self.pool) .fetch_all(&self.pool)
.await .await
.map_err(|e| DBError::DBError(Box::new(e)))?; .map_err(|e| DBError::DBError(Box::new(e)))?
} else {
sqlx::query_as!(
InnerForge,
"SELECT
hostname,
last_crawl_on,
starchart_forge_type.name,
imported
FROM
starchart_forges
INNER JOIN
starchart_forge_type
ON
starchart_forges.forge_type = starchart_forge_type.id
WHERE
starchart_forges.imported = false
ORDER BY
starchart_forges.ID
LIMIT $1 OFFSET $2;
",
limit,
offset
)
.fetch_all(&self.pool)
.await
.map_err(|e| DBError::DBError(Box::new(e)))?
};
let mut forges: Vec<Forge> = Vec::with_capacity(inter_forges.len()); let mut forges: Vec<Forge> = Vec::with_capacity(inter_forges.len());
inter_forges.drain(0..).for_each(|f| forges.push(f.into())); inter_forges.drain(0..).for_each(|f| forges.push(f.into()));

View file

@ -33,7 +33,7 @@ pub async fn forges(db: WebDB, q: web::Query<OptionalPage>) -> ServiceResult<imp
let q = q.into_inner(); let q = q.into_inner();
let q: Page = q.into(); let q: Page = q.into();
let offset = q.page * LIMIT; let offset = q.page * LIMIT;
let forges = db.get_all_forges(offset, LIMIT).await?; let forges = db.get_all_forges(false, offset, LIMIT).await?;
Ok(HttpResponse::Ok().json(forges)) Ok(HttpResponse::Ok().json(forges))
} }

View file

@ -189,7 +189,7 @@ impl Crawler {
break; break;
} }
let forges = c.db.get_all_forges(offset, LIMIT).await.unwrap(); let forges = c.db.get_all_forges(false, offset, LIMIT).await.unwrap();
if forges.is_empty() { if forges.is_empty() {
c.federate.tar().await.unwrap(); c.federate.tar().await.unwrap();
page = 0; page = 0;