2021-12-02 23:03:26 +08:00
|
|
|
use actix_web::{web, HttpRequest, HttpResponse};
|
|
|
|
use meilisearch_error::ResponseError;
|
|
|
|
use meilisearch_lib::tasks::task::TaskId;
|
2021-12-06 22:45:41 +08:00
|
|
|
use meilisearch_lib::tasks::TaskFilter;
|
2021-12-02 23:03:26 +08:00
|
|
|
use meilisearch_lib::MeiliSearch;
|
|
|
|
use serde_json::json;
|
|
|
|
|
|
|
|
use crate::analytics::Analytics;
|
|
|
|
use crate::extractors::authentication::{policies::*, GuardedData};
|
|
|
|
use crate::task::{TaskListView, TaskView};
|
|
|
|
|
|
|
|
pub fn configure(cfg: &mut web::ServiceConfig) {
|
|
|
|
cfg.service(web::resource("").route(web::get().to(get_tasks)))
|
|
|
|
.service(web::resource("/{task_id}").route(web::get().to(get_task)));
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn get_tasks(
|
2021-11-09 01:31:27 +08:00
|
|
|
meilisearch: GuardedData<ActionPolicy<{ actions::TASKS_GET }>, MeiliSearch>,
|
2021-12-02 23:03:26 +08:00
|
|
|
req: HttpRequest,
|
|
|
|
analytics: web::Data<dyn Analytics>,
|
|
|
|
) -> Result<HttpResponse, ResponseError> {
|
|
|
|
analytics.publish(
|
|
|
|
"Tasks Seen".to_string(),
|
|
|
|
json!({ "per_task_uid": false }),
|
|
|
|
Some(&req),
|
|
|
|
);
|
|
|
|
|
2021-12-06 22:45:41 +08:00
|
|
|
let filters = meilisearch.filters().indexes.as_ref().map(|indexes| {
|
|
|
|
let mut filters = TaskFilter::default();
|
|
|
|
for index in indexes {
|
|
|
|
filters.filter_index(index.to_string());
|
|
|
|
}
|
|
|
|
filters
|
|
|
|
});
|
|
|
|
|
2021-12-02 23:03:26 +08:00
|
|
|
let tasks: TaskListView = meilisearch
|
2021-12-06 22:45:41 +08:00
|
|
|
.list_tasks(filters, None, None)
|
2021-12-02 23:03:26 +08:00
|
|
|
.await?
|
|
|
|
.into_iter()
|
|
|
|
.map(TaskView::from)
|
|
|
|
.collect::<Vec<_>>()
|
|
|
|
.into();
|
|
|
|
|
|
|
|
Ok(HttpResponse::Ok().json(tasks))
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn get_task(
|
2021-11-09 01:31:27 +08:00
|
|
|
meilisearch: GuardedData<ActionPolicy<{ actions::TASKS_GET }>, MeiliSearch>,
|
2021-12-02 23:03:26 +08:00
|
|
|
task_id: web::Path<TaskId>,
|
|
|
|
req: HttpRequest,
|
|
|
|
analytics: web::Data<dyn Analytics>,
|
|
|
|
) -> Result<HttpResponse, ResponseError> {
|
|
|
|
analytics.publish(
|
|
|
|
"Tasks Seen".to_string(),
|
|
|
|
json!({ "per_task_uid": true }),
|
|
|
|
Some(&req),
|
|
|
|
);
|
|
|
|
|
2021-12-06 22:45:41 +08:00
|
|
|
let filters = meilisearch.filters().indexes.as_ref().map(|indexes| {
|
|
|
|
let mut filters = TaskFilter::default();
|
|
|
|
for index in indexes {
|
|
|
|
filters.filter_index(index.to_string());
|
|
|
|
}
|
|
|
|
filters
|
|
|
|
});
|
|
|
|
|
2021-12-02 23:03:26 +08:00
|
|
|
let task: TaskView = meilisearch
|
2021-12-06 22:45:41 +08:00
|
|
|
.get_task(task_id.into_inner(), filters)
|
2021-12-02 23:03:26 +08:00
|
|
|
.await?
|
|
|
|
.into();
|
|
|
|
|
|
|
|
Ok(HttpResponse::Ok().json(task))
|
|
|
|
}
|