2021-12-01 21:55:38 +00:00
|
|
|
/// https://docs.joinmastodon.org/methods/timelines/
|
2021-11-07 08:52:57 +00:00
|
|
|
use actix_web::{get, web, HttpResponse, Scope};
|
2021-10-05 18:10:14 +00:00
|
|
|
use actix_web_httpauth::extractors::bearer::BearerAuth;
|
2021-04-09 00:22:17 +00:00
|
|
|
|
|
|
|
use crate::config::Config;
|
|
|
|
use crate::database::{Pool, get_database_client};
|
|
|
|
use crate::errors::HttpError;
|
2021-10-05 18:10:14 +00:00
|
|
|
use crate::mastodon_api::oauth::auth::get_current_user;
|
2021-04-09 00:22:17 +00:00
|
|
|
use crate::mastodon_api::statuses::types::Status;
|
2021-11-24 16:39:30 +00:00
|
|
|
use crate::models::posts::helpers::{
|
|
|
|
get_actions_for_posts,
|
|
|
|
get_reposted_posts,
|
|
|
|
};
|
2021-11-07 08:52:57 +00:00
|
|
|
use crate::models::posts::queries::get_home_timeline;
|
2021-12-01 21:55:38 +00:00
|
|
|
use super::types::TimelineQueryParams;
|
2021-04-09 00:22:17 +00:00
|
|
|
|
2021-11-07 08:52:57 +00:00
|
|
|
#[get("/home")]
|
|
|
|
async fn home_timeline(
|
2021-10-05 18:10:14 +00:00
|
|
|
auth: BearerAuth,
|
2021-04-09 00:22:17 +00:00
|
|
|
config: web::Data<Config>,
|
|
|
|
db_pool: web::Data<Pool>,
|
2021-12-01 21:55:38 +00:00
|
|
|
query_params: web::Query<TimelineQueryParams>,
|
2021-04-09 00:22:17 +00:00
|
|
|
) -> Result<HttpResponse, HttpError> {
|
|
|
|
let db_client = &**get_database_client(&db_pool).await?;
|
2021-10-05 18:10:14 +00:00
|
|
|
let current_user = get_current_user(db_client, auth.token()).await?;
|
2021-12-01 21:55:38 +00:00
|
|
|
let mut posts = get_home_timeline(
|
|
|
|
db_client,
|
|
|
|
¤t_user.id,
|
|
|
|
query_params.limit,
|
|
|
|
query_params.max_id,
|
|
|
|
).await?;
|
2021-11-24 16:39:30 +00:00
|
|
|
get_reposted_posts(db_client, posts.iter_mut().collect()).await?;
|
2021-10-18 22:35:52 +00:00
|
|
|
get_actions_for_posts(
|
|
|
|
db_client,
|
|
|
|
¤t_user.id,
|
|
|
|
posts.iter_mut().collect(),
|
|
|
|
).await?;
|
|
|
|
let statuses: Vec<Status> = posts
|
2021-04-09 00:22:17 +00:00
|
|
|
.into_iter()
|
|
|
|
.map(|post| Status::from_post(post, &config.instance_url()))
|
|
|
|
.collect();
|
|
|
|
Ok(HttpResponse::Ok().json(statuses))
|
|
|
|
}
|
2021-11-07 08:52:57 +00:00
|
|
|
|
|
|
|
pub fn timeline_api_scope() -> Scope {
|
|
|
|
web::scope("/api/v1/timelines")
|
|
|
|
.service(home_timeline)
|
|
|
|
}
|