feat: federated search

This commit is contained in:
Aravinth Manivannan 2023-03-02 18:51:06 +05:30
parent 7f4e6d5bda
commit 3270ef89ee
Signed by: realaravinth
GPG key ID: AD9F0F08E855ED88
2 changed files with 36 additions and 8 deletions

View file

@ -145,12 +145,9 @@ impl Repository {
.get("en")
.as_ref()
.unwrap()
.short_description.as_deref();
let website = self
.description
.get("en")
.unwrap()
.documentation.as_deref();
.short_description
.as_deref();
let website = self.description.get("en").unwrap().documentation.as_deref();
AddRepository {
html_link: self.url.as_str(),
tags,

View file

@ -15,18 +15,40 @@
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
use crate::errors::*;
use crate::{errors::*, WebCtx};
use actix_web::web;
use actix_web::{HttpResponse, Responder};
use actix_web_codegen_const_routes::post;
use db_core::Repository;
use url::Url;
use crate::Ctx;
use crate::WebDB;
pub use crate::api::{SearchRepositoryReq, ROUTES};
impl Ctx {
async fn client_federated_search(
&self,
mut starchart_url: Url,
) -> ServiceResult<Vec<Repository>> {
starchart_url.set_path(ROUTES.forges);
Ok(self
.client
.get(starchart_url)
.send()
.await
.unwrap()
.json()
.await
.unwrap())
}
}
#[post(path = "ROUTES.search.repository")]
pub async fn search_repository(
payload: web::Json<SearchRepositoryReq>,
ctx: WebCtx,
db: WebDB,
) -> ServiceResult<impl Responder> {
let payload = payload.into_inner();
@ -36,7 +58,16 @@ pub async fn search_repository(
format!("*{}*", payload.query)
};
let local_resp = db.search_repository(&query).await?;
Ok(HttpResponse::Ok().json(local_resp))
let mut federated_resp = Vec::default();
for starchart in db.search_mini_index(&query).await?.iter() {
let resp = ctx.client_federated_search(Url::parse(starchart)?).await?;
federated_resp.extend(resp);
}
federated_resp.extend(local_resp);
Ok(HttpResponse::Ok().json(federated_resp))
}
pub fn services(cfg: &mut web::ServiceConfig) {