2023-04-24 15:35:32 +00:00
|
|
|
use actix_web::{dev::ConnectionInfo, get, post, web, HttpResponse, Scope};
|
2022-08-25 15:21:59 +00:00
|
|
|
use actix_web_httpauth::extractors::bearer::BearerAuth;
|
2022-09-07 10:43:50 +00:00
|
|
|
use uuid::Uuid;
|
2022-08-25 15:21:59 +00:00
|
|
|
|
2023-04-24 15:35:32 +00:00
|
|
|
use crate::http::get_request_base_url;
|
|
|
|
use crate::mastodon_api::{
|
|
|
|
accounts::types::Account, errors::MastodonError, oauth::auth::get_current_user,
|
|
|
|
};
|
2023-02-18 23:52:48 +00:00
|
|
|
use mitra_config::Config;
|
2023-03-30 20:27:17 +00:00
|
|
|
use mitra_models::{
|
|
|
|
database::{get_database_client, DbPool},
|
2023-04-24 15:35:32 +00:00
|
|
|
invoices::queries::get_invoice_by_id,
|
2023-03-30 20:27:17 +00:00
|
|
|
subscriptions::queries::get_subscription_by_participants,
|
|
|
|
users::types::Permission,
|
|
|
|
};
|
2023-04-08 19:20:12 +00:00
|
|
|
|
2022-08-25 23:01:08 +00:00
|
|
|
use super::types::{
|
2023-04-24 15:35:32 +00:00
|
|
|
Invoice, SubscriptionAuthorizationQueryParams, SubscriptionDetails, SubscriptionOption,
|
2022-09-05 22:08:38 +00:00
|
|
|
SubscriptionQueryParams,
|
2022-08-25 23:01:08 +00:00
|
|
|
};
|
2022-08-25 15:21:59 +00:00
|
|
|
|
2022-09-08 09:58:00 +00:00
|
|
|
#[get("/authorize")]
|
2022-08-25 15:21:59 +00:00
|
|
|
pub async fn authorize_subscription(
|
|
|
|
auth: BearerAuth,
|
2023-04-08 19:20:12 +00:00
|
|
|
_config: web::Data<Config>,
|
2022-12-03 21:23:52 +00:00
|
|
|
db_pool: web::Data<DbPool>,
|
2023-04-08 19:20:12 +00:00
|
|
|
_query_params: web::Query<SubscriptionAuthorizationQueryParams>,
|
2023-02-25 22:27:07 +00:00
|
|
|
) -> Result<HttpResponse, MastodonError> {
|
2022-08-25 15:21:59 +00:00
|
|
|
let db_client = &**get_database_client(&db_pool).await?;
|
2023-04-08 19:20:12 +00:00
|
|
|
let _current_user = get_current_user(db_client, auth.token()).await?;
|
2023-04-25 11:19:04 +00:00
|
|
|
// We don't have subscriptions
|
|
|
|
Err(MastodonError::PermissionError)
|
2022-08-25 15:21:59 +00:00
|
|
|
}
|
|
|
|
|
2022-09-02 18:28:27 +00:00
|
|
|
#[get("/options")]
|
|
|
|
async fn get_subscription_options(
|
|
|
|
auth: BearerAuth,
|
2022-12-03 21:23:52 +00:00
|
|
|
db_pool: web::Data<DbPool>,
|
2023-02-25 22:27:07 +00:00
|
|
|
) -> Result<HttpResponse, MastodonError> {
|
2022-09-02 18:28:27 +00:00
|
|
|
let db_client = &**get_database_client(&db_pool).await?;
|
|
|
|
let current_user = get_current_user(db_client, auth.token()).await?;
|
2023-04-24 15:35:32 +00:00
|
|
|
let options: Vec<SubscriptionOption> = current_user
|
|
|
|
.profile
|
|
|
|
.payment_options
|
|
|
|
.into_inner()
|
|
|
|
.into_iter()
|
2022-09-02 18:28:27 +00:00
|
|
|
.filter_map(SubscriptionOption::from_payment_option)
|
|
|
|
.collect();
|
|
|
|
Ok(HttpResponse::Ok().json(options))
|
|
|
|
}
|
|
|
|
|
2022-09-08 10:05:34 +00:00
|
|
|
#[post("/options")]
|
2022-09-15 20:18:14 +00:00
|
|
|
pub async fn register_subscription_option(
|
2022-08-25 15:21:59 +00:00
|
|
|
auth: BearerAuth,
|
2023-02-21 21:39:42 +00:00
|
|
|
connection_info: ConnectionInfo,
|
2022-08-25 15:21:59 +00:00
|
|
|
config: web::Data<Config>,
|
2022-12-03 21:23:52 +00:00
|
|
|
db_pool: web::Data<DbPool>,
|
2023-04-08 19:20:12 +00:00
|
|
|
_subscription_option: web::Json<SubscriptionOption>,
|
2023-02-25 22:27:07 +00:00
|
|
|
) -> Result<HttpResponse, MastodonError> {
|
2023-01-21 00:23:15 +00:00
|
|
|
let db_client = &mut **get_database_client(&db_pool).await?;
|
2023-04-08 19:20:12 +00:00
|
|
|
let current_user = get_current_user(db_client, auth.token()).await?;
|
2023-04-24 15:35:32 +00:00
|
|
|
if !current_user
|
|
|
|
.role
|
|
|
|
.has_permission(Permission::ManageSubscriptionOptions)
|
|
|
|
{
|
2023-02-25 22:27:07 +00:00
|
|
|
return Err(MastodonError::PermissionError);
|
2023-02-04 16:57:55 +00:00
|
|
|
};
|
2022-08-25 15:21:59 +00:00
|
|
|
|
2023-02-21 21:23:12 +00:00
|
|
|
let account = Account::from_user(
|
2023-02-21 21:39:42 +00:00
|
|
|
&get_request_base_url(connection_info),
|
2023-02-21 21:23:12 +00:00
|
|
|
&config.instance_url(),
|
|
|
|
current_user,
|
|
|
|
);
|
2022-08-25 15:21:59 +00:00
|
|
|
Ok(HttpResponse::Ok().json(account))
|
|
|
|
}
|
|
|
|
|
2022-09-05 22:08:38 +00:00
|
|
|
#[get("/find")]
|
|
|
|
async fn find_subscription(
|
2022-12-03 21:23:52 +00:00
|
|
|
db_pool: web::Data<DbPool>,
|
2022-09-05 22:08:38 +00:00
|
|
|
query_params: web::Query<SubscriptionQueryParams>,
|
2023-02-25 22:27:07 +00:00
|
|
|
) -> Result<HttpResponse, MastodonError> {
|
2022-09-05 22:08:38 +00:00
|
|
|
let db_client = &**get_database_client(&db_pool).await?;
|
|
|
|
let subscription = get_subscription_by_participants(
|
|
|
|
db_client,
|
|
|
|
&query_params.sender_id,
|
|
|
|
&query_params.recipient_id,
|
2023-04-24 15:35:32 +00:00
|
|
|
)
|
|
|
|
.await?;
|
2022-09-05 22:08:38 +00:00
|
|
|
let details = SubscriptionDetails {
|
|
|
|
id: subscription.id,
|
|
|
|
expires_at: subscription.expires_at,
|
|
|
|
};
|
|
|
|
Ok(HttpResponse::Ok().json(details))
|
|
|
|
}
|
|
|
|
|
2022-09-07 10:43:50 +00:00
|
|
|
#[get("/invoices/{invoice_id}")]
|
|
|
|
async fn get_invoice(
|
2022-12-03 21:23:52 +00:00
|
|
|
db_pool: web::Data<DbPool>,
|
2022-09-07 10:43:50 +00:00
|
|
|
invoice_id: web::Path<Uuid>,
|
2023-02-25 22:27:07 +00:00
|
|
|
) -> Result<HttpResponse, MastodonError> {
|
2022-09-07 10:43:50 +00:00
|
|
|
let db_client = &**get_database_client(&db_pool).await?;
|
|
|
|
let db_invoice = get_invoice_by_id(db_client, &invoice_id).await?;
|
|
|
|
let invoice = Invoice::from(db_invoice);
|
|
|
|
Ok(HttpResponse::Ok().json(invoice))
|
|
|
|
}
|
|
|
|
|
2022-08-25 15:21:59 +00:00
|
|
|
pub fn subscription_api_scope() -> Scope {
|
|
|
|
web::scope("/api/v1/subscriptions")
|
2022-09-08 09:58:00 +00:00
|
|
|
.service(authorize_subscription)
|
2022-09-02 18:28:27 +00:00
|
|
|
.service(get_subscription_options)
|
2022-09-15 20:18:14 +00:00
|
|
|
.service(register_subscription_option)
|
2022-09-05 22:08:38 +00:00
|
|
|
.service(find_subscription)
|
2022-09-07 10:43:50 +00:00
|
|
|
.service(get_invoice)
|
2022-08-25 15:21:59 +00:00
|
|
|
}
|