mirror of
https://github.com/n08i40k/schedule-parser-rusted.git
synced 2025-12-06 17:57:47 +03:00
feat!: add telegram auth and async refactor
- Removed "/schedule/update-download-url" endpoint, this mechanism was replaced by Yandex Cloud FaaS. Ура :) - Improved schedule caching mechanism. - Added Telegram WebApp authentication support. - Reworked endpoints responses and errors mechanism. - Refactored application state management. - Make synchronous database operations, middlewares and extractors to asynchronous. - Made user password field optional to support multiple auth methods. - Renamed users table column "version" to "android_version" and made it nullable.
This commit is contained in:
@@ -1,85 +1,62 @@
|
||||
use self::schema::*;
|
||||
use crate::app_state::AppState;
|
||||
use crate::database::driver::users::UserSave;
|
||||
use crate::database::models::User;
|
||||
use crate::extractors::base::SyncExtractor;
|
||||
use crate::routes::schema::IntoResponseAsError;
|
||||
use crate::utility::mutex::MutexScope;
|
||||
use crate::extractors::base::AsyncExtractor;
|
||||
use crate::state::AppState;
|
||||
use actix_web::{post, web};
|
||||
|
||||
#[utoipa::path(responses((status = OK)))]
|
||||
#[post("/change-group")]
|
||||
pub async fn change_group(
|
||||
app_state: web::Data<AppState>,
|
||||
user: SyncExtractor<User>,
|
||||
user: AsyncExtractor<User>,
|
||||
data: web::Json<Request>,
|
||||
) -> ServiceResponse {
|
||||
let mut user = user.into_inner();
|
||||
|
||||
if user.group == data.group {
|
||||
return ErrorCode::SameGroup.into_response();
|
||||
if user.group.is_some_and(|group| group == data.group) {
|
||||
return Ok(()).into();
|
||||
}
|
||||
|
||||
if let Some(e) = app_state.schedule.scope(|schedule| match schedule {
|
||||
Some(schedule) => {
|
||||
if schedule.data.groups.contains_key(&data.group) {
|
||||
None
|
||||
} else {
|
||||
Some(ErrorCode::NotFound)
|
||||
}
|
||||
}
|
||||
None => Some(ErrorCode::NoSchedule),
|
||||
}) {
|
||||
return e.into_response();
|
||||
if !app_state
|
||||
.get_schedule_snapshot()
|
||||
.await
|
||||
.data
|
||||
.groups
|
||||
.contains_key(&data.group)
|
||||
{
|
||||
return Err(ErrorCode::NotFound).into();
|
||||
}
|
||||
|
||||
user.group = data.into_inner().group;
|
||||
|
||||
if let Some(e) = user.save(&app_state).err() {
|
||||
eprintln!("Failed to update user: {e}");
|
||||
return ErrorCode::InternalServerError.into_response();
|
||||
}
|
||||
|
||||
|
||||
user.group = Some(data.into_inner().group);
|
||||
user.save(&app_state).await.unwrap();
|
||||
|
||||
Ok(()).into()
|
||||
}
|
||||
|
||||
mod schema {
|
||||
use actix_macros::{IntoResponseErrorNamed, StatusCode};
|
||||
use actix_macros::ErrResponse;
|
||||
use derive_more::Display;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use utoipa::ToSchema;
|
||||
|
||||
pub type ServiceResponse = crate::routes::schema::Response<(), ErrorCode>;
|
||||
|
||||
#[derive(Serialize, Deserialize, ToSchema)]
|
||||
#[derive(Deserialize, ToSchema)]
|
||||
#[schema(as = ChangeGroup::Request)]
|
||||
pub struct Request {
|
||||
/// Group name.
|
||||
// Group.
|
||||
pub group: String,
|
||||
}
|
||||
|
||||
#[derive(Clone, Serialize, ToSchema, StatusCode, Display, IntoResponseErrorNamed)]
|
||||
#[derive(Clone, Serialize, Display, ToSchema, ErrResponse)]
|
||||
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
|
||||
#[schema(as = ChangeGroup::ErrorCode)]
|
||||
#[status_code = "actix_web::http::StatusCode::CONFLICT"]
|
||||
pub enum ErrorCode {
|
||||
/// Schedules have not yet been received.
|
||||
#[display("Schedule not parsed yet.")]
|
||||
#[status_code = "actix_web::http::StatusCode::SERVICE_UNAVAILABLE"]
|
||||
NoSchedule,
|
||||
|
||||
/// Passed the same group name that is currently there.
|
||||
#[display("Passed the same group name as it is at the moment.")]
|
||||
SameGroup,
|
||||
|
||||
/// The required group does not exist.
|
||||
#[display("The required group does not exist.")]
|
||||
#[status_code = "actix_web::http::StatusCode::NOT_FOUND"]
|
||||
NotFound,
|
||||
|
||||
/// Server-side error.
|
||||
#[display("Internal server error.")]
|
||||
#[status_code = "actix_web::http::StatusCode::INTERNAL_SERVER_ERROR"]
|
||||
InternalServerError,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,41 +1,39 @@
|
||||
use self::schema::*;
|
||||
use crate::app_state::AppState;
|
||||
use crate::database::driver;
|
||||
use crate::database::driver::users::UserSave;
|
||||
use crate::database::models::User;
|
||||
use crate::extractors::base::SyncExtractor;
|
||||
use crate::routes::schema::IntoResponseAsError;
|
||||
use crate::extractors::base::AsyncExtractor;
|
||||
use crate::state::AppState;
|
||||
use actix_web::{post, web};
|
||||
|
||||
#[utoipa::path(responses((status = OK)))]
|
||||
#[post("/change-username")]
|
||||
pub async fn change_username(
|
||||
app_state: web::Data<AppState>,
|
||||
user: SyncExtractor<User>,
|
||||
user: AsyncExtractor<User>,
|
||||
data: web::Json<Request>,
|
||||
) -> ServiceResponse {
|
||||
let mut user = user.into_inner();
|
||||
|
||||
if user.username == data.username {
|
||||
return ErrorCode::SameUsername.into_response();
|
||||
return Ok(()).into();
|
||||
}
|
||||
|
||||
if driver::users::get_by_username(&app_state, &data.username).is_ok() {
|
||||
return ErrorCode::AlreadyExists.into_response();
|
||||
if driver::users::get_by_username(&app_state, &data.username)
|
||||
.await
|
||||
.is_ok()
|
||||
{
|
||||
return Err(ErrorCode::AlreadyExists).into();
|
||||
}
|
||||
|
||||
user.username = data.into_inner().username;
|
||||
|
||||
if let Some(e) = user.save(&app_state).err() {
|
||||
eprintln!("Failed to update user: {e}");
|
||||
return ErrorCode::InternalServerError.into_response();
|
||||
}
|
||||
user.save(&app_state).await.unwrap();
|
||||
|
||||
Ok(()).into()
|
||||
}
|
||||
|
||||
mod schema {
|
||||
use actix_macros::{IntoResponseErrorNamed, StatusCode};
|
||||
use actix_macros::ErrResponse;
|
||||
use derive_more::Display;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use utoipa::ToSchema;
|
||||
@@ -49,22 +47,13 @@ mod schema {
|
||||
pub username: String,
|
||||
}
|
||||
|
||||
#[derive(Clone, Serialize, ToSchema, StatusCode, Display, IntoResponseErrorNamed)]
|
||||
#[derive(Clone, Serialize, Display, ToSchema, ErrResponse)]
|
||||
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
|
||||
#[schema(as = ChangeUsername::ErrorCode)]
|
||||
#[status_code = "actix_web::http::StatusCode::CONFLICT"]
|
||||
pub enum ErrorCode {
|
||||
/// The same name that is currently present is passed.
|
||||
#[display("Passed the same name as it is at the moment.")]
|
||||
SameUsername,
|
||||
|
||||
/// A user with this name already exists.
|
||||
#[display("A user with this name already exists.")]
|
||||
AlreadyExists,
|
||||
|
||||
/// Server-side error.
|
||||
#[display("Internal server error.")]
|
||||
#[status_code = "actix_web::http::StatusCode::INTERNAL_SERVER_ERROR"]
|
||||
InternalServerError,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
use crate::database::models::User;
|
||||
use crate::extractors::base::SyncExtractor;
|
||||
use actix_web::get;
|
||||
use crate::extractors::base::AsyncExtractor;
|
||||
use crate::routes::schema::user::UserResponse;
|
||||
use actix_web::get;
|
||||
|
||||
#[utoipa::path(responses((status = OK, body = UserResponse)))]
|
||||
#[get("/me")]
|
||||
pub async fn me(user: SyncExtractor<User>) -> UserResponse {
|
||||
pub async fn me(user: AsyncExtractor<User>) -> UserResponse {
|
||||
user.into_inner().into()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user