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;
|
2022-05-06 15:03:43 +00:00
|
|
|
use crate::mastodon_api::statuses::helpers::build_status_list;
|
2022-05-05 20:11:43 +00:00
|
|
|
use crate::models::posts::queries::{
|
|
|
|
get_home_timeline,
|
|
|
|
get_local_timeline,
|
|
|
|
get_posts_by_tag,
|
|
|
|
};
|
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?;
|
2022-05-06 15:03:43 +00:00
|
|
|
let posts = get_home_timeline(
|
2021-12-01 21:55:38 +00:00
|
|
|
db_client,
|
|
|
|
¤t_user.id,
|
|
|
|
query_params.max_id,
|
2022-09-29 21:15:54 +00:00
|
|
|
query_params.limit.inner(),
|
2021-12-01 21:55:38 +00:00
|
|
|
).await?;
|
2022-05-06 15:03:43 +00:00
|
|
|
let statuses = build_status_list(
|
2021-10-18 22:35:52 +00:00
|
|
|
db_client,
|
2022-05-06 15:03:43 +00:00
|
|
|
&config.instance_url(),
|
|
|
|
Some(¤t_user),
|
|
|
|
posts,
|
2021-10-18 22:35:52 +00:00
|
|
|
).await?;
|
2021-04-09 00:22:17 +00:00
|
|
|
Ok(HttpResponse::Ok().json(statuses))
|
|
|
|
}
|
2021-11-07 08:52:57 +00:00
|
|
|
|
2022-05-05 20:11:43 +00:00
|
|
|
/// Local timeline ("local" parameter is ignored)
|
|
|
|
#[get("/public")]
|
|
|
|
async fn public_timeline(
|
|
|
|
auth: BearerAuth,
|
|
|
|
config: web::Data<Config>,
|
|
|
|
db_pool: web::Data<Pool>,
|
|
|
|
query_params: web::Query<TimelineQueryParams>,
|
|
|
|
) -> Result<HttpResponse, HttpError> {
|
|
|
|
let db_client = &**get_database_client(&db_pool).await?;
|
|
|
|
let current_user = get_current_user(db_client, auth.token()).await?;
|
|
|
|
let posts = get_local_timeline(
|
|
|
|
db_client,
|
|
|
|
¤t_user.id,
|
|
|
|
query_params.max_id,
|
2022-09-29 21:15:54 +00:00
|
|
|
query_params.limit.inner(),
|
2022-05-05 20:11:43 +00:00
|
|
|
).await?;
|
|
|
|
let statuses = build_status_list(
|
|
|
|
db_client,
|
|
|
|
&config.instance_url(),
|
|
|
|
Some(¤t_user),
|
|
|
|
posts,
|
|
|
|
).await?;
|
|
|
|
Ok(HttpResponse::Ok().json(statuses))
|
|
|
|
}
|
|
|
|
|
2021-12-12 13:38:10 +00:00
|
|
|
#[get("/tag/{hashtag}")]
|
|
|
|
async fn hashtag_timeline(
|
|
|
|
auth: Option<BearerAuth>,
|
|
|
|
config: web::Data<Config>,
|
|
|
|
db_pool: web::Data<Pool>,
|
2022-04-09 17:04:43 +00:00
|
|
|
hashtag: web::Path<String>,
|
2021-12-12 13:38:10 +00:00
|
|
|
query_params: web::Query<TimelineQueryParams>,
|
|
|
|
) -> Result<HttpResponse, HttpError> {
|
|
|
|
let db_client = &**get_database_client(&db_pool).await?;
|
|
|
|
let maybe_current_user = match auth {
|
|
|
|
Some(auth) => Some(get_current_user(db_client, auth.token()).await?),
|
|
|
|
None => None,
|
|
|
|
};
|
2022-05-06 15:03:43 +00:00
|
|
|
let posts = get_posts_by_tag(
|
2021-12-12 13:38:10 +00:00
|
|
|
db_client,
|
|
|
|
&hashtag,
|
2022-01-07 01:10:41 +00:00
|
|
|
maybe_current_user.as_ref().map(|user| &user.id),
|
2021-12-12 13:38:10 +00:00
|
|
|
query_params.max_id,
|
2022-09-29 21:15:54 +00:00
|
|
|
query_params.limit.inner(),
|
2021-12-12 13:38:10 +00:00
|
|
|
).await?;
|
2022-05-06 15:03:43 +00:00
|
|
|
let statuses = build_status_list(
|
|
|
|
db_client,
|
|
|
|
&config.instance_url(),
|
|
|
|
maybe_current_user.as_ref(),
|
|
|
|
posts,
|
|
|
|
).await?;
|
2021-12-12 13:38:10 +00:00
|
|
|
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)
|
2022-05-05 20:11:43 +00:00
|
|
|
.service(public_timeline)
|
2021-12-12 13:38:10 +00:00
|
|
|
.service(hashtag_timeline)
|
2021-11-07 08:52:57 +00:00
|
|
|
}
|