31 Commits

Author SHA1 Message Date
e3904a255b chore(release): bump version to 1.1.0 2025-09-02 09:23:29 +04:00
829c1cf68d chore(deps): bump calamine version 2025-09-02 09:20:02 +04:00
6a535f8d73 chore(ci): remove creating .env.test 2025-09-02 09:20:02 +04:00
5e5cd53f46 chore(ci): remove creating .env.test 2025-09-02 09:11:30 +04:00
8d59e37976 refactor(android)!: remove FCM support 2025-09-02 09:08:17 +04:00
5e39fc9acc feat(schedule)!: move schedule parser, downloader, and updater to external library
This can be used to support more schedule formats in the future.
2025-09-02 08:59:59 +04:00
7c973bfda0 refactor(middlewares): move MiddlewareError from crate::utility to crate::middlewares 2025-09-02 08:52:15 +04:00
8fba0fc709 feat(env): add ability to set custom .env file for testing on local machine 2025-06-13 01:03:06 +04:00
983ff4fa5e feat(env): expose .env.test 2025-06-13 01:02:08 +04:00
fb6f3fc05f chore(deps): upgrade dependencies 2025-06-13 00:56:58 +04:00
e64011ba16 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.
2025-06-08 01:43:45 +04:00
6a106a366c feat(parser): add ability to parse mistyped date 2025-06-08 01:03:50 +04:00
4fca22662c feat(parser)!: rework of subgroups parsing 2025-06-08 01:03:00 +04:00
d23092a32a feat(parser): add lesson types "course project" and "course project defense" 2025-05-27 02:06:13 +04:00
01bfa38969 feat(parser): speed improvement, lesson type guessing and parsing of merged lesson cabinets 2025-05-27 02:03:54 +04:00
851ec9225f refactor(parser): improve readability 2025-05-26 21:12:23 +04:00
8de1891724 chore(release): bump version to 1.0.5 2025-05-26 05:30:44 +04:00
4cf6df379e fix(parser): fix lessons merging 2025-05-26 05:24:13 +04:00
ba8b164b6a refactor(parser): rewrite some parts of code 2025-05-26 05:24:08 +04:00
ff9d7d6c3a fix(cache): fix setting cache_update_required flag in cache status 2025-05-25 17:39:23 +04:00
9090716f87 fix(test): fix test sign_up_invalid_group 2025-05-25 15:57:18 +04:00
ee992f1b55 chore(xls): update schedule xls 2025-05-25 15:49:52 +04:00
7f71fb1616 refactor(env): remove unsave env::set_var call 2025-05-25 15:48:43 +04:00
234055eaeb feat(test): add ability to use test env without schedule 2025-05-25 15:48:10 +04:00
fceffb900d release/v1.0.3 2025-04-18 00:29:04 +04:00
49ce0005dc Исправление работы подключения к сайтам из-за отсутствия сертификатов. 2025-04-18 00:28:55 +04:00
4c738085f2 release/v1.0.2 2025-04-18 00:11:55 +04:00
20602eb863 Улучшенное отображение ошибок при обновлении ссылки расписания. 2025-04-18 00:11:05 +04:00
e04d462223 1.0.1 2025-04-17 23:08:58 +04:00
22af02464d Исправление работы авторизации с помощью VK ID. 2025-04-17 23:07:19 +04:00
9a517519db User-Agent для reqwest теперь устанавливается с помощью переменной окружения. 2025-04-17 22:41:42 +04:00
90 changed files with 4061 additions and 5392 deletions

26
.env.test Normal file
View File

@@ -0,0 +1,26 @@
# Schedule
# SCHEDULE_INIT_URL=
SCHEDULE_DISABLE_AUTO_UPDATE=1
# Basic authorization
JWT_SECRET="test-secret-at-least-256-bits-used"
# VKID
VK_ID_CLIENT_ID=0
VK_ID_REDIRECT_URI="vk0://vk.com/blank.html"
# Telegram Mini-App
TELEGRAM_BOT_ID=0
TELEGRAM_MINI_APP_HOST=example.com
TELEGRAM_TEST_DC=false
# Yandex Cloud
YANDEX_CLOUD_API_KEY=""
YANDEX_CLOUD_FUNC_ID=""
# Firebase
# GOOGLE_APPLICATION_CREDENTIALS=
# LOGGING
RUST_BACKTRACE=1
# RUST_LOG=debug

View File

@@ -40,13 +40,18 @@ jobs:
- name: Test
run: |
touch .env.test
cargo test --verbose
env:
DATABASE_URL: ${{ env.TEST_DB }}
SCHEDULE_DISABLE_AUTO_UPDATE: 1
JWT_SECRET: "test-secret-at-least-256-bits-used"
VKID_CLIENT_ID: 0
VKID_REDIRECT_URI: "vk0://vk.com/blank.html"
VK_ID_CLIENT_ID: 0
VK_ID_REDIRECT_URI: "vk0://vk.com/blank.html"
TELEGRAM_BOT_ID: 0
TELEGRAM_MINI_APP_HOST: example.com
TELEGRAM_TEST_DC: false
YANDEX_CLOUD_API_KEY: ""
YANDEX_CLOUD_FUNC_ID: ""
build:
name: Build
runs-on: ubuntu-latest

View File

@@ -3,6 +3,7 @@ name: cargo test
on:
push:
branches: [ "master" ]
tags-ignore: [ "release/v*" ]
permissions:
contents: read
@@ -17,14 +18,20 @@ jobs:
steps:
- uses: actions/checkout@v4
- name: Build
run: cargo build
- name: Create .env.test
run: touch .env.test
- name: Run tests
run: cargo test
env:
DATABASE_URL: ${{ secrets.TEST_DATABASE_URL }}
SCHEDULE_DISABLE_AUTO_UPDATE: 1
JWT_SECRET: "test-secret-at-least-256-bits-used"
VKID_CLIENT_ID: 0
VKID_REDIRECT_URI: "vk0://vk.com/blank.html"
VK_ID_CLIENT_ID: 0
VK_ID_REDIRECT_URI: "vk0://vk.com/blank.html"
TELEGRAM_BOT_ID: 0
TELEGRAM_MINI_APP_HOST: example.com
TELEGRAM_TEST_DC: false
YANDEX_CLOUD_API_KEY: ""
YANDEX_CLOUD_FUNC_ID: ""

View File

@@ -4,9 +4,13 @@
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/lib/schedule_parser/src" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/benches" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/actix-macros/src" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/actix-test/src" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/schedule-parser/benches" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/schedule-parser/src" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/providers/base/src" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/providers/provider-engels-polytechnic/src" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/providers/src" isTestSource="false" />
<excludeFolder url="file://$MODULE_DIR$/actix-macros/target" />
<excludeFolder url="file://$MODULE_DIR$/actix-test/target" />
<excludeFolder url="file://$MODULE_DIR$/target" />

2163
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@@ -1,9 +1,9 @@
[workspace]
members = ["actix-macros", "actix-test"]
members = ["actix-macros", "actix-test", "providers"]
[package]
name = "schedule-parser-rusted"
version = "0.8.0"
version = "1.1.0"
edition = "2024"
publish = false
@@ -11,43 +11,65 @@ publish = false
debug = true
[dependencies]
actix-web = "4.10.2"
providers = { path = "providers" }
actix-macros = { path = "actix-macros" }
bcrypt = "0.17.0"
calamine = "0.26.1"
# serve api
actix-web = "4"
# basic
chrono = { version = "0.4.40", features = ["serde"] }
derive_more = "2.0.1"
derive_more = { version = "2", features = ["full"] }
dotenvy = "0.15.7"
# sql
diesel = { version = "2.2.8", features = ["postgres"] }
diesel-derive-enum = { git = "https://github.com/Havunen/diesel-derive-enum.git", features = ["postgres"] }
dotenvy = "0.15.7"
# logging
env_logger = "0.11.7"
firebase-messaging-rs = { git = "https://github.com/i10416/firebase-messaging-rs.git" }
# async
tokio = { version = "1.44.1", features = ["macros", "rt-multi-thread"] }
tokio-util = "0.7.16"
futures-util = "0.3.31"
fuzzy-matcher = "0.3.7"
# authorization
bcrypt = "0.17.0"
jsonwebtoken = { version = "9.3.1", features = ["use_pem"] }
hex = "0.4.3"
mime = "0.3.17"
# creating users
objectid = "0.2.0"
regex = "1.11.1"
# schedule downloader
reqwest = { version = "0.12.15", features = ["json"] }
sentry = "0.37.0"
sentry-actix = "0.37.0"
mime = "0.3.17"
# error handling
sentry = "0.42.0"
sentry-actix = "0.42.0"
# [de]serializing
serde = { version = "1.0.219", features = ["derive"] }
serde_json = "1.0.140"
serde_with = "3.12.0"
serde_repr = "0.1.20"
sha1 = "0.11.0-pre.5"
tokio = { version = "1.44.1", features = ["macros", "rt-multi-thread"] }
rand = "0.9.0"
sha1 = "0.11.0-rc.0"
# documentation
utoipa = { version = "5", features = ["actix_extras", "chrono"] }
utoipa-rapidoc = { version = "6.0.0", features = ["actix-web"] }
utoipa-rapidoc = { version = "6", features = ["actix-web"] }
utoipa-actix-web = "0.1"
uuid = { version = "1.16.0", features = ["v4"] }
uuid = { version = "1", features = ["v4"] }
hex-literal = "1"
log = "0.4"
# telegram webdata deciding and verify
base64 = "0.22"
percent-encoding = "2.3"
ed25519-dalek = "2"
[dev-dependencies]
providers = { path = "providers", features = ["test"] }
actix-test = { path = "actix-test" }
criterion = "0.5.1"
[[bench]]
name = "parse"
harness = false

View File

@@ -6,7 +6,7 @@ ARG BINARY_NAME
WORKDIR /app/
RUN apt update && \
apt install -y libpq5
apt install -y libpq5 ca-certificates openssl
COPY ./${BINARY_NAME} /bin/main
RUN chmod +x /bin/main

View File

@@ -1,7 +0,0 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 4
[[package]]
name = "actix-utility-macros"
version = "0.1.0"

View File

@@ -4,9 +4,9 @@ version = "0.1.0"
edition = "2024"
[dependencies]
syn = "2.0.100"
quote = "1.0.40"
proc-macro2 = "1.0.94"
syn = "2"
quote = "1"
proc-macro2 = "1"
[lib]
proc-macro = true

View File

@@ -62,7 +62,7 @@ mod shared {
}
}
mod response_error_message {
mod middleware_error {
use proc_macro::TokenStream;
use quote::quote;
@@ -81,28 +81,7 @@ mod response_error_message {
fn error_response(&self) -> ::actix_web::HttpResponse<BoxBody> {
::actix_web::HttpResponse::build(self.status_code())
.json(crate::utility::error::ResponseErrorMessage::new(self.clone()))
}
}
})
}
}
mod status_code {
use proc_macro::TokenStream;
use quote::quote;
pub fn fmt(ast: &syn::DeriveInput) -> TokenStream {
let name = &ast.ident;
let status_code_arms = super::shared::get_arms(ast);
TokenStream::from(quote! {
impl crate::routes::schema::PartialStatusCode for #name {
fn status_code(&self) -> ::actix_web::http::StatusCode {
match self {
#(#status_code_arms)*
}
.json(crate::middlewares::error::MiddlewareError::new(self.clone()))
}
}
})
@@ -130,7 +109,7 @@ mod responder_json {
}
}
mod into_response_error {
mod ok_response {
use proc_macro::TokenStream;
use quote::quote;
@@ -138,46 +117,37 @@ mod into_response_error {
let name = &ast.ident;
TokenStream::from(quote! {
impl ::core::convert::Into<crate::routes::schema::ResponseError<#name>> for #name {
fn into(self) -> crate::routes::schema::ResponseError<#name> {
crate::routes::schema::ResponseError {
code: self,
message: ::core::option::Option::None,
}
}
}
impl<T> crate::routes::schema::IntoResponseAsError<T> for #name
where
T: ::serde::ser::Serialize + ::utoipa::PartialSchema {}
})
}
pub fn fmt_named(ast: &syn::DeriveInput) -> TokenStream {
let name = &ast.ident;
TokenStream::from(quote! {
impl ::core::convert::Into<crate::routes::schema::ResponseError<#name>> for #name {
fn into(self) -> crate::routes::schema::ResponseError<#name> {
crate::routes::schema::ResponseError {
message: ::core::option::Option::Some(format!("{}", self)),
code: self,
}
}
}
impl<T> crate::routes::schema::IntoResponseAsError<T> for #name
where
T: ::serde::ser::Serialize + ::utoipa::PartialSchema {}
impl crate::routes::schema::PartialOkResponse for #name {}
})
}
}
#[proc_macro_derive(ResponseErrorMessage, attributes(status_code))]
pub fn rem_derive(input: TokenStream) -> TokenStream {
mod err_response {
use proc_macro::TokenStream;
use quote::quote;
pub fn fmt(ast: &syn::DeriveInput) -> TokenStream {
let name = &ast.ident;
let status_code_arms = super::shared::get_arms(ast);
TokenStream::from(quote! {
impl crate::routes::schema::PartialErrResponse for #name {
fn status_code(&self) -> ::actix_web::http::StatusCode {
match self {
#(#status_code_arms)*
}
}
}
})
}
}
#[proc_macro_derive(MiddlewareError, attributes(status_code))]
pub fn moddleware_error_derive(input: TokenStream) -> TokenStream {
let ast = syn::parse(input).unwrap();
response_error_message::fmt(&ast)
middleware_error::fmt(&ast)
}
#[proc_macro_derive(ResponderJson)]
@@ -187,23 +157,16 @@ pub fn responser_json_derive(input: TokenStream) -> TokenStream {
responder_json::fmt(&ast)
}
#[proc_macro_derive(IntoResponseError)]
pub fn into_response_error_derive(input: TokenStream) -> TokenStream {
#[proc_macro_derive(OkResponse)]
pub fn ok_response_derive(input: TokenStream) -> TokenStream {
let ast = syn::parse(input).unwrap();
into_response_error::fmt(&ast)
ok_response::fmt(&ast)
}
#[proc_macro_derive(IntoResponseErrorNamed)]
pub fn into_response_error_named_derive(input: TokenStream) -> TokenStream {
#[proc_macro_derive(ErrResponse, attributes(status_code))]
pub fn err_response_derive(input: TokenStream) -> TokenStream {
let ast = syn::parse(input).unwrap();
into_response_error::fmt_named(&ast)
}
#[proc_macro_derive(StatusCode, attributes(status_code))]
pub fn status_code_derive(input: TokenStream) -> TokenStream {
let ast = syn::parse(input).unwrap();
status_code::fmt(&ast)
err_response::fmt(&ast)
}

1520
actix-test/Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@@ -4,5 +4,5 @@ version = "0.1.0"
edition = "2024"
[dependencies]
actix-http = "3.10.0"
actix-web = "4.10.2"
actix-http = "3"
actix-web = "4"

View File

@@ -1,12 +0,0 @@
use criterion::{Criterion, criterion_group, criterion_main};
use schedule_parser_rusted::parser::parse_xls;
pub fn bench_parse_xls(c: &mut Criterion) {
let buffer: Vec<u8> = include_bytes!("../schedule.xls").to_vec();
c.bench_function("parse_xls", |b| b.iter(|| parse_xls(&buffer).unwrap()));
}
criterion_group!(benches, bench_parse_xls);
criterion_main!(benches);

View File

@@ -0,0 +1,2 @@
ALTER TABLE users DROP CONSTRAINT users_telegram_id_key;
ALTER TABLE users DROP COLUMN telegram_id;

View File

@@ -0,0 +1,2 @@
ALTER TABLE users ADD telegram_id int8 NULL;
ALTER TABLE users ADD CONSTRAINT users_telegram_id_key UNIQUE (telegram_id);

View File

@@ -0,0 +1,2 @@
UPDATE users SET "password" = '' WHERE "password" IS NULL;
ALTER TABLE users ALTER COLUMN "password" SET NOT NULL;

View File

@@ -0,0 +1 @@
ALTER TABLE users ALTER COLUMN "password" DROP NOT NULL;

View File

@@ -0,0 +1,3 @@
UPDATE users SET "android_version" = '' WHERE "android_version" IS NULL;
ALTER TABLE users ALTER COLUMN "android_version" SET NOT NULL;
ALTER TABLE users RENAME COLUMN android_version TO "version";

View File

@@ -0,0 +1,2 @@
ALTER TABLE users RENAME COLUMN "version" TO android_version;
ALTER TABLE users ALTER COLUMN android_version DROP NOT NULL;

View File

@@ -0,0 +1,2 @@
UPDATE users SET "group" = '' WHERE "group" IS NULL;
ALTER TABLE users ALTER COLUMN "group" SET NOT NULL;

View File

@@ -0,0 +1 @@
ALTER TABLE users ALTER COLUMN "group" DROP NOT NULL;

View File

@@ -0,0 +1,2 @@
UPDATE users SET "access_token" = '' WHERE "access_token" IS NULL;
ALTER TABLE users ALTER COLUMN "access_token" SET NOT NULL;

View File

@@ -0,0 +1 @@
ALTER TABLE users ALTER COLUMN "access_token" DROP NOT NULL;

12
providers/Cargo.toml Normal file
View File

@@ -0,0 +1,12 @@
[package]
name = "providers"
version = "0.1.0"
edition = "2024"
[features]
test = ["provider-engels-polytechnic/test"]
[dependencies]
base = { path = "base" }
provider-engels-polytechnic = { path = "provider-engels-polytechnic" }

17
providers/base/Cargo.toml Normal file
View File

@@ -0,0 +1,17 @@
[package]
name = "base"
version = "0.1.0"
edition = "2024"
[dependencies]
tokio-util = "0.7.16"
async-trait = "0.1.89"
chrono = { version = "0.4.41", features = ["serde"] }
serde = { version = "1.0.219", features = ["derive"] }
serde_repr = "0.1.20"
utoipa = { version = "5.4.0", features = ["macros", "chrono"] }
sha1 = "0.11.0-rc.0"

View File

@@ -1,4 +1,6 @@
use sha1::Digest;
use sha1::digest::OutputSizeUser;
use sha1::digest::typenum::Unsigned;
use std::hash::Hasher;
/// Hesher returning hash from the algorithm implementing Digest
@@ -12,7 +14,20 @@ where
{
/// Obtain hash.
pub fn finalize(self) -> String {
hex::encode(self.digest.finalize().0)
static ALPHABET: [char; 16] = [
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F',
];
let mut hex = String::with_capacity(<D as OutputSizeUser>::OutputSize::USIZE * 2);
for byte in self.digest.finalize().0.into_iter() {
let byte: u8 = byte;
hex.push(ALPHABET[(byte >> 4) as usize]);
hex.push(ALPHABET[(byte & 0xF) as usize]);
}
hex
}
}

289
providers/base/src/lib.rs Normal file
View File

@@ -0,0 +1,289 @@
use crate::hasher::DigestHasher;
use async_trait::async_trait;
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use serde_repr::{Deserialize_repr, Serialize_repr};
use sha1::{Digest, Sha1};
use std::collections::HashMap;
use std::hash::Hash;
use std::sync::Arc;
use tokio_util::sync::CancellationToken;
use utoipa::ToSchema;
mod hasher;
// pub(crate) mod internal {
// use super::{LessonBoundaries, LessonType};
// use chrono::{DateTime, Utc};
//
// /// Data cell storing the group name.
// pub struct GroupCellInfo {
// /// Column index.
// pub column: u32,
//
// /// Text in the cell.
// pub name: String,
// }
//
// /// Data cell storing the line.
// pub struct DayCellInfo {
// /// Line index.
// pub row: u32,
//
// /// Column index.
// pub column: u32,
//
// /// Day name.
// pub name: String,
//
// /// Date of the day.
// pub date: DateTime<Utc>,
// }
//
// /// Data on the time of lessons from the second column of the schedule.
// pub struct BoundariesCellInfo {
// /// Temporary segment of the lesson.
// pub time_range: LessonBoundaries,
//
// /// Type of lesson.
// pub lesson_type: LessonType,
//
// /// The lesson index.
// pub default_index: Option<u32>,
//
// /// The frame of the cell.
// pub xls_range: ((u32, u32), (u32, u32)),
// }
// }
/// The beginning and end of the lesson.
#[derive(Clone, Hash, Debug, Serialize, Deserialize, ToSchema)]
pub struct LessonBoundaries {
/// The beginning of a lesson.
pub start: DateTime<Utc>,
/// The end of the lesson.
pub end: DateTime<Utc>,
}
/// Type of lesson.
#[derive(Clone, Hash, PartialEq, Debug, Serialize_repr, Deserialize_repr, ToSchema)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
#[repr(u8)]
pub enum LessonType {
/// Обычная.
Default = 0,
/// Допы.
Additional,
/// Перемена.
Break,
/// Консультация.
Consultation,
/// Самостоятельная работа.
IndependentWork,
/// Зачёт.
Exam,
/// Зачёт с оценкой.
ExamWithGrade,
/// Экзамен.
ExamDefault,
/// Курсовой проект.
CourseProject,
/// Защита курсового проекта.
CourseProjectDefense,
}
#[derive(Clone, Hash, Debug, Serialize, Deserialize, ToSchema)]
pub struct LessonSubGroup {
/// Cabinet, if present.
pub cabinet: Option<String>,
/// Full name of the teacher.
pub teacher: Option<String>,
}
#[derive(Clone, Hash, Debug, Serialize, Deserialize, ToSchema)]
#[serde(rename_all = "camelCase")]
pub struct Lesson {
/// Type.
#[serde(rename = "type")]
pub lesson_type: LessonType,
/// Lesson indexes, if present.
pub range: Option<[u8; 2]>,
/// Name.
pub name: Option<String>,
/// The beginning and end.
pub time: LessonBoundaries,
/// List of subgroups.
#[serde(rename = "subgroups")]
pub subgroups: Option<Vec<Option<LessonSubGroup>>>,
/// Group name, if this is a schedule for teachers.
pub group: Option<String>,
}
#[derive(Clone, Hash, Debug, Serialize, Deserialize, ToSchema)]
pub struct Day {
/// Day of the week.
pub name: String,
/// Address of another corps.
pub street: Option<String>,
/// Date.
pub date: DateTime<Utc>,
/// List of lessons on this day.
pub lessons: Vec<Lesson>,
}
#[derive(Clone, Hash, Debug, Serialize, Deserialize, ToSchema)]
pub struct ScheduleEntry {
/// The name of the group or name of the teacher.
pub name: String,
/// List of six days.
pub days: Vec<Day>,
}
#[derive(Clone)]
pub struct ParsedSchedule {
/// List of groups.
pub groups: HashMap<String, ScheduleEntry>,
/// List of teachers.
pub teachers: HashMap<String, ScheduleEntry>,
}
/// Represents a snapshot of the schedule parsed from an XLS file.
#[derive(Clone)]
pub struct ScheduleSnapshot {
/// Timestamp when the Polytechnic website was queried for the schedule.
pub fetched_at: DateTime<Utc>,
/// Timestamp indicating when the schedule was last updated on the Polytechnic website.
///
/// <note>
/// This value is determined by the website's content and does not depend on the application.
/// </note>
pub updated_at: DateTime<Utc>,
/// URL pointing to the XLS file containing the source schedule data.
pub url: String,
/// Parsed schedule data in the application's internal representation.
pub data: ParsedSchedule,
}
impl ScheduleSnapshot {
/// Converting the schedule data into a hash.
/// ### Important!
/// The hash does not depend on the dates.
/// If the application is restarted, but the file with source schedule will remain unchanged, then the hash will not change.
pub fn hash(&self) -> String {
let mut hasher = DigestHasher::from(Sha1::new());
self.data.teachers.iter().for_each(|e| e.hash(&mut hasher));
self.data.groups.iter().for_each(|e| e.hash(&mut hasher));
hasher.finalize()
}
/// Simply updates the value of [`ScheduleSnapshot::fetched_at`].
/// Used for auto-updates.
pub fn update(&mut self) {
self.fetched_at = Utc::now();
}
}
// #[derive(Clone, Debug, Display, Error, ToSchema)]
// #[display("row {row}, column {column}")]
// pub struct ErrorCellPos {
// pub row: u32,
// pub column: u32,
// }
//
// #[derive(Clone, Debug, Display, Error, ToSchema)]
// #[display("'{data}' at {pos}")]
// pub struct ErrorCell {
// pub pos: ErrorCellPos,
// pub data: String,
// }
//
// impl ErrorCell {
// pub fn new(row: u32, column: u32, data: String) -> Self {
// Self {
// pos: ErrorCellPos { row, column },
// data,
// }
// }
// }
// #[derive(Clone, Debug, Display, Error, ToSchema)]
// pub enum ParseError {
// /// Errors related to reading XLS file.
// #[display("{_0:?}: Failed to read XLS file.")]
// #[schema(value_type = String)]
// BadXLS(Arc<calamine::XlsError>),
//
// /// Not a single sheet was found.
// #[display("No work sheets found.")]
// NoWorkSheets,
//
// /// There are no data on the boundaries of the sheet.
// #[display("There is no data on work sheet boundaries.")]
// UnknownWorkSheetRange,
//
// /// Failed to read the beginning and end of the lesson from the cell
// #[display("Failed to read lesson start and end from {_0}.")]
// LessonBoundaries(ErrorCell),
//
// /// Not found the beginning and the end corresponding to the lesson.
// #[display("No start and end times matching the lesson (at {_0}) was found.")]
// LessonTimeNotFound(ErrorCellPos),
// }
//
// impl Serialize for ParseError {
// fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
// where
// S: Serializer,
// {
// match self {
// ParseError::BadXLS(_) => serializer.serialize_str("BAD_XLS"),
// ParseError::NoWorkSheets => serializer.serialize_str("NO_WORK_SHEETS"),
// ParseError::UnknownWorkSheetRange => {
// serializer.serialize_str("UNKNOWN_WORK_SHEET_RANGE")
// }
// ParseError::LessonBoundaries(_) => serializer.serialize_str("GLOBAL_TIME"),
// ParseError::LessonTimeNotFound(_) => serializer.serialize_str("LESSON_TIME_NOT_FOUND"),
// }
// }
// }
#[async_trait]
pub trait ScheduleProvider
where
Self: Sync + Send,
{
/// Returns ok when task has been canceled.
/// Returns err when error appeared while trying to parse or download schedule
async fn start_auto_update_task(
&self,
cancellation_token: CancellationToken,
) -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>>;
async fn get_schedule(&self) -> Arc<ScheduleSnapshot>;
}

View File

@@ -0,0 +1,32 @@
[package]
name = "provider-engels-polytechnic"
version = "0.1.0"
edition = "2024"
[features]
test = []
[dependencies]
base = { path = "../base" }
tokio = { version = "1.47.1", features = ["sync", "macros", "time"] }
tokio-util = "0.7.16"
chrono = { version = "0.4.41", features = ["serde"] }
serde = { version = "1.0.219", features = ["derive"] }
derive_more = { version = "2.0.1", features = ["error", "display"] }
utoipa = { version = "5.4.0", features = ["macros", "chrono"] }
calamine = "0.30.0"
async-trait = "0.1.89"
reqwest = "0.12.23"
ua_generator = "0.5.22"
regex = "1.11.1"
strsim = "0.11.1"
log = "0.4.27"
sentry = "0.42.0"

View File

@@ -0,0 +1,85 @@
use crate::updater::Updater;
use async_trait::async_trait;
use base::{ScheduleProvider, ScheduleSnapshot};
use std::ops::DerefMut;
use std::sync::Arc;
use std::time::Duration;
use tokio::sync::RwLock;
use tokio::time::interval;
use tokio_util::sync::CancellationToken;
pub use crate::updater::UpdateSource;
mod parser;
mod updater;
mod xls_downloader;
#[cfg(feature = "test")]
pub mod test_utils {
pub use crate::parser::test_utils::test_result;
}
pub struct EngelsPolytechnicProvider {
updater: Updater,
snapshot: Arc<ScheduleSnapshot>,
}
impl EngelsPolytechnicProvider {
pub async fn new(
update_source: UpdateSource,
) -> Result<Arc<dyn ScheduleProvider>, crate::updater::error::Error> {
let (updater, snapshot) = Updater::new(update_source).await?;
Ok(Arc::new(Wrapper {
inner: RwLock::new(Self {
updater,
snapshot: Arc::new(snapshot),
}),
}))
}
}
pub struct Wrapper {
inner: RwLock<EngelsPolytechnicProvider>,
}
#[async_trait]
impl ScheduleProvider for Wrapper {
async fn start_auto_update_task(
&self,
cancellation_token: CancellationToken,
) -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> {
let mut ticker = interval(Duration::from_secs(60 * 30));
ticker.tick().await; // bc we already have the latest schedule, when instantiating provider
loop {
tokio::select! {
_ = ticker.tick() => {
let mut lock = self.inner.write().await;
let this= lock.deref_mut();
log::info!("Updating schedule...");
match this.updater.update(&mut this.snapshot).await {
Ok(snapshot) => {
this.snapshot = Arc::new(snapshot);
},
Err(err) => {
cancellation_token.cancel();
return Err(err.into());
}
}
}
_ = cancellation_token.cancelled() => {
return Ok(());
}
}
}
}
async fn get_schedule(&self) -> Arc<ScheduleSnapshot> {
self.inner.read().await.snapshot.clone()
}
}

View File

@@ -0,0 +1,21 @@
#[macro_export]
macro_rules! or_continue {
( $e:expr ) => {{
if let Some(x) = $e {
x
} else {
continue;
}
}};
}
#[macro_export]
macro_rules! or_break {
( $e:expr ) => {{
if let Some(x) = $e {
x
} else {
break;
}
}};
}

View File

@@ -0,0 +1,912 @@
use crate::or_continue;
use crate::parser::error::{ErrorCell, ErrorCellPos};
use crate::parser::worksheet::WorkSheet;
use crate::parser::LessonParseResult::{Lessons, Street};
use base::LessonType::Break;
use base::{
Day, Lesson, LessonBoundaries, LessonSubGroup, LessonType, ParsedSchedule, ScheduleEntry,
};
use calamine::{open_workbook_from_rs, Reader, Xls};
use chrono::{DateTime, Duration, NaiveDate, NaiveTime, Utc};
use regex::Regex;
use std::collections::HashMap;
use std::io::Cursor;
use std::sync::LazyLock;
mod macros;
mod worksheet;
pub mod error {
use derive_more::{Display, Error};
use serde::{Serialize, Serializer};
use std::sync::Arc;
use utoipa::ToSchema;
#[derive(Clone, Debug, Display, Error, ToSchema)]
#[display("row {row}, column {column}")]
pub struct ErrorCellPos {
pub row: u32,
pub column: u32,
}
#[derive(Clone, Debug, Display, Error, ToSchema)]
#[display("'{data}' at {pos}")]
pub struct ErrorCell {
pub pos: ErrorCellPos,
pub data: String,
}
impl ErrorCell {
pub fn new(row: u32, column: u32, data: String) -> Self {
Self {
pos: ErrorCellPos { row, column },
data,
}
}
}
#[derive(Clone, Debug, Display, Error, ToSchema)]
pub enum Error {
/// Errors related to reading XLS file.
#[display("{_0:?}: Failed to read XLS file.")]
#[schema(value_type = String)]
BadXLS(Arc<calamine::XlsError>),
/// Not a single sheet was found.
#[display("No work sheets found.")]
NoWorkSheets,
/// There are no data on the boundaries of the sheet.
#[display("There is no data on work sheet boundaries.")]
UnknownWorkSheetRange,
/// Failed to read the beginning and end of the lesson from the cell
#[display("Failed to read lesson start and end from {_0}.")]
LessonBoundaries(ErrorCell),
/// Not found the beginning and the end corresponding to the lesson.
#[display("No start and end times matching the lesson (at {_0}) was found.")]
LessonTimeNotFound(ErrorCellPos),
}
impl Serialize for Error {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
match self {
Error::BadXLS(_) => serializer.serialize_str("BAD_XLS"),
Error::NoWorkSheets => serializer.serialize_str("NO_WORK_SHEETS"),
Error::UnknownWorkSheetRange => {
serializer.serialize_str("UNKNOWN_WORK_SHEET_RANGE")
}
Error::LessonBoundaries(_) => serializer.serialize_str("GLOBAL_TIME"),
Error::LessonTimeNotFound(_) => serializer.serialize_str("LESSON_TIME_NOT_FOUND"),
}
}
}
}
/// Data cell storing the group name.
pub struct GroupCellInfo {
/// Column index.
pub column: u32,
/// Text in the cell.
pub name: String,
}
/// Data cell storing the line.
pub struct DayCellInfo {
/// Line index.
pub row: u32,
/// Column index.
pub column: u32,
/// Day name.
pub name: String,
/// Date of the day.
pub date: DateTime<Utc>,
}
/// Data on the time of lessons from the second column of the schedule.
pub struct BoundariesCellInfo {
/// Temporary segment of the lesson.
pub time_range: LessonBoundaries,
/// Type of lesson.
pub lesson_type: LessonType,
/// The lesson index.
pub default_index: Option<u32>,
/// The frame of the cell.
pub xls_range: ((u32, u32), (u32, u32)),
}
/// Obtaining a "skeleton" schedule from the working sheet.
fn parse_skeleton(
worksheet: &WorkSheet,
) -> Result<(Vec<DayCellInfo>, Vec<GroupCellInfo>), crate::parser::error::Error> {
let mut groups: Vec<GroupCellInfo> = Vec::new();
let mut days: Vec<(u32, String, Option<DateTime<Utc>>)> = Vec::new();
let worksheet_start = worksheet
.start()
.ok_or(error::Error::UnknownWorkSheetRange)?;
let worksheet_end = worksheet.end().ok_or(error::Error::UnknownWorkSheetRange)?;
let mut row = worksheet_start.0;
while row < worksheet_end.0 {
row += 1;
let day_full_name = or_continue!(worksheet.get_string_from_cell(row, 0));
// parse groups row when days column will found
if groups.is_empty() {
// переход на предыдущую строку
row -= 1;
for column in (worksheet_start.1 + 2)..=worksheet_end.1 {
groups.push(GroupCellInfo {
column,
name: or_continue!(worksheet.get_string_from_cell(row, column))
.replace(" ", ""),
});
}
// возврат на текущую строку
row += 1;
}
let (day_name, day_date) = {
let space_index = match day_full_name.find(' ') {
Some(index) => {
if index < 10 {
break;
} else {
index
}
}
None => break,
};
let name = day_full_name[..space_index].to_string();
let date_slice = &day_full_name[space_index + 1..];
let date = NaiveDate::parse_from_str(date_slice, "%d.%m.%Y")
.map(|date| date.and_time(NaiveTime::default()).and_utc())
.ok();
(name, date)
};
days.push((row, day_name, day_date));
}
// fix unparsable day dates
let days_max = days.len().min(5);
for i in 0..days_max {
if days[i].2.is_none() && days[i + 1].2.is_some() {
days[i].2 = Some(days[i + 1].2.unwrap() - Duration::days(1));
}
}
for i in 0..days_max {
let i = days_max - i;
if days[i - 1].2.is_none() && days[i].2.is_some() {
days[i - 1].2 = Some(days[i].2.unwrap() - Duration::days(1));
}
}
let days = days
.into_iter()
.map(|day| DayCellInfo {
row: day.0,
column: 0,
name: day.1,
date: day.2.unwrap(),
})
.collect();
Ok((days, groups))
}
/// The result of obtaining a lesson from the cell.
enum LessonParseResult {
/// List of lessons long from one to two.
///
/// The number of lessons will be equal to one if the couple is the first in the day,
/// otherwise the list from the change template and the lesson itself will be returned.
Lessons(Vec<Lesson>),
/// Street on which the Polytechnic Corps is located.
Street(String),
}
// noinspection GrazieInspection
/// Obtaining a non-standard type of lesson by name.
fn guess_lesson_type(text: &String) -> Option<LessonType> {
static MAP: LazyLock<HashMap<&str, LessonType>> = LazyLock::new(|| {
HashMap::from([
("консультация", LessonType::Consultation),
("самостоятельная работа", LessonType::IndependentWork),
("зачет", LessonType::Exam),
("зачет с оценкой", LessonType::ExamWithGrade),
("экзамен", LessonType::ExamDefault),
("курсовой проект", LessonType::CourseProject),
("защита курсового проекта", LessonType::CourseProjectDefense),
])
});
let name_lower = text.to_lowercase();
match MAP
.iter()
.map(|(text, lesson_type)| (lesson_type, strsim::levenshtein(text, &*name_lower)))
.filter(|x| x.1 <= 4)
.min_by_key(|(_, score)| *score)
{
None => None,
Some(v) => Some(v.0.clone()),
}
}
/// Getting a pair or street from a cell.
fn parse_lesson(
worksheet: &WorkSheet,
day: &Day,
day_boundaries: &Vec<BoundariesCellInfo>,
lesson_boundaries: &BoundariesCellInfo,
group_column: u32,
) -> Result<LessonParseResult, crate::parser::error::Error> {
let row = lesson_boundaries.xls_range.0.0;
let name = {
let cell_data = match worksheet.get_string_from_cell(row, group_column) {
Some(x) => x,
None => return Ok(Lessons(Vec::new())),
};
static OTHER_STREET_RE: LazyLock<Regex> =
LazyLock::new(|| Regex::new(r"^[А-Я][а-я]+[,\s]\d+$").unwrap());
if OTHER_STREET_RE.is_match(&cell_data) {
return Ok(Street(cell_data));
}
cell_data
};
let cell_range = worksheet.get_merge_from_start(row, group_column);
let (default_range, lesson_time) = {
let end_time_arr = day_boundaries
.iter()
.filter(|time| time.xls_range.1.0 == cell_range.1.0)
.collect::<Vec<&BoundariesCellInfo>>();
let end_time = end_time_arr
.first()
.ok_or(error::Error::LessonTimeNotFound(ErrorCellPos {
row,
column: group_column,
}))?;
let range: Option<[u8; 2]> = if lesson_boundaries.default_index != None {
let default = lesson_boundaries.default_index.unwrap() as u8;
Some([default, end_time.default_index.unwrap() as u8])
} else {
None
};
let time = LessonBoundaries {
start: lesson_boundaries.time_range.start,
end: end_time.time_range.end,
};
Ok((range, time))
}?;
let (name, mut subgroups, lesson_type) = parse_name_and_subgroups(&name)?;
{
let cabinets: Vec<String> = parse_cabinets(
worksheet,
(cell_range.0.0, cell_range.1.0),
group_column + 1,
);
let cab_count = cabinets.len();
if cab_count == 1 {
// Назначаем этот кабинет всем подгруппам
let cab = Some(cabinets.get(0).unwrap().clone());
for subgroup in &mut subgroups {
if let Some(subgroup) = subgroup {
subgroup.cabinet = cab.clone()
}
}
} else if cab_count == 2 {
while subgroups.len() < cab_count {
subgroups.push(subgroups.last().unwrap_or(&None).clone());
}
for i in 0..cab_count {
let subgroup = subgroups.get_mut(i).unwrap();
let cabinet = Some(cabinets.get(i).unwrap().clone());
match subgroup {
None => {
let _ = subgroup.insert(LessonSubGroup {
teacher: None,
cabinet,
});
}
Some(subgroup) => {
subgroup.cabinet = cabinet;
}
}
}
}
};
let lesson = Lesson {
lesson_type: lesson_type.unwrap_or(lesson_boundaries.lesson_type.clone()),
range: default_range,
name: Some(name),
time: lesson_time,
subgroups: if subgroups.len() == 2
&& subgroups.get(0).unwrap().is_none()
&& subgroups.get(1).unwrap().is_none()
{
None
} else {
Some(subgroups)
},
group: None,
};
let prev_lesson = if day.lessons.is_empty() {
return Ok(Lessons(Vec::from([lesson])));
} else {
&day.lessons[day.lessons.len() - 1]
};
Ok(Lessons(Vec::from([
Lesson {
lesson_type: Break,
range: None,
name: None,
time: LessonBoundaries {
start: prev_lesson.time.end,
end: lesson.time.start,
},
subgroups: Some(Vec::new()),
group: None,
},
lesson,
])))
}
/// Obtaining a list of cabinets to the right of the lesson cell.
fn parse_cabinets(worksheet: &WorkSheet, row_range: (u32, u32), column: u32) -> Vec<String> {
let mut cabinets: Vec<String> = Vec::new();
for row in row_range.0..row_range.1 {
let raw = or_continue!(worksheet.get_string_from_cell(row, column));
let clean = raw.replace("\n", " ");
let parts: Vec<&str> = clean.split(" ").collect();
parts.iter().take(2).for_each(|part| {
let clean_part = part.to_string().trim().to_string();
cabinets.push(clean_part);
});
break;
}
cabinets
}
//noinspection GrazieInspection
/// Getting the "pure" name of the lesson and list of teachers from the text of the lesson cell.
fn parse_name_and_subgroups(
text: &String,
) -> Result<(String, Vec<Option<LessonSubGroup>>, Option<LessonType>), crate::parser::error::Error>
{
// Части названия пары:
// 1. Само название.
// 2. Список преподавателей и подгрупп.
// 3. "Модификатор" (чаще всего).
//
// Регулярное выражение для получения ФИО преподавателей и номеров подгрупп (aka. второй части).
// (?:[А-Я][а-я]+\s?(?:[А-Я][\s.]*){2}(?:\(\d\s?[а-я]+\))?(?:, )?)+[\s.]*
//
// Подробнее:
// (?:
// [А-Я][а-я]+ - Фамилия.
// \s? - Кто знает, будет ли там пробел.
// (?:[А-Я][\s.]*){2} - Имя и отчество с учётом случайных пробелов и точек.
// (?:
// \( - Открытие подгруппы.
// \s? - Кто знает, будет ли там пробел.
// \d - Номер подгруппы.
// \s? - Кто знает, будет ли там пробел.
// [а-я\s]+ - Слово "подгруппа" с учётов ошибок.
// \) - Закрытие подгруппы.
// )? - Явное указание подгруппы может отсутствовать по понятным причинам.
// (?:, )? - Разделители между отдельными частями.
// )+
// [\s.]* - Забираем с собой всякий мусор, что бы не передать его в третью часть.
static NAMES_REGEX: LazyLock<Regex> = LazyLock::new(|| {
Regex::new(
r"(?:[А-Я][а-я]+\s?(?:[А-Я][\s.]*){2}(?:\(\s*\d\s*[а-я\s]+\))?(?:[\s,]+)?){1,2}+[\s.,]*",
)
.unwrap()
});
// Отчистка
static CLEAN_RE: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"[\s\n\t]+").unwrap());
let text = CLEAN_RE
.replace(&text.replace(&[' ', '\t', '\n'], " "), " ")
.to_string();
let (lesson_name, subgroups, lesson_type) = match NAMES_REGEX.captures(&text) {
Some(captures) => {
let capture = captures.get(0).unwrap();
let subgroups: Vec<Option<LessonSubGroup>> = {
let src = capture.as_str().replace(&[' ', '.'], "");
let mut shared_subgroup = false;
let mut subgroups: [Option<LessonSubGroup>; 2] = [None, None];
for name in src.split(',') {
let open_bracket_index = name.find('(');
let number: u8 = open_bracket_index
.map_or(0, |index| name[(index + 1)..(index + 2)].parse().unwrap());
let teacher_name = {
let name_end = open_bracket_index.unwrap_or_else(|| name.len());
// Я ебал. Как же я долго до этого доходил.
format!(
"{} {}.{}.",
name.get(..name_end - 4).unwrap(),
name.get(name_end - 4..name_end - 2).unwrap(),
name.get(name_end - 2..name_end).unwrap(),
)
};
let lesson = Some(LessonSubGroup {
cabinet: None,
teacher: Some(teacher_name),
});
match number {
0 => {
subgroups[0] = lesson;
subgroups[1] = None;
shared_subgroup = true;
break;
}
num => {
// 1 - 1 = 0 | 2 - 1 = 1 | 3 - 1 = 2 (schedule index to array index)
// 0 % 2 = 0 | 1 % 2 = 1 | 2 % 2 = 0 (clamp)
let normalised = (num - 1) % 2;
subgroups[normalised as usize] = lesson;
}
}
}
if shared_subgroup {
Vec::from([subgroups[0].take()])
} else {
Vec::from(subgroups)
}
};
let name = text[..capture.start()].trim().to_string();
let extra = text[capture.end()..].trim().to_string();
let lesson_type = if extra.len() > 4 {
let result = guess_lesson_type(&extra);
if result.is_none() {
#[cfg(not(debug_assertions))]
sentry::capture_message(
&*format!("Не удалось угадать тип пары '{}'!", extra),
sentry::Level::Warning,
);
#[cfg(debug_assertions)]
log::warn!("Не удалось угадать тип пары '{}'!", extra);
}
result
} else {
None
};
(name, subgroups, lesson_type)
}
None => (text, Vec::new(), None),
};
Ok((lesson_name, subgroups, lesson_type))
}
/// Getting the start and end of a pair from a cell in the first column of a document.
///
/// # Arguments
///
/// * `cell_data`: text in cell.
/// * `date`: date of the current day.
fn parse_lesson_boundaries_cell(
cell_data: &String,
date: DateTime<Utc>,
) -> Option<LessonBoundaries> {
static TIME_RE: LazyLock<Regex> =
LazyLock::new(|| Regex::new(r"(\d+\.\d+)-(\d+\.\d+)").unwrap());
let parse_res = if let Some(captures) = TIME_RE.captures(cell_data) {
captures
} else {
return None;
};
let start_match = parse_res.get(1).unwrap().as_str();
let start_parts: Vec<&str> = start_match.split(".").collect();
let end_match = parse_res.get(2).unwrap().as_str();
let end_parts: Vec<&str> = end_match.split(".").collect();
static GET_TIME: fn(DateTime<Utc>, &Vec<&str>) -> DateTime<Utc> = |date, parts| {
date + Duration::hours(parts[0].parse::<i64>().unwrap() - 4)
+ Duration::minutes(parts[1].parse::<i64>().unwrap())
};
Some(LessonBoundaries {
start: GET_TIME(date.clone(), &start_parts),
end: GET_TIME(date, &end_parts),
})
}
/// Parse the column of the document to obtain a list of day's lesson boundaries.
///
/// # Arguments
///
/// * `worksheet`: document.
/// * `date`: date of the current day.
/// * `row_range`: row boundaries of the current day.
/// * `column`: column with the required data.
fn parse_day_boundaries(
worksheet: &WorkSheet,
date: DateTime<Utc>,
row_range: (u32, u32),
column: u32,
) -> Result<Vec<BoundariesCellInfo>, crate::parser::error::Error> {
let mut day_times: Vec<BoundariesCellInfo> = Vec::new();
for row in row_range.0..row_range.1 {
let time_cell = if let Some(str) = worksheet.get_string_from_cell(row, column) {
str
} else {
continue;
};
let lesson_time = parse_lesson_boundaries_cell(&time_cell, date.clone()).ok_or(
error::Error::LessonBoundaries(ErrorCell::new(row, column, time_cell.clone())),
)?;
// type
let lesson_type = if time_cell.contains("пара") {
LessonType::Default
} else {
LessonType::Additional
};
// lesson index
let default_index = if lesson_type == LessonType::Default {
Some(
time_cell
.chars()
.next()
.unwrap()
.to_string()
.parse::<u32>()
.unwrap(),
)
} else {
None
};
day_times.push(BoundariesCellInfo {
time_range: lesson_time,
lesson_type,
default_index,
xls_range: worksheet.get_merge_from_start(row, column),
});
}
Ok(day_times)
}
/// Parse the column of the document to obtain a list of week's lesson boundaries.
///
/// # Arguments
///
/// * `worksheet`: document.
/// * `week_markup`: markup of the current week.
fn parse_week_boundaries(
worksheet: &WorkSheet,
week_markup: &Vec<DayCellInfo>,
) -> Result<Vec<Vec<BoundariesCellInfo>>, crate::parser::error::Error> {
let mut result: Vec<Vec<BoundariesCellInfo>> = Vec::new();
let worksheet_end_row = worksheet.end().unwrap().0;
let lesson_time_column = week_markup[0].column + 1;
for day_index in 0..week_markup.len() {
let day_markup = &week_markup[day_index];
// Если текущий день не последнему, то индекс строки следующего дня.
// Если текущий день - последний, то индекс последней строки документа.
let end_row = if day_index != week_markup.len() - 1 {
week_markup[day_index + 1].row
} else {
worksheet_end_row
};
let day_boundaries = parse_day_boundaries(
&worksheet,
day_markup.date.clone(),
(day_markup.row, end_row),
lesson_time_column,
)?;
result.push(day_boundaries);
}
Ok(result)
}
/// Conversion of the list of couples of groups in the list of lessons of teachers.
fn convert_groups_to_teachers(
groups: &HashMap<String, ScheduleEntry>,
) -> HashMap<String, ScheduleEntry> {
let mut teachers: HashMap<String, ScheduleEntry> = HashMap::new();
let empty_days: Vec<Day> = groups
.values()
.next()
.unwrap()
.days
.iter()
.map(|day| Day {
name: day.name.clone(),
street: day.street.clone(),
date: day.date.clone(),
lessons: vec![],
})
.collect();
for group in groups.values() {
for (index, day) in group.days.iter().enumerate() {
for group_lesson in &day.lessons {
if group_lesson.lesson_type == Break {
continue;
}
if group_lesson.subgroups.is_none() {
continue;
}
let subgroups = group_lesson.subgroups.as_ref().unwrap();
for subgroup in subgroups {
let teacher = match subgroup {
None => continue,
Some(subgroup) => match &subgroup.teacher {
None => continue,
Some(teacher) => teacher,
},
};
if teacher == "Ошибка в расписании" {
continue;
}
if !teachers.contains_key(teacher) {
teachers.insert(
teacher.clone(),
ScheduleEntry {
name: teacher.clone(),
days: empty_days.to_vec(),
},
);
}
let teacher_day = teachers
.get_mut(teacher)
.unwrap()
.days
.get_mut(index)
.unwrap();
teacher_day.lessons.push({
let mut lesson = group_lesson.clone();
lesson.group = Some(group.name.clone());
lesson
});
}
}
}
}
teachers.iter_mut().for_each(|(_, teacher)| {
teacher.days.iter_mut().for_each(|day| {
day.lessons
.sort_by(|a, b| a.range.as_ref().unwrap()[1].cmp(&b.range.as_ref().unwrap()[1]))
})
});
teachers
}
/// Reading XLS Document from the buffer and converting it into the schedule ready to use.
///
/// # Arguments
///
/// * `buffer`: XLS data containing schedule.
///
/// returns: Result<ParseResult, crate::parser::error::Error>
///
/// # Examples
///
/// ```
/// use schedule_parser::parse_xls;
///
/// let result = parse_xls(&include_bytes!("../../schedule.xls").to_vec());
///
/// assert!(result.is_ok(), "{}", result.err().unwrap());
///
/// assert_ne!(result.as_ref().unwrap().groups.len(), 0);
/// assert_ne!(result.as_ref().unwrap().teachers.len(), 0);
/// ```
pub fn parse_xls(buffer: &Vec<u8>) -> Result<ParsedSchedule, crate::parser::error::Error> {
let cursor = Cursor::new(&buffer);
let mut workbook: Xls<_> =
open_workbook_from_rs(cursor).map_err(|e| error::Error::BadXLS(std::sync::Arc::new(e)))?;
let worksheet = {
let (worksheet_name, worksheet) = workbook
.worksheets()
.first()
.ok_or(error::Error::NoWorkSheets)?
.clone();
let worksheet_merges = workbook
.worksheet_merge_cells(&*worksheet_name)
.ok_or(error::Error::NoWorkSheets)?;
WorkSheet {
data: worksheet,
merges: worksheet_merges,
}
};
let (week_markup, groups_markup) = parse_skeleton(&worksheet)?;
let week_boundaries = parse_week_boundaries(&worksheet, &week_markup)?;
let mut groups: HashMap<String, ScheduleEntry> = HashMap::new();
for group_markup in groups_markup {
let mut group = ScheduleEntry {
name: group_markup.name,
days: Vec::new(),
};
for day_index in 0..(&week_markup).len() {
let day_markup = &week_markup[day_index];
let mut day = Day {
name: day_markup.name.clone(),
street: None,
date: day_markup.date,
lessons: Vec::new(),
};
let day_boundaries = &week_boundaries[day_index];
for lesson_boundaries in day_boundaries {
match &mut parse_lesson(
&worksheet,
&day,
&day_boundaries,
&lesson_boundaries,
group_markup.column,
)? {
Lessons(lesson) => day.lessons.append(lesson),
Street(street) => day.street = Some(street.to_owned()),
}
}
group.days.push(day);
}
groups.insert(group.name.clone(), group);
}
Ok(ParsedSchedule {
teachers: convert_groups_to_teachers(&groups),
groups,
})
}
#[cfg(any(test, feature = "test"))]
pub mod test_utils {
use super::*;
use base::ParsedSchedule;
pub fn test_result() -> Result<ParsedSchedule, crate::parser::error::Error> {
parse_xls(&include_bytes!("../../../../test-data/engels-polytechnic.xls").to_vec())
}
}
#[cfg(test)]
pub mod tests {
#[test]
fn read() {
let result = super::test_utils::test_result();
assert!(result.is_ok(), "{}", result.err().unwrap());
assert_ne!(result.as_ref().unwrap().groups.len(), 0);
assert_ne!(result.as_ref().unwrap().teachers.len(), 0);
}
#[test]
fn test_split_lesson() {
let result = super::test_utils::test_result();
assert!(result.is_ok(), "{}", result.err().unwrap());
let result = result.unwrap();
assert!(result.groups.contains_key("ИС-214/23"));
let group = result.groups.get("ИС-214/23").unwrap();
let thursday = group.days.get(3).unwrap();
assert_eq!(thursday.lessons.len(), 1);
let lesson = &thursday.lessons[0];
assert_eq!(lesson.range.unwrap()[1], 3);
assert!(lesson.subgroups.is_some());
let subgroups = lesson.subgroups.as_ref().unwrap();
assert_eq!(subgroups.len(), 2);
assert_eq!(
subgroups[0].as_ref().unwrap().cabinet,
Some("44".to_string())
);
assert_eq!(
subgroups[1].as_ref().unwrap().cabinet,
Some("43".to_string())
);
}
}

View File

@@ -0,0 +1,58 @@
use regex::Regex;
use std::ops::Deref;
use std::sync::LazyLock;
/// XLS WorkSheet data.
pub struct WorkSheet {
pub data: calamine::Range<calamine::Data>,
pub merges: Vec<calamine::Dimensions>,
}
impl Deref for WorkSheet {
type Target = calamine::Range<calamine::Data>;
fn deref(&self) -> &Self::Target {
&self.data
}
}
impl WorkSheet {
/// Getting a line from the required cell.
pub fn get_string_from_cell(&self, row: u32, col: u32) -> Option<String> {
let cell_data = if let Some(data) = self.get((row as usize, col as usize)) {
data.to_string()
} else {
return None;
};
if cell_data.trim().is_empty() {
return None;
}
static NL_RE: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"[\n\r]+").unwrap());
static SP_RE: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"\s+").unwrap());
let trimmed_data = SP_RE
.replace_all(&NL_RE.replace_all(&cell_data, " "), " ")
.trim()
.to_string();
if trimmed_data.is_empty() {
None
} else {
Some(trimmed_data)
}
}
/// Obtaining the boundaries of the cell along its upper left coordinate.
pub fn get_merge_from_start(&self, row: u32, column: u32) -> ((u32, u32), (u32, u32)) {
match self
.merges
.iter()
.find(|merge| merge.start.0 == row && merge.start.1 == column)
{
Some(merge) => (merge.start, (merge.end.0 + 1, merge.end.1 + 1)),
None => ((row, column), (row + 1, column + 1)),
}
}
}

View File

@@ -0,0 +1,263 @@
use crate::parser::parse_xls;
use crate::updater::error::{Error, QueryUrlError, SnapshotCreationError};
use crate::xls_downloader::{FetchError, XlsDownloader};
use base::ScheduleSnapshot;
pub enum UpdateSource {
Prepared(ScheduleSnapshot),
Url(String),
GrabFromSite {
yandex_api_key: String,
yandex_func_id: String,
},
}
pub struct Updater {
downloader: XlsDownloader,
update_source: UpdateSource,
}
pub mod error {
use crate::xls_downloader::FetchError;
use derive_more::{Display, Error};
#[derive(Debug, Display, Error)]
pub enum Error {
/// An error occurred while querying the Yandex Cloud API for a URL.
///
/// This may result from network failures, invalid API credentials, or issues with the Yandex Cloud Function invocation.
/// See [`QueryUrlError`] for more details about specific causes.
QueryUrlFailed(QueryUrlError),
/// The schedule snapshot creation process failed.
///
/// This can happen due to URL conflicts (same URL already in use), failed network requests,
/// download errors, or invalid XLS file content. See [`SnapshotCreationError`] for details.
SnapshotCreationFailed(SnapshotCreationError),
}
/// Errors that may occur when querying the Yandex Cloud API to retrieve a URL.
#[derive(Debug, Display, Error)]
pub enum QueryUrlError {
/// Occurs when the request to the Yandex Cloud API fails.
///
/// This may be due to network issues, invalid API key, incorrect function ID, or other
/// problems with the Yandex Cloud Function invocation.
#[display("An error occurred during the request to the Yandex Cloud API: {_0}")]
RequestFailed(reqwest::Error),
}
/// Errors that may occur during the creation of a schedule snapshot.
#[derive(Debug, Display, Error)]
pub enum SnapshotCreationError {
/// The URL is the same as the one already being used (no update needed).
#[display("The URL is the same as the one already being used.")]
SameUrl,
/// The URL query for the XLS file failed to execute, either due to network issues or invalid API parameters.
#[display("Failed to fetch URL: {_0}")]
FetchFailed(FetchError),
/// Downloading the XLS file content failed after successfully obtaining the URL.
#[display("Download failed: {_0}")]
DownloadFailed(FetchError),
/// The XLS file could not be parsed into a valid schedule format.
#[display("Schedule data is invalid: {_0}")]
InvalidSchedule(crate::parser::error::Error),
}
}
impl Updater {
/// Constructs a new `ScheduleSnapshot` by downloading and parsing schedule data from the specified URL.
///
/// This method first checks if the provided URL is the same as the one already configured in the downloader.
/// If different, it updates the downloader's URL, fetches the XLS content, parses it, and creates a snapshot.
/// Errors are returned for URL conflicts, network issues, download failures, or invalid data.
///
/// # Arguments
///
/// * `downloader`: A mutable reference to an `XLSDownloader` implementation used to fetch and parse the schedule data.
/// * `url`: The source URL pointing to the XLS file containing schedule data.
///
/// returns: Result<ScheduleSnapshot, SnapshotCreationError>
pub async fn new_snapshot(
downloader: &mut XlsDownloader,
url: String,
) -> Result<ScheduleSnapshot, SnapshotCreationError> {
if downloader.url.as_ref().is_some_and(|_url| _url.eq(&url)) {
return Err(SnapshotCreationError::SameUrl);
}
let head_result = downloader.set_url(&*url).await.map_err(|error| {
if let FetchError::Unknown(error) = &error {
sentry::capture_error(&error);
}
SnapshotCreationError::FetchFailed(error)
})?;
let xls_data = downloader
.fetch(false)
.await
.map_err(|error| {
if let FetchError::Unknown(error) = &error {
sentry::capture_error(&error);
}
SnapshotCreationError::DownloadFailed(error)
})?
.data
.unwrap();
let parse_result = parse_xls(&xls_data).map_err(|error| {
sentry::capture_error(&error);
SnapshotCreationError::InvalidSchedule(error)
})?;
Ok(ScheduleSnapshot {
fetched_at: head_result.requested_at,
updated_at: head_result.uploaded_at,
url,
data: parse_result,
})
}
/// Queries the Yandex Cloud Function (FaaS) to obtain a URL for the schedule file.
///
/// This sends a POST request to the specified Yandex Cloud Function endpoint,
/// using the provided API key for authentication. The returned URI is combined
/// with the "https://politehnikum-eng.ru" base domain to form the complete URL.
///
/// # Arguments
///
/// * `api_key` - Authentication token for Yandex Cloud API
/// * `func_id` - ID of the target Yandex Cloud Function to invoke
///
/// # Returns
///
/// Result containing:
/// - `Ok(String)` - Complete URL constructed from the Function's response
/// - `Err(QueryUrlError)` - If the request or response processing fails
async fn query_url(api_key: &str, func_id: &str) -> Result<String, QueryUrlError> {
let client = reqwest::Client::new();
let uri = client
.post(format!(
"https://functions.yandexcloud.net/{}?integration=raw",
func_id
))
.header("Authorization", format!("Api-Key {}", api_key))
.send()
.await
.map_err(|error| QueryUrlError::RequestFailed(error))?
.text()
.await
.map_err(|error| QueryUrlError::RequestFailed(error))?;
Ok(format!("https://politehnikum-eng.ru{}", uri.trim()))
}
/// Initializes the schedule by fetching the URL from the environment or Yandex Cloud Function (FaaS)
/// and creating a [`ScheduleSnapshot`] with the downloaded data.
///
/// # Arguments
///
/// * `downloader`: Mutable reference to an `XLSDownloader` implementation used to fetch and parse the schedule
/// * `app_env`: Reference to the application environment containing either a predefined URL or Yandex Cloud credentials
///
/// # Returns
///
/// Returns `Ok(())` if the snapshot was successfully initialized, or an `Error` if:
/// - URL query to Yandex Cloud failed ([`QueryUrlError`])
/// - Schedule snapshot creation failed ([`SnapshotCreationError`])
pub async fn new(update_source: UpdateSource) -> Result<(Self, ScheduleSnapshot), Error> {
let mut this = Updater {
downloader: XlsDownloader::new(),
update_source,
};
if let UpdateSource::Prepared(snapshot) = &this.update_source {
let snapshot = snapshot.clone();
return Ok((this, snapshot));
}
let url = match &this.update_source {
UpdateSource::Url(url) => {
log::info!("The default link {} will be used", url);
url.clone()
}
UpdateSource::GrabFromSite {
yandex_api_key,
yandex_func_id,
} => {
log::info!("Obtaining a link using FaaS...");
Self::query_url(yandex_api_key, yandex_func_id)
.await
.map_err(|error| Error::QueryUrlFailed(error))?
}
_ => unreachable!(),
};
log::info!("For the initial setup, a link {} will be used", url);
let snapshot = Self::new_snapshot(&mut this.downloader, url)
.await
.map_err(|error| Error::SnapshotCreationFailed(error))?;
log::info!("Schedule snapshot successfully created!");
Ok((this, snapshot))
}
/// Updates the schedule snapshot by querying the latest URL from FaaS and checking for changes.
/// If the URL hasn't changed, only updates the [`fetched_at`] timestamp. If changed, downloads
/// and parses the new schedule data.
///
/// # Arguments
///
/// * `downloader`: XLS file downloader used to fetch and parse the schedule data
/// * `app_env`: Application environment containing Yandex Cloud configuration and auto-update settings
///
/// returns: `Result<(), Error>` - Returns error if URL query fails or schedule parsing encounters issues
///
/// # Safety
///
/// Use `unsafe` to access the initialized snapshot, guaranteed valid by prior `init()` call
pub async fn update(
&mut self,
current_snapshot: &ScheduleSnapshot,
) -> Result<ScheduleSnapshot, Error> {
if let UpdateSource::Prepared(snapshot) = &self.update_source {
let mut snapshot = snapshot.clone();
snapshot.update();
return Ok(snapshot);
}
let url = match &self.update_source {
UpdateSource::Url(url) => url.clone(),
UpdateSource::GrabFromSite {
yandex_api_key,
yandex_func_id,
} => Self::query_url(yandex_api_key.as_str(), yandex_func_id.as_str())
.await
.map_err(|error| Error::QueryUrlFailed(error))?,
_ => unreachable!(),
};
let snapshot = match Self::new_snapshot(&mut self.downloader, url).await {
Ok(snapshot) => snapshot,
Err(SnapshotCreationError::SameUrl) => {
let mut clone = current_snapshot.clone();
clone.update();
clone
}
Err(error) => return Err(Error::SnapshotCreationFailed(error)),
};
Ok(snapshot)
}
}

View File

@@ -0,0 +1,237 @@
use chrono::{DateTime, Utc};
use derive_more::{Display, Error};
use std::mem::discriminant;
use std::sync::Arc;
use utoipa::ToSchema;
/// XLS data retrieval errors.
#[derive(Clone, Debug, ToSchema, Display, Error)]
pub enum FetchError {
/// File url is not set.
#[display("The link to the timetable was not provided earlier.")]
NoUrlProvided,
/// Unknown error.
#[display("An unknown error occurred while downloading the file.")]
#[schema(value_type = String)]
Unknown(Arc<reqwest::Error>),
/// Server returned a status code different from 200.
#[display("Server returned a status code {status_code}.")]
BadStatusCode { status_code: u16 },
/// The url leads to a file of a different type.
#[display("The link leads to a file of type '{content_type}'.")]
BadContentType { content_type: String },
/// Server doesn't return expected headers.
#[display("Server doesn't return expected header(s) '{expected_header}'.")]
BadHeaders { expected_header: String },
}
impl FetchError {
pub fn unknown(error: Arc<reqwest::Error>) -> Self {
Self::Unknown(error)
}
pub fn bad_status_code(status_code: u16) -> Self {
Self::BadStatusCode { status_code }
}
pub fn bad_content_type(content_type: &str) -> Self {
Self::BadContentType {
content_type: content_type.to_string(),
}
}
pub fn bad_headers(expected_header: &str) -> Self {
Self::BadHeaders {
expected_header: expected_header.to_string(),
}
}
}
impl PartialEq for FetchError {
fn eq(&self, other: &Self) -> bool {
discriminant(self) == discriminant(other)
}
}
/// Result of XLS data retrieval.
#[derive(Debug, PartialEq)]
pub struct FetchOk {
/// File upload date.
pub uploaded_at: DateTime<Utc>,
/// Date data received.
pub requested_at: DateTime<Utc>,
/// File data.
pub data: Option<Vec<u8>>,
}
impl FetchOk {
/// Result without file content.
pub fn head(uploaded_at: DateTime<Utc>) -> Self {
FetchOk {
uploaded_at,
requested_at: Utc::now(),
data: None,
}
}
/// Full result.
pub fn get(uploaded_at: DateTime<Utc>, data: Vec<u8>) -> Self {
FetchOk {
uploaded_at,
requested_at: Utc::now(),
data: Some(data),
}
}
}
pub type FetchResult = Result<FetchOk, FetchError>;
pub struct XlsDownloader {
pub url: Option<String>,
}
impl XlsDownloader {
pub fn new() -> Self {
XlsDownloader { url: None }
}
async fn fetch_specified(url: &str, head: bool) -> FetchResult {
let client = reqwest::Client::new();
let response = if head {
client.head(url)
} else {
client.get(url)
}
.header("User-Agent", ua_generator::ua::spoof_chrome_ua())
.send()
.await
.map_err(|e| FetchError::unknown(Arc::new(e)))?;
if response.status().as_u16() != 200 {
return Err(FetchError::bad_status_code(response.status().as_u16()));
}
let headers = response.headers();
let content_type = headers
.get("Content-Type")
.ok_or(FetchError::bad_headers("Content-Type"))?;
if !headers.contains_key("etag") {
return Err(FetchError::bad_headers("etag"));
}
let last_modified = headers
.get("last-modified")
.ok_or(FetchError::bad_headers("last-modified"))?;
if content_type != "application/vnd.ms-excel" {
return Err(FetchError::bad_content_type(content_type.to_str().unwrap()));
}
let last_modified = DateTime::parse_from_rfc2822(&last_modified.to_str().unwrap())
.unwrap()
.with_timezone(&Utc);
Ok(if head {
FetchOk::head(last_modified)
} else {
FetchOk::get(last_modified, response.bytes().await.unwrap().to_vec())
})
}
pub async fn fetch(&self, head: bool) -> FetchResult {
if self.url.is_none() {
Err(FetchError::NoUrlProvided)
} else {
Self::fetch_specified(&*self.url.as_ref().unwrap(), head).await
}
}
pub async fn set_url(&mut self, url: &str) -> FetchResult {
let result = Self::fetch_specified(url, true).await;
if let Ok(_) = result {
self.url = Some(url.to_string());
}
result
}
}
#[cfg(test)]
mod tests {
use crate::xls_downloader::{FetchError, XlsDownloader};
#[tokio::test]
async fn bad_url() {
let url = "bad_url";
let mut downloader = XlsDownloader::new();
assert!(downloader.set_url(url).await.is_err());
}
#[tokio::test]
async fn bad_status_code() {
let url = "https://www.google.com/not-found";
let mut downloader = XlsDownloader::new();
assert_eq!(
downloader.set_url(url).await,
Err(FetchError::bad_status_code(404))
);
}
#[tokio::test]
async fn bad_headers() {
let url = "https://www.google.com/favicon.ico";
let mut downloader = XlsDownloader::new();
assert_eq!(
downloader.set_url(url).await,
Err(FetchError::BadHeaders {
expected_header: "ETag".to_string(),
})
);
}
#[tokio::test]
async fn bad_content_type() {
let url = "https://s3.aero-storage.ldragol.ru/679e5d1145a6ad00843ad3f1/67ddb59fd46303008396ac96%2Fexample.txt";
let mut downloader = XlsDownloader::new();
assert!(downloader.set_url(url).await.is_err());
}
#[tokio::test]
async fn ok() {
let url = "https://s3.aero-storage.ldragol.ru/679e5d1145a6ad00843ad3f1/67ddb5fad46303008396ac97%2Fschedule.xls";
let mut downloader = XlsDownloader::new();
assert!(downloader.set_url(url).await.is_ok());
}
#[tokio::test]
async fn downloader_ok() {
let url = "https://s3.aero-storage.ldragol.ru/679e5d1145a6ad00843ad3f1/67ddb5fad46303008396ac97%2Fschedule.xls";
let mut downloader = XlsDownloader::new();
assert!(downloader.set_url(url).await.is_ok());
assert!(downloader.fetch(false).await.is_ok());
}
#[tokio::test]
async fn downloader_no_url_provided() {
let downloader = XlsDownloader::new();
let result = downloader.fetch(false).await;
assert_eq!(result, Err(FetchError::NoUrlProvided));
}
}

9
providers/src/lib.rs Normal file
View File

@@ -0,0 +1,9 @@
pub use base;
pub use provider_engels_polytechnic::EngelsPolytechnicProvider;
pub use provider_engels_polytechnic::UpdateSource as EngelsPolytechnicUpdateSource;
#[cfg(feature = "test")]
pub mod test_utils {
pub use provider_engels_polytechnic::test_utils as engels_polytechnic;
}

Binary file not shown.

View File

@@ -1,88 +0,0 @@
use crate::parser::schema::ParseResult;
use crate::utility::hasher::DigestHasher;
use crate::xls_downloader::basic_impl::BasicXlsDownloader;
use actix_web::web;
use chrono::{DateTime, Utc};
use diesel::{Connection, PgConnection};
use firebase_messaging_rs::FCMClient;
use sha1::{Digest, Sha1};
use std::env;
use std::hash::Hash;
use std::sync::Mutex;
#[derive(Clone)]
pub struct Schedule {
pub etag: String,
pub fetched_at: DateTime<Utc>,
pub updated_at: DateTime<Utc>,
pub parsed_at: DateTime<Utc>,
pub data: ParseResult,
}
#[derive(Clone)]
pub struct VkId {
pub client_id: i32,
pub redirect_url: String,
}
impl VkId {
pub fn new() -> Self {
Self {
client_id: env::var("VKID_CLIENT_ID")
.expect("VKID_CLIENT_ID must be set")
.parse()
.expect("VKID_CLIENT_ID must be integer"),
redirect_url: env::var("VKID_REDIRECT_URI").expect("VKID_REDIRECT_URI must be set"),
}
}
}
impl Schedule {
pub fn hash(&self) -> String {
let mut hasher = DigestHasher::from(Sha1::new());
self.etag.hash(&mut hasher);
self.data.teachers.iter().for_each(|e| e.hash(&mut hasher));
self.data.groups.iter().for_each(|e| e.hash(&mut hasher));
hasher.finalize()
}
}
/// Common data provided to endpoints.
pub struct AppState {
pub downloader: Mutex<BasicXlsDownloader>,
pub schedule: Mutex<Option<Schedule>>,
pub database: Mutex<PgConnection>,
pub vk_id: VkId,
pub fcm_client: Option<Mutex<FCMClient>>, // в рантайме не меняется, так что опционален мьютекс, а не данные в нём.
}
impl AppState {
pub async fn new() -> Self {
let database_url = env::var("DATABASE_URL").expect("DATABASE_URL must be set");
Self {
downloader: Mutex::new(BasicXlsDownloader::new()),
schedule: Mutex::new(None),
database: Mutex::new(
PgConnection::establish(&database_url)
.unwrap_or_else(|_| panic!("Error connecting to {}", database_url)),
),
vk_id: VkId::new(),
fcm_client: if env::var("GOOGLE_APPLICATION_CREDENTIALS").is_ok() {
Some(Mutex::new(
FCMClient::new().await.expect("FCM client must be created"),
))
} else {
None
},
}
}
}
/// Create a new object web::Data<AppState>.
pub async fn app_state() -> web::Data<AppState> {
web::Data::new(AppState::new().await)
}

View File

@@ -1,77 +1,82 @@
pub mod users {
use crate::app_state::AppState;
use crate::database::models::User;
use crate::database::schema::users::dsl::users;
use crate::database::schema::users::dsl::*;
use crate::utility::mutex::MutexScope;
use crate::state::AppState;
use actix_web::web;
use diesel::{ExpressionMethods, QueryResult, insert_into};
use diesel::{insert_into, ExpressionMethods, QueryResult};
use diesel::{QueryDsl, RunQueryDsl};
use diesel::{SaveChangesDsl, SelectableHelper};
use std::ops::DerefMut;
pub fn get(state: &web::Data<AppState>, _id: &String) -> QueryResult<User> {
state.database.scope(|conn| {
users
.filter(id.eq(_id))
.select(User::as_select())
.first(conn)
})
pub async fn get(state: &web::Data<AppState>, _id: &String) -> QueryResult<User> {
users
.filter(id.eq(_id))
.select(User::as_select())
.first(state.get_database().await.deref_mut())
}
pub fn get_by_username(state: &web::Data<AppState>, _username: &String) -> QueryResult<User> {
state.database.scope(|conn| {
users
.filter(username.eq(_username))
.select(User::as_select())
.first(conn)
})
pub async fn get_by_username(
state: &web::Data<AppState>,
_username: &String,
) -> QueryResult<User> {
users
.filter(username.eq(_username))
.select(User::as_select())
.first(state.get_database().await.deref_mut())
}
//noinspection RsTraitObligations
pub fn get_by_vk_id(state: &web::Data<AppState>, _vk_id: i32) -> QueryResult<User> {
state.database.scope(|conn| {
users
.filter(vk_id.eq(_vk_id))
.select(User::as_select())
.first(conn)
})
pub async fn get_by_vk_id(state: &web::Data<AppState>, _vk_id: i32) -> QueryResult<User> {
users
.filter(vk_id.eq(_vk_id))
.select(User::as_select())
.first(state.get_database().await.deref_mut())
}
//noinspection RsTraitObligations
pub async fn get_by_telegram_id(
state: &web::Data<AppState>,
_telegram_id: i64,
) -> QueryResult<User> {
users
.filter(telegram_id.eq(_telegram_id))
.select(User::as_select())
.first(state.get_database().await.deref_mut())
}
//noinspection DuplicatedCode
pub fn contains_by_username(state: &web::Data<AppState>, _username: &String) -> bool {
pub async fn contains_by_username(state: &web::Data<AppState>, _username: &String) -> bool {
// и как это нахуй сократить блять примеров нихуя нет, нихуя не работает
// как меня этот раст заебал уже
state.database.scope(|conn| {
match users
.filter(username.eq(_username))
.count()
.get_result::<i64>(conn)
{
Ok(count) => count > 0,
Err(_) => false,
}
})
match users
.filter(username.eq(_username))
.count()
.get_result::<i64>(state.get_database().await.deref_mut())
{
Ok(count) => count > 0,
Err(_) => false,
}
}
//noinspection DuplicatedCode
//noinspection RsTraitObligations
pub fn contains_by_vk_id(state: &web::Data<AppState>, _vk_id: i32) -> bool {
state.database.scope(|conn| {
match users
.filter(vk_id.eq(_vk_id))
.count()
.get_result::<i64>(conn)
{
Ok(count) => count > 0,
Err(_) => false,
}
})
pub async fn contains_by_vk_id(state: &web::Data<AppState>, _vk_id: i32) -> bool {
match users
.filter(vk_id.eq(_vk_id))
.count()
.get_result::<i64>(state.get_database().await.deref_mut())
{
Ok(count) => count > 0,
Err(_) => false,
}
}
pub fn insert(state: &web::Data<AppState>, user: &User) -> QueryResult<usize> {
state
.database
.scope(|conn| insert_into(users).values(user).execute(conn))
pub async fn insert(state: &web::Data<AppState>, user: &User) -> QueryResult<usize> {
insert_into(users)
.values(user)
.execute(state.get_database().await.deref_mut())
}
/// Function declaration [User::save][UserSave::save].
@@ -113,51 +118,31 @@ pub mod users {
/// }
/// }
/// ```
fn save(&self, state: &web::Data<AppState>) -> QueryResult<User>;
async fn save(&self, state: &web::Data<AppState>) -> QueryResult<User>;
}
/// Implementation of [UserSave][UserSave] trait.
impl UserSave for User {
fn save(&self, state: &web::Data<AppState>) -> QueryResult<User> {
state.database.scope(|conn| self.save_changes::<Self>(conn))
async fn save(&self, state: &web::Data<AppState>) -> QueryResult<User> {
self.save_changes::<Self>(state.get_database().await.deref_mut())
}
}
#[cfg(test)]
pub fn delete_by_username(state: &web::Data<AppState>, _username: &String) -> bool {
state.database.scope(|conn| {
match diesel::delete(users.filter(username.eq(_username))).execute(conn) {
Ok(count) => count > 0,
Err(_) => false,
}
})
pub async fn delete_by_username(state: &web::Data<AppState>, _username: &String) -> bool {
match diesel::delete(users.filter(username.eq(_username)))
.execute(state.get_database().await.deref_mut())
{
Ok(count) => count > 0,
Err(_) => false,
}
}
#[cfg(test)]
pub fn insert_or_ignore(state: &web::Data<AppState>, user: &User) -> QueryResult<usize> {
state.database.scope(|conn| {
insert_into(users)
.values(user)
.on_conflict_do_nothing()
.execute(conn)
})
}
}
pub mod fcm {
use crate::app_state::AppState;
use crate::database::models::{FCM, User};
use crate::utility::mutex::MutexScope;
use actix_web::web;
use diesel::QueryDsl;
use diesel::RunQueryDsl;
use diesel::{BelongingToDsl, QueryResult, SelectableHelper};
pub fn from_user(state: &web::Data<AppState>, user: &User) -> QueryResult<FCM> {
state.database.scope(|conn| {
FCM::belonging_to(&user)
.select(FCM::as_select())
.get_result(conn)
})
pub async fn insert_or_ignore(state: &web::Data<AppState>, user: &User) -> QueryResult<usize> {
insert_into(users)
.values(user)
.on_conflict_do_nothing()
.execute(state.get_database().await.deref_mut())
}
}

View File

@@ -1,3 +1,3 @@
pub mod schema;
pub mod models;
pub mod driver;
pub mod models;
pub mod schema;

View File

@@ -38,22 +38,25 @@ pub struct User {
pub username: String,
/// BCrypt password hash.
pub password: String,
pub password: Option<String>,
/// ID of the linked VK account.
pub vk_id: Option<i32>,
/// JWT access token.
pub access_token: String,
pub access_token: Option<String>,
/// Group.
pub group: String,
pub group: Option<String>,
/// Role.
pub role: UserRole,
/// Version of the installed Polytechnic+ application.
pub version: String,
pub android_version: Option<String>,
/// ID of the linked Telegram account.
pub telegram_id: Option<i64>,
}
#[derive(

View File

@@ -21,12 +21,13 @@ diesel::table! {
users (id) {
id -> Text,
username -> Text,
password -> Text,
password -> Nullable<Text>,
vk_id -> Nullable<Int4>,
access_token -> Text,
group -> Text,
access_token -> Nullable<Text>,
group -> Nullable<Text>,
role -> UserRole,
version -> Text,
android_version -> Nullable<Text>,
telegram_id -> Nullable<Int8>,
}
}

View File

@@ -1,24 +1,24 @@
use crate::app_state::AppState;
use crate::database::driver;
use crate::database::models::{FCM, User};
use crate::extractors::base::{FromRequestSync, SyncExtractor};
use crate::database::models::User;
use crate::extractors::base::FromRequestAsync;
use crate::state::AppState;
use crate::utility::jwt;
use actix_macros::ResponseErrorMessage;
use actix_macros::MiddlewareError;
use actix_web::body::BoxBody;
use actix_web::dev::Payload;
use actix_web::http::header;
use actix_web::{FromRequest, HttpRequest, web};
use actix_web::{web, HttpRequest};
use derive_more::Display;
use serde::{Deserialize, Serialize};
use std::fmt::Debug;
#[derive(Clone, Debug, Serialize, Deserialize, Display, ResponseErrorMessage)]
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, Display, MiddlewareError)]
#[status_code = "actix_web::http::StatusCode::UNAUTHORIZED"]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
pub enum Error {
/// There is no Authorization header in the request.
#[display("No Authorization header found")]
NoHeader,
/// There is no Authorization header or cookie in the request.
#[display("No Authorization header or cookie found")]
NoHeaderOrCookieFound,
/// Unknown authorization type other than Bearer.
#[display("Bearer token is required")]
@@ -39,72 +39,59 @@ impl Error {
}
}
fn get_access_token_from_header(req: &HttpRequest) -> Result<String, Error> {
let header_value = req
.headers()
.get(header::AUTHORIZATION)
.ok_or(Error::NoHeaderOrCookieFound)?
.to_str()
.map_err(|_| Error::NoHeaderOrCookieFound)?
.to_string();
let parts = header_value
.split_once(' ')
.ok_or(Error::UnknownAuthorizationType)?;
if parts.0 != "Bearer" {
Err(Error::UnknownAuthorizationType)
} else {
Ok(parts.1.to_string())
}
}
fn get_access_token_from_cookies(req: &HttpRequest) -> Result<String, Error> {
let cookie = req
.cookie("access_token")
.ok_or(Error::NoHeaderOrCookieFound)?;
Ok(cookie.value().to_string())
}
/// User extractor from request with Bearer access token.
impl FromRequestSync for User {
impl FromRequestAsync for User {
type Error = actix_web::Error;
fn from_request_sync(req: &HttpRequest, _: &mut Payload) -> Result<Self, Self::Error> {
let authorization = req
.headers()
.get(header::AUTHORIZATION)
.ok_or(Error::NoHeader.into_err())?
.to_str()
.map_err(|_| Error::NoHeader.into_err())?
.to_string();
async fn from_request_async(
req: &HttpRequest,
_payload: &mut Payload,
) -> Result<Self, Self::Error> {
let access_token = match get_access_token_from_header(req) {
Err(Error::NoHeaderOrCookieFound) => {
get_access_token_from_cookies(req).map_err(|error| error.into_err())?
}
Err(error) => {
return Err(error.into_err());
}
Ok(access_token) => access_token,
};
let parts: Vec<&str> = authorization.split(' ').collect();
if parts.len() != 2 || parts[0] != "Bearer" {
return Err(Error::UnknownAuthorizationType.into_err());
}
let user_id = jwt::verify_and_decode(&parts[1].to_string())
let user_id = jwt::verify_and_decode(&access_token)
.map_err(|_| Error::InvalidAccessToken.into_err())?;
let app_state = req.app_data::<web::Data<AppState>>().unwrap();
driver::users::get(&app_state, &user_id).map_err(|_| Error::NoUser.into())
}
}
pub struct UserExtractor<const FCM: bool> {
user: User,
fcm: Option<FCM>,
}
impl<const FCM: bool> UserExtractor<{ FCM }> {
pub fn user(&self) -> &User {
&self.user
}
pub fn fcm(&self) -> &Option<FCM> {
if !FCM {
panic!("FCM marked as not required, but it has been requested")
}
&self.fcm
}
}
/// Extractor of user and additional parameters from request with Bearer token.
impl<const FCM: bool> FromRequestSync for UserExtractor<{ FCM }> {
type Error = actix_web::Error;
fn from_request_sync(req: &HttpRequest, payload: &mut Payload) -> Result<Self, Self::Error> {
let user = SyncExtractor::<User>::from_request(req, payload)
.into_inner()?
.into_inner();
let app_state = req.app_data::<web::Data<AppState>>().unwrap();
Ok(Self {
fcm: if FCM {
driver::fcm::from_user(&app_state, &user).ok()
} else {
None
},
user,
})
driver::users::get(app_state, &user_id)
.await
.map_err(|_| Error::NoUser.into())
}
}

View File

@@ -57,18 +57,22 @@ pub trait FromRequestAsync: Sized {
/// web::Json(user)
/// }
/// ```
async fn from_request_async(req: HttpRequest, payload: Payload) -> Result<Self, Self::Error>;
async fn from_request_async(
req: &HttpRequest,
payload: &mut Payload,
) -> Result<Self, Self::Error>;
}
impl<T: FromRequestAsync> FromRequest for AsyncExtractor<T> {
type Error = T::Error;
type Future = LocalBoxFuture<'static, Result<Self, Self::Error>>;
fn from_request(req: &HttpRequest, payload: &mut Payload) -> Self::Future {
fn from_request(req: &HttpRequest, _payload: &mut Payload) -> Self::Future {
let req = req.clone();
let payload = payload.take();
let mut payload = Payload::None;
Box::pin(async move {
T::from_request_async(req, payload)
T::from_request_async(&req, &mut payload)
.await
.map(|res| Self(res))
})
@@ -82,6 +86,7 @@ pub struct SyncExtractor<T>(T);
impl<T> SyncExtractor<T> {
/// Retrieving an object extracted with the extractor.
#[allow(unused)]
pub fn into_inner(self) -> T {
self.0
}

View File

@@ -1 +0,0 @@
pub mod parser;

View File

@@ -1,21 +1,19 @@
use crate::app_state::{AppState, app_state};
use crate::middlewares::authorization::JWTAuthorization;
use crate::middlewares::content_type::ContentTypeBootstrap;
use crate::state::{new_app_state, AppState};
use actix_web::dev::{ServiceFactory, ServiceRequest};
use actix_web::{App, Error, HttpServer};
use dotenvy::dotenv;
use log::info;
use std::io;
use utoipa_actix_web::AppExt;
use utoipa_actix_web::scope::Scope;
use utoipa_actix_web::AppExt;
use utoipa_rapidoc::RapiDoc;
mod app_state;
mod state;
mod database;
mod parser;
mod xls_downloader;
mod extractors;
mod middlewares;
mod routes;
@@ -47,17 +45,18 @@ pub fn get_api_scope<
ignore: &["/group-names", "/teacher-names"],
})
.service(routes::schedule::schedule)
.service(routes::schedule::update_download_url)
.service(routes::schedule::cache_status)
.service(routes::schedule::group)
.service(routes::schedule::group_names)
.service(routes::schedule::teacher)
.service(routes::schedule::teacher_names);
let fcm_scope = utoipa_actix_web::scope("/fcm")
.wrap(JWTAuthorization::default())
.service(routes::fcm::update_callback)
.service(routes::fcm::set_token);
let flow_scope = utoipa_actix_web::scope("/flow")
.wrap(JWTAuthorization {
ignore: &["/telegram-auth"],
})
.service(routes::flow::telegram_auth)
.service(routes::flow::telegram_complete);
let vk_id_scope = utoipa_actix_web::scope("/vkid") //
.service(routes::vk_id::oauth);
@@ -66,14 +65,14 @@ pub fn get_api_scope<
.service(auth_scope)
.service(users_scope)
.service(schedule_scope)
.service(fcm_scope)
.service(flow_scope)
.service(vk_id_scope)
}
async fn async_main() -> io::Result<()> {
println!("Starting server...");
info!("Запуск сервера...");
let app_state = app_state().await;
let app_state = new_app_state().await.unwrap();
HttpServer::new(move || {
let (app, api) = App::new()
@@ -112,8 +111,6 @@ fn main() -> io::Result<()> {
},
));
unsafe { std::env::set_var("RUST_BACKTRACE", "1") };
dotenv().unwrap();
env_logger::init();

View File

@@ -1,11 +1,12 @@
use crate::database::models::User;
use crate::extractors::authorized_user;
use crate::extractors::base::FromRequestSync;
use crate::extractors::base::FromRequestAsync;
use actix_web::body::{BoxBody, EitherBody};
use actix_web::dev::{Payload, Service, ServiceRequest, ServiceResponse, Transform, forward_ready};
use actix_web::{Error, HttpRequest, ResponseError};
use futures_util::future::LocalBoxFuture;
use std::future::{Ready, ready};
use std::rc::Rc;
/// Middleware guard working with JWT tokens.
pub struct JWTAuthorization {
@@ -21,7 +22,7 @@ impl Default for JWTAuthorization {
impl<S, B> Transform<S, ServiceRequest> for JWTAuthorization
where
S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = Error>,
S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = Error> + 'static,
S::Future: 'static,
B: 'static,
{
@@ -33,14 +34,14 @@ where
fn new_transform(&self, service: S) -> Self::Future {
ready(Ok(JWTAuthorizationMiddleware {
service,
service: Rc::new(service),
ignore: self.ignore,
}))
}
}
pub struct JWTAuthorizationMiddleware<S> {
service: S,
service: Rc<S>,
/// List of ignored endpoints.
ignore: &'static [&'static str],
}
@@ -52,12 +53,11 @@ where
B: 'static,
{
/// Checking the validity of the token.
fn check_authorization(
&self,
req: &HttpRequest,
payload: &mut Payload,
) -> Result<(), authorized_user::Error> {
User::from_request_sync(req, payload)
async fn check_authorization(req: &HttpRequest) -> Result<(), authorized_user::Error> {
let mut payload = Payload::None;
User::from_request_async(req, &mut payload)
.await
.map(|_| ())
.map_err(|e| e.as_error::<authorized_user::Error>().unwrap().clone())
}
@@ -79,9 +79,9 @@ where
}
}
impl<'a, S, B> Service<ServiceRequest> for JWTAuthorizationMiddleware<S>
impl<S, B> Service<ServiceRequest> for JWTAuthorizationMiddleware<S>
where
S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = Error>,
S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = Error> + 'static,
S::Future: 'static,
B: 'static,
{
@@ -97,20 +97,19 @@ where
return Box::pin(async move { Ok(fut.await?.map_into_left_body()) });
}
let (http_req, mut payload) = req.into_parts();
let service = Rc::clone(&self.service);
if let Err(err) = self.check_authorization(&http_req, &mut payload) {
return Box::pin(async move {
Ok(ServiceResponse::new(
http_req,
Box::pin(async move {
match Self::check_authorization(req.request()).await {
Ok(_) => {
let fut = service.call(req).await?;
Ok(fut.map_into_left_body())
}
Err(err) => Ok(ServiceResponse::new(
req.into_parts().0,
err.error_response().map_into_right_body(),
))
});
}
let req = ServiceRequest::from_parts(http_req, payload);
let fut = self.service.call(req);
Box::pin(async move { Ok(fut.await?.map_into_left_body()) })
)),
}
})
}
}

View File

@@ -1,15 +1,15 @@
use std::fmt::{Write};
use std::fmt::Display;
use serde::{Deserialize, Serialize};
use std::fmt::Display;
use std::fmt::Write;
/// Server response to errors within Middleware.
#[derive(Serialize, Deserialize)]
pub struct ResponseErrorMessage<T: Display> {
pub struct MiddlewareError<T: Display> {
code: T,
message: String,
}
impl<T: Display + Serialize> ResponseErrorMessage<T> {
impl<T: Display + Serialize> MiddlewareError<T> {
pub fn new(code: T) -> Self {
let mut message = String::new();
write!(&mut message, "{}", code).unwrap();

View File

@@ -1,2 +1,4 @@
pub mod error;
pub mod authorization;
pub mod content_type;

View File

@@ -1,743 +0,0 @@
use crate::parser::LessonParseResult::{Lessons, Street};
use crate::parser::schema::LessonType::Break;
use crate::parser::schema::{
Day, ErrorCell, ErrorCellPos, Lesson, LessonSubGroup, LessonTime, LessonType, ParseError,
ParseResult, ScheduleEntry,
};
use calamine::{Reader, Xls, open_workbook_from_rs};
use chrono::{DateTime, Duration, NaiveDateTime, Utc};
use fuzzy_matcher::FuzzyMatcher;
use fuzzy_matcher::skim::SkimMatcherV2;
use regex::Regex;
use std::collections::HashMap;
use std::io::Cursor;
use std::sync::LazyLock;
pub mod schema;
/// Data cell storing the line.
struct InternalId {
/// Line index.
row: u32,
/// Column index.
column: u32,
/// Text in the cell.
name: String,
}
/// Data on the time of lessons from the second column of the schedule.
struct InternalTime {
/// Temporary segment of the lesson.
time_range: LessonTime,
/// Type of lesson.
lesson_type: LessonType,
/// The lesson index.
default_index: Option<u32>,
/// The frame of the cell.
xls_range: ((u32, u32), (u32, u32)),
}
/// Working sheet type alias.
type WorkSheet = calamine::Range<calamine::Data>;
/// Getting a line from the required cell.
fn get_string_from_cell(worksheet: &WorkSheet, row: u32, col: u32) -> Option<String> {
let cell_data = if let Some(data) = worksheet.get((row as usize, col as usize)) {
data.to_string()
} else {
return None;
};
if cell_data.trim().is_empty() {
return None;
}
static NL_RE: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"[\n\r]+").unwrap());
static SP_RE: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"\s+").unwrap());
let trimmed_data = SP_RE
.replace_all(&NL_RE.replace_all(&cell_data, " "), " ")
.trim()
.to_string();
if trimmed_data.is_empty() {
None
} else {
Some(trimmed_data)
}
}
/// Obtaining the boundaries of the cell along its upper left coordinate.
fn get_merge_from_start(worksheet: &WorkSheet, row: u32, column: u32) -> ((u32, u32), (u32, u32)) {
let worksheet_end = worksheet.end().unwrap();
let row_end: u32 = {
let mut r: u32 = 0;
for _r in (row + 1)..worksheet_end.0 {
r = _r;
if let Some(_) = worksheet.get((_r as usize, column as usize)) {
break;
}
}
r
};
let column_end: u32 = {
let mut c: u32 = 0;
for _c in (column + 1)..worksheet_end.1 {
c = _c;
if let Some(_) = worksheet.get((row as usize, _c as usize)) {
break;
}
}
c
};
((row, column), (row_end, column_end))
}
/// Obtaining a "skeleton" schedule from the working sheet.
fn parse_skeleton(worksheet: &WorkSheet) -> Result<(Vec<InternalId>, Vec<InternalId>), ParseError> {
let range = &worksheet;
let mut is_parsed = false;
let mut groups: Vec<InternalId> = Vec::new();
let mut days: Vec<InternalId> = Vec::new();
let start = range.start().ok_or(ParseError::UnknownWorkSheetRange)?;
let end = range.end().ok_or(ParseError::UnknownWorkSheetRange)?;
let mut row = start.0;
while row < end.0 {
row += 1;
let day_name_opt = get_string_from_cell(&worksheet, row, 0);
if day_name_opt.is_none() {
continue;
}
let day_name = day_name_opt.unwrap();
if !is_parsed {
is_parsed = true;
row -= 1;
for column in (start.1 + 2)..=end.1 {
let group_name = get_string_from_cell(&worksheet, row, column);
if group_name.is_none() {
continue;
}
groups.push(InternalId {
row,
column,
name: group_name.unwrap(),
});
}
row += 1;
}
days.push(InternalId {
row,
column: 0,
name: day_name.clone(),
});
if days.len() > 2 && day_name.starts_with("Суббота") {
break;
}
}
Ok((days, groups))
}
/// The result of obtaining a lesson from the cell.
enum LessonParseResult {
/// List of lessons long from one to two.
///
/// The number of lessons will be equal to one if the couple is the first in the day,
/// otherwise the list from the change template and the lesson itself will be returned.
Lessons(Vec<Lesson>),
/// Street on which the Polytechnic Corps is located.
Street(String),
}
trait StringInnerSlice {
/// Obtaining a line from the line on the initial and final index.
fn inner_slice(&self, from: usize, to: usize) -> Self;
}
impl StringInnerSlice for String {
fn inner_slice(&self, from: usize, to: usize) -> Self {
self.chars()
.take(from)
.chain(self.chars().skip(to))
.collect()
}
}
// noinspection GrazieInspection
/// Obtaining a non-standard type of lesson by name.
fn guess_lesson_type(name: &String) -> Option<(String, LessonType)> {
let map: HashMap<String, LessonType> = HashMap::from([
("(консультация)".to_string(), LessonType::Consultation),
(
"самостоятельная работа".to_string(),
LessonType::IndependentWork,
),
("зачет".to_string(), LessonType::Exam),
("зачет с оценкой".to_string(), LessonType::ExamWithGrade),
("экзамен".to_string(), LessonType::ExamDefault),
]);
let matcher = SkimMatcherV2::default();
let name_lower = name.to_lowercase();
type SearchResult<'a> = (&'a LessonType, i64, Vec<usize>);
let mut search_results: Vec<SearchResult> = map
.iter()
.map(|entry| -> SearchResult {
if let Some((score, indices)) = matcher.fuzzy_indices(&*name_lower, entry.0) {
return (entry.1, score, indices);
}
(entry.1, 0, Vec::new())
})
.collect();
search_results.sort_by(|a, b| b.1.cmp(&a.1));
let guessed_type = search_results.first().unwrap();
if guessed_type.1 > 80 {
Some((
name.inner_slice(guessed_type.2[0], guessed_type.2[guessed_type.2.len() - 1]),
guessed_type.0.clone(),
))
} else {
None
}
}
/// Getting a pair or street from a cell.
fn parse_lesson(
worksheet: &WorkSheet,
day: &mut Day,
day_times: &Vec<InternalTime>,
time: &InternalTime,
column: u32,
) -> Result<LessonParseResult, ParseError> {
let row = time.xls_range.0.0;
let (name, lesson_type) = {
let raw_name_opt = get_string_from_cell(&worksheet, row, column);
if raw_name_opt.is_none() {
return Ok(Lessons(Vec::new()));
}
let raw_name = raw_name_opt.unwrap();
static OTHER_STREET_RE: LazyLock<Regex> =
LazyLock::new(|| Regex::new(r"^[А-Я][а-я]+,?\s?[0-9]+$").unwrap());
if OTHER_STREET_RE.is_match(&raw_name) {
return Ok(Street(raw_name));
}
if let Some(guess) = guess_lesson_type(&raw_name) {
guess
} else {
(raw_name, time.lesson_type.clone())
}
};
let (default_range, lesson_time) = || -> Result<(Option<[u8; 2]>, LessonTime), ParseError> {
// check if multi-lesson
let cell_range = get_merge_from_start(worksheet, row, column);
let end_time_arr = day_times
.iter()
.filter(|time| time.xls_range.1.0 == cell_range.1.0)
.collect::<Vec<&InternalTime>>();
let end_time = end_time_arr
.first()
.ok_or(ParseError::LessonTimeNotFound(ErrorCellPos { row, column }))?;
let range: Option<[u8; 2]> = if time.default_index != None {
let default = time.default_index.unwrap() as u8;
Some([default, end_time.default_index.unwrap() as u8])
} else {
None
};
let time = LessonTime {
start: time.time_range.start,
end: end_time.time_range.end,
};
Ok((range, time))
}()?;
let (name, mut subgroups) = parse_name_and_subgroups(&name)?;
{
let cabinets: Vec<String> = parse_cabinets(worksheet, row, column + 1);
// Если количество кабинетов равно 1, назначаем этот кабинет всем подгруппам
if cabinets.len() == 1 {
for subgroup in &mut subgroups {
subgroup.cabinet = Some(cabinets.get(0).or(Some(&String::new())).unwrap().clone())
}
}
// Если количество кабинетов совпадает с количеством подгрупп, назначаем кабинеты по порядку
else if cabinets.len() == subgroups.len() {
for subgroup in &mut subgroups {
subgroup.cabinet = Some(
cabinets
.get((subgroup.number - 1) as usize)
.unwrap()
.clone(),
);
}
}
// Если количество кабинетов больше количества подгрупп, делаем ещё одну подгруппу.
else if cabinets.len() > subgroups.len() {
for index in 0..subgroups.len() {
subgroups[index].cabinet = Some(cabinets[index].clone());
}
while cabinets.len() > subgroups.len() {
subgroups.push(LessonSubGroup {
number: (subgroups.len() + 1) as u8,
cabinet: Some(cabinets[subgroups.len()].clone()),
teacher: "Ошибка в расписании".to_string(),
});
}
}
// Если кабинетов нет, но есть подгруппы, назначаем им значение "??"
else {
for subgroup in &mut subgroups {
subgroup.cabinet = Some("??".to_string());
}
}
cabinets
};
let lesson = Lesson {
lesson_type,
default_range,
name: Some(name),
time: lesson_time,
subgroups: Some(subgroups),
group: None,
};
let prev_lesson = if day.lessons.len() == 0 {
return Ok(Lessons(Vec::from([lesson])));
} else {
&day.lessons[day.lessons.len() - 1]
};
Ok(Lessons(Vec::from([
Lesson {
lesson_type: Break,
default_range: None,
name: None,
time: LessonTime {
start: prev_lesson.time.end,
end: lesson.time.start,
},
subgroups: Some(Vec::new()),
group: None,
},
lesson,
])))
}
/// Obtaining a list of cabinets to the right of the lesson cell.
fn parse_cabinets(worksheet: &WorkSheet, row: u32, column: u32) -> Vec<String> {
let mut cabinets: Vec<String> = Vec::new();
if let Some(raw) = get_string_from_cell(&worksheet, row, column) {
let clean = raw.replace("\n", " ");
let parts: Vec<&str> = clean.split(" ").collect();
for part in parts {
let clean_part = part.to_string().trim().to_string();
cabinets.push(clean_part);
}
}
cabinets
}
/// Getting the "pure" name of the lesson and list of teachers from the text of the lesson cell.
fn parse_name_and_subgroups(name: &String) -> Result<(String, Vec<LessonSubGroup>), ParseError> {
static LESSON_RE: LazyLock<Regex> =
LazyLock::new(|| Regex::new(r"(?:[А-Я][а-я]+[А-Я]{2}(?:\([0-9][а-я]+\))?)+$").unwrap());
static TEACHER_RE: LazyLock<Regex> =
LazyLock::new(|| Regex::new(r"([А-Я][а-я]+)([А-Я])([А-Я])(?:\(([0-9])[а-я]+\))?").unwrap());
static CLEAN_RE: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"[\s.,]+").unwrap());
static END_CLEAN_RE: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"[.\s]+$").unwrap());
let (teachers, lesson_name) = {
let clean_name = CLEAN_RE.replace_all(&name, "").to_string();
if let Some(captures) = LESSON_RE.captures(&clean_name) {
let capture = captures.get(0).unwrap();
let capture_str = capture.as_str().to_string();
let capture_name: String = capture_str.chars().take(5).collect();
(
END_CLEAN_RE.replace(&capture_str, "").to_string(),
END_CLEAN_RE
.replace(&name[0..name.find(&*capture_name).unwrap()], "")
.to_string(),
)
} else {
return Ok((END_CLEAN_RE.replace(&name, "").to_string(), Vec::new()));
}
};
let mut subgroups: Vec<LessonSubGroup> = Vec::new();
let teacher_it = TEACHER_RE.captures_iter(&teachers);
for captures in teacher_it {
subgroups.push(LessonSubGroup {
number: match captures.get(4) {
Some(capture) => capture.as_str().to_string().parse::<u8>().unwrap(),
None => 0,
},
cabinet: None,
teacher: format!(
"{} {}.{}.",
captures.get(1).unwrap().as_str().to_string(),
captures.get(2).unwrap().as_str().to_string(),
captures.get(3).unwrap().as_str().to_string()
),
});
}
// фикс, если у кого-то отсутствует индекс подгруппы
if subgroups.len() == 1 {
let index = subgroups[0].number;
if index == 0 {
subgroups[0].number = 1u8;
} else {
subgroups.push(LessonSubGroup {
number: if index == 1 { 2 } else { 1 },
cabinet: None,
teacher: "Только у другой".to_string(),
});
}
} else if subgroups.len() == 2 {
// если индексы отсутствуют у обоих, ставим поочерёдно
if subgroups[0].number == 0 && subgroups[1].number == 0 {
subgroups[0].number = 1;
subgroups[1].number = 2;
}
// если индекс отсутствует у первого, ставим 2, если у второго индекс 1 и наоборот
else if subgroups[0].number == 0 {
subgroups[0].number = if subgroups[1].number == 1 { 2 } else { 1 };
}
// если индекс отсутствует у второго, ставим 2, если у первого индекс 1 и наоборот
else if subgroups[1].number == 0 {
subgroups[1].number = if subgroups[0].number == 1 { 2 } else { 1 };
}
}
if subgroups.len() == 2 && subgroups[0].number == 2 && subgroups[1].number == 1 {
subgroups.reverse()
}
Ok((lesson_name, subgroups))
}
/// Conversion of the list of couples of groups in the list of lessons of teachers.
fn convert_groups_to_teachers(
groups: &HashMap<String, ScheduleEntry>,
) -> HashMap<String, ScheduleEntry> {
let mut teachers: HashMap<String, ScheduleEntry> = HashMap::new();
let empty_days: Vec<Day> = groups
.values()
.next()
.unwrap()
.days
.iter()
.map(|day| Day {
name: day.name.clone(),
street: day.street.clone(),
date: day.date.clone(),
lessons: vec![],
})
.collect();
for group in groups.values() {
for (index, day) in group.days.iter().enumerate() {
for group_lesson in &day.lessons {
if group_lesson.lesson_type == Break {
continue;
}
if group_lesson.subgroups.is_none() {
continue;
}
let subgroups = group_lesson.subgroups.as_ref().unwrap();
for subgroup in subgroups {
if subgroup.teacher == "Ошибка в расписании" {
continue;
}
if !teachers.contains_key(&subgroup.teacher) {
teachers.insert(
subgroup.teacher.clone(),
ScheduleEntry {
name: subgroup.teacher.clone(),
days: empty_days.to_vec(),
},
);
}
let teacher_day = teachers
.get_mut(&subgroup.teacher)
.unwrap()
.days
.get_mut(index)
.unwrap();
teacher_day.lessons.push({
let mut lesson = group_lesson.clone();
lesson.group = Some(group.name.clone());
lesson
});
}
}
}
}
teachers.iter_mut().for_each(|(_, teacher)| {
teacher.days.iter_mut().for_each(|day| {
day.lessons.sort_by(|a, b| {
a.default_range.as_ref().unwrap()[1].cmp(&b.default_range.as_ref().unwrap()[1])
})
})
});
teachers
}
/// Reading XLS Document from the buffer and converting it into the schedule ready to use.
///
/// # Arguments
///
/// * `buffer`: XLS data containing schedule.
///
/// returns: Result<ParseResult, ParseError>
///
/// # Examples
///
/// ```
/// use schedule_parser_rusted::parser::parse_xls;
///
/// let result = parse_xls(&include_bytes!("../../schedule.xls").to_vec());
///
/// assert!(result.is_ok());
///
/// assert_ne!(result.as_ref().unwrap().groups.len(), 0);
/// assert_ne!(result.as_ref().unwrap().teachers.len(), 0);
/// ```
pub fn parse_xls(buffer: &Vec<u8>) -> Result<ParseResult, ParseError> {
let cursor = Cursor::new(&buffer);
let mut workbook: Xls<_> =
open_workbook_from_rs(cursor).map_err(|e| ParseError::BadXLS(std::sync::Arc::new(e)))?;
let worksheet: WorkSheet = workbook
.worksheets()
.first()
.ok_or(ParseError::NoWorkSheets)?
.1
.to_owned();
let (days_markup, groups_markup) = parse_skeleton(&worksheet)?;
let mut groups: HashMap<String, ScheduleEntry> = HashMap::new();
let mut days_times: Vec<Vec<InternalTime>> = Vec::new();
let saturday_end_row = worksheet.end().unwrap().0;
for group_markup in groups_markup {
let mut group = ScheduleEntry {
name: group_markup.name,
days: Vec::new(),
};
for day_index in 0..(&days_markup).len() {
let day_markup = &days_markup[day_index];
let mut day = {
let space_index = day_markup.name.find(' ').unwrap();
let name = day_markup.name[..space_index].to_string();
let date_raw = day_markup.name[space_index + 1..].to_string();
let date_add = format!("{} 00:00:00", date_raw);
let date = NaiveDateTime::parse_from_str(&*date_add, "%d.%m.%Y %H:%M:%S");
Day {
name,
street: None,
date: date.unwrap().and_utc(),
lessons: Vec::new(),
}
};
let lesson_time_column = days_markup[0].column + 1;
let row_distance = if day_index != days_markup.len() - 1 {
days_markup[day_index + 1].row
} else {
saturday_end_row
} - day_markup.row;
if days_times.len() != 6 {
let mut day_times: Vec<InternalTime> = Vec::new();
for row in day_markup.row..(day_markup.row + row_distance) {
// time
let time_opt = get_string_from_cell(&worksheet, row, lesson_time_column);
if time_opt.is_none() {
continue;
}
let time = time_opt.unwrap();
// type
let lesson_type = if time.contains("пара") {
LessonType::Default
} else {
LessonType::Additional
};
// lesson index
let default_index = if lesson_type == LessonType::Default {
Some(
time.chars()
.next()
.unwrap()
.to_string()
.parse::<u32>()
.unwrap(),
)
} else {
None
};
// time
let time_range = {
static TIME_RE: LazyLock<Regex> =
LazyLock::new(|| Regex::new(r"(\d+\.\d+)-(\d+\.\d+)").unwrap());
let parse_res = TIME_RE.captures(&time).ok_or(ParseError::GlobalTime(
ErrorCell::new(row, lesson_time_column, time.clone()),
))?;
let start_match = parse_res.get(1).unwrap().as_str();
let start_parts: Vec<&str> = start_match.split(".").collect();
let end_match = parse_res.get(2).unwrap().as_str();
let end_parts: Vec<&str> = end_match.split(".").collect();
static GET_TIME: fn(DateTime<Utc>, &Vec<&str>) -> DateTime<Utc> =
|date, parts| {
date + Duration::hours(parts[0].parse::<i64>().unwrap() - 4)
+ Duration::minutes(parts[1].parse::<i64>().unwrap())
};
LessonTime {
start: GET_TIME(day.date.clone(), &start_parts),
end: GET_TIME(day.date.clone(), &end_parts),
}
};
day_times.push(InternalTime {
time_range,
lesson_type,
default_index,
xls_range: get_merge_from_start(&worksheet, row, lesson_time_column),
});
}
days_times.push(day_times);
}
let day_times = &days_times[day_index];
for time in day_times {
match &mut parse_lesson(
&worksheet,
&mut day,
&day_times,
&time,
group_markup.column,
)? {
Lessons(l) => day.lessons.append(l),
Street(s) => day.street = Some(s.to_owned()),
}
}
group.days.push(day);
}
groups.insert(group.name.clone(), group);
}
Ok(ParseResult {
teachers: convert_groups_to_teachers(&groups),
groups,
})
}
#[cfg(test)]
pub mod tests {
use super::*;
pub fn test_result() -> Result<ParseResult, ParseError> {
parse_xls(&include_bytes!("../../schedule.xls").to_vec())
}
#[test]
fn read() {
let result = test_result();
assert!(result.is_ok());
assert_ne!(result.as_ref().unwrap().groups.len(), 0);
assert_ne!(result.as_ref().unwrap().teachers.len(), 0);
}
}

View File

@@ -1,180 +0,0 @@
use chrono::{DateTime, Utc};
use derive_more::{Display, Error};
use serde::{Deserialize, Serialize, Serializer};
use serde_repr::{Deserialize_repr, Serialize_repr};
use std::collections::HashMap;
use std::sync::Arc;
use utoipa::ToSchema;
/// The beginning and end of the lesson.
#[derive(Clone, Hash, Debug, Serialize, Deserialize, ToSchema)]
pub struct LessonTime {
/// The beginning of a lesson.
pub start: DateTime<Utc>,
/// The end of the lesson.
pub end: DateTime<Utc>,
}
/// Type of lesson.
#[derive(Clone, Hash, PartialEq, Debug, Serialize_repr, Deserialize_repr, ToSchema)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
#[repr(u8)]
pub enum LessonType {
/// Обычная.
Default = 0,
/// Допы.
Additional,
/// Перемена.
Break,
/// Консультация.
Consultation,
/// Самостоятельная работа.
IndependentWork,
/// Зачёт.
Exam,
/// Зачёт с оценкой.
ExamWithGrade,
/// Экзамен.
ExamDefault,
}
#[derive(Clone, Hash, Debug, Serialize, Deserialize, ToSchema)]
pub struct LessonSubGroup {
/// Index of subgroup.
pub number: u8,
/// Cabinet, if present.
pub cabinet: Option<String>,
/// Full name of the teacher.
pub teacher: String,
}
#[derive(Clone, Hash, Debug, Serialize, Deserialize, ToSchema)]
#[serde(rename_all = "camelCase")]
pub struct Lesson {
/// Type.
#[serde(rename = "type")]
pub lesson_type: LessonType,
/// Lesson indexes, if present.
pub default_range: Option<[u8; 2]>,
/// Name.
pub name: Option<String>,
/// The beginning and end.
pub time: LessonTime,
/// List of subgroups.
#[serde(rename = "subGroups")]
pub subgroups: Option<Vec<LessonSubGroup>>,
/// Group name, if this is a schedule for teachers.
pub group: Option<String>,
}
#[derive(Clone, Hash, Debug, Serialize, Deserialize, ToSchema)]
pub struct Day {
/// Day of the week.
pub name: String,
/// Address of another corps.
pub street: Option<String>,
/// Date.
pub date: DateTime<Utc>,
/// List of lessons on this day.
pub lessons: Vec<Lesson>,
}
#[derive(Clone, Hash, Debug, Serialize, Deserialize, ToSchema)]
pub struct ScheduleEntry {
/// The name of the group or name of the teacher.
pub name: String,
/// List of six days.
pub days: Vec<Day>,
}
#[derive(Clone)]
pub struct ParseResult {
/// List of groups.
pub groups: HashMap<String, ScheduleEntry>,
/// List of teachers.
pub teachers: HashMap<String, ScheduleEntry>,
}
#[derive(Clone, Debug, Display, Error, ToSchema)]
#[display("row {row}, column {column}")]
pub struct ErrorCellPos {
pub row: u32,
pub column: u32,
}
#[derive(Clone, Debug, Display, Error, ToSchema)]
#[display("'{data}' at {pos}")]
pub struct ErrorCell {
pub pos: ErrorCellPos,
pub data: String,
}
impl ErrorCell {
pub fn new(row: u32, column: u32, data: String) -> Self {
Self {
pos: ErrorCellPos { row, column },
data,
}
}
}
#[derive(Clone, Debug, Display, Error, ToSchema)]
pub enum ParseError {
/// Errors related to reading XLS file.
#[display("{_0:?}: Failed to read XLS file.")]
#[schema(value_type = String)]
BadXLS(Arc<calamine::XlsError>),
/// Not a single sheet was found.
#[display("No work sheets found.")]
NoWorkSheets,
/// There are no data on the boundaries of the sheet.
#[display("There is no data on work sheet boundaries.")]
UnknownWorkSheetRange,
/// Failed to read the beginning and end of the lesson from the line
#[display("Failed to read lesson start and end times from {_0}.")]
GlobalTime(ErrorCell),
/// Not found the beginning and the end corresponding to the lesson.
#[display("No start and end times matching the lesson (at {_0}) was found.")]
LessonTimeNotFound(ErrorCellPos),
}
impl Serialize for ParseError {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
match self {
ParseError::BadXLS(_) => serializer.serialize_str("BAD_XLS"),
ParseError::NoWorkSheets => serializer.serialize_str("NO_WORK_SHEETS"),
ParseError::UnknownWorkSheetRange => {
serializer.serialize_str("UNKNOWN_WORK_SHEET_RANGE")
}
ParseError::GlobalTime(_) => serializer.serialize_str("GLOBAL_TIME"),
ParseError::LessonTimeNotFound(_) => serializer.serialize_str("LESSON_TIME_NOT_FOUND"),
}
}
}

View File

@@ -1,6 +1,6 @@
mod shared;
mod sign_in;
mod sign_up;
mod shared;
pub use sign_in::*;
pub use sign_up::*;

View File

@@ -1,9 +1,6 @@
use crate::utility::jwt::DEFAULT_ALGORITHM;
use jsonwebtoken::errors::ErrorKind;
use jsonwebtoken::{decode, DecodingKey, Validation};
use jsonwebtoken::{Algorithm, DecodingKey, Validation, decode};
use serde::{Deserialize, Serialize};
use std::env;
use std::sync::LazyLock;
#[derive(Deserialize, Serialize)]
struct TokenData {
@@ -17,7 +14,7 @@ struct TokenData {
#[derive(Debug, Serialize, Deserialize)]
struct Claims {
sub: String,
sub: i32,
iis: String,
jti: i32,
app: i32,
@@ -52,17 +49,10 @@ const VK_PUBLIC_KEY: &str = concat!(
"-----END PUBLIC KEY-----"
);
static VK_ID_CLIENT_ID: LazyLock<i32> = LazyLock::new(|| {
env::var("VK_ID_CLIENT_ID")
.expect("VK_ID_CLIENT_ID must be set")
.parse::<i32>()
.expect("VK_ID_CLIENT_ID must be i32")
});
pub fn parse_vk_id(token_str: &String) -> Result<i32, Error> {
pub fn parse_vk_id(token_str: &String, client_id: i32) -> Result<i32, Error> {
let dkey = DecodingKey::from_rsa_pem(VK_PUBLIC_KEY.as_bytes()).unwrap();
match decode::<Claims>(&token_str, &dkey, &Validation::new(DEFAULT_ALGORITHM)) {
match decode::<Claims>(&token_str, &dkey, &Validation::new(Algorithm::RS256)) {
Ok(token_data) => {
let claims = token_data.claims;
@@ -70,13 +60,10 @@ pub fn parse_vk_id(token_str: &String) -> Result<i32, Error> {
Err(Error::UnknownIssuer(claims.iis))
} else if claims.jti != 21 {
Err(Error::UnknownType(claims.jti))
} else if claims.app != *VK_ID_CLIENT_ID {
} else if claims.app != client_id {
Err(Error::UnknownClientId(claims.app))
} else {
match claims.sub.parse::<i32>() {
Ok(sub) => Ok(sub),
Err(_) => Err(Error::InvalidToken),
}
Ok(claims.sub)
}
}
Err(err) => Err(match err.into_kind() {

View File

@@ -1,14 +1,12 @@
use self::schema::*;
use crate::database::driver;
use crate::database::models::User;
use crate::database::driver::users::UserSave;
use crate::routes::auth::shared::parse_vk_id;
use crate::routes::auth::sign_in::schema::SignInData::{Default, Vk};
use crate::routes::auth::sign_in::schema::SignInData::{Default, VkOAuth};
use crate::routes::schema::ResponseError;
use crate::routes::schema::user::UserResponse;
use crate::routes::schema::{IntoResponseAsError, ResponseError};
use crate::utility::mutex::MutexScope;
use crate::{AppState, utility};
use actix_web::{post, web};
use diesel::SaveChangesDsl;
use web::Json;
async fn sign_in_combined(
@@ -16,14 +14,18 @@ async fn sign_in_combined(
app_state: &web::Data<AppState>,
) -> Result<UserResponse, ErrorCode> {
let user = match &data {
Default(data) => driver::users::get_by_username(&app_state, &data.username),
Vk(id) => driver::users::get_by_vk_id(&app_state, *id),
Default(data) => driver::users::get_by_username(&app_state, &data.username).await,
VkOAuth(id) => driver::users::get_by_vk_id(&app_state, *id).await,
};
match user {
Ok(mut user) => {
if let Default(data) = data {
match bcrypt::verify(&data.password, &user.password) {
if user.password.is_none() {
return Err(ErrorCode::IncorrectCredentials);
}
match bcrypt::verify(&data.password, &user.password.as_ref().unwrap()) {
Ok(result) => {
if !result {
return Err(ErrorCode::IncorrectCredentials);
@@ -35,12 +37,9 @@ async fn sign_in_combined(
}
}
user.access_token = utility::jwt::encode(&user.id);
user.access_token = Some(utility::jwt::encode(&user.id));
app_state.database.scope(|conn| {
user.save_changes::<User>(conn)
.expect("Failed to update user")
});
user.save(&app_state).await.expect("Failed to update user");
Ok(user.into())
}
@@ -71,15 +70,17 @@ pub async fn sign_in_vk(
) -> ServiceResponse {
let data = data_json.into_inner();
match parse_vk_id(&data.access_token) {
Ok(id) => sign_in_combined(Vk(id), &app_state).await.into(),
Err(_) => ErrorCode::InvalidVkAccessToken.into_response(),
match parse_vk_id(&data.access_token, app_state.get_env().vk_id.client_id) {
Ok(id) => sign_in_combined(VkOAuth(id), &app_state).await,
Err(_) => Err(ErrorCode::InvalidVkAccessToken),
}
.into()
}
mod schema {
use crate::routes::schema::user::UserResponse;
use actix_macros::{IntoResponseError, StatusCode};
use actix_macros::ErrResponse;
use derive_more::Display;
use serde::{Deserialize, Serialize};
use utoipa::ToSchema;
@@ -109,15 +110,17 @@ mod schema {
pub type ServiceResponse = crate::routes::schema::Response<UserResponse, ErrorCode>;
#[derive(Serialize, ToSchema, Clone, IntoResponseError, StatusCode)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
#[derive(Clone, Serialize, Display, ToSchema, ErrResponse)]
#[schema(as = SignIn::ErrorCode)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
#[status_code = "actix_web::http::StatusCode::NOT_ACCEPTABLE"]
pub enum ErrorCode {
/// Incorrect username or password.
#[display("Incorrect username or password.")]
IncorrectCredentials,
/// Invalid VK ID token.
#[display("Invalid VK ID token.")]
InvalidVkAccessToken,
}
@@ -129,7 +132,7 @@ mod schema {
Default(Request),
/// Identifier of the attached account VK.
Vk(i32),
VkOAuth(i32),
}
}
@@ -184,14 +187,16 @@ mod tests {
&User {
id: id.clone(),
username,
password: bcrypt::hash("example".to_string(), bcrypt::DEFAULT_COST).unwrap(),
password: Some(bcrypt::hash("example".to_string(), bcrypt::DEFAULT_COST).unwrap()),
vk_id: None,
access_token: utility::jwt::encode(&id),
group: "ИС-214/23".to_string(),
telegram_id: None,
access_token: Some(utility::jwt::encode(&id)),
group: Some("ИС-214/23".to_string()),
role: UserRole::Student,
version: "1.0.0".to_string(),
android_version: None,
},
)
.await
.unwrap();
}

View File

@@ -2,11 +2,10 @@ use self::schema::*;
use crate::AppState;
use crate::database::driver;
use crate::database::models::UserRole;
use crate::routes::auth::shared::{Error, parse_vk_id};
use crate::routes::auth::shared::parse_vk_id;
use crate::routes::schema::ResponseError;
use crate::routes::schema::user::UserResponse;
use crate::routes::schema::{IntoResponseAsError, ResponseError};
use actix_web::{post, web};
use rand::{Rng, rng};
use web::Json;
async fn sign_up_combined(
@@ -18,29 +17,31 @@ async fn sign_up_combined(
return Err(ErrorCode::DisallowedRole);
}
// If specified group doesn't exist in schedule.
let schedule_opt = app_state.schedule.lock().unwrap();
if let Some(schedule) = &*schedule_opt {
if !schedule.data.groups.contains_key(&data.group) {
return Err(ErrorCode::InvalidGroupName);
}
if !app_state
.get_schedule_snapshot("eng_polytechnic")
.await
.unwrap()
.data
.groups
.contains_key(&data.group)
{
return Err(ErrorCode::InvalidGroupName);
}
// If user with specified username already exists.
if driver::users::contains_by_username(&app_state, &data.username) {
if driver::users::contains_by_username(&app_state, &data.username).await {
return Err(ErrorCode::UsernameAlreadyExists);
}
// If user with specified VKID already exists.
if let Some(id) = data.vk_id {
if driver::users::contains_by_vk_id(&app_state, id) {
if driver::users::contains_by_vk_id(&app_state, id).await {
return Err(ErrorCode::VkAlreadyExists);
}
}
let user = data.into();
driver::users::insert(&app_state, &user).unwrap();
driver::users::insert(&app_state, &user).await.unwrap();
Ok(UserResponse::from(&user)).into()
}
@@ -56,7 +57,7 @@ pub async fn sign_up(data_json: Json<Request>, app_state: web::Data<AppState>) -
sign_up_combined(
SignUpData {
username: data.username,
password: data.password,
password: Some(data.password),
vk_id: None,
group: data.group,
role: data.role,
@@ -79,40 +80,32 @@ pub async fn sign_up_vk(
) -> ServiceResponse {
let data = data_json.into_inner();
match parse_vk_id(&data.access_token) {
Ok(id) => sign_up_combined(
SignUpData {
username: data.username,
password: rng()
.sample_iter(&rand::distr::Alphanumeric)
.take(16)
.map(char::from)
.collect(),
vk_id: Some(id),
group: data.group,
role: data.role,
version: data.version,
},
&app_state,
)
.await
.into(),
Err(err) => {
if err != Error::Expired {
eprintln!("Failed to parse vk id token!");
eprintln!("{:?}", err);
}
ErrorCode::InvalidVkAccessToken.into_response()
match parse_vk_id(&data.access_token, app_state.get_env().vk_id.client_id) {
Ok(id) => {
sign_up_combined(
SignUpData {
username: data.username,
password: None,
vk_id: Some(id),
group: data.group,
role: data.role,
version: data.version,
},
&app_state,
)
.await
}
Err(_) => Err(ErrorCode::InvalidVkAccessToken),
}
.into()
}
mod schema {
use crate::database::models::{User, UserRole};
use crate::routes::schema::user::UserResponse;
use crate::utility;
use actix_macros::{IntoResponseError, StatusCode};
use actix_macros::ErrResponse;
use derive_more::Display;
use objectid::ObjectId;
use serde::{Deserialize, Serialize};
@@ -170,24 +163,29 @@ mod schema {
pub type ServiceResponse = crate::routes::schema::Response<UserResponse, ErrorCode>;
#[derive(Clone, Serialize, utoipa::ToSchema, IntoResponseError, StatusCode)]
#[derive(Clone, Serialize, Display, utoipa::ToSchema, ErrResponse)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
#[schema(as = SignUp::ErrorCode)]
#[status_code = "actix_web::http::StatusCode::NOT_ACCEPTABLE"]
pub enum ErrorCode {
/// Conveyed the role of Admin.
#[display("Conveyed the role of Admin.")]
DisallowedRole,
/// Unknown name of the group.
#[display("Unknown name of the group.")]
InvalidGroupName,
/// User with this name is already registered.
#[display("User with this name is already registered.")]
UsernameAlreadyExists,
/// Invalid VK ID token.
#[display("Invalid VK ID token.")]
InvalidVkAccessToken,
/// User with such an account VK is already registered.
#[display("User with such an account VK is already registered.")]
VkAlreadyExists,
}
@@ -195,13 +193,14 @@ mod schema {
/// Data for registration.
pub struct SignUpData {
// TODO: сделать ограничение на минимальную и максимальную длину при регистрации и смене.
/// User name.
pub username: String,
/// Password.
///
/// Should be present even if registration occurs using the VK ID token.
pub password: String,
pub password: Option<String>,
/// Account identifier VK.
pub vk_id: Option<i32>,
@@ -218,18 +217,23 @@ mod schema {
impl Into<User> for SignUpData {
fn into(self) -> User {
assert_ne!(self.password.is_some(), self.vk_id.is_some());
let id = ObjectId::new().unwrap().to_string();
let access_token = utility::jwt::encode(&id);
let access_token = Some(utility::jwt::encode(&id));
User {
id,
username: self.username,
password: bcrypt::hash(self.password, bcrypt::DEFAULT_COST).unwrap(),
password: self
.password
.map(|x| bcrypt::hash(x, bcrypt::DEFAULT_COST).unwrap()),
vk_id: self.vk_id,
telegram_id: None,
access_token,
group: self.group,
group: Some(self.group),
role: self.role,
version: self.version,
android_version: Some(self.version),
}
}
}
@@ -248,21 +252,21 @@ mod tests {
use actix_web::http::StatusCode;
use actix_web::test;
struct SignUpPartial {
username: String,
group: String,
struct SignUpPartial<'a> {
username: &'a str,
group: &'a str,
role: UserRole,
}
async fn sign_up_client(data: SignUpPartial) -> ServiceResponse {
async fn sign_up_client(data: SignUpPartial<'_>) -> ServiceResponse {
let app = test_app(test_app_state().await, sign_up).await;
let req = test::TestRequest::with_uri("/sign-up")
.method(Method::POST)
.set_json(Request {
username: data.username.clone(),
username: data.username.to_string(),
password: "example".to_string(),
group: data.group.clone(),
group: data.group.to_string(),
role: data.role.clone(),
version: "1.0.0".to_string(),
})
@@ -278,13 +282,13 @@ mod tests {
test_env();
let app_state = static_app_state().await;
driver::users::delete_by_username(&app_state, &"test::sign_up_valid".to_string());
driver::users::delete_by_username(&app_state, &"test::sign_up_valid".to_string()).await;
// test
let resp = sign_up_client(SignUpPartial {
username: "test::sign_up_valid".to_string(),
group: "ИС-214/23".to_string(),
username: "test::sign_up_valid",
group: "ИС-214/23",
role: UserRole::Student,
})
.await;
@@ -299,11 +303,11 @@ mod tests {
test_env();
let app_state = static_app_state().await;
driver::users::delete_by_username(&app_state, &"test::sign_up_multiple".to_string());
driver::users::delete_by_username(&app_state, &"test::sign_up_multiple".to_string()).await;
let create = sign_up_client(SignUpPartial {
username: "test::sign_up_multiple".to_string(),
group: "ИС-214/23".to_string(),
username: "test::sign_up_multiple",
group: "ИС-214/23",
role: UserRole::Student,
})
.await;
@@ -311,8 +315,8 @@ mod tests {
assert_eq!(create.status(), StatusCode::OK);
let resp = sign_up_client(SignUpPartial {
username: "test::sign_up_multiple".to_string(),
group: "ИС-214/23".to_string(),
username: "test::sign_up_multiple",
group: "ИС-214/23",
role: UserRole::Student,
})
.await;
@@ -326,8 +330,8 @@ mod tests {
// test
let resp = sign_up_client(SignUpPartial {
username: "test::sign_up_invalid_role".to_string(),
group: "ИС-214/23".to_string(),
username: "test::sign_up_invalid_role",
group: "ИС-214/23",
role: UserRole::Admin,
})
.await;
@@ -341,8 +345,8 @@ mod tests {
// test
let resp = sign_up_client(SignUpPartial {
username: "test::sign_up_invalid_group".to_string(),
group: "invalid_group".to_string(),
username: "test::sign_up_invalid_group",
group: "invalid_group",
role: UserRole::Student,
})
.await;

View File

@@ -1,5 +0,0 @@
mod update_callback;
mod set_token;
pub use update_callback::*;
pub use set_token::*;

View File

@@ -1,114 +0,0 @@
use crate::app_state::AppState;
use crate::database;
use crate::database::models::FCM;
use crate::extractors::authorized_user::UserExtractor;
use crate::extractors::base::SyncExtractor;
use crate::utility::mutex::{MutexScope, MutexScopeAsync};
use actix_web::{HttpResponse, Responder, patch, web};
use diesel::{RunQueryDsl, SaveChangesDsl};
use firebase_messaging_rs::FCMClient;
use firebase_messaging_rs::topic::{TopicManagementError, TopicManagementSupport};
use serde::Deserialize;
#[derive(Debug, Deserialize)]
struct Params {
pub token: String,
}
async fn get_fcm(
app_state: &web::Data<AppState>,
user_data: &UserExtractor<true>,
token: String,
) -> Result<FCM, diesel::result::Error> {
match user_data.fcm() {
Some(fcm) => {
let mut fcm = fcm.clone();
fcm.token = token;
Ok(fcm)
}
None => {
let fcm = FCM {
user_id: user_data.user().id.clone(),
token,
topics: vec![],
};
match app_state.database.scope(|conn| {
diesel::insert_into(database::schema::fcm::table)
.values(&fcm)
.execute(conn)
}) {
Ok(_) => Ok(fcm),
Err(e) => Err(e),
}
}
}
}
#[utoipa::path(responses((status = OK)))]
#[patch("/set-token")]
pub async fn set_token(
app_state: web::Data<AppState>,
web::Query(params): web::Query<Params>,
user_data: SyncExtractor<UserExtractor<true>>,
) -> impl Responder {
let user_data = user_data.into_inner();
// If token not changes - exit.
if let Some(fcm) = user_data.fcm() {
if fcm.token == params.token {
return HttpResponse::Ok();
}
}
let fcm = get_fcm(&app_state, &user_data, params.token.clone()).await;
if let Err(e) = fcm {
eprintln!("Failed to get FCM: {e}");
return HttpResponse::Ok();
}
let mut fcm = fcm.ok().unwrap();
// Add default topics.
if !fcm.topics.contains(&Some("common".to_string())) {
fcm.topics.push(Some("common".to_string()));
}
// Subscribe to default topics.
if let Some(e) = app_state
.fcm_client
.as_ref()
.unwrap()
.async_scope(
async |client: &mut FCMClient| -> Result<(), TopicManagementError> {
let mut tokens: Vec<String> = Vec::new();
tokens.push(fcm.token.clone());
for topic in fcm.topics.clone() {
if let Some(topic) = topic {
client.register_tokens_to_topic(topic.clone(), tokens.clone()).await?;
}
}
Ok(())
},
)
.await
.err()
{
eprintln!("Failed to subscribe token to topic: {:?}", e);
return HttpResponse::Ok();
}
// Write updates to db.
if let Some(e) = app_state
.database
.scope(|conn| fcm.save_changes::<FCM>(conn))
.err()
{
eprintln!("Failed to update FCM object: {e}");
}
HttpResponse::Ok()
}

View File

@@ -1,32 +0,0 @@
use crate::app_state::AppState;
use crate::database::models::User;
use crate::extractors::base::SyncExtractor;
use crate::utility::mutex::MutexScope;
use actix_web::{HttpResponse, Responder, post, web};
use diesel::SaveChangesDsl;
#[utoipa::path(responses(
(status = OK),
(status = INTERNAL_SERVER_ERROR)
))]
#[post("/update-callback/{version}")]
async fn update_callback(
app_state: web::Data<AppState>,
version: web::Path<String>,
user: SyncExtractor<User>,
) -> impl Responder {
let mut user = user.into_inner();
user.version = version.into_inner();
match app_state
.database
.scope(|conn| user.save_changes::<User>(conn))
{
Ok(_) => HttpResponse::Ok(),
Err(e) => {
eprintln!("Failed to update user: {}", e);
HttpResponse::InternalServerError()
}
}
}

5
src/routes/flow/mod.rs Normal file
View File

@@ -0,0 +1,5 @@
mod telegram_auth;
mod telegram_complete;
pub use telegram_auth::*;
pub use telegram_complete::*;

View File

@@ -0,0 +1,183 @@
use self::schema::*;
use crate::database::driver;
use crate::database::driver::users::UserSave;
use crate::database::models::{User, UserRole};
use crate::routes::schema::ResponseError;
use crate::utility::telegram::{WebAppInitDataMap, WebAppUser};
use crate::{AppState, utility};
use actix_web::{post, web};
use chrono::{DateTime, Duration, Utc};
use objectid::ObjectId;
use std::sync::Arc;
use web::Json;
#[utoipa::path(responses(
(status = OK, body = Response),
(status = UNAUTHORIZED, body = ResponseError<ErrorCode>),
))]
#[post("/telegram-auth")]
pub async fn telegram_auth(
data_json: Json<Request>,
app_state: web::Data<AppState>,
) -> ServiceResponse {
let init_data = WebAppInitDataMap::from_str(data_json.into_inner().init_data);
// for (key, value) in &init_data.data_map {
// println!("key: {} | value: {}", key, value);
// }
{
let env = &app_state.get_env().telegram;
if let Err(error) = init_data.verify(env.bot_id, env.test_dc) {
return Err(ErrorCode::InvalidInitData(Arc::new(error))).into();
}
}
let auth_date = DateTime::<Utc>::from_timestamp(
init_data
.data_map
.get("auth_date")
.unwrap()
.parse()
.unwrap(),
0,
)
.unwrap();
if Utc::now() - auth_date > Duration::minutes(5) {
return Err(ErrorCode::ExpiredInitData).into();
}
let web_app_user =
serde_json::from_str::<WebAppUser>(init_data.data_map.get("user").unwrap()).unwrap();
let mut user = {
match driver::users::get_by_telegram_id(&app_state, web_app_user.id).await {
Ok(value) => Ok(value),
Err(_) => {
let new_user = User {
id: ObjectId::new().unwrap().to_string(),
username: format!("telegram_{}", web_app_user.id), // можно оставить, а можно поменять
password: None, // ибо нехуй
vk_id: None,
telegram_id: Some(web_app_user.id),
access_token: None, // установится ниже
group: None,
role: UserRole::Student, // TODO: при реге проверять данные
android_version: None,
};
driver::users::insert(&app_state, &new_user)
.await
.map(|_| new_user)
}
}
.expect("Failed to get or add user")
};
user.access_token = Some(utility::jwt::encode(&user.id));
user.save(&app_state).await.expect("Failed to update user");
Ok(Response::new(
&*user.access_token.unwrap(),
user.group.is_some(),
))
.into()
}
mod schema {
use crate::routes::schema::PartialOkResponse;
use crate::state::AppState;
use crate::utility::telegram::VerifyError;
use actix_macros::ErrResponse;
use actix_web::body::EitherBody;
use actix_web::cookie::CookieBuilder;
use actix_web::cookie::time::OffsetDateTime;
use actix_web::{HttpRequest, HttpResponse, web};
use derive_more::Display;
use serde::{Deserialize, Serialize, Serializer};
use std::ops::Add;
use std::sync::Arc;
use utoipa::ToSchema;
#[derive(Debug, Deserialize, Serialize, ToSchema)]
#[serde(rename_all = "camelCase")]
#[schema(as = Flow::TelegramAuth::Request)]
pub struct Request {
/// Telegram WebApp init data.
pub init_data: String,
}
#[derive(Serialize, ToSchema)]
#[serde(rename_all = "camelCase")]
#[schema(as = Flow::TelegramAuth::Response)]
pub struct Response {
#[serde(skip)]
#[schema(ignore)]
access_token: String,
pub completed: bool,
}
impl Response {
pub fn new(access_token: &str, completed: bool) -> Self {
Self {
access_token: access_token.to_string(),
completed,
}
}
}
impl PartialOkResponse for Response {
fn post_process(
&mut self,
request: &HttpRequest,
response: &mut HttpResponse<EitherBody<String>>,
) -> () {
let access_token = &self.access_token;
let app_state = request.app_data::<web::Data<AppState>>().unwrap();
let mini_app_host = &*app_state.get_env().telegram.mini_app_host;
let cookie = CookieBuilder::new("access_token", access_token)
.domain(mini_app_host)
.path("/")
.expires(
OffsetDateTime::now_utc().add(std::time::Duration::from_secs(60 * 60 * 24 * 7)),
)
.http_only(true)
.secure(true)
.finish();
response.add_cookie(&cookie).unwrap();
}
}
pub type ServiceResponse = crate::routes::schema::Response<Response, ErrorCode>;
#[derive(Clone, ToSchema, Display, ErrResponse)]
#[status_code = "actix_web::http::StatusCode::UNAUTHORIZED"]
#[schema(as = Flow::TelegramAuth::ErrorCode)]
pub enum ErrorCode {
#[display("Invalid init data provided: {_0}")]
#[schema(value_type = String)]
InvalidInitData(Arc<VerifyError>),
#[display("Expired init data provided.")]
ExpiredInitData,
}
impl Serialize for ErrorCode {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
match self {
ErrorCode::InvalidInitData(_) => serializer.serialize_str("INVALID_INIT_DATA"),
ErrorCode::ExpiredInitData => serializer.serialize_str("EXPIRED_INIT_DATA"),
}
}
}
}

View File

@@ -0,0 +1,95 @@
use self::schema::*;
use crate::AppState;
use crate::database::driver;
use crate::database::driver::users::UserSave;
use crate::database::models::User;
use crate::extractors::base::AsyncExtractor;
use crate::routes::schema::ResponseError;
use actix_web::{post, web};
use web::Json;
#[utoipa::path(responses(
(status = OK),
(status = CONFLICT, body = ResponseError<ErrorCode>),
(status = INTERNAL_SERVER_ERROR, body = ResponseError<ErrorCode>),
(status = BAD_REQUEST, body = ResponseError<ErrorCode>)
))]
#[post("/telegram-complete")]
pub async fn telegram_complete(
data: Json<Request>,
app_state: web::Data<AppState>,
user: AsyncExtractor<User>,
) -> ServiceResponse {
let mut user = user.into_inner();
// проверка на перезапись уже имеющихся данных
if user.group.is_some() {
return Err(ErrorCode::AlreadyCompleted).into();
}
let data = data.into_inner();
// замена существующего имени, если оно отличается
if user.username != data.username {
if driver::users::contains_by_username(&app_state, &data.username).await {
return Err(ErrorCode::UsernameAlreadyExists).into();
}
user.username = data.username;
}
// проверка на существование группы
if !app_state
.get_schedule_snapshot("eng_polytechnic")
.await
.unwrap()
.data
.groups
.contains_key(&data.group)
{
return Err(ErrorCode::InvalidGroupName).into();
}
user.group = Some(data.group);
user.save(&app_state).await.expect("Failed to update user");
Ok(()).into()
}
mod schema {
use actix_macros::ErrResponse;
use derive_more::Display;
use serde::{Deserialize, Serialize};
use utoipa::ToSchema;
#[derive(Debug, Deserialize, Serialize, ToSchema)]
#[schema(as = Flow::TelegramFill::Request)]
pub struct Request {
/// Username.
pub username: String,
/// Group.
pub group: String,
}
pub type ServiceResponse = crate::routes::schema::Response<(), ErrorCode>;
#[derive(Clone, Serialize, Display, ToSchema, ErrResponse)]
#[status_code = "actix_web::http::StatusCode::UNAUTHORIZED"]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
#[schema(as = Flow::TelegramFill::ErrorCode)]
pub enum ErrorCode {
#[display("This flow already completed.")]
#[status_code = "actix_web::http::StatusCode::CONFLICT"]
AlreadyCompleted,
#[display("Username is already exists.")]
#[status_code = "actix_web::http::StatusCode::BAD_REQUEST"]
UsernameAlreadyExists,
#[display("The required group does not exist.")]
#[status_code = "actix_web::http::StatusCode::BAD_REQUEST"]
InvalidGroupName,
}
}

View File

@@ -1,5 +1,5 @@
pub mod auth;
pub mod fcm;
pub mod flow;
pub mod schedule;
mod schema;
pub mod users;

View File

@@ -7,17 +7,5 @@ use actix_web::{get, web};
))]
#[get("/cache-status")]
pub async fn cache_status(app_state: web::Data<AppState>) -> CacheStatus {
// Prevent thread lock
let has_schedule = app_state
.schedule
.lock()
.as_ref()
.map(|res| res.is_some())
.unwrap();
match has_schedule {
true => CacheStatus::from(&app_state),
false => CacheStatus::default(),
}
.into()
CacheStatus::from(&app_state).await.into()
}

View File

@@ -1,12 +1,13 @@
use self::schema::*;
use crate::AppState;
use crate::database::models::User;
use crate::extractors::base::SyncExtractor;
use crate::routes::schema::{IntoResponseAsError, ResponseError};
use crate::extractors::base::AsyncExtractor;
use crate::routes::schedule::schema::ScheduleEntryResponse;
use crate::routes::schema::ResponseError;
use actix_web::{get, web};
#[utoipa::path(responses(
(status = OK, body = Response),
(status = OK, body = ScheduleEntryResponse),
(
status = SERVICE_UNAVAILABLE,
body = ResponseError<ErrorCode>,
@@ -25,68 +26,43 @@ use actix_web::{get, web};
),
))]
#[get("/group")]
pub async fn group(user: SyncExtractor<User>, app_state: web::Data<AppState>) -> ServiceResponse {
// Prevent thread lock
let schedule_lock = app_state.schedule.lock().unwrap();
pub async fn group(user: AsyncExtractor<User>, app_state: web::Data<AppState>) -> ServiceResponse {
match &user.into_inner().group {
None => Err(ErrorCode::SignUpNotCompleted),
match schedule_lock.as_ref() {
None => ErrorCode::NoSchedule.into_response(),
Some(schedule) => match schedule.data.groups.get(&user.into_inner().group) {
None => ErrorCode::NotFound.into_response(),
Some(entry) => Ok(entry.clone().into()).into(),
Some(group) => match app_state
.get_schedule_snapshot("eng_polytechnic")
.await
.unwrap()
.data
.groups
.get(group)
{
None => Err(ErrorCode::NotFound),
Some(entry) => Ok(entry.clone().into()),
},
}
.into()
}
mod schema {
use crate::parser::schema::ScheduleEntry;
use actix_macros::{IntoResponseErrorNamed, StatusCode};
use chrono::{DateTime, NaiveDateTime, Utc};
use crate::routes::schedule::schema::ScheduleEntryResponse;
use actix_macros::ErrResponse;
use derive_more::Display;
use serde::Serialize;
use utoipa::ToSchema;
pub type ServiceResponse = crate::routes::schema::Response<Response, ErrorCode>;
pub type ServiceResponse = crate::routes::schema::Response<ScheduleEntryResponse, ErrorCode>;
#[derive(Serialize, ToSchema)]
#[schema(as = GetGroup::Response)]
#[serde(rename_all = "camelCase")]
pub struct Response {
/// Group schedule.
pub group: ScheduleEntry,
/// ## Outdated variable.
///
/// By default, an empty list is returned.
#[deprecated = "Will be removed in future versions"]
pub updated: Vec<i32>,
/// ## Outdated variable.
///
/// By default, the initial date for unix.
#[deprecated = "Will be removed in future versions"]
pub updated_at: DateTime<Utc>,
}
#[allow(deprecated)]
impl From<ScheduleEntry> for Response {
fn from(group: ScheduleEntry) -> Self {
Self {
group,
updated: Vec::new(),
updated_at: NaiveDateTime::default().and_utc(),
}
}
}
#[derive(Clone, Serialize, ToSchema, StatusCode, Display, IntoResponseErrorNamed)]
#[derive(Clone, Serialize, Display, ToSchema, ErrResponse)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
#[schema(as = GroupSchedule::ErrorCode)]
pub enum ErrorCode {
/// Schedules have not yet been parsed.
#[status_code = "actix_web::http::StatusCode::SERVICE_UNAVAILABLE"]
#[display("Schedule not parsed yet.")]
NoSchedule,
/// The user tried to access the API without completing singing up.
#[status_code = "actix_web::http::StatusCode::FORBIDDEN"]
#[display("You have not completed signing up.")]
SignUpNotCompleted,
/// Group not found.
#[status_code = "actix_web::http::StatusCode::NOT_FOUND"]

View File

@@ -1,48 +1,35 @@
use self::schema::*;
use crate::AppState;
use crate::routes::schedule::schema::ErrorCode;
use crate::routes::schema::{IntoResponseAsError, ResponseError};
use actix_web::{get, web};
#[utoipa::path(responses(
(status = OK, body = Response),
(status = SERVICE_UNAVAILABLE, body = ResponseError<ErrorCode>),
))]
#[utoipa::path(responses((status = OK, body = Response)))]
#[get("/group-names")]
pub async fn group_names(app_state: web::Data<AppState>) -> ServiceResponse {
// Prevent thread lock
let schedule_lock = app_state.schedule.lock().unwrap();
pub async fn group_names(app_state: web::Data<AppState>) -> Response {
let mut names: Vec<String> = app_state
.get_schedule_snapshot("eng_polytechnic")
.await
.unwrap()
.data
.groups
.keys()
.cloned()
.collect();
match schedule_lock.as_ref() {
None => ErrorCode::NoSchedule.into_response(),
Some(schedule) => {
let mut names: Vec<String> = schedule.data.groups.keys().cloned().collect();
names.sort();
names.sort();
Ok(names.into()).into()
}
}
.into()
Response { names }
}
mod schema {
use crate::routes::schedule::schema::ErrorCode;
use actix_macros::ResponderJson;
use serde::Serialize;
use utoipa::ToSchema;
pub type ServiceResponse = crate::routes::schema::Response<Response, ErrorCode>;
#[derive(Serialize, ToSchema)]
#[derive(Serialize, ToSchema, ResponderJson)]
#[schema(as = GetGroupNames::Response)]
pub struct Response {
/// List of group names sorted in alphabetical order.
#[schema(examples(json!(["ИС-214/23"])))]
pub names: Vec<String>,
}
impl From<Vec<String>> for Response {
fn from(names: Vec<String>) -> Self {
Self { names }
}
}
}

View File

@@ -2,10 +2,9 @@ mod cache_status;
mod group;
mod group_names;
mod schedule;
mod schema;
mod teacher;
mod teacher_names;
mod schema;
mod update_download_url;
pub use cache_status::*;
pub use group::*;
@@ -13,4 +12,3 @@ pub use group_names::*;
pub use schedule::*;
pub use teacher::*;
pub use teacher_names::*;
pub use update_download_url::*;

View File

@@ -1,25 +1,9 @@
use self::schema::*;
use crate::app_state::AppState;
use crate::routes::schedule::schema::{ErrorCode, ScheduleView};
use crate::routes::schema::{IntoResponseAsError, ResponseError};
use crate::routes::schedule::schema::ScheduleView;
use crate::state::AppState;
use actix_web::{get, web};
#[utoipa::path(responses(
(status = OK, body = ScheduleView),
(status = SERVICE_UNAVAILABLE, body = ResponseError<ErrorCode>)
))]
#[utoipa::path(responses((status = OK, body = ScheduleView)))]
#[get("/")]
pub async fn schedule(app_state: web::Data<AppState>) -> ServiceResponse {
match ScheduleView::try_from(&app_state) {
Ok(res) => Ok(res).into(),
Err(e) => match e {
ErrorCode::NoSchedule => ErrorCode::NoSchedule.into_response(),
},
}
}
mod schema {
use crate::routes::schedule::schema::{ErrorCode, ScheduleView};
pub type ServiceResponse = crate::routes::schema::Response<ScheduleView, ErrorCode>;
pub async fn schedule(app_state: web::Data<AppState>) -> ScheduleView {
ScheduleView::from(&app_state).await
}

View File

@@ -1,25 +1,18 @@
use crate::app_state::{AppState, Schedule};
use crate::parser::schema::ScheduleEntry;
use actix_macros::{IntoResponseErrorNamed, ResponderJson, StatusCode};
use crate::state::AppState;
use actix_macros::{OkResponse, ResponderJson};
use actix_web::web;
use chrono::{DateTime, Duration, Utc};
use derive_more::Display;
use providers::base::{ScheduleEntry, ScheduleSnapshot};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::ops::Deref;
use utoipa::ToSchema;
/// Response from schedule server.
#[derive(Serialize, ToSchema)]
#[derive(Serialize, ToSchema, OkResponse, ResponderJson)]
#[serde(rename_all = "camelCase")]
pub struct ScheduleView {
/// ETag schedules on polytechnic server.
etag: String,
/// Schedule update date on polytechnic website.
uploaded_at: DateTime<Utc>,
/// Date last downloaded from the Polytechnic server.
downloaded_at: DateTime<Utc>,
/// Url to xls file.
url: String,
/// Groups schedule.
groups: HashMap<String, ScheduleEntry>,
@@ -28,80 +21,66 @@ pub struct ScheduleView {
teachers: HashMap<String, ScheduleEntry>,
}
#[derive(Clone, Serialize, ToSchema, StatusCode, Display, IntoResponseErrorNamed)]
#[status_code = "actix_web::http::StatusCode::SERVICE_UNAVAILABLE"]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
#[schema(as = ScheduleShared::ErrorCode)]
pub enum ErrorCode {
/// Schedules not yet parsed.
#[display("Schedule not parsed yet.")]
NoSchedule,
#[derive(Serialize, ToSchema, OkResponse)]
pub struct ScheduleEntryResponse(ScheduleEntry);
impl From<ScheduleEntry> for ScheduleEntryResponse {
fn from(value: ScheduleEntry) -> Self {
Self(value)
}
}
impl TryFrom<&web::Data<AppState>> for ScheduleView {
type Error = ErrorCode;
impl ScheduleView {
pub async fn from(app_state: &web::Data<AppState>) -> Self {
let schedule = app_state
.get_schedule_snapshot("eng_polytechnic")
.await
.unwrap()
.deref()
.clone();
fn try_from(app_state: &web::Data<AppState>) -> Result<Self, Self::Error> {
if let Some(schedule) = app_state.schedule.lock().unwrap().clone() {
Ok(Self {
etag: schedule.etag,
uploaded_at: schedule.updated_at,
downloaded_at: schedule.parsed_at,
groups: schedule.data.groups,
teachers: schedule.data.teachers,
})
} else {
Err(ErrorCode::NoSchedule)
Self {
url: schedule.url,
groups: schedule.data.groups,
teachers: schedule.data.teachers,
}
}
}
/// Cached schedule status.
#[derive(Serialize, Deserialize, ToSchema, ResponderJson)]
#[derive(Serialize, Deserialize, ToSchema, ResponderJson, OkResponse)]
#[serde(rename_all = "camelCase")]
pub struct CacheStatus {
/// Schedule hash.
pub cache_hash: String,
/// Whether the schedule reference needs to be updated.
pub cache_update_required: bool,
pub hash: String,
/// Last cache update date.
pub last_cache_update: i64,
pub fetched_at: i64,
/// Cached schedule update date.
///
/// Determined by the polytechnic's server.
pub last_schedule_update: i64,
pub updated_at: i64,
}
impl CacheStatus {
pub fn default() -> Self {
CacheStatus {
cache_hash: "0000000000000000000000000000000000000000".to_string(),
cache_update_required: true,
last_cache_update: 0,
last_schedule_update: 0,
}
pub async fn from(value: &web::Data<AppState>) -> Self {
From::<&ScheduleSnapshot>::from(
value
.get_schedule_snapshot("eng_polytechnic")
.await
.unwrap()
.deref(),
)
}
}
impl From<&web::Data<AppState>> for CacheStatus {
fn from(value: &web::Data<AppState>) -> Self {
let schedule_lock = value.schedule.lock().unwrap();
let schedule = schedule_lock.as_ref().unwrap();
CacheStatus::from(schedule)
}
}
impl From<&Schedule> for CacheStatus {
fn from(value: &Schedule) -> Self {
impl From<&ScheduleSnapshot> for CacheStatus {
fn from(value: &ScheduleSnapshot) -> Self {
Self {
cache_hash: value.hash(),
cache_update_required: (value.fetched_at - Utc::now()) > Duration::minutes(5),
last_cache_update: value.fetched_at.timestamp(),
last_schedule_update: value.updated_at.timestamp(),
hash: value.hash(),
fetched_at: value.fetched_at.timestamp(),
updated_at: value.updated_at.timestamp(),
}
}
}

View File

@@ -1,18 +1,11 @@
use self::schema::*;
use crate::routes::schema::{IntoResponseAsError, ResponseError};
use crate::AppState;
use crate::routes::schema::ResponseError;
use actix_web::{get, web};
use providers::base::ScheduleEntry;
#[utoipa::path(responses(
(status = OK, body = Response),
(
status = SERVICE_UNAVAILABLE,
body = ResponseError<ErrorCode>,
example = json!({
"code": "NO_SCHEDULE",
"message": "Schedule not parsed yet."
})
),
(status = OK, body = ScheduleEntry),
(
status = NOT_FOUND,
body = ResponseError<ErrorCode>,
@@ -23,72 +16,35 @@ use actix_web::{get, web};
),
))]
#[get("/teacher/{name}")]
pub async fn teacher(
name: web::Path<String>,
app_state: web::Data<AppState>,
) -> ServiceResponse {
// Prevent thread lock
let schedule_lock = app_state.schedule.lock().unwrap();
pub async fn teacher(name: web::Path<String>, app_state: web::Data<AppState>) -> ServiceResponse {
match app_state
.get_schedule_snapshot("eng_polytechnic")
.await
.unwrap()
.data
.teachers
.get(&name.into_inner())
{
None => Err(ErrorCode::NotFound),
match schedule_lock.as_ref() {
None => ErrorCode::NoSchedule.into_response(),
Some(schedule) => match schedule.data.teachers.get(&name.into_inner()) {
None => ErrorCode::NotFound.into_response(),
Some(entry) => Ok(entry.clone().into()).into(),
},
Some(entry) => Ok(entry.clone().into()),
}
.into()
}
mod schema {
use crate::parser::schema::ScheduleEntry;
use actix_macros::{IntoResponseErrorNamed, StatusCode};
use chrono::{DateTime, NaiveDateTime, Utc};
use crate::routes::schedule::schema::ScheduleEntryResponse;
use actix_macros::ErrResponse;
use derive_more::Display;
use serde::Serialize;
use utoipa::ToSchema;
pub type ServiceResponse = crate::routes::schema::Response<Response, ErrorCode>;
pub type ServiceResponse = crate::routes::schema::Response<ScheduleEntryResponse, ErrorCode>;
#[derive(Serialize, ToSchema)]
#[schema(as = GetTeacher::Response)]
#[serde(rename_all = "camelCase")]
pub struct Response {
/// Teacher's schedule.
pub teacher: ScheduleEntry,
/// ## Deprecated variable.
///
/// By default, an empty list is returned.
#[deprecated = "Will be removed in future versions"]
pub updated: Vec<i32>,
/// ## Deprecated variable.
///
/// Defaults to the Unix start date.
#[deprecated = "Will be removed in future versions"]
pub updated_at: DateTime<Utc>,
}
#[allow(deprecated)]
impl From<ScheduleEntry> for Response {
fn from(teacher: ScheduleEntry) -> Self {
Self {
teacher,
updated: Vec::new(),
updated_at: NaiveDateTime::default().and_utc(),
}
}
}
#[derive(Clone, Serialize, ToSchema, StatusCode, Display, IntoResponseErrorNamed)]
#[derive(Clone, Serialize, Display, ToSchema, ErrResponse)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
#[schema(as = TeacherSchedule::ErrorCode)]
pub enum ErrorCode {
/// Schedules have not yet been parsed.
#[status_code = "actix_web::http::StatusCode::SERVICE_UNAVAILABLE"]
#[display("Schedule not parsed yet.")]
NoSchedule,
/// Teacher not found.
#[status_code = "actix_web::http::StatusCode::NOT_FOUND"]
#[display("Required teacher not found.")]

View File

@@ -1,48 +1,35 @@
use self::schema::*;
use crate::AppState;
use crate::routes::schedule::schema::ErrorCode;
use crate::routes::schema::{IntoResponseAsError, ResponseError};
use actix_web::{get, web};
#[utoipa::path(responses(
(status = OK, body = Response),
(status = SERVICE_UNAVAILABLE, body = ResponseError<ErrorCode>),
))]
#[utoipa::path(responses((status = OK, body = Response)))]
#[get("/teacher-names")]
pub async fn teacher_names(app_state: web::Data<AppState>) -> ServiceResponse {
// Prevent thread lock
let schedule_lock = app_state.schedule.lock().unwrap();
pub async fn teacher_names(app_state: web::Data<AppState>) -> Response {
let mut names: Vec<String> = app_state
.get_schedule_snapshot("eng_polytechnic")
.await
.unwrap()
.data
.teachers
.keys()
.cloned()
.collect();
match schedule_lock.as_ref() {
None => ErrorCode::NoSchedule.into_response(),
Some(schedule) => {
let mut names: Vec<String> = schedule.data.teachers.keys().cloned().collect();
names.sort();
names.sort();
Ok(names.into()).into()
}
}
.into()
Response { names }
}
mod schema {
use crate::routes::schedule::schema::ErrorCode;
use actix_macros::ResponderJson;
use serde::Serialize;
use utoipa::ToSchema;
pub type ServiceResponse = crate::routes::schema::Response<Response, ErrorCode>;
#[derive(Serialize, ToSchema)]
#[derive(Serialize, ToSchema, ResponderJson)]
#[schema(as = GetTeacherNames::Response)]
pub struct Response {
/// List of teacher names sorted alphabetically.
#[schema(examples(json!(["Хомченко Н.Е."])))]
pub names: Vec<String>,
}
impl From<Vec<String>> for Response {
fn from(names: Vec<String>) -> Self {
Self { names }
}
}
}

View File

@@ -1,137 +0,0 @@
use self::schema::*;
use crate::AppState;
use crate::app_state::Schedule;
use crate::parser::parse_xls;
use crate::routes::schedule::schema::CacheStatus;
use crate::routes::schema::{IntoResponseAsError, ResponseError};
use crate::xls_downloader::interface::XLSDownloader;
use actix_web::web::Json;
use actix_web::{patch, web};
use chrono::Utc;
#[utoipa::path(responses(
(status = OK, body = CacheStatus),
(status = NOT_ACCEPTABLE, body = ResponseError<ErrorCode>),
))]
#[patch("/update-download-url")]
pub async fn update_download_url(
data: Json<Request>,
app_state: web::Data<AppState>,
) -> ServiceResponse {
if !data.url.starts_with("https://politehnikum-eng.ru/") {
return ErrorCode::NonWhitelistedHost.into_response();
}
let mut downloader = app_state.downloader.lock().unwrap();
if let Some(url) = &downloader.url {
if url.eq(&data.url) {
return Ok(CacheStatus::from(&app_state)).into();
}
}
match downloader.set_url(data.url.clone()).await {
Ok(fetch_result) => {
let mut schedule = app_state.schedule.lock().unwrap();
if schedule.is_some()
&& fetch_result.uploaded_at < schedule.as_ref().unwrap().updated_at
{
return ErrorCode::OutdatedSchedule.into_response();
}
match downloader.fetch(false).await {
Ok(download_result) => match parse_xls(&download_result.data.unwrap()) {
Ok(data) => {
*schedule = Some(Schedule {
etag: download_result.etag,
fetched_at: download_result.requested_at,
updated_at: download_result.uploaded_at,
parsed_at: Utc::now(),
data,
});
Ok(CacheStatus::from(schedule.as_ref().unwrap())).into()
}
Err(error) => {
sentry::capture_error(&error);
ErrorCode::InvalidSchedule(error).into_response()
}
},
Err(error) => {
eprintln!("Unknown url provided {}", data.url);
eprintln!("{:?}", error);
ErrorCode::DownloadFailed.into_response()
}
}
}
Err(error) => {
eprintln!("Unknown url provided {}", data.url);
eprintln!("{:?}", error);
ErrorCode::FetchFailed.into_response()
}
}
}
mod schema {
use crate::parser::schema::ParseError;
use crate::routes::schedule::schema::CacheStatus;
use actix_macros::{IntoResponseErrorNamed, StatusCode};
use derive_more::Display;
use serde::{Deserialize, Serialize, Serializer};
use utoipa::ToSchema;
pub type ServiceResponse = crate::routes::schema::Response<CacheStatus, ErrorCode>;
#[derive(Serialize, Deserialize, ToSchema)]
pub struct Request {
/// Schedule link.
pub url: String,
}
#[derive(Clone, ToSchema, StatusCode, Display, IntoResponseErrorNamed)]
#[status_code = "actix_web::http::StatusCode::NOT_ACCEPTABLE"]
#[schema(as = SetDownloadUrl::ErrorCode)]
pub enum ErrorCode {
/// Transferred link with host different from politehnikum-eng.ru.
#[display("URL with unknown host provided. Provide url with 'politehnikum-eng.ru' host.")]
NonWhitelistedHost,
/// Failed to retrieve file metadata.
#[display("Unable to retrieve metadata from the specified URL.")]
FetchFailed,
/// Failed to download the file.
#[display("Unable to retrieve data from the specified URL.")]
DownloadFailed,
/// The link leads to an outdated schedule.
///
/// An outdated schedule refers to a schedule that was published earlier
/// than is currently available.
#[display("The schedule is older than it already is.")]
OutdatedSchedule,
/// Failed to parse the schedule.
#[display("{_0}")]
InvalidSchedule(ParseError),
}
impl Serialize for ErrorCode {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
match self {
ErrorCode::NonWhitelistedHost => serializer.serialize_str("NON_WHITELISTED_HOST"),
ErrorCode::FetchFailed => serializer.serialize_str("FETCH_FAILED"),
ErrorCode::DownloadFailed => serializer.serialize_str("DOWNLOAD_FAILED"),
ErrorCode::OutdatedSchedule => serializer.serialize_str("OUTDATED_SCHEDULE"),
ErrorCode::InvalidSchedule(_) => serializer.serialize_str("INVALID_SCHEDULE"),
}
}
}
}

View File

@@ -4,22 +4,19 @@ use actix_web::http::StatusCode;
use actix_web::{HttpRequest, HttpResponse, Responder};
use serde::{Serialize, Serializer};
use std::convert::Into;
use std::fmt::Display;
use utoipa::PartialSchema;
pub struct Response<T, E>(pub Result<T, E>)
where
T: Serialize + PartialSchema,
E: Serialize + PartialSchema + Clone + PartialStatusCode;
pub trait PartialStatusCode {
fn status_code(&self) -> StatusCode;
}
T: Serialize + PartialSchema + PartialOkResponse,
E: Serialize + PartialSchema + Display + PartialErrResponse;
/// Transform Response<T, E> into Result<T, E>
impl<T, E> Into<Result<T, E>> for Response<T, E>
where
T: Serialize + PartialSchema,
E: Serialize + PartialSchema + Clone + PartialStatusCode,
T: Serialize + PartialSchema + PartialOkResponse,
E: Serialize + PartialSchema + Display + PartialErrResponse,
{
fn into(self) -> Result<T, E> {
self.0
@@ -29,8 +26,8 @@ where
/// Transform T into Response<T, E>
impl<T, E> From<Result<T, E>> for Response<T, E>
where
T: Serialize + PartialSchema,
E: Serialize + PartialSchema + Clone + PartialStatusCode,
T: Serialize + PartialSchema + PartialOkResponse,
E: Serialize + PartialSchema + Display + PartialErrResponse,
{
fn from(value: Result<T, E>) -> Self {
Response(value)
@@ -40,17 +37,16 @@ where
/// Serialize Response<T, E>
impl<T, E> Serialize for Response<T, E>
where
T: Serialize + PartialSchema,
E: Serialize + PartialSchema + Clone + PartialStatusCode + Into<ResponseError<E>>,
T: Serialize + PartialSchema + PartialOkResponse,
E: Serialize + PartialSchema + Display + PartialErrResponse + Clone + Into<ResponseError<E>>,
{
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
match &self.0 {
Ok(ok) => serializer.serialize_some::<T>(&ok),
Err(err) => serializer
.serialize_some::<ResponseError<E>>(&ResponseError::<E>::from(err.clone().into())),
Ok(ok) => serializer.serialize_some(&ok),
Err(err) => serializer.serialize_some(&ResponseError::<E>::from(err.clone().into())),
}
}
}
@@ -58,12 +54,12 @@ where
/// Transform Response<T, E> to HttpResponse<String>
impl<T, E> Responder for Response<T, E>
where
T: Serialize + PartialSchema,
E: Serialize + PartialSchema + Clone + PartialStatusCode + Into<ResponseError<E>>,
T: Serialize + PartialSchema + PartialOkResponse,
E: Serialize + PartialSchema + Display + PartialErrResponse + Clone + Into<ResponseError<E>>,
{
type Body = EitherBody<String>;
fn respond_to(self, _: &HttpRequest) -> HttpResponse<Self::Body> {
fn respond_to(mut self, request: &HttpRequest) -> HttpResponse<Self::Body> {
match serde_json::to_string(&self) {
Ok(body) => {
let code = match &self.0 {
@@ -71,13 +67,19 @@ where
Err(e) => e.status_code(),
};
match HttpResponse::build(code)
let mut response = match HttpResponse::build(code)
.content_type(mime::APPLICATION_JSON)
.message_body(body)
{
Ok(res) => res.map_into_left_body(),
Err(err) => HttpResponse::from_error(err).map_into_right_body(),
};
if let Ok(ok) = &mut self.0 {
ok.post_process(request, &mut response);
}
response
}
Err(err) => {
@@ -87,61 +89,80 @@ where
}
}
/// ResponseError<T>
///
/// Field `message` is optional for backwards compatibility with Android App, that produces error if new fields will be added to JSON response.
#[derive(Serialize, utoipa::ToSchema)]
pub struct ResponseError<T: Serialize + PartialSchema> {
pub code: T,
#[serde(skip_serializing_if = "Option::is_none")]
pub message: Option<String>,
/// Трейт для всех положительных ответов от сервера
pub trait PartialOkResponse {
fn post_process(
&mut self,
_request: &HttpRequest,
_response: &mut HttpResponse<EitherBody<String>>,
) -> () {
}
}
pub trait IntoResponseAsError<T>
impl PartialOkResponse for () {}
/// Трейт для всех отрицательных ответов от сервера
pub trait PartialErrResponse {
fn status_code(&self) -> StatusCode;
}
/// ResponseError<T>
#[derive(Serialize, utoipa::ToSchema)]
pub struct ResponseError<T: Serialize + PartialSchema + Clone> {
pub code: T,
pub message: String,
}
impl<T> From<T> for ResponseError<T>
where
T: Serialize + PartialSchema,
Self: Serialize + PartialSchema + Clone + PartialStatusCode + Into<ResponseError<Self>>,
T: Serialize + PartialSchema + Display + Clone,
{
fn into_response(self) -> Response<T, Self> {
Response(Err(self))
fn from(code: T) -> Self {
Self {
message: format!("{}", code),
code,
}
}
}
pub mod user {
use crate::database::models::{User, UserRole};
use actix_macros::ResponderJson;
use actix_macros::{OkResponse, ResponderJson};
use serde::Serialize;
//noinspection SpellCheckingInspection
/// Используется для скрытия чувствительных полей, таких как хеш пароля или FCM
#[derive(Serialize, utoipa::ToSchema, ResponderJson)]
/// Используется для скрытия чувствительных полей, таких как хеш пароля
#[derive(Serialize, utoipa::ToSchema, ResponderJson, OkResponse)]
#[serde(rename_all = "camelCase")]
pub struct UserResponse {
/// UUID
#[schema(examples("67dcc9a9507b0000772744a2"))]
id: String,
pub id: String,
/// Имя пользователя
#[schema(examples("n08i40k"))]
username: String,
pub username: String,
/// Группа
#[schema(examples("ИС-214/23"))]
group: String,
pub group: Option<String>,
/// Роль
role: UserRole,
pub role: UserRole,
/// Идентификатор привязанного аккаунта VK
#[schema(examples(498094647, json!(null)))]
vk_id: Option<i32>,
pub vk_id: Option<i32>,
/// Идентификатор привязанного аккаунта Telegram
#[schema(examples(996004735, json!(null)))]
pub telegram_id: Option<i64>,
/// JWT токен доступа
#[schema(examples(
"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6IjY3ZGNjOWE5NTA3YjAwMDA3NzI3NDRhMiIsImlhdCI6IjE3NDMxMDgwOTkiLCJleHAiOiIxODY5MjUyMDk5In0.rMgXRb3JbT9AvLK4eiY9HMB5LxgUudkpQyoWKOypZFY"
))]
access_token: String,
pub access_token: Option<String>,
}
/// Create UserResponse from User ref.
@@ -153,6 +174,7 @@ pub mod user {
group: user.group.clone(),
role: user.role.clone(),
vk_id: user.vk_id.clone(),
telegram_id: user.telegram_id.clone(),
access_token: user.access_token.clone(),
}
}
@@ -167,6 +189,7 @@ pub mod user {
group: user.group,
role: user.role,
vk_id: user.vk_id,
telegram_id: user.telegram_id,
access_token: user.access_token,
}
}

View File

@@ -1,85 +1,63 @@
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("eng_polytechnic")
.await
.unwrap()
.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,
}
}

View File

@@ -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,
}
}

View File

@@ -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()
}

View File

@@ -1,6 +1,6 @@
use self::schema::*;
use crate::app_state::AppState;
use crate::routes::schema::{IntoResponseAsError, ResponseError};
use crate::routes::schema::ResponseError;
use crate::state::AppState;
use actix_web::{post, web};
use serde::Deserialize;
use std::collections::HashMap;
@@ -35,7 +35,7 @@ async fn oauth(data: web::Json<Request>, app_state: web::Data<AppState>) -> Serv
let data = data.into_inner();
let state = Uuid::new_v4().simple().to_string();
let vk_id = &app_state.vk_id;
let vk_id = &app_state.get_env().vk_id;
let client_id = vk_id.client_id.clone().to_string();
let mut params = HashMap::new();
@@ -56,24 +56,27 @@ async fn oauth(data: web::Json<Request>, app_state: web::Data<AppState>) -> Serv
{
Ok(res) => {
if !res.status().is_success() {
return ErrorCode::VkIdError.into_response();
return Err(ErrorCode::VkIdError).into();
}
if let Ok(auth_data) = res.json::<VkIdAuthResponse>().await {
Ok(Response {
match res.json::<VkIdAuthResponse>().await {
Ok(auth_data) => Ok(Response {
access_token: auth_data.id_token,
})
.into()
} else {
ErrorCode::VkIdError.into_response()
}),
Err(error) => {
sentry::capture_error(&error);
Err(ErrorCode::VkIdError)
}
}
}
Err(_) => ErrorCode::VkIdError.into_response(),
Err(_) => Err(ErrorCode::VkIdError),
}
.into()
}
mod schema {
use actix_macros::{IntoResponseErrorNamed, StatusCode};
use actix_macros::{ErrResponse, OkResponse};
use derive_more::Display;
use serde::{Deserialize, Serialize};
use utoipa::ToSchema;
@@ -94,7 +97,7 @@ mod schema {
pub device_id: String,
}
#[derive(Serialize, ToSchema)]
#[derive(Serialize, ToSchema, OkResponse)]
#[serde(rename_all = "camelCase")]
#[schema(as = VkIdOAuth::Response)]
pub struct Response {
@@ -102,7 +105,7 @@ mod schema {
pub access_token: String,
}
#[derive(Clone, Serialize, ToSchema, IntoResponseErrorNamed, StatusCode, Display)]
#[derive(Clone, Serialize, Display, ToSchema, ErrResponse)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
#[schema(as = VkIdOAuth::ErrorCode)]
#[status_code = "actix_web::http::StatusCode::NOT_ACCEPTABLE"]

23
src/state/env/mod.rs vendored Normal file
View File

@@ -0,0 +1,23 @@
pub mod schedule;
pub mod telegram;
pub mod vk_id;
#[cfg(not(test))]
pub mod yandex_cloud;
pub use self::schedule::ScheduleEnvData;
pub use self::telegram::TelegramEnvData;
pub use self::vk_id::VkIdEnvData;
#[cfg(not(test))]
pub use self::yandex_cloud::YandexCloudEnvData;
#[derive(Default)]
pub struct AppEnv {
pub schedule: ScheduleEnvData,
pub telegram: TelegramEnvData,
pub vk_id: VkIdEnvData,
#[cfg(not(test))]
pub yandex_cloud: YandexCloudEnvData,
}

19
src/state/env/schedule.rs vendored Normal file
View File

@@ -0,0 +1,19 @@
use std::env;
#[derive(Clone)]
pub struct ScheduleEnvData {
#[cfg(not(test))]
pub url: Option<String>,
pub auto_update: bool,
}
impl Default for ScheduleEnvData {
fn default() -> Self {
Self {
#[cfg(not(test))]
url: env::var("SCHEDULE_INIT_URL").ok(),
auto_update: !env::var("SCHEDULE_DISABLE_AUTO_UPDATE")
.is_ok_and(|v| v.eq("1") || v.eq("true")),
}
}
}

28
src/state/env/telegram.rs vendored Normal file
View File

@@ -0,0 +1,28 @@
use std::env;
#[derive(Clone)]
pub struct TelegramEnvData {
pub bot_id: i64,
pub mini_app_host: String,
pub test_dc: bool,
}
impl Default for TelegramEnvData {
fn default() -> Self {
let _self = Self {
bot_id: env::var("TELEGRAM_BOT_ID")
.expect("TELEGRAM_BOT_ID must be set")
.parse()
.expect("TELEGRAM_BOT_ID must be integer"),
mini_app_host: env::var("TELEGRAM_MINI_APP_HOST")
.expect("TELEGRAM_MINI_APP_HOST must be set"),
test_dc: env::var("TELEGRAM_TEST_DC").is_ok_and(|v| v.eq("1") || v.eq("true")),
};
if _self.test_dc {
log::warn!("Using test data-center of telegram!");
}
_self
}
}

19
src/state/env/vk_id.rs vendored Normal file
View File

@@ -0,0 +1,19 @@
use std::env;
#[derive(Clone)]
pub struct VkIdEnvData {
pub client_id: i32,
pub redirect_url: String,
}
impl Default for VkIdEnvData {
fn default() -> Self {
Self {
client_id: env::var("VK_ID_CLIENT_ID")
.expect("VK_ID_CLIENT_ID must be set")
.parse()
.expect("VK_ID_CLIENT_ID must be integer"),
redirect_url: env::var("VK_ID_REDIRECT_URI").expect("VK_ID_REDIRECT_URI must be set"),
}
}
}

16
src/state/env/yandex_cloud.rs vendored Normal file
View File

@@ -0,0 +1,16 @@
use std::env;
#[derive(Clone)]
pub struct YandexCloudEnvData {
pub api_key: String,
pub func_id: String,
}
impl Default for YandexCloudEnvData {
fn default() -> Self {
Self {
api_key: env::var("YANDEX_CLOUD_API_KEY").expect("YANDEX_CLOUD_API_KEY must be set"),
func_id: env::var("YANDEX_CLOUD_FUNC_ID").expect("YANDEX_CLOUD_FUNC_ID must be set"),
}
}
}

95
src/state/mod.rs Normal file
View File

@@ -0,0 +1,95 @@
mod env;
pub use crate::state::env::AppEnv;
use actix_web::web;
use diesel::{Connection, PgConnection};
use providers::base::{ScheduleProvider, ScheduleSnapshot};
use std::collections::HashMap;
use std::sync::Arc;
use tokio::sync::{Mutex, MutexGuard};
use tokio_util::sync::CancellationToken;
/// Common data provided to endpoints.
pub struct AppState {
cancel_token: CancellationToken,
database: Mutex<PgConnection>,
providers: HashMap<String, Arc<dyn ScheduleProvider>>,
env: AppEnv,
}
impl AppState {
pub async fn new() -> Result<Self, Box<dyn std::error::Error>> {
let database_url = std::env::var("DATABASE_URL").expect("DATABASE_URL must be set");
let env = AppEnv::default();
let providers: HashMap<String, Arc<dyn ScheduleProvider>> = HashMap::from([(
"eng_polytechnic".to_string(),
providers::EngelsPolytechnicProvider::new({
#[cfg(test)]
{
providers::EngelsPolytechnicUpdateSource::Prepared(ScheduleSnapshot {
url: "".to_string(),
fetched_at: chrono::DateTime::default(),
updated_at: chrono::DateTime::default(),
data: providers::test_utils::engels_polytechnic::test_result().unwrap(),
})
}
#[cfg(not(test))]
{
if let Some(url) = &env.schedule.url {
providers::EngelsPolytechnicUpdateSource::Url(url.clone())
} else {
providers::EngelsPolytechnicUpdateSource::GrabFromSite {
yandex_api_key: env.yandex_cloud.api_key.clone(),
yandex_func_id: env.yandex_cloud.func_id.clone(),
}
}
}
})
.await?,
)]);
let this = Self {
cancel_token: CancellationToken::new(),
database: Mutex::new(
PgConnection::establish(&database_url)
.unwrap_or_else(|_| panic!("Error connecting to {}", database_url)),
),
env,
providers,
};
if this.env.schedule.auto_update {
for (_, provider) in &this.providers {
let provider = provider.clone();
let cancel_token = this.cancel_token.clone();
tokio::spawn(async move { provider.start_auto_update_task(cancel_token).await });
}
}
Ok(this)
}
pub async fn get_schedule_snapshot(&'_ self, provider: &str) -> Option<Arc<ScheduleSnapshot>> {
if let Some(provider) = self.providers.get(provider) {
return Some(provider.get_schedule().await);
}
None
}
pub async fn get_database(&'_ self) -> MutexGuard<'_, PgConnection> {
self.database.lock().await
}
pub fn get_env(&self) -> &AppEnv {
&self.env
}
}
/// Create a new object web::Data<AppState>.
pub async fn new_app_state() -> Result<web::Data<AppState>, Box<dyn std::error::Error>> {
Ok(web::Data::new(AppState::new().await?))
}

View File

@@ -1,25 +1,19 @@
#[cfg(test)]
pub(crate) mod tests {
use crate::app_state::{AppState, Schedule, app_state};
use crate::parser::tests::test_result;
use crate::state::{new_app_state, AppState};
use actix_web::web;
use log::info;
use tokio::sync::OnceCell;
pub fn test_env() {
dotenvy::from_path(".env.test").expect("Failed to load test environment file");
info!("Loading test environment file...");
dotenvy::from_filename(".env.test.local")
.or_else(|_| dotenvy::from_filename(".env.test"))
.expect("Failed to load test environment file");
}
pub async fn test_app_state() -> web::Data<AppState> {
let state = app_state().await;
let mut schedule_lock = state.schedule.lock().unwrap();
*schedule_lock = Some(Schedule {
etag: "".to_string(),
fetched_at: Default::default(),
updated_at: Default::default(),
parsed_at: Default::default(),
data: test_result().unwrap(),
});
let state = new_app_state().await.unwrap();
state.clone()
}

View File

@@ -1,4 +1,2 @@
pub mod jwt;
pub mod error;
pub mod hasher;
pub mod mutex;
pub mod telegram;

View File

@@ -1,77 +0,0 @@
use std::ops::DerefMut;
use std::sync::Mutex;
pub trait MutexScope<T, ScopeFn, ScopeFnOutput>
where
ScopeFn: FnOnce(&mut T) -> ScopeFnOutput,
{
/// Replaces manually creating a mutex lock to perform operations on the data it manages.
///
/// # Arguments
///
/// * `f`: Function (mostly lambda) to which a reference to the mutable object stored in the mutex will be passed.
///
/// returns: Return value of `f` function.
///
/// # Examples
///
/// ```
/// let mtx: Mutex<i32> = Mutex::new(10);
///
/// let res = mtx.scope(|x| { *x = *x * 2; *x });
/// assert_eq!(res, *mtx.lock().unwrap());
/// ```
fn scope(&self, f: ScopeFn) -> ScopeFnOutput;
}
impl<T, ScopeFn, ScopeFnOutput> MutexScope<T, ScopeFn, ScopeFnOutput> for Mutex<T>
where
ScopeFn: FnOnce(&mut T) -> ScopeFnOutput,
{
fn scope(&self, f: ScopeFn) -> ScopeFnOutput {
let mut lock = self.lock().unwrap();
let inner = lock.deref_mut();
f(inner)
}
}
pub trait MutexScopeAsync<T> {
/// ## Asynchronous variant of [MutexScope::scope][MutexScope::scope].
///
/// Replaces manually creating a mutex lock to perform operations on the data it manages.
///
/// # Arguments
///
/// * `f`: Asynchronous function (mostly lambda) to which a reference to the mutable object stored in the mutex will be passed.
///
/// returns: Return value of `f` function.
///
/// # Examples
///
/// ```
/// let mtx: Mutex<i32> = Mutex::new(10);
///
/// let res = mtx.async_scope(async |x| { *x = *x * 2; *x }).await;
/// assert_eq!(res, *mtx.lock().unwrap());
/// ```
async fn async_scope<'a, F, FnFut, FnOut>(&'a self, f: F) -> FnOut
where
FnFut: Future<Output = FnOut>,
F: FnOnce(&'a mut T) -> FnFut,
T: 'a;
}
impl<T> MutexScopeAsync<T> for Mutex<T> {
async fn async_scope<'a, F, FnFut, FnOut>(&'a self, f: F) -> FnOut
where
FnFut: Future<Output = FnOut>,
F: FnOnce(&'a mut T) -> FnFut,
T: 'a,
{
let mut guard = self.lock().unwrap();
let ptr: &'a mut T = unsafe { &mut *(guard.deref_mut() as *mut _) };
f(ptr).await
}
}

91
src/utility/telegram.rs Normal file
View File

@@ -0,0 +1,91 @@
use base64::Engine;
use derive_more::{Display, Error};
use ed25519_dalek::Verifier;
use hex_literal::hex;
use serde::Deserialize;
use std::collections::HashMap;
pub struct WebAppInitDataMap {
pub data_map: HashMap<String, String>,
}
#[derive(Deserialize)]
pub struct WebAppUser {
pub id: i64,
}
#[derive(Clone, Debug, Display, Error)]
pub enum VerifyError {
#[display("No signature found.")]
NoSignature,
#[display("The provided signature was corrupted.")]
BadSignature,
#[display("The expected signature does not match the actual one.")]
IntegrityCheckFailed,
}
impl WebAppInitDataMap {
pub fn from_str(data: String) -> Self {
let mut this = Self {
data_map: HashMap::new(),
};
data.split('&')
.map(|kv| kv.split_once('=').unwrap_or_else(|| (kv, "")))
.for_each(|(key, value)| {
this.data_map.insert(key.to_string(), value.to_string());
});
if let Some(user) = this.data_map.get_mut("user") {
*user = percent_encoding::percent_decode_str(&*user)
.decode_utf8_lossy()
.to_string();
}
this
}
pub fn verify(&self, bot_id: i64, test_dc: bool) -> Result<(), VerifyError> {
//noinspection ALL
const TELEGRAM_PUBLIC_KEY: [[u8; 32]; 2] = [
hex!("e7bf03a2fa4602af4580703d88dda5bb59f32ed8b02a56c187fe7d34caed242d"),
hex!("40055058a4ee38156a06562e52eece92a771bcd8346a8c4615cb7376eddf72ec"),
];
let verifying_key = ed25519_dalek::VerifyingKey::from_bytes(
&TELEGRAM_PUBLIC_KEY[if test_dc { 1 } else { 0 }],
)
.unwrap();
let signature = {
let raw = self
.data_map
.get("signature")
.ok_or(VerifyError::NoSignature)?;
let bytes = base64::prelude::BASE64_URL_SAFE_NO_PAD
.decode(raw)
.map_err(|_| VerifyError::BadSignature)?;
ed25519_dalek::Signature::from_slice(bytes.as_slice())
.map_err(|_| VerifyError::BadSignature)?
};
let data_check_string = format!("{}:WebAppData\n{}", bot_id, {
let mut vec = self
.data_map
.iter()
.filter(|(key, _)| !["hash", "signature"].iter().any(|variant| variant == key))
.map(|(key, value)| format!("{}={}", key, value))
.collect::<Vec<String>>();
vec.sort();
vec.join("\n")
});
verifying_key
.verify(data_check_string.as_bytes(), &signature)
.map_err(|_| VerifyError::IntegrityCheckFailed)
}
}

View File

@@ -1,219 +0,0 @@
use crate::xls_downloader::interface::{FetchError, FetchOk, FetchResult, XLSDownloader};
use chrono::{DateTime, Utc};
pub struct BasicXlsDownloader {
pub url: Option<String>,
}
async fn fetch_specified(url: &String, user_agent: String, head: bool) -> FetchResult {
let client = reqwest::Client::new();
let response = if head {
client.head(url)
} else {
client.get(url)
}
.header("User-Agent", user_agent)
.send()
.await;
match response {
Ok(r) => {
if r.status().as_u16() != 200 {
return Err(FetchError::BadStatusCode);
}
let headers = r.headers();
let content_type = headers.get("Content-Type");
let etag = headers.get("etag");
let last_modified = headers.get("last-modified");
let date = headers.get("date");
if content_type.is_none() || etag.is_none() || last_modified.is_none() || date.is_none()
{
Err(FetchError::BadHeaders)
} else if content_type.unwrap() != "application/vnd.ms-excel" {
Err(FetchError::BadContentType)
} else {
let etag = etag.unwrap().to_str().unwrap().to_string();
let last_modified =
DateTime::parse_from_rfc2822(&last_modified.unwrap().to_str().unwrap())
.unwrap()
.with_timezone(&Utc);
Ok(if head {
FetchOk::head(etag, last_modified)
} else {
FetchOk::get(etag, last_modified, r.bytes().await.unwrap().to_vec())
})
}
}
Err(_) => Err(FetchError::Unknown),
}
}
impl BasicXlsDownloader {
pub fn new() -> Self {
BasicXlsDownloader { url: None }
}
}
impl XLSDownloader for BasicXlsDownloader {
async fn fetch(&self, head: bool) -> FetchResult {
if self.url.is_none() {
Err(FetchError::NoUrlProvided)
} else {
fetch_specified(
self.url.as_ref().unwrap(),
"t.me/polytechnic_next".to_string(),
head,
)
.await
}
}
async fn set_url(&mut self, url: String) -> FetchResult {
let result = fetch_specified(&url, "t.me/polytechnic_next".to_string(), true).await;
if let Ok(_) = result {
self.url = Some(url);
}
result
}
}
#[cfg(test)]
mod tests {
use crate::xls_downloader::basic_impl::{BasicXlsDownloader, fetch_specified};
use crate::xls_downloader::interface::{FetchError, XLSDownloader};
#[tokio::test]
async fn bad_url() {
let url = "bad_url".to_string();
let user_agent = String::new();
let results = [
fetch_specified(&url, user_agent.clone(), true).await,
fetch_specified(&url, user_agent.clone(), false).await,
];
assert!(results[0].is_err());
assert!(results[1].is_err());
}
#[tokio::test]
async fn bad_status_code() {
let url = "https://www.google.com/not-found".to_string();
let user_agent = String::new();
let results = [
fetch_specified(&url, user_agent.clone(), true).await,
fetch_specified(&url, user_agent.clone(), false).await,
];
assert!(results[0].is_err());
assert!(results[1].is_err());
assert_eq!(
*results[0].as_ref().err().unwrap(),
FetchError::BadStatusCode
);
assert_eq!(
*results[1].as_ref().err().unwrap(),
FetchError::BadStatusCode
);
}
#[tokio::test]
async fn bad_headers() {
let url = "https://www.google.com/favicon.ico".to_string();
let user_agent = String::new();
let results = [
fetch_specified(&url, user_agent.clone(), true).await,
fetch_specified(&url, user_agent.clone(), false).await,
];
assert!(results[0].is_err());
assert!(results[1].is_err());
assert_eq!(*results[0].as_ref().err().unwrap(), FetchError::BadHeaders);
assert_eq!(*results[1].as_ref().err().unwrap(), FetchError::BadHeaders);
}
#[tokio::test]
async fn bad_content_type() {
let url = "https://s3.aero-storage.ldragol.ru/679e5d1145a6ad00843ad3f1/67ddb59fd46303008396ac96%2Fexample.txt".to_string();
let user_agent = String::new();
let results = [
fetch_specified(&url, user_agent.clone(), true).await,
fetch_specified(&url, user_agent.clone(), false).await,
];
assert!(results[0].is_err());
assert!(results[1].is_err());
assert_eq!(
*results[0].as_ref().err().unwrap(),
FetchError::BadContentType
);
assert_eq!(
*results[1].as_ref().err().unwrap(),
FetchError::BadContentType
);
}
#[tokio::test]
async fn ok() {
let url = "https://s3.aero-storage.ldragol.ru/679e5d1145a6ad00843ad3f1/67ddb5fad46303008396ac97%2Fschedule.xls".to_string();
let user_agent = String::new();
let results = [
fetch_specified(&url, user_agent.clone(), true).await,
fetch_specified(&url, user_agent.clone(), false).await,
];
assert!(results[0].is_ok());
assert!(results[1].is_ok());
}
#[tokio::test]
async fn downloader_set_ok() {
let url = "https://s3.aero-storage.ldragol.ru/679e5d1145a6ad00843ad3f1/67ddb5fad46303008396ac97%2Fschedule.xls".to_string();
let mut downloader = BasicXlsDownloader::new();
assert!(downloader.set_url(url).await.is_ok());
}
#[tokio::test]
async fn downloader_set_err() {
let url = "bad_url".to_string();
let mut downloader = BasicXlsDownloader::new();
assert!(downloader.set_url(url).await.is_err());
}
#[tokio::test]
async fn downloader_ok() {
let url = "https://s3.aero-storage.ldragol.ru/679e5d1145a6ad00843ad3f1/67ddb5fad46303008396ac97%2Fschedule.xls".to_string();
let mut downloader = BasicXlsDownloader::new();
assert!(downloader.set_url(url).await.is_ok());
assert!(downloader.fetch(false).await.is_ok());
}
#[tokio::test]
async fn downloader_no_url_provided() {
let downloader = BasicXlsDownloader::new();
let result = downloader.fetch(false).await;
assert!(result.is_err());
assert_eq!(result.err().unwrap(), FetchError::NoUrlProvided);
}
}

View File

@@ -1,67 +0,0 @@
use chrono::{DateTime, Utc};
/// XLS data retrieval errors.
#[derive(PartialEq, Debug)]
pub enum FetchError {
/// File url is not set.
NoUrlProvided,
/// Unknown error.
Unknown,
/// Server returned a status code different from 200.
BadStatusCode,
/// The url leads to a file of a different type.
BadContentType,
/// Server doesn't return expected headers.
BadHeaders,
}
/// Result of XLS data retrieval.
pub struct FetchOk {
/// ETag object.
pub etag: String,
/// File upload date.
pub uploaded_at: DateTime<Utc>,
/// Date data received.
pub requested_at: DateTime<Utc>,
/// File data.
pub data: Option<Vec<u8>>,
}
impl FetchOk {
/// Result without file content.
pub fn head(etag: String, uploaded_at: DateTime<Utc>) -> Self {
FetchOk {
etag,
uploaded_at,
requested_at: Utc::now(),
data: None,
}
}
/// Full result.
pub fn get(etag: String, uploaded_at: DateTime<Utc>, data: Vec<u8>) -> Self {
FetchOk {
etag,
uploaded_at,
requested_at: Utc::now(),
data: Some(data),
}
}
}
pub type FetchResult = Result<FetchOk, FetchError>;
pub trait XLSDownloader {
/// Get data about the file, and optionally its content.
async fn fetch(&self, head: bool) -> FetchResult;
/// Setting the file link.
async fn set_url(&mut self, url: String) -> FetchResult;
}

View File

@@ -1,2 +0,0 @@
pub mod basic_impl;
pub mod interface;

Binary file not shown.