mirror of
https://github.com/n08i40k/schedule-parser-rusted.git
synced 2025-12-06 17:57:47 +03:00
Compare commits
13 Commits
release/v1
...
b664ba578d
| Author | SHA1 | Date | |
|---|---|---|---|
|
b664ba578d
|
|||
|
983967f8b0
|
|||
|
e5760120e2
|
|||
|
a28fb66dd4
|
|||
|
3780fb3136
|
|||
|
6c71bc19f5
|
|||
|
2d0041dc8b
|
|||
|
b5d372e109
|
|||
|
84dca02c34
|
|||
|
6c9d3b3b31
|
|||
|
a348b1b99b
|
|||
|
ff12ee5da2
|
|||
|
35f707901f
|
142
.github/workflows/build.yml
vendored
Normal file
142
.github/workflows/build.yml
vendored
Normal file
@@ -0,0 +1,142 @@
|
|||||||
|
name: build
|
||||||
|
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
branches: [ "master" ]
|
||||||
|
tags-ignore: [ "release/v*" ]
|
||||||
|
|
||||||
|
permissions:
|
||||||
|
contents: write
|
||||||
|
|
||||||
|
env:
|
||||||
|
CARGO_TERM_COLOR: always
|
||||||
|
|
||||||
|
BINARY_NAME: schedule-parser-rusted
|
||||||
|
|
||||||
|
TEST_DB: ${{ secrets.TEST_DATABASE_URL }}
|
||||||
|
|
||||||
|
SENTRY_AUTH_TOKEN: ${{ secrets.SENTRY_AUTH_TOKEN }}
|
||||||
|
SENTRY_ORG: ${{ secrets.SENTRY_ORG }}
|
||||||
|
SENTRY_PROJECT: ${{ secrets.SENTRY_PROJECT }}
|
||||||
|
|
||||||
|
DOCKER_IMAGE_NAME: ${{ github.repository }}
|
||||||
|
|
||||||
|
DOCKER_REGISTRY_HOST: registry.n08i40k.ru
|
||||||
|
DOCKER_REGISTRY_USERNAME: ${{ github.repository_owner }}
|
||||||
|
DOCKER_REGISTRY_PASSWORD: ${{ secrets.DOCKER_REGISTRY_PASSWORD }}
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
test:
|
||||||
|
name: Test
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v4
|
||||||
|
with:
|
||||||
|
fetch-depth: 0
|
||||||
|
|
||||||
|
- name: Setup Rust
|
||||||
|
uses: actions-rust-lang/setup-rust-toolchain@v1.11.0
|
||||||
|
with:
|
||||||
|
toolchain: stable
|
||||||
|
|
||||||
|
- name: Test
|
||||||
|
run: |
|
||||||
|
cargo test --verbose
|
||||||
|
env:
|
||||||
|
DATABASE_URL: ${{ env.TEST_DB }}
|
||||||
|
SCHEDULE_DISABLE_AUTO_UPDATE: 1
|
||||||
|
JWT_SECRET: "test-secret-at-least-256-bits-used"
|
||||||
|
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
|
||||||
|
needs: test
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v4
|
||||||
|
with:
|
||||||
|
fetch-depth: 0
|
||||||
|
|
||||||
|
- name: Setup Rust
|
||||||
|
uses: actions-rust-lang/setup-rust-toolchain@v1.11.0
|
||||||
|
with:
|
||||||
|
toolchain: stable
|
||||||
|
|
||||||
|
- name: Build
|
||||||
|
run: cargo build --release --verbose
|
||||||
|
|
||||||
|
- name: Extract debug symbols
|
||||||
|
run: |
|
||||||
|
objcopy --only-keep-debug target/release/${{ env.BINARY_NAME }}{,.d}
|
||||||
|
objcopy --strip-debug --strip-unneeded target/release/${{ env.BINARY_NAME }}
|
||||||
|
objcopy --add-gnu-debuglink target/release/${{ env.BINARY_NAME }}{.d,}
|
||||||
|
|
||||||
|
- name: Setup sentry-cli
|
||||||
|
uses: matbour/setup-sentry-cli@v2.0.0
|
||||||
|
with:
|
||||||
|
version: latest
|
||||||
|
token: ${{ env.SENTRY_AUTH_TOKEN }}
|
||||||
|
organization: ${{ env.SENTRY_ORG }}
|
||||||
|
project: ${{ env.SENTRY_PROJECT }}
|
||||||
|
|
||||||
|
- name: Upload debug symbols to Sentry
|
||||||
|
run: |
|
||||||
|
sentry-cli debug-files upload --include-sources .
|
||||||
|
|
||||||
|
- name: Upload build binary artifact
|
||||||
|
uses: actions/upload-artifact@v4
|
||||||
|
with:
|
||||||
|
name: release-binary
|
||||||
|
path: target/release/${{ env.BINARY_NAME }}
|
||||||
|
|
||||||
|
- name: Upload build debug symbols artifact
|
||||||
|
uses: actions/upload-artifact@v4
|
||||||
|
with:
|
||||||
|
name: release-symbols
|
||||||
|
path: target/release/${{ env.BINARY_NAME }}.d
|
||||||
|
|
||||||
|
docker:
|
||||||
|
name: Build & Push Docker Image
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
needs: build
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v4
|
||||||
|
|
||||||
|
- name: Download build artifacts
|
||||||
|
uses: actions/download-artifact@v4
|
||||||
|
with:
|
||||||
|
name: release-binary
|
||||||
|
|
||||||
|
- name: Setup Docker Buildx
|
||||||
|
uses: docker/setup-buildx-action@v3.10.0
|
||||||
|
|
||||||
|
- name: Login to Registry
|
||||||
|
uses: docker/login-action@v3.4.0
|
||||||
|
with:
|
||||||
|
registry: ${{ env.DOCKER_REGISTRY_HOST }}
|
||||||
|
username: ${{ env.DOCKER_REGISTRY_USERNAME }}
|
||||||
|
password: ${{ env.DOCKER_REGISTRY_PASSWORD }}
|
||||||
|
|
||||||
|
- name: Extract Docker metadata
|
||||||
|
id: meta
|
||||||
|
uses: docker/metadata-action@v5.7.0
|
||||||
|
with:
|
||||||
|
images: ${{ env.DOCKER_REGISTRY_HOST }}/${{ env.DOCKER_IMAGE_NAME }}
|
||||||
|
|
||||||
|
- name: Build and push Docker image
|
||||||
|
id: build-and-push
|
||||||
|
uses: docker/build-push-action@v6.15.0
|
||||||
|
with:
|
||||||
|
context: .
|
||||||
|
push: true
|
||||||
|
tags: ${{ steps.meta.outputs.tags }}
|
||||||
|
labels: ${{ steps.meta.outputs.labels }}
|
||||||
|
cache-from: type=gha
|
||||||
|
cache-to: type=gha,mode=max
|
||||||
|
build-args: |
|
||||||
|
"BINARY_NAME=${{ env.BINARY_NAME }}"
|
||||||
2
.github/workflows/test.yml
vendored
2
.github/workflows/test.yml
vendored
@@ -2,7 +2,7 @@ name: cargo test
|
|||||||
|
|
||||||
on:
|
on:
|
||||||
push:
|
push:
|
||||||
branches: [ "master" ]
|
branches: [ "development" ]
|
||||||
tags-ignore: [ "release/v*" ]
|
tags-ignore: [ "release/v*" ]
|
||||||
|
|
||||||
permissions:
|
permissions:
|
||||||
|
|||||||
863
Cargo.lock
generated
863
Cargo.lock
generated
File diff suppressed because it is too large
Load Diff
18
Cargo.toml
18
Cargo.toml
@@ -3,7 +3,7 @@ members = ["actix-macros", "actix-test", "providers"]
|
|||||||
|
|
||||||
[package]
|
[package]
|
||||||
name = "schedule-parser-rusted"
|
name = "schedule-parser-rusted"
|
||||||
version = "1.2.0"
|
version = "1.3.0"
|
||||||
edition = "2024"
|
edition = "2024"
|
||||||
publish = false
|
publish = false
|
||||||
|
|
||||||
@@ -21,7 +21,7 @@ actix-macros = { path = "actix-macros" }
|
|||||||
actix-web = "4.11.0"
|
actix-web = "4.11.0"
|
||||||
|
|
||||||
# basic
|
# basic
|
||||||
chrono = { version = "0.4.41", features = ["serde"] }
|
chrono = { version = "0.4.42", features = ["serde"] }
|
||||||
derive_more = { version = "2.0.1", features = ["full"] }
|
derive_more = { version = "2.0.1", features = ["full"] }
|
||||||
dotenvy = "0.15.7"
|
dotenvy = "0.15.7"
|
||||||
|
|
||||||
@@ -48,13 +48,13 @@ reqwest = { version = "0.12.23", features = ["json"] }
|
|||||||
mime = "0.3.17"
|
mime = "0.3.17"
|
||||||
|
|
||||||
# error handling
|
# error handling
|
||||||
sentry = "0.42.0"
|
sentry = "0.43.0"
|
||||||
sentry-actix = "0.42.0"
|
sentry-actix = "0.43.0"
|
||||||
|
|
||||||
# [de]serializing
|
# [de]serializing
|
||||||
serde = { version = "1.0.219", features = ["derive"] }
|
serde = { version = "1", features = ["derive"] }
|
||||||
serde_json = "1.0.143"
|
serde_json = "1"
|
||||||
serde_with = "3.14.0"
|
serde_with = "3.14"
|
||||||
|
|
||||||
sha1 = "0.11.0-rc.2"
|
sha1 = "0.11.0-rc.2"
|
||||||
|
|
||||||
@@ -65,12 +65,12 @@ utoipa-actix-web = "0.1.2"
|
|||||||
|
|
||||||
uuid = { version = "1.18.1", features = ["v4"] }
|
uuid = { version = "1.18.1", features = ["v4"] }
|
||||||
hex-literal = "1"
|
hex-literal = "1"
|
||||||
log = "0.4.27"
|
log = "0.4.28"
|
||||||
|
|
||||||
# telegram webdata deciding and verify
|
# telegram webdata deciding and verify
|
||||||
base64 = "0.22.1"
|
base64 = "0.22.1"
|
||||||
percent-encoding = "2.3.2"
|
percent-encoding = "2.3.2"
|
||||||
ed25519-dalek = "3.0.0-pre.0"
|
ed25519-dalek = "3.0.0-pre.1"
|
||||||
|
|
||||||
# development tracing
|
# development tracing
|
||||||
console-subscriber = { version = "0.4.1", optional = true }
|
console-subscriber = { version = "0.4.1", optional = true }
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ mod shared {
|
|||||||
use quote::{ToTokens, quote};
|
use quote::{ToTokens, quote};
|
||||||
use syn::{Attribute, DeriveInput};
|
use syn::{Attribute, DeriveInput};
|
||||||
|
|
||||||
pub fn find_status_code(attrs: &Vec<Attribute>) -> Option<proc_macro2::TokenStream> {
|
pub fn find_status_code(attrs: &[Attribute]) -> Option<proc_macro2::TokenStream> {
|
||||||
attrs
|
attrs
|
||||||
.iter()
|
.iter()
|
||||||
.find_map(|attr| -> Option<proc_macro2::TokenStream> {
|
.find_map(|attr| -> Option<proc_macro2::TokenStream> {
|
||||||
@@ -41,14 +41,12 @@ mod shared {
|
|||||||
|
|
||||||
let mut status_code_arms: Vec<proc_macro2::TokenStream> = variants
|
let mut status_code_arms: Vec<proc_macro2::TokenStream> = variants
|
||||||
.iter()
|
.iter()
|
||||||
.map(|v| -> Option<proc_macro2::TokenStream> {
|
.filter_map(|v| -> Option<proc_macro2::TokenStream> {
|
||||||
let status_code = find_status_code(&v.attrs)?;
|
let status_code = find_status_code(&v.attrs)?;
|
||||||
let variant_name = &v.ident;
|
let variant_name = &v.ident;
|
||||||
|
|
||||||
Some(quote! { #name::#variant_name => #status_code, })
|
Some(quote! { #name::#variant_name => #status_code, })
|
||||||
})
|
})
|
||||||
.filter(|v| v.is_some())
|
|
||||||
.map(|v| v.unwrap())
|
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
if status_code_arms.len() < variants.len() {
|
if status_code_arms.len() < variants.len() {
|
||||||
|
|||||||
@@ -59,4 +59,5 @@ impl Query {
|
|||||||
define_is_exists!(user, id, str, Id);
|
define_is_exists!(user, id, str, Id);
|
||||||
define_is_exists!(user, username, str, Username);
|
define_is_exists!(user, username, str, Username);
|
||||||
define_is_exists!(user, telegram_id, i64, TelegramId);
|
define_is_exists!(user, telegram_id, i64, TelegramId);
|
||||||
|
define_is_exists!(user, vk_id, i32, VkId);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -100,6 +100,9 @@ pub enum LessonType {
|
|||||||
|
|
||||||
/// Защита курсового проекта.
|
/// Защита курсового проекта.
|
||||||
CourseProjectDefense,
|
CourseProjectDefense,
|
||||||
|
|
||||||
|
/// Практическое занятие.
|
||||||
|
Practice
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Hash, Debug, Serialize, Deserialize, ToSchema)]
|
#[derive(Clone, Hash, Debug, Serialize, Deserialize, ToSchema)]
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "provider-engels-polytechnic"
|
name = "provider-engels-polytechnic"
|
||||||
version = "0.1.0"
|
version = "0.2.0"
|
||||||
edition = "2024"
|
edition = "2024"
|
||||||
|
|
||||||
[features]
|
[features]
|
||||||
@@ -20,7 +20,7 @@ derive_more = { version = "2.0.1", features = ["error", "display"] }
|
|||||||
|
|
||||||
utoipa = { version = "5.4.0", features = ["macros", "chrono"] }
|
utoipa = { version = "5.4.0", features = ["macros", "chrono"] }
|
||||||
|
|
||||||
calamine = "0.30.0"
|
calamine = "0.30"
|
||||||
async-trait = "0.1.89"
|
async-trait = "0.1.89"
|
||||||
|
|
||||||
reqwest = "0.12.23"
|
reqwest = "0.12.23"
|
||||||
@@ -28,5 +28,5 @@ ua_generator = "0.5.22"
|
|||||||
regex = "1.11.2"
|
regex = "1.11.2"
|
||||||
strsim = "0.11.1"
|
strsim = "0.11.1"
|
||||||
log = "0.4.27"
|
log = "0.4.27"
|
||||||
sentry = "0.42.0"
|
sentry = "0.43.0"
|
||||||
|
|
||||||
|
|||||||
@@ -25,7 +25,7 @@ pub struct EngelsPolytechnicProvider {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl EngelsPolytechnicProvider {
|
impl EngelsPolytechnicProvider {
|
||||||
pub async fn new(
|
pub async fn get(
|
||||||
update_source: UpdateSource,
|
update_source: UpdateSource,
|
||||||
) -> Result<Arc<dyn ScheduleProvider>, crate::updater::error::Error> {
|
) -> Result<Arc<dyn ScheduleProvider>, crate::updater::error::Error> {
|
||||||
let (updater, snapshot) = Updater::new(update_source).await?;
|
let (updater, snapshot) = Updater::new(update_source).await?;
|
||||||
@@ -60,7 +60,7 @@ impl ScheduleProvider for Wrapper {
|
|||||||
|
|
||||||
log::info!("Updating schedule...");
|
log::info!("Updating schedule...");
|
||||||
|
|
||||||
match this.updater.update(&mut this.snapshot).await {
|
match this.updater.update(&this.snapshot).await {
|
||||||
Ok(snapshot) => {
|
Ok(snapshot) => {
|
||||||
this.snapshot = Arc::new(snapshot);
|
this.snapshot = Arc::new(snapshot);
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
use crate::or_continue;
|
use crate::or_continue;
|
||||||
use crate::parser::error::{ErrorCell, ErrorCellPos};
|
use crate::parser::error::{Error, ErrorCell, ErrorCellPos};
|
||||||
use crate::parser::worksheet::WorkSheet;
|
use crate::parser::worksheet::WorkSheet;
|
||||||
use crate::parser::LessonParseResult::{Lessons, Street};
|
use crate::parser::LessonParseResult::{Lessons, Street};
|
||||||
use base::LessonType::Break;
|
use base::LessonType::Break;
|
||||||
@@ -230,7 +230,7 @@ enum LessonParseResult {
|
|||||||
|
|
||||||
// noinspection GrazieInspection
|
// noinspection GrazieInspection
|
||||||
/// Obtaining a non-standard type of lesson by name.
|
/// Obtaining a non-standard type of lesson by name.
|
||||||
fn guess_lesson_type(text: &String) -> Option<LessonType> {
|
fn guess_lesson_type(text: &str) -> Option<LessonType> {
|
||||||
static MAP: LazyLock<HashMap<&str, LessonType>> = LazyLock::new(|| {
|
static MAP: LazyLock<HashMap<&str, LessonType>> = LazyLock::new(|| {
|
||||||
HashMap::from([
|
HashMap::from([
|
||||||
("консультация", LessonType::Consultation),
|
("консультация", LessonType::Consultation),
|
||||||
@@ -240,27 +240,24 @@ fn guess_lesson_type(text: &String) -> Option<LessonType> {
|
|||||||
("экзамен", LessonType::ExamDefault),
|
("экзамен", LessonType::ExamDefault),
|
||||||
("курсовой проект", LessonType::CourseProject),
|
("курсовой проект", LessonType::CourseProject),
|
||||||
("защита курсового проекта", LessonType::CourseProjectDefense),
|
("защита курсового проекта", LessonType::CourseProjectDefense),
|
||||||
|
("практическое занятие", LessonType::Practice),
|
||||||
])
|
])
|
||||||
});
|
});
|
||||||
|
|
||||||
let name_lower = text.to_lowercase();
|
let name_lower = text.to_lowercase();
|
||||||
|
|
||||||
match MAP
|
MAP.iter()
|
||||||
.iter()
|
.map(|(text, lesson_type)| (lesson_type, strsim::levenshtein(text, &name_lower)))
|
||||||
.map(|(text, lesson_type)| (lesson_type, strsim::levenshtein(text, &*name_lower)))
|
|
||||||
.filter(|x| x.1 <= 4)
|
.filter(|x| x.1 <= 4)
|
||||||
.min_by_key(|(_, score)| *score)
|
.min_by_key(|(_, score)| *score)
|
||||||
{
|
.map(|v| v.0.clone())
|
||||||
None => None,
|
|
||||||
Some(v) => Some(v.0.clone()),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Getting a pair or street from a cell.
|
/// Getting a pair or street from a cell.
|
||||||
fn parse_lesson(
|
fn parse_lesson(
|
||||||
worksheet: &WorkSheet,
|
worksheet: &WorkSheet,
|
||||||
day: &Day,
|
day: &Day,
|
||||||
day_boundaries: &Vec<BoundariesCellInfo>,
|
day_boundaries: &[BoundariesCellInfo],
|
||||||
lesson_boundaries: &BoundariesCellInfo,
|
lesson_boundaries: &BoundariesCellInfo,
|
||||||
group_column: u32,
|
group_column: u32,
|
||||||
) -> Result<LessonParseResult, crate::parser::error::Error> {
|
) -> Result<LessonParseResult, crate::parser::error::Error> {
|
||||||
@@ -297,7 +294,7 @@ fn parse_lesson(
|
|||||||
column: group_column,
|
column: group_column,
|
||||||
}))?;
|
}))?;
|
||||||
|
|
||||||
let range: Option<[u8; 2]> = if lesson_boundaries.default_index != None {
|
let range: Option<[u8; 2]> = if lesson_boundaries.default_index.is_some() {
|
||||||
let default = lesson_boundaries.default_index.unwrap() as u8;
|
let default = lesson_boundaries.default_index.unwrap() as u8;
|
||||||
Some([default, end_time.default_index.unwrap() as u8])
|
Some([default, end_time.default_index.unwrap() as u8])
|
||||||
} else {
|
} else {
|
||||||
@@ -312,7 +309,11 @@ fn parse_lesson(
|
|||||||
Ok((range, time))
|
Ok((range, time))
|
||||||
}?;
|
}?;
|
||||||
|
|
||||||
let (name, mut subgroups, lesson_type) = parse_name_and_subgroups(&name)?;
|
let ParsedLessonName {
|
||||||
|
name,
|
||||||
|
mut subgroups,
|
||||||
|
r#type: lesson_type,
|
||||||
|
} = parse_name_and_subgroups(&name)?;
|
||||||
|
|
||||||
{
|
{
|
||||||
let cabinets: Vec<String> = parse_cabinets(
|
let cabinets: Vec<String> = parse_cabinets(
|
||||||
@@ -325,12 +326,10 @@ fn parse_lesson(
|
|||||||
|
|
||||||
if cab_count == 1 {
|
if cab_count == 1 {
|
||||||
// Назначаем этот кабинет всем подгруппам
|
// Назначаем этот кабинет всем подгруппам
|
||||||
let cab = Some(cabinets.get(0).unwrap().clone());
|
let cab = Some(cabinets.first().unwrap().clone());
|
||||||
|
|
||||||
for subgroup in &mut subgroups {
|
for subgroup in subgroups.iter_mut().flatten() {
|
||||||
if let Some(subgroup) = subgroup {
|
subgroup.cabinet = cab.clone()
|
||||||
subgroup.cabinet = cab.clone()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} else if cab_count == 2 {
|
} else if cab_count == 2 {
|
||||||
while subgroups.len() < cab_count {
|
while subgroups.len() < cab_count {
|
||||||
@@ -361,10 +360,7 @@ fn parse_lesson(
|
|||||||
range: default_range,
|
range: default_range,
|
||||||
name: Some(name),
|
name: Some(name),
|
||||||
time: lesson_time,
|
time: lesson_time,
|
||||||
subgroups: if subgroups.len() == 2
|
subgroups: if subgroups.len() == 2 && subgroups.iter().all(|x| x.is_none()) {
|
||||||
&& subgroups.get(0).unwrap().is_none()
|
|
||||||
&& subgroups.get(1).unwrap().is_none()
|
|
||||||
{
|
|
||||||
None
|
None
|
||||||
} else {
|
} else {
|
||||||
Some(subgroups)
|
Some(subgroups)
|
||||||
@@ -416,12 +412,15 @@ fn parse_cabinets(worksheet: &WorkSheet, row_range: (u32, u32), column: u32) ->
|
|||||||
cabinets
|
cabinets
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct ParsedLessonName {
|
||||||
|
name: String,
|
||||||
|
subgroups: Vec<Option<LessonSubGroup>>,
|
||||||
|
r#type: Option<LessonType>,
|
||||||
|
}
|
||||||
|
|
||||||
//noinspection GrazieInspection
|
//noinspection GrazieInspection
|
||||||
/// Getting the "pure" name of the lesson and list of teachers from the text of the lesson cell.
|
/// Getting the "pure" name of the lesson and list of teachers from the text of the lesson cell.
|
||||||
fn parse_name_and_subgroups(
|
fn parse_name_and_subgroups(text: &str) -> Result<ParsedLessonName, Error> {
|
||||||
text: &String,
|
|
||||||
) -> Result<(String, Vec<Option<LessonSubGroup>>, Option<LessonType>), crate::parser::error::Error>
|
|
||||||
{
|
|
||||||
// Части названия пары:
|
// Части названия пары:
|
||||||
// 1. Само название.
|
// 1. Само название.
|
||||||
// 2. Список преподавателей и подгрупп.
|
// 2. Список преподавателей и подгрупп.
|
||||||
@@ -449,7 +448,7 @@ fn parse_name_and_subgroups(
|
|||||||
|
|
||||||
static NAMES_REGEX: LazyLock<Regex> = LazyLock::new(|| {
|
static NAMES_REGEX: LazyLock<Regex> = LazyLock::new(|| {
|
||||||
Regex::new(
|
Regex::new(
|
||||||
r"(?:[А-Я][а-я]+\s?(?:[А-Я][\s.]*){2}(?:\(\s*\d\s*[а-я\s]+\))?(?:[\s,]+)?){1,2}+[\s.,]*",
|
r"(?:[А-Я][а-я]+\s?(?:[А-Я][\s.]*){2}(?:\(?\s*\d\s*[а-я\s]+\)?)?(?:[\s,.]+)?){1,2}+[\s.,]*",
|
||||||
)
|
)
|
||||||
.unwrap()
|
.unwrap()
|
||||||
});
|
});
|
||||||
@@ -458,7 +457,7 @@ fn parse_name_and_subgroups(
|
|||||||
static CLEAN_RE: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"[\s\n\t]+").unwrap());
|
static CLEAN_RE: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"[\s\n\t]+").unwrap());
|
||||||
|
|
||||||
let text = CLEAN_RE
|
let text = CLEAN_RE
|
||||||
.replace(&text.replace(&[' ', '\t', '\n'], " "), " ")
|
.replace(&text.replace([' ', '\t', '\n'], " ").replace(",", ""), " ")
|
||||||
.to_string();
|
.to_string();
|
||||||
|
|
||||||
let (lesson_name, subgroups, lesson_type) = match NAMES_REGEX.captures(&text) {
|
let (lesson_name, subgroups, lesson_type) = match NAMES_REGEX.captures(&text) {
|
||||||
@@ -466,19 +465,21 @@ fn parse_name_and_subgroups(
|
|||||||
let capture = captures.get(0).unwrap();
|
let capture = captures.get(0).unwrap();
|
||||||
|
|
||||||
let subgroups: Vec<Option<LessonSubGroup>> = {
|
let subgroups: Vec<Option<LessonSubGroup>> = {
|
||||||
let src = capture.as_str().replace(&[' ', '.'], "");
|
let src = capture.as_str().replace([' ', '.'], "");
|
||||||
|
|
||||||
let mut shared_subgroup = false;
|
let mut shared_subgroup = false;
|
||||||
let mut subgroups: [Option<LessonSubGroup>; 2] = [None, None];
|
let mut subgroups: [Option<LessonSubGroup>; 2] = [None, None];
|
||||||
|
|
||||||
for name in src.split(',') {
|
for name in src.split(',') {
|
||||||
let open_bracket_index = name.find('(');
|
let digit_index = name.find(|c: char| c.is_ascii_digit());
|
||||||
|
|
||||||
let number: u8 = open_bracket_index
|
let number: u8 =
|
||||||
.map_or(0, |index| name[(index + 1)..(index + 2)].parse().unwrap());
|
digit_index.map_or(0, |index| name[(index)..(index + 1)].parse().unwrap());
|
||||||
|
|
||||||
let teacher_name = {
|
let teacher_name = {
|
||||||
let name_end = open_bracket_index.unwrap_or_else(|| name.len());
|
let name_end = name
|
||||||
|
.find(|c: char| !c.is_alphabetic())
|
||||||
|
.unwrap_or(name.len());
|
||||||
|
|
||||||
// Я ебал. Как же я долго до этого доходил.
|
// Я ебал. Как же я долго до этого доходил.
|
||||||
format!(
|
format!(
|
||||||
@@ -527,7 +528,7 @@ fn parse_name_and_subgroups(
|
|||||||
if result.is_none() {
|
if result.is_none() {
|
||||||
#[cfg(not(debug_assertions))]
|
#[cfg(not(debug_assertions))]
|
||||||
sentry::capture_message(
|
sentry::capture_message(
|
||||||
&*format!("Не удалось угадать тип пары '{}'!", extra),
|
&format!("Не удалось угадать тип пары '{}'!", extra),
|
||||||
sentry::Level::Warning,
|
sentry::Level::Warning,
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -545,7 +546,11 @@ fn parse_name_and_subgroups(
|
|||||||
None => (text, Vec::new(), None),
|
None => (text, Vec::new(), None),
|
||||||
};
|
};
|
||||||
|
|
||||||
Ok((lesson_name, subgroups, lesson_type))
|
Ok(ParsedLessonName {
|
||||||
|
name: lesson_name,
|
||||||
|
subgroups,
|
||||||
|
r#type: lesson_type,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Getting the start and end of a pair from a cell in the first column of a document.
|
/// Getting the start and end of a pair from a cell in the first column of a document.
|
||||||
@@ -554,18 +559,11 @@ fn parse_name_and_subgroups(
|
|||||||
///
|
///
|
||||||
/// * `cell_data`: text in cell.
|
/// * `cell_data`: text in cell.
|
||||||
/// * `date`: date of the current day.
|
/// * `date`: date of the current day.
|
||||||
fn parse_lesson_boundaries_cell(
|
fn parse_lesson_boundaries_cell(cell_data: &str, date: DateTime<Utc>) -> Option<LessonBoundaries> {
|
||||||
cell_data: &String,
|
|
||||||
date: DateTime<Utc>,
|
|
||||||
) -> Option<LessonBoundaries> {
|
|
||||||
static TIME_RE: LazyLock<Regex> =
|
static TIME_RE: LazyLock<Regex> =
|
||||||
LazyLock::new(|| Regex::new(r"(\d+\.\d+)-(\d+\.\d+)").unwrap());
|
LazyLock::new(|| Regex::new(r"(\d+\.\d+)-(\d+\.\d+)").unwrap());
|
||||||
|
|
||||||
let parse_res = if let Some(captures) = TIME_RE.captures(cell_data) {
|
let parse_res = TIME_RE.captures(cell_data)?;
|
||||||
captures
|
|
||||||
} else {
|
|
||||||
return None;
|
|
||||||
};
|
|
||||||
|
|
||||||
let start_match = parse_res.get(1).unwrap().as_str();
|
let start_match = parse_res.get(1).unwrap().as_str();
|
||||||
let start_parts: Vec<&str> = start_match.split(".").collect();
|
let start_parts: Vec<&str> = start_match.split(".").collect();
|
||||||
@@ -579,7 +577,7 @@ fn parse_lesson_boundaries_cell(
|
|||||||
};
|
};
|
||||||
|
|
||||||
Some(LessonBoundaries {
|
Some(LessonBoundaries {
|
||||||
start: GET_TIME(date.clone(), &start_parts),
|
start: GET_TIME(date, &start_parts),
|
||||||
end: GET_TIME(date, &end_parts),
|
end: GET_TIME(date, &end_parts),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -607,7 +605,7 @@ fn parse_day_boundaries(
|
|||||||
continue;
|
continue;
|
||||||
};
|
};
|
||||||
|
|
||||||
let lesson_time = parse_lesson_boundaries_cell(&time_cell, date.clone()).ok_or(
|
let lesson_time = parse_lesson_boundaries_cell(&time_cell, date).ok_or(
|
||||||
error::Error::LessonBoundaries(ErrorCell::new(row, column, time_cell.clone())),
|
error::Error::LessonBoundaries(ErrorCell::new(row, column, time_cell.clone())),
|
||||||
)?;
|
)?;
|
||||||
|
|
||||||
@@ -652,7 +650,7 @@ fn parse_day_boundaries(
|
|||||||
/// * `week_markup`: markup of the current week.
|
/// * `week_markup`: markup of the current week.
|
||||||
fn parse_week_boundaries(
|
fn parse_week_boundaries(
|
||||||
worksheet: &WorkSheet,
|
worksheet: &WorkSheet,
|
||||||
week_markup: &Vec<DayCellInfo>,
|
week_markup: &[DayCellInfo],
|
||||||
) -> Result<Vec<Vec<BoundariesCellInfo>>, crate::parser::error::Error> {
|
) -> Result<Vec<Vec<BoundariesCellInfo>>, crate::parser::error::Error> {
|
||||||
let mut result: Vec<Vec<BoundariesCellInfo>> = Vec::new();
|
let mut result: Vec<Vec<BoundariesCellInfo>> = Vec::new();
|
||||||
|
|
||||||
@@ -671,8 +669,8 @@ fn parse_week_boundaries(
|
|||||||
};
|
};
|
||||||
|
|
||||||
let day_boundaries = parse_day_boundaries(
|
let day_boundaries = parse_day_boundaries(
|
||||||
&worksheet,
|
worksheet,
|
||||||
day_markup.date.clone(),
|
day_markup.date,
|
||||||
(day_markup.row, end_row),
|
(day_markup.row, end_row),
|
||||||
lesson_time_column,
|
lesson_time_column,
|
||||||
)?;
|
)?;
|
||||||
@@ -698,7 +696,7 @@ fn convert_groups_to_teachers(
|
|||||||
.map(|day| Day {
|
.map(|day| Day {
|
||||||
name: day.name.clone(),
|
name: day.name.clone(),
|
||||||
street: day.street.clone(),
|
street: day.street.clone(),
|
||||||
date: day.date.clone(),
|
date: day.date,
|
||||||
lessons: vec![],
|
lessons: vec![],
|
||||||
})
|
})
|
||||||
.collect();
|
.collect();
|
||||||
@@ -774,19 +772,6 @@ fn convert_groups_to_teachers(
|
|||||||
/// * `buffer`: XLS data containing schedule.
|
/// * `buffer`: XLS data containing schedule.
|
||||||
///
|
///
|
||||||
/// returns: Result<ParseResult, crate::parser::error::Error>
|
/// 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> {
|
pub fn parse_xls(buffer: &Vec<u8>) -> Result<ParsedSchedule, crate::parser::error::Error> {
|
||||||
let cursor = Cursor::new(&buffer);
|
let cursor = Cursor::new(&buffer);
|
||||||
let mut workbook: Xls<_> =
|
let mut workbook: Xls<_> =
|
||||||
@@ -800,7 +785,7 @@ pub fn parse_xls(buffer: &Vec<u8>) -> Result<ParsedSchedule, crate::parser::erro
|
|||||||
.clone();
|
.clone();
|
||||||
|
|
||||||
let worksheet_merges = workbook
|
let worksheet_merges = workbook
|
||||||
.worksheet_merge_cells(&*worksheet_name)
|
.worksheet_merge_cells(&worksheet_name)
|
||||||
.ok_or(error::Error::NoWorkSheets)?;
|
.ok_or(error::Error::NoWorkSheets)?;
|
||||||
|
|
||||||
WorkSheet {
|
WorkSheet {
|
||||||
@@ -820,7 +805,7 @@ pub fn parse_xls(buffer: &Vec<u8>) -> Result<ParsedSchedule, crate::parser::erro
|
|||||||
days: Vec::new(),
|
days: Vec::new(),
|
||||||
};
|
};
|
||||||
|
|
||||||
for day_index in 0..(&week_markup).len() {
|
for day_index in 0..week_markup.len() {
|
||||||
let day_markup = &week_markup[day_index];
|
let day_markup = &week_markup[day_index];
|
||||||
|
|
||||||
let mut day = Day {
|
let mut day = Day {
|
||||||
@@ -836,8 +821,8 @@ pub fn parse_xls(buffer: &Vec<u8>) -> Result<ParsedSchedule, crate::parser::erro
|
|||||||
match &mut parse_lesson(
|
match &mut parse_lesson(
|
||||||
&worksheet,
|
&worksheet,
|
||||||
&day,
|
&day,
|
||||||
&day_boundaries,
|
day_boundaries,
|
||||||
&lesson_boundaries,
|
lesson_boundaries,
|
||||||
group_markup.column,
|
group_markup.column,
|
||||||
)? {
|
)? {
|
||||||
Lessons(lesson) => day.lessons.append(lesson),
|
Lessons(lesson) => day.lessons.append(lesson),
|
||||||
|
|||||||
@@ -46,14 +46,17 @@ pub mod error {
|
|||||||
/// problems with the Yandex Cloud Function invocation.
|
/// problems with the Yandex Cloud Function invocation.
|
||||||
#[display("An error occurred during the request to the Yandex Cloud API: {_0}")]
|
#[display("An error occurred during the request to the Yandex Cloud API: {_0}")]
|
||||||
RequestFailed(reqwest::Error),
|
RequestFailed(reqwest::Error),
|
||||||
|
|
||||||
|
#[display("Unable to fetch Uri in 3 retries")]
|
||||||
|
UriFetchFailed,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Errors that may occur during the creation of a schedule snapshot.
|
/// Errors that may occur during the creation of a schedule snapshot.
|
||||||
#[derive(Debug, Display, Error)]
|
#[derive(Debug, Display, Error)]
|
||||||
pub enum SnapshotCreationError {
|
pub enum SnapshotCreationError {
|
||||||
/// The URL is the same as the one already being used (no update needed).
|
/// The ETag is the same (no update needed).
|
||||||
#[display("The URL is the same as the one already being used.")]
|
#[display("The ETag is the same.")]
|
||||||
SameUrl,
|
Same,
|
||||||
|
|
||||||
/// The URL query for the XLS file failed to execute, either due to network issues or invalid API parameters.
|
/// 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}")]
|
#[display("Failed to fetch URL: {_0}")]
|
||||||
@@ -86,11 +89,7 @@ impl Updater {
|
|||||||
downloader: &mut XlsDownloader,
|
downloader: &mut XlsDownloader,
|
||||||
url: String,
|
url: String,
|
||||||
) -> Result<ScheduleSnapshot, SnapshotCreationError> {
|
) -> Result<ScheduleSnapshot, SnapshotCreationError> {
|
||||||
if downloader.url.as_ref().is_some_and(|_url| _url.eq(&url)) {
|
let head_result = downloader.set_url(&url).await.map_err(|error| {
|
||||||
return Err(SnapshotCreationError::SameUrl);
|
|
||||||
}
|
|
||||||
|
|
||||||
let head_result = downloader.set_url(&*url).await.map_err(|error| {
|
|
||||||
if let FetchError::Unknown(error) = &error {
|
if let FetchError::Unknown(error) = &error {
|
||||||
sentry::capture_error(&error);
|
sentry::capture_error(&error);
|
||||||
}
|
}
|
||||||
@@ -98,6 +97,10 @@ impl Updater {
|
|||||||
SnapshotCreationError::FetchFailed(error)
|
SnapshotCreationError::FetchFailed(error)
|
||||||
})?;
|
})?;
|
||||||
|
|
||||||
|
if downloader.etag == Some(head_result.etag) {
|
||||||
|
return Err(SnapshotCreationError::Same);
|
||||||
|
}
|
||||||
|
|
||||||
let xls_data = downloader
|
let xls_data = downloader
|
||||||
.fetch(false)
|
.fetch(false)
|
||||||
.await
|
.await
|
||||||
@@ -144,18 +147,43 @@ impl Updater {
|
|||||||
async fn query_url(api_key: &str, func_id: &str) -> Result<String, QueryUrlError> {
|
async fn query_url(api_key: &str, func_id: &str) -> Result<String, QueryUrlError> {
|
||||||
let client = reqwest::Client::new();
|
let client = reqwest::Client::new();
|
||||||
|
|
||||||
let uri = client
|
let uri = {
|
||||||
.post(format!(
|
// вот бы добавили named-scopes как в котлине,
|
||||||
"https://functions.yandexcloud.net/{}?integration=raw",
|
// чтоб мне не пришлось такой хуйнёй страдать.
|
||||||
func_id
|
#[allow(unused_assignments)]
|
||||||
))
|
let mut uri = String::new();
|
||||||
.header("Authorization", format!("Api-Key {}", api_key))
|
let mut counter = 0;
|
||||||
.send()
|
|
||||||
.await
|
loop {
|
||||||
.map_err(|error| QueryUrlError::RequestFailed(error))?
|
if counter == 3 {
|
||||||
.text()
|
return Err(QueryUrlError::UriFetchFailed);
|
||||||
.await
|
}
|
||||||
.map_err(|error| QueryUrlError::RequestFailed(error))?;
|
|
||||||
|
counter += 1;
|
||||||
|
|
||||||
|
uri = client
|
||||||
|
.post(format!(
|
||||||
|
"https://functions.yandexcloud.net/{}?integration=raw",
|
||||||
|
func_id
|
||||||
|
))
|
||||||
|
.header("Authorization", format!("Api-Key {}", api_key))
|
||||||
|
.send()
|
||||||
|
.await
|
||||||
|
.map_err(QueryUrlError::RequestFailed)?
|
||||||
|
.text()
|
||||||
|
.await
|
||||||
|
.map_err(QueryUrlError::RequestFailed)?;
|
||||||
|
|
||||||
|
if uri.is_empty() {
|
||||||
|
log::warn!("[{}] Unable to get uri! Retrying in 5 seconds...", counter);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
uri
|
||||||
|
};
|
||||||
|
|
||||||
Ok(format!("https://politehnikum-eng.ru{}", uri.trim()))
|
Ok(format!("https://politehnikum-eng.ru{}", uri.trim()))
|
||||||
}
|
}
|
||||||
@@ -196,7 +224,7 @@ impl Updater {
|
|||||||
log::info!("Obtaining a link using FaaS...");
|
log::info!("Obtaining a link using FaaS...");
|
||||||
Self::query_url(yandex_api_key, yandex_func_id)
|
Self::query_url(yandex_api_key, yandex_func_id)
|
||||||
.await
|
.await
|
||||||
.map_err(|error| Error::QueryUrlFailed(error))?
|
.map_err(Error::QueryUrlFailed)?
|
||||||
}
|
}
|
||||||
_ => unreachable!(),
|
_ => unreachable!(),
|
||||||
};
|
};
|
||||||
@@ -205,7 +233,7 @@ impl Updater {
|
|||||||
|
|
||||||
let snapshot = Self::new_snapshot(&mut this.downloader, url)
|
let snapshot = Self::new_snapshot(&mut this.downloader, url)
|
||||||
.await
|
.await
|
||||||
.map_err(|error| Error::SnapshotCreationFailed(error))?;
|
.map_err(Error::SnapshotCreationFailed)?;
|
||||||
|
|
||||||
log::info!("Schedule snapshot successfully created!");
|
log::info!("Schedule snapshot successfully created!");
|
||||||
|
|
||||||
@@ -243,13 +271,13 @@ impl Updater {
|
|||||||
yandex_func_id,
|
yandex_func_id,
|
||||||
} => Self::query_url(yandex_api_key.as_str(), yandex_func_id.as_str())
|
} => Self::query_url(yandex_api_key.as_str(), yandex_func_id.as_str())
|
||||||
.await
|
.await
|
||||||
.map_err(|error| Error::QueryUrlFailed(error))?,
|
.map_err(Error::QueryUrlFailed)?,
|
||||||
_ => unreachable!(),
|
_ => unreachable!(),
|
||||||
};
|
};
|
||||||
|
|
||||||
let snapshot = match Self::new_snapshot(&mut self.downloader, url).await {
|
let snapshot = match Self::new_snapshot(&mut self.downloader, url).await {
|
||||||
Ok(snapshot) => snapshot,
|
Ok(snapshot) => snapshot,
|
||||||
Err(SnapshotCreationError::SameUrl) => {
|
Err(SnapshotCreationError::Same) => {
|
||||||
let mut clone = current_snapshot.clone();
|
let mut clone = current_snapshot.clone();
|
||||||
clone.update();
|
clone.update();
|
||||||
|
|
||||||
|
|||||||
@@ -66,25 +66,30 @@ pub struct FetchOk {
|
|||||||
/// Date data received.
|
/// Date data received.
|
||||||
pub requested_at: DateTime<Utc>,
|
pub requested_at: DateTime<Utc>,
|
||||||
|
|
||||||
|
/// Etag.
|
||||||
|
pub etag: String,
|
||||||
|
|
||||||
/// File data.
|
/// File data.
|
||||||
pub data: Option<Vec<u8>>,
|
pub data: Option<Vec<u8>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl FetchOk {
|
impl FetchOk {
|
||||||
/// Result without file content.
|
/// Result without file content.
|
||||||
pub fn head(uploaded_at: DateTime<Utc>) -> Self {
|
pub fn head(uploaded_at: DateTime<Utc>, etag: String) -> Self {
|
||||||
FetchOk {
|
FetchOk {
|
||||||
uploaded_at,
|
uploaded_at,
|
||||||
requested_at: Utc::now(),
|
requested_at: Utc::now(),
|
||||||
|
etag,
|
||||||
data: None,
|
data: None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Full result.
|
/// Full result.
|
||||||
pub fn get(uploaded_at: DateTime<Utc>, data: Vec<u8>) -> Self {
|
pub fn get(uploaded_at: DateTime<Utc>, etag: String, data: Vec<u8>) -> Self {
|
||||||
FetchOk {
|
FetchOk {
|
||||||
uploaded_at,
|
uploaded_at,
|
||||||
requested_at: Utc::now(),
|
requested_at: Utc::now(),
|
||||||
|
etag,
|
||||||
data: Some(data),
|
data: Some(data),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -94,11 +99,15 @@ pub type FetchResult = Result<FetchOk, FetchError>;
|
|||||||
|
|
||||||
pub struct XlsDownloader {
|
pub struct XlsDownloader {
|
||||||
pub url: Option<String>,
|
pub url: Option<String>,
|
||||||
|
pub etag: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl XlsDownloader {
|
impl XlsDownloader {
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
XlsDownloader { url: None }
|
XlsDownloader {
|
||||||
|
url: None,
|
||||||
|
etag: None,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn fetch_specified(url: &str, head: bool) -> FetchResult {
|
async fn fetch_specified(url: &str, head: bool) -> FetchResult {
|
||||||
@@ -124,9 +133,12 @@ impl XlsDownloader {
|
|||||||
.get("Content-Type")
|
.get("Content-Type")
|
||||||
.ok_or(FetchError::bad_headers("Content-Type"))?;
|
.ok_or(FetchError::bad_headers("Content-Type"))?;
|
||||||
|
|
||||||
if !headers.contains_key("etag") {
|
let etag = headers
|
||||||
return Err(FetchError::bad_headers("etag"));
|
.get("etag")
|
||||||
}
|
.ok_or(FetchError::bad_headers("etag"))?
|
||||||
|
.to_str()
|
||||||
|
.or(Err(FetchError::bad_headers("etag")))?
|
||||||
|
.to_string();
|
||||||
|
|
||||||
let last_modified = headers
|
let last_modified = headers
|
||||||
.get("last-modified")
|
.get("last-modified")
|
||||||
@@ -136,14 +148,18 @@ impl XlsDownloader {
|
|||||||
return Err(FetchError::bad_content_type(content_type.to_str().unwrap()));
|
return Err(FetchError::bad_content_type(content_type.to_str().unwrap()));
|
||||||
}
|
}
|
||||||
|
|
||||||
let last_modified = DateTime::parse_from_rfc2822(&last_modified.to_str().unwrap())
|
let last_modified = DateTime::parse_from_rfc2822(last_modified.to_str().unwrap())
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.with_timezone(&Utc);
|
.with_timezone(&Utc);
|
||||||
|
|
||||||
Ok(if head {
|
Ok(if head {
|
||||||
FetchOk::head(last_modified)
|
FetchOk::head(last_modified, etag)
|
||||||
} else {
|
} else {
|
||||||
FetchOk::get(last_modified, response.bytes().await.unwrap().to_vec())
|
FetchOk::get(
|
||||||
|
last_modified,
|
||||||
|
etag,
|
||||||
|
response.bytes().await.unwrap().to_vec(),
|
||||||
|
)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -151,14 +167,14 @@ impl XlsDownloader {
|
|||||||
if self.url.is_none() {
|
if self.url.is_none() {
|
||||||
Err(FetchError::NoUrlProvided)
|
Err(FetchError::NoUrlProvided)
|
||||||
} else {
|
} else {
|
||||||
Self::fetch_specified(&*self.url.as_ref().unwrap(), head).await
|
Self::fetch_specified(self.url.as_ref().unwrap(), head).await
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn set_url(&mut self, url: &str) -> FetchResult {
|
pub async fn set_url(&mut self, url: &str) -> FetchResult {
|
||||||
let result = Self::fetch_specified(url, true).await;
|
let result = Self::fetch_specified(url, true).await;
|
||||||
|
|
||||||
if let Ok(_) = result {
|
if result.is_ok() {
|
||||||
self.url = Some(url.to_string());
|
self.url = Some(url.to_string());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -11,7 +11,6 @@ use database::query::Query;
|
|||||||
use derive_more::Display;
|
use derive_more::Display;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use std::fmt::Debug;
|
use std::fmt::Debug;
|
||||||
use std::ops::Deref;
|
|
||||||
|
|
||||||
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, Display, MiddlewareError)]
|
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, Display, MiddlewareError)]
|
||||||
#[status_code = "actix_web::http::StatusCode::UNAUTHORIZED"]
|
#[status_code = "actix_web::http::StatusCode::UNAUTHORIZED"]
|
||||||
|
|||||||
@@ -5,7 +5,6 @@ use std::future::{Ready, ready};
|
|||||||
use std::ops;
|
use std::ops;
|
||||||
|
|
||||||
/// # Async extractor.
|
/// # Async extractor.
|
||||||
|
|
||||||
/// Asynchronous object extractor from a query.
|
/// Asynchronous object extractor from a query.
|
||||||
pub struct AsyncExtractor<T>(T);
|
pub struct AsyncExtractor<T>(T);
|
||||||
|
|
||||||
@@ -80,7 +79,6 @@ impl<T: FromRequestAsync> FromRequest for AsyncExtractor<T> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// # Sync extractor.
|
/// # Sync extractor.
|
||||||
|
|
||||||
/// Synchronous object extractor from a query.
|
/// Synchronous object extractor from a query.
|
||||||
pub struct SyncExtractor<T>(T);
|
pub struct SyncExtractor<T>(T);
|
||||||
|
|
||||||
|
|||||||
@@ -1,25 +1,20 @@
|
|||||||
use crate::extractors::authorized_user;
|
use crate::extractors::authorized_user;
|
||||||
use crate::extractors::base::FromRequestAsync;
|
use crate::extractors::base::FromRequestAsync;
|
||||||
use actix_web::body::{BoxBody, EitherBody};
|
use actix_web::body::{BoxBody, EitherBody};
|
||||||
use actix_web::dev::{Payload, Service, ServiceRequest, ServiceResponse, Transform, forward_ready};
|
use actix_web::dev::{forward_ready, Payload, Service, ServiceRequest, ServiceResponse, Transform};
|
||||||
use actix_web::{Error, HttpRequest, ResponseError};
|
use actix_web::{Error, HttpRequest, ResponseError};
|
||||||
use futures_util::future::LocalBoxFuture;
|
|
||||||
use std::future::{Ready, ready};
|
|
||||||
use std::rc::Rc;
|
|
||||||
use database::entity::User;
|
use database::entity::User;
|
||||||
|
use futures_util::future::LocalBoxFuture;
|
||||||
|
use std::future::{ready, Ready};
|
||||||
|
use std::rc::Rc;
|
||||||
|
|
||||||
/// Middleware guard working with JWT tokens.
|
/// Middleware guard working with JWT tokens.
|
||||||
|
#[derive(Default)]
|
||||||
pub struct JWTAuthorization {
|
pub struct JWTAuthorization {
|
||||||
/// List of ignored endpoints.
|
/// List of ignored endpoints.
|
||||||
pub ignore: &'static [&'static str],
|
pub ignore: &'static [&'static str],
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for JWTAuthorization {
|
|
||||||
fn default() -> Self {
|
|
||||||
Self { ignore: &[] }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<S, B> Transform<S, ServiceRequest> for JWTAuthorization
|
impl<S, B> Transform<S, ServiceRequest> for JWTAuthorization
|
||||||
where
|
where
|
||||||
S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = Error> + 'static,
|
S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = Error> + 'static,
|
||||||
@@ -70,8 +65,8 @@ where
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Some(other) = path.as_bytes().iter().nth(ignore.len()) {
|
if let Some(other) = path.as_bytes().get(ignore.len()) {
|
||||||
return ['?' as u8, '/' as u8].contains(other);
|
return [b'?', b'/'].contains(other);
|
||||||
}
|
}
|
||||||
|
|
||||||
true
|
true
|
||||||
|
|||||||
@@ -1,10 +1,10 @@
|
|||||||
use actix_web::Error;
|
|
||||||
use actix_web::body::{BoxBody, EitherBody};
|
use actix_web::body::{BoxBody, EitherBody};
|
||||||
use actix_web::dev::{Service, ServiceRequest, ServiceResponse, Transform, forward_ready};
|
use actix_web::dev::{forward_ready, Service, ServiceRequest, ServiceResponse, Transform};
|
||||||
use actix_web::http::header;
|
use actix_web::http::header;
|
||||||
use actix_web::http::header::HeaderValue;
|
use actix_web::http::header::HeaderValue;
|
||||||
|
use actix_web::Error;
|
||||||
use futures_util::future::LocalBoxFuture;
|
use futures_util::future::LocalBoxFuture;
|
||||||
use std::future::{Ready, ready};
|
use std::future::{ready, Ready};
|
||||||
|
|
||||||
/// Middleware to specify the encoding in the Content-Type header.
|
/// Middleware to specify the encoding in the Content-Type header.
|
||||||
pub struct ContentTypeBootstrap;
|
pub struct ContentTypeBootstrap;
|
||||||
@@ -30,7 +30,7 @@ pub struct ContentTypeMiddleware<S> {
|
|||||||
service: S,
|
service: S,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, S, B> Service<ServiceRequest> for ContentTypeMiddleware<S>
|
impl<S, B> Service<ServiceRequest> for ContentTypeMiddleware<S>
|
||||||
where
|
where
|
||||||
S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = Error>,
|
S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = Error>,
|
||||||
S::Future: 'static,
|
S::Future: 'static,
|
||||||
@@ -49,13 +49,14 @@ where
|
|||||||
let mut response = fut.await?;
|
let mut response = fut.await?;
|
||||||
|
|
||||||
let headers = response.response_mut().headers_mut();
|
let headers = response.response_mut().headers_mut();
|
||||||
if let Some(content_type) = headers.get("Content-Type") {
|
|
||||||
if content_type == "application/json" {
|
if let Some(content_type) = headers.get("Content-Type")
|
||||||
headers.insert(
|
&& content_type == "application/json"
|
||||||
header::CONTENT_TYPE,
|
{
|
||||||
HeaderValue::from_static("application/json; charset=utf8"),
|
headers.insert(
|
||||||
);
|
header::CONTENT_TYPE,
|
||||||
}
|
HeaderValue::from_static("application/json; charset=utf8"),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(response.map_into_left_body())
|
Ok(response.map_into_left_body())
|
||||||
|
|||||||
@@ -2,16 +2,6 @@ use jsonwebtoken::errors::ErrorKind;
|
|||||||
use jsonwebtoken::{Algorithm, DecodingKey, Validation, decode};
|
use jsonwebtoken::{Algorithm, DecodingKey, Validation, decode};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
#[derive(Deserialize, Serialize)]
|
|
||||||
struct TokenData {
|
|
||||||
iis: String,
|
|
||||||
sub: i32,
|
|
||||||
app: i32,
|
|
||||||
exp: i32,
|
|
||||||
iat: i32,
|
|
||||||
jti: i32,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, Serialize, Deserialize)]
|
#[derive(Debug, Serialize, Deserialize)]
|
||||||
struct Claims {
|
struct Claims {
|
||||||
sub: i32,
|
sub: i32,
|
||||||
@@ -22,7 +12,7 @@ struct Claims {
|
|||||||
|
|
||||||
#[derive(Debug, PartialEq)]
|
#[derive(Debug, PartialEq)]
|
||||||
pub enum Error {
|
pub enum Error {
|
||||||
JwtError(ErrorKind),
|
Jwt(ErrorKind),
|
||||||
InvalidSignature,
|
InvalidSignature,
|
||||||
InvalidToken,
|
InvalidToken,
|
||||||
Expired,
|
Expired,
|
||||||
@@ -49,10 +39,10 @@ const VK_PUBLIC_KEY: &str = concat!(
|
|||||||
"-----END PUBLIC KEY-----"
|
"-----END PUBLIC KEY-----"
|
||||||
);
|
);
|
||||||
|
|
||||||
pub fn parse_vk_id(token_str: &String, client_id: i32) -> Result<i32, Error> {
|
pub fn parse_vk_id(token_str: &str, client_id: i32) -> Result<i32, Error> {
|
||||||
let dkey = DecodingKey::from_rsa_pem(VK_PUBLIC_KEY.as_bytes()).unwrap();
|
let dkey = DecodingKey::from_rsa_pem(VK_PUBLIC_KEY.as_bytes()).unwrap();
|
||||||
|
|
||||||
match decode::<Claims>(&token_str, &dkey, &Validation::new(Algorithm::RS256)) {
|
match decode::<Claims>(token_str, &dkey, &Validation::new(Algorithm::RS256)) {
|
||||||
Ok(token_data) => {
|
Ok(token_data) => {
|
||||||
let claims = token_data.claims;
|
let claims = token_data.claims;
|
||||||
|
|
||||||
@@ -77,7 +67,7 @@ pub fn parse_vk_id(token_str: &String, client_id: i32) -> Result<i32, Error> {
|
|||||||
ErrorKind::Base64(_) => Error::InvalidToken,
|
ErrorKind::Base64(_) => Error::InvalidToken,
|
||||||
ErrorKind::Json(_) => Error::InvalidToken,
|
ErrorKind::Json(_) => Error::InvalidToken,
|
||||||
ErrorKind::Utf8(_) => Error::InvalidToken,
|
ErrorKind::Utf8(_) => Error::InvalidToken,
|
||||||
kind => Error::JwtError(kind),
|
kind => Error::Jwt(kind),
|
||||||
}),
|
}),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -28,7 +28,7 @@ async fn sign_in_combined(
|
|||||||
return Err(ErrorCode::IncorrectCredentials);
|
return Err(ErrorCode::IncorrectCredentials);
|
||||||
}
|
}
|
||||||
|
|
||||||
match bcrypt::verify(&data.password, &user.password.as_ref().unwrap()) {
|
match bcrypt::verify(&data.password, user.password.as_ref().unwrap()) {
|
||||||
Ok(result) => {
|
Ok(result) => {
|
||||||
if !result {
|
if !result {
|
||||||
return Err(ErrorCode::IncorrectCredentials);
|
return Err(ErrorCode::IncorrectCredentials);
|
||||||
@@ -124,8 +124,6 @@ mod schema {
|
|||||||
InvalidVkAccessToken,
|
InvalidVkAccessToken,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Internal
|
|
||||||
|
|
||||||
/// Type of authorization.
|
/// Type of authorization.
|
||||||
pub enum SignInData {
|
pub enum SignInData {
|
||||||
/// User and password name and password.
|
/// User and password name and password.
|
||||||
@@ -187,7 +185,7 @@ mod tests {
|
|||||||
id: Set(id.clone()),
|
id: Set(id.clone()),
|
||||||
username: Set(username),
|
username: Set(username),
|
||||||
password: Set(Some(
|
password: Set(Some(
|
||||||
bcrypt::hash("example".to_string(), bcrypt::DEFAULT_COST).unwrap(),
|
bcrypt::hash("example", bcrypt::DEFAULT_COST).unwrap(),
|
||||||
)),
|
)),
|
||||||
vk_id: Set(None),
|
vk_id: Set(None),
|
||||||
telegram_id: Set(None),
|
telegram_id: Set(None),
|
||||||
|
|||||||
@@ -8,7 +8,6 @@ use database::entity::sea_orm_active_enums::UserRole;
|
|||||||
use database::entity::ActiveUser;
|
use database::entity::ActiveUser;
|
||||||
use database::query::Query;
|
use database::query::Query;
|
||||||
use database::sea_orm::ActiveModelTrait;
|
use database::sea_orm::ActiveModelTrait;
|
||||||
use std::ops::Deref;
|
|
||||||
use web::Json;
|
use web::Json;
|
||||||
|
|
||||||
async fn sign_up_combined(
|
async fn sign_up_combined(
|
||||||
@@ -42,13 +41,12 @@ async fn sign_up_combined(
|
|||||||
}
|
}
|
||||||
|
|
||||||
// If user with specified VKID already exists.
|
// If user with specified VKID already exists.
|
||||||
if let Some(id) = data.vk_id {
|
if let Some(id) = data.vk_id
|
||||||
if Query::find_user_by_vk_id(db, id)
|
&& Query::is_user_exists_by_vk_id(db, id)
|
||||||
.await
|
.await
|
||||||
.is_ok_and(|user| user.is_some())
|
.expect("Failed to check user existence")
|
||||||
{
|
{
|
||||||
return Err(ErrorCode::VkAlreadyExists);
|
return Err(ErrorCode::VkAlreadyExists);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let active_user: ActiveUser = data.into();
|
let active_user: ActiveUser = data.into();
|
||||||
@@ -202,8 +200,6 @@ mod schema {
|
|||||||
VkAlreadyExists,
|
VkAlreadyExists,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Internal
|
|
||||||
|
|
||||||
/// Data for registration.
|
/// Data for registration.
|
||||||
pub struct SignUpData {
|
pub struct SignUpData {
|
||||||
// TODO: сделать ограничение на минимальную и максимальную длину при регистрации и смене.
|
// TODO: сделать ограничение на минимальную и максимальную длину при регистрации и смене.
|
||||||
@@ -228,21 +224,21 @@ mod schema {
|
|||||||
pub version: String,
|
pub version: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Into<ActiveUser> for SignUpData {
|
impl From<SignUpData> for ActiveUser {
|
||||||
fn into(self) -> ActiveUser {
|
fn from(value: SignUpData) -> Self {
|
||||||
assert_ne!(self.password.is_some(), self.vk_id.is_some());
|
assert_ne!(value.password.is_some(), value.vk_id.is_some());
|
||||||
|
|
||||||
ActiveUser {
|
ActiveUser {
|
||||||
id: Set(ObjectId::new().unwrap().to_string()),
|
id: Set(ObjectId::new().unwrap().to_string()),
|
||||||
username: Set(self.username),
|
username: Set(value.username),
|
||||||
password: Set(self
|
password: Set(value
|
||||||
.password
|
.password
|
||||||
.map(|x| bcrypt::hash(x, bcrypt::DEFAULT_COST).unwrap())),
|
.map(|x| bcrypt::hash(x, bcrypt::DEFAULT_COST).unwrap())),
|
||||||
vk_id: Set(self.vk_id),
|
vk_id: Set(value.vk_id),
|
||||||
telegram_id: Set(None),
|
telegram_id: Set(None),
|
||||||
group: Set(Some(self.group)),
|
group: Set(Some(value.group)),
|
||||||
role: Set(self.role),
|
role: Set(value.role),
|
||||||
android_version: Set(Some(self.version)),
|
android_version: Set(Some(value.version)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -262,7 +258,6 @@ mod tests {
|
|||||||
use database::entity::{UserColumn, UserEntity};
|
use database::entity::{UserColumn, UserEntity};
|
||||||
use database::sea_orm::ColumnTrait;
|
use database::sea_orm::ColumnTrait;
|
||||||
use database::sea_orm::{EntityTrait, QueryFilter};
|
use database::sea_orm::{EntityTrait, QueryFilter};
|
||||||
use std::ops::Deref;
|
|
||||||
|
|
||||||
struct SignUpPartial<'a> {
|
struct SignUpPartial<'a> {
|
||||||
username: &'a str,
|
username: &'a str,
|
||||||
|
|||||||
@@ -9,7 +9,6 @@ use database::entity::ActiveUser;
|
|||||||
use database::query::Query;
|
use database::query::Query;
|
||||||
use database::sea_orm::{ActiveModelTrait, Set};
|
use database::sea_orm::{ActiveModelTrait, Set};
|
||||||
use objectid::ObjectId;
|
use objectid::ObjectId;
|
||||||
use std::ops::Deref;
|
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use web::Json;
|
use web::Json;
|
||||||
|
|
||||||
@@ -50,25 +49,29 @@ pub async fn telegram_auth(
|
|||||||
let web_app_user =
|
let web_app_user =
|
||||||
serde_json::from_str::<WebAppUser>(init_data.data_map.get("user").unwrap()).unwrap();
|
serde_json::from_str::<WebAppUser>(init_data.data_map.get("user").unwrap()).unwrap();
|
||||||
|
|
||||||
let user =
|
let user = match Query::find_user_by_telegram_id(app_state.get_database(), web_app_user.id)
|
||||||
match Query::find_user_by_telegram_id(app_state.get_database(), web_app_user.id).await {
|
.await
|
||||||
Ok(Some(value)) => Ok(value),
|
.expect("Failed to find user by telegram id")
|
||||||
_ => {
|
{
|
||||||
let new_user = ActiveUser {
|
Some(value) => value,
|
||||||
id: Set(ObjectId::new().unwrap().to_string()),
|
None => {
|
||||||
username: Set(format!("telegram_{}", web_app_user.id)), // можно оставить, а можно поменять
|
let new_user = ActiveUser {
|
||||||
password: Set(None), // ибо нехуй
|
id: Set(ObjectId::new().unwrap().to_string()),
|
||||||
vk_id: Set(None),
|
username: Set(format!("telegram_{}", web_app_user.id)), // можно оставить, а можно поменять
|
||||||
telegram_id: Set(Some(web_app_user.id)),
|
password: Set(None), // ибо нехуй
|
||||||
group: Set(None),
|
vk_id: Set(None),
|
||||||
role: Set(UserRole::Student), // TODO: при реге проверять данные
|
telegram_id: Set(Some(web_app_user.id)),
|
||||||
android_version: Set(None),
|
group: Set(None),
|
||||||
};
|
role: Set(UserRole::Student), // TODO: при реге проверять данные
|
||||||
|
android_version: Set(None),
|
||||||
|
};
|
||||||
|
|
||||||
new_user.insert(app_state.get_database()).await
|
new_user
|
||||||
}
|
.insert(app_state.get_database())
|
||||||
|
.await
|
||||||
|
.expect("Failed to insert user")
|
||||||
}
|
}
|
||||||
.expect("Failed to get or add user");
|
};
|
||||||
|
|
||||||
let access_token = utility::jwt::encode(&user.id);
|
let access_token = utility::jwt::encode(&user.id);
|
||||||
Ok(Response::new(&access_token, user.group.is_some())).into()
|
Ok(Response::new(&access_token, user.group.is_some())).into()
|
||||||
@@ -122,7 +125,7 @@ mod schema {
|
|||||||
&mut self,
|
&mut self,
|
||||||
request: &HttpRequest,
|
request: &HttpRequest,
|
||||||
response: &mut HttpResponse<EitherBody<String>>,
|
response: &mut HttpResponse<EitherBody<String>>,
|
||||||
) -> () {
|
) {
|
||||||
let access_token = &self.access_token;
|
let access_token = &self.access_token;
|
||||||
|
|
||||||
let app_state = request.app_data::<web::Data<AppState>>().unwrap();
|
let app_state = request.app_data::<web::Data<AppState>>().unwrap();
|
||||||
|
|||||||
@@ -6,7 +6,6 @@ use actix_web::{post, web};
|
|||||||
use database::entity::User;
|
use database::entity::User;
|
||||||
use database::query::Query;
|
use database::query::Query;
|
||||||
use database::sea_orm::{ActiveModelTrait, IntoActiveModel, Set};
|
use database::sea_orm::{ActiveModelTrait, IntoActiveModel, Set};
|
||||||
use std::ops::Deref;
|
|
||||||
use web::Json;
|
use web::Json;
|
||||||
|
|
||||||
#[utoipa::path(responses(
|
#[utoipa::path(responses(
|
||||||
|
|||||||
@@ -1,11 +1,17 @@
|
|||||||
use crate::AppState;
|
|
||||||
use crate::routes::schedule::schema::CacheStatus;
|
use crate::routes::schedule::schema::CacheStatus;
|
||||||
|
use crate::AppState;
|
||||||
use actix_web::{get, web};
|
use actix_web::{get, web};
|
||||||
|
use std::ops::Deref;
|
||||||
|
|
||||||
#[utoipa::path(responses(
|
#[utoipa::path(responses(
|
||||||
(status = OK, body = CacheStatus),
|
(status = OK, body = CacheStatus),
|
||||||
))]
|
))]
|
||||||
#[get("/cache-status")]
|
#[get("/cache-status")]
|
||||||
pub async fn cache_status(app_state: web::Data<AppState>) -> CacheStatus {
|
pub async fn cache_status(app_state: web::Data<AppState>) -> CacheStatus {
|
||||||
CacheStatus::from(&app_state).await.into()
|
app_state
|
||||||
|
.get_schedule_snapshot("eng_polytechnic")
|
||||||
|
.await
|
||||||
|
.unwrap()
|
||||||
|
.deref()
|
||||||
|
.into()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
mod cache_status;
|
mod cache_status;
|
||||||
mod group;
|
mod group;
|
||||||
mod group_names;
|
mod group_names;
|
||||||
mod schedule;
|
mod get;
|
||||||
mod schema;
|
mod schema;
|
||||||
mod teacher;
|
mod teacher;
|
||||||
mod teacher_names;
|
mod teacher_names;
|
||||||
@@ -9,6 +9,6 @@ mod teacher_names;
|
|||||||
pub use cache_status::*;
|
pub use cache_status::*;
|
||||||
pub use group::*;
|
pub use group::*;
|
||||||
pub use group_names::*;
|
pub use group_names::*;
|
||||||
pub use schedule::*;
|
pub use get::*;
|
||||||
pub use teacher::*;
|
pub use teacher::*;
|
||||||
pub use teacher_names::*;
|
pub use teacher_names::*;
|
||||||
|
|||||||
@@ -63,18 +63,6 @@ pub struct CacheStatus {
|
|||||||
pub updated_at: i64,
|
pub updated_at: i64,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl CacheStatus {
|
|
||||||
pub async fn from(value: &web::Data<AppState>) -> Self {
|
|
||||||
From::<&ScheduleSnapshot>::from(
|
|
||||||
value
|
|
||||||
.get_schedule_snapshot("eng_polytechnic")
|
|
||||||
.await
|
|
||||||
.unwrap()
|
|
||||||
.deref(),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From<&ScheduleSnapshot> for CacheStatus {
|
impl From<&ScheduleSnapshot> for CacheStatus {
|
||||||
fn from(value: &ScheduleSnapshot) -> Self {
|
fn from(value: &ScheduleSnapshot) -> Self {
|
||||||
Self {
|
Self {
|
||||||
|
|||||||
@@ -13,13 +13,13 @@ where
|
|||||||
E: Serialize + PartialSchema + Display + PartialErrResponse;
|
E: Serialize + PartialSchema + Display + PartialErrResponse;
|
||||||
|
|
||||||
/// Transform Response<T, E> into Result<T, E>
|
/// Transform Response<T, E> into Result<T, E>
|
||||||
impl<T, E> Into<Result<T, E>> for Response<T, E>
|
impl<T, E> From<Response<T, E>> for Result<T, E>
|
||||||
where
|
where
|
||||||
T: Serialize + PartialSchema + PartialOkResponse,
|
T: Serialize + PartialSchema + PartialOkResponse,
|
||||||
E: Serialize + PartialSchema + Display + PartialErrResponse,
|
E: Serialize + PartialSchema + Display + PartialErrResponse,
|
||||||
{
|
{
|
||||||
fn into(self) -> Result<T, E> {
|
fn from(value: Response<T, E>) -> Self {
|
||||||
self.0
|
value.0
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -46,7 +46,7 @@ where
|
|||||||
{
|
{
|
||||||
match &self.0 {
|
match &self.0 {
|
||||||
Ok(ok) => serializer.serialize_some(&ok),
|
Ok(ok) => serializer.serialize_some(&ok),
|
||||||
Err(err) => serializer.serialize_some(&ResponseError::<E>::from(err.clone().into())),
|
Err(err) => serializer.serialize_some(&err.clone().into()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -95,7 +95,7 @@ pub trait PartialOkResponse {
|
|||||||
&mut self,
|
&mut self,
|
||||||
_request: &HttpRequest,
|
_request: &HttpRequest,
|
||||||
_response: &mut HttpResponse<EitherBody<String>>,
|
_response: &mut HttpResponse<EitherBody<String>>,
|
||||||
) -> () {
|
) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -173,8 +173,8 @@ pub mod user {
|
|||||||
username: user.username.clone(),
|
username: user.username.clone(),
|
||||||
group: user.group.clone(),
|
group: user.group.clone(),
|
||||||
role: user.role.clone(),
|
role: user.role.clone(),
|
||||||
vk_id: user.vk_id.clone(),
|
vk_id: user.vk_id,
|
||||||
telegram_id: user.telegram_id.clone(),
|
telegram_id: user.telegram_id,
|
||||||
access_token: Some(access_token),
|
access_token: Some(access_token),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -188,8 +188,8 @@ pub mod user {
|
|||||||
username: user.username.clone(),
|
username: user.username.clone(),
|
||||||
group: user.group.clone(),
|
group: user.group.clone(),
|
||||||
role: user.role.clone(),
|
role: user.role.clone(),
|
||||||
vk_id: user.vk_id.clone(),
|
vk_id: user.vk_id,
|
||||||
telegram_id: user.telegram_id.clone(),
|
telegram_id: user.telegram_id,
|
||||||
access_token: None,
|
access_token: None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,7 +4,6 @@ use crate::state::AppState;
|
|||||||
use actix_web::{post, web};
|
use actix_web::{post, web};
|
||||||
use database::entity::User;
|
use database::entity::User;
|
||||||
use database::sea_orm::{ActiveModelTrait, IntoActiveModel, Set};
|
use database::sea_orm::{ActiveModelTrait, IntoActiveModel, Set};
|
||||||
use std::ops::Deref;
|
|
||||||
|
|
||||||
#[utoipa::path(responses((status = OK)))]
|
#[utoipa::path(responses((status = OK)))]
|
||||||
#[post("/change-group")]
|
#[post("/change-group")]
|
||||||
|
|||||||
@@ -5,7 +5,6 @@ use actix_web::{post, web};
|
|||||||
use database::entity::User;
|
use database::entity::User;
|
||||||
use database::query::Query;
|
use database::query::Query;
|
||||||
use database::sea_orm::{ActiveModelTrait, IntoActiveModel, Set};
|
use database::sea_orm::{ActiveModelTrait, IntoActiveModel, Set};
|
||||||
use std::ops::Deref;
|
|
||||||
|
|
||||||
#[utoipa::path(responses((status = OK)))]
|
#[utoipa::path(responses((status = OK)))]
|
||||||
#[post("/change-username")]
|
#[post("/change-username")]
|
||||||
|
|||||||
@@ -2,10 +2,12 @@ mod env;
|
|||||||
|
|
||||||
pub use crate::state::env::AppEnv;
|
pub use crate::state::env::AppEnv;
|
||||||
use actix_web::web;
|
use actix_web::web;
|
||||||
use database::sea_orm::{Database, DatabaseConnection};
|
use database::migration::{Migrator, MigratorTrait};
|
||||||
|
use database::sea_orm::{ConnectOptions, Database, DatabaseConnection};
|
||||||
use providers::base::{ScheduleProvider, ScheduleSnapshot};
|
use providers::base::{ScheduleProvider, ScheduleSnapshot};
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
use std::time::Duration;
|
||||||
use tokio_util::sync::CancellationToken;
|
use tokio_util::sync::CancellationToken;
|
||||||
|
|
||||||
/// Common data provided to endpoints.
|
/// Common data provided to endpoints.
|
||||||
@@ -23,7 +25,7 @@ impl AppState {
|
|||||||
let env = AppEnv::default();
|
let env = AppEnv::default();
|
||||||
let providers: HashMap<String, Arc<dyn ScheduleProvider>> = HashMap::from([(
|
let providers: HashMap<String, Arc<dyn ScheduleProvider>> = HashMap::from([(
|
||||||
"eng_polytechnic".to_string(),
|
"eng_polytechnic".to_string(),
|
||||||
providers::EngelsPolytechnicProvider::new({
|
providers::EngelsPolytechnicProvider::get({
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
{
|
{
|
||||||
providers::EngelsPolytechnicUpdateSource::Prepared(ScheduleSnapshot {
|
providers::EngelsPolytechnicUpdateSource::Prepared(ScheduleSnapshot {
|
||||||
@@ -55,16 +57,31 @@ impl AppState {
|
|||||||
database
|
database
|
||||||
} else {
|
} else {
|
||||||
let database_url = std::env::var("DATABASE_URL").expect("DATABASE_URL must be set");
|
let database_url = std::env::var("DATABASE_URL").expect("DATABASE_URL must be set");
|
||||||
Database::connect(&database_url)
|
|
||||||
|
let mut opt = ConnectOptions::new(database_url.clone());
|
||||||
|
|
||||||
|
opt.max_connections(4)
|
||||||
|
.min_connections(2)
|
||||||
|
.connect_timeout(Duration::from_secs(10))
|
||||||
|
.idle_timeout(Duration::from_secs(8))
|
||||||
|
.sqlx_logging(true);
|
||||||
|
|
||||||
|
let database = Database::connect(opt)
|
||||||
.await
|
.await
|
||||||
.unwrap_or_else(|_| panic!("Error connecting to {}", database_url))
|
.unwrap_or_else(|_| panic!("Error connecting to {}", database_url));
|
||||||
|
|
||||||
|
Migrator::up(&database, None)
|
||||||
|
.await
|
||||||
|
.expect("Failed to run database migrations");
|
||||||
|
|
||||||
|
database
|
||||||
},
|
},
|
||||||
env,
|
env,
|
||||||
providers,
|
providers,
|
||||||
};
|
};
|
||||||
|
|
||||||
if this.env.schedule.auto_update {
|
if this.env.schedule.auto_update {
|
||||||
for (_, provider) in &this.providers {
|
for provider in this.providers.values() {
|
||||||
let provider = provider.clone();
|
let provider = provider.clone();
|
||||||
let cancel_token = this.cancel_token.clone();
|
let cancel_token = this.cancel_token.clone();
|
||||||
|
|
||||||
@@ -93,6 +110,8 @@ impl AppState {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Create a new object web::Data<AppState>.
|
/// Create a new object web::Data<AppState>.
|
||||||
pub async fn new_app_state(database: Option<DatabaseConnection>) -> Result<web::Data<AppState>, Box<dyn std::error::Error>> {
|
pub async fn new_app_state(
|
||||||
|
database: Option<DatabaseConnection>,
|
||||||
|
) -> Result<web::Data<AppState>, Box<dyn std::error::Error>> {
|
||||||
Ok(web::Data::new(AppState::new(database).await?))
|
Ok(web::Data::new(AppState::new(database).await?))
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -24,14 +24,13 @@ static ENCODING_KEY: LazyLock<EncodingKey> = LazyLock::new(|| {
|
|||||||
});
|
});
|
||||||
|
|
||||||
/// Token verification errors.
|
/// Token verification errors.
|
||||||
#[allow(dead_code)]
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub enum Error {
|
pub enum Error {
|
||||||
/// The token has a different signature.
|
/// The token has a different signature.
|
||||||
InvalidSignature,
|
InvalidSignature,
|
||||||
|
|
||||||
/// Token reading error.
|
/// Token reading error.
|
||||||
InvalidToken(ErrorKind),
|
InvalidToken,
|
||||||
|
|
||||||
/// Token expired.
|
/// Token expired.
|
||||||
Expired,
|
Expired,
|
||||||
@@ -63,13 +62,13 @@ struct Claims {
|
|||||||
pub(crate) const DEFAULT_ALGORITHM: Algorithm = Algorithm::HS256;
|
pub(crate) const DEFAULT_ALGORITHM: Algorithm = Algorithm::HS256;
|
||||||
|
|
||||||
/// Checking the token and extracting the UUID of the user account from it.
|
/// Checking the token and extracting the UUID of the user account from it.
|
||||||
pub fn verify_and_decode(token: &String) -> Result<String, Error> {
|
pub fn verify_and_decode(token: &str) -> Result<String, Error> {
|
||||||
let mut validation = Validation::new(DEFAULT_ALGORITHM);
|
let mut validation = Validation::new(DEFAULT_ALGORITHM);
|
||||||
|
|
||||||
validation.required_spec_claims.remove("exp");
|
validation.required_spec_claims.remove("exp");
|
||||||
validation.validate_exp = false;
|
validation.validate_exp = false;
|
||||||
|
|
||||||
let result = decode::<Claims>(&token, &*DECODING_KEY, &validation);
|
let result = decode::<Claims>(token, &DECODING_KEY, &validation);
|
||||||
|
|
||||||
match result {
|
match result {
|
||||||
Ok(token_data) => {
|
Ok(token_data) => {
|
||||||
@@ -82,13 +81,13 @@ pub fn verify_and_decode(token: &String) -> Result<String, Error> {
|
|||||||
Err(err) => Err(match err.into_kind() {
|
Err(err) => Err(match err.into_kind() {
|
||||||
ErrorKind::InvalidSignature => Error::InvalidSignature,
|
ErrorKind::InvalidSignature => Error::InvalidSignature,
|
||||||
ErrorKind::ExpiredSignature => Error::Expired,
|
ErrorKind::ExpiredSignature => Error::Expired,
|
||||||
kind => Error::InvalidToken(kind),
|
_ => Error::InvalidToken,
|
||||||
}),
|
}),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Creating a user token.
|
/// Creating a user token.
|
||||||
pub fn encode(id: &String) -> String {
|
pub fn encode(id: &str) -> String {
|
||||||
let header = Header {
|
let header = Header {
|
||||||
typ: Some(String::from("JWT")),
|
typ: Some(String::from("JWT")),
|
||||||
..Default::default()
|
..Default::default()
|
||||||
@@ -98,12 +97,12 @@ pub fn encode(id: &String) -> String {
|
|||||||
let exp = iat + Duration::days(365 * 4);
|
let exp = iat + Duration::days(365 * 4);
|
||||||
|
|
||||||
let claims = Claims {
|
let claims = Claims {
|
||||||
id: id.clone(),
|
id: id.to_string(),
|
||||||
iat: iat.timestamp().unsigned_abs(),
|
iat: iat.timestamp().unsigned_abs(),
|
||||||
exp: exp.timestamp().unsigned_abs(),
|
exp: exp.timestamp().unsigned_abs(),
|
||||||
};
|
};
|
||||||
|
|
||||||
jsonwebtoken::encode(&header, &claims, &*ENCODING_KEY).unwrap()
|
jsonwebtoken::encode(&header, &claims, &ENCODING_KEY).unwrap()
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
@@ -115,7 +114,7 @@ mod tests {
|
|||||||
fn test_encode() {
|
fn test_encode() {
|
||||||
test_env();
|
test_env();
|
||||||
|
|
||||||
assert_eq!(encode(&"test".to_string()).is_empty(), false);
|
assert!(!encode("test").is_empty());
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@@ -128,7 +127,7 @@ mod tests {
|
|||||||
assert!(result.is_err());
|
assert!(result.is_err());
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
result.err().unwrap(),
|
result.err().unwrap(),
|
||||||
Error::InvalidToken(ErrorKind::InvalidToken)
|
Error::InvalidToken
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -33,7 +33,7 @@ impl WebAppInitDataMap {
|
|||||||
};
|
};
|
||||||
|
|
||||||
data.split('&')
|
data.split('&')
|
||||||
.map(|kv| kv.split_once('=').unwrap_or_else(|| (kv, "")))
|
.map(|kv| kv.split_once('=').unwrap_or((kv, "")))
|
||||||
.for_each(|(key, value)| {
|
.for_each(|(key, value)| {
|
||||||
this.data_map.insert(key.to_string(), value.to_string());
|
this.data_map.insert(key.to_string(), value.to_string());
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user