mirror of
https://github.com/n08i40k/schedule-parser-rusted.git
synced 2025-12-06 17:57:47 +03:00
Compare commits
33 Commits
9f7460973e
...
release/v1
| Author | SHA1 | Date | |
|---|---|---|---|
|
fceffb900d
|
|||
|
49ce0005dc
|
|||
|
4c738085f2
|
|||
|
20602eb863
|
|||
|
e04d462223
|
|||
|
22af02464d
|
|||
|
9a517519db
|
|||
|
65376e75f7
|
|||
|
bef6163c1b
|
|||
|
283858fea3
|
|||
|
66ad4ef938
|
|||
|
28f59389ed
|
|||
|
e71ab0526d
|
|||
|
ff05614404
|
|||
|
9cc03c4ffe
|
|||
|
5068fe3069
|
|||
|
2fd6d787a0
|
|||
|
7a1b32d843
|
|||
|
542258df01
|
|||
|
ccaabfe909
|
|||
|
4c5e0761eb
|
|||
|
057dac5b09
|
|||
|
5b6f5c830f
|
|||
|
680419ea78
|
|||
|
30c985a3d7
|
|||
|
70a7480ea3
|
|||
|
1add903f36
|
|||
|
f703cc8326
|
|||
|
ab1cbd795e
|
|||
|
0316f58592
|
|||
|
a95494d3be
|
|||
|
844c89a365
|
|||
|
ba86dfc3fe
|
169
.github/workflows/release.yml
vendored
Normal file
169
.github/workflows/release.yml
vendored
Normal file
@@ -0,0 +1,169 @@
|
|||||||
|
name: release
|
||||||
|
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
tags: [ "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: |
|
||||||
|
touch .env.test
|
||||||
|
cargo test --verbose
|
||||||
|
env:
|
||||||
|
DATABASE_URL: ${{ env.TEST_DB }}
|
||||||
|
JWT_SECRET: "test-secret-at-least-256-bits-used"
|
||||||
|
VKID_CLIENT_ID: 0
|
||||||
|
VKID_REDIRECT_URI: "vk0://vk.com/blank.html"
|
||||||
|
REQWEST_USER_AGENT: "Dalvik/2.1.0 (Linux; U; Android 6.0.1; OPPO R9s Build/MMB29M)"
|
||||||
|
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 }}"
|
||||||
|
release:
|
||||||
|
name: Create GitHub Release
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
needs:
|
||||||
|
- build
|
||||||
|
- docker
|
||||||
|
# noinspection GrazieInspection,SpellCheckingInspection
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v4
|
||||||
|
with:
|
||||||
|
fetch-depth: 0
|
||||||
|
|
||||||
|
- name: Generate changelog
|
||||||
|
run: |
|
||||||
|
LAST_TAG=$(git describe --tags --abbrev=0 HEAD^)
|
||||||
|
echo "## Коммиты с прошлого релиза $LAST_TAG" > CHANGELOG.md
|
||||||
|
git log $LAST_TAG..HEAD --oneline >> CHANGELOG.md
|
||||||
|
|
||||||
|
- name: Download build artifacts
|
||||||
|
uses: actions/download-artifact@v4
|
||||||
|
with:
|
||||||
|
pattern: release-*
|
||||||
|
merge-multiple: true
|
||||||
|
|
||||||
|
- name: Create Release
|
||||||
|
id: create_release
|
||||||
|
uses: ncipollo/release-action@v1.16.0
|
||||||
|
env:
|
||||||
|
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||||
|
with:
|
||||||
|
artifacts: "${{ env.BINARY_NAME }},${{ env.BINARY_NAME }}.d"
|
||||||
|
bodyFile: CHANGELOG.md
|
||||||
17
.github/workflows/test.yml
vendored
17
.github/workflows/test.yml
vendored
@@ -1,10 +1,9 @@
|
|||||||
name: Tests
|
name: cargo test
|
||||||
|
|
||||||
on:
|
on:
|
||||||
push:
|
push:
|
||||||
branches: [ "master" ]
|
branches: [ "master" ]
|
||||||
pull_request:
|
tags-ignore: [ "release/v*" ]
|
||||||
branches: [ "master" ]
|
|
||||||
|
|
||||||
permissions:
|
permissions:
|
||||||
contents: read
|
contents: read
|
||||||
@@ -20,6 +19,14 @@ jobs:
|
|||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v4
|
- uses: actions/checkout@v4
|
||||||
- name: Build
|
- name: Build
|
||||||
run: cargo build --verbose
|
run: cargo build
|
||||||
|
- name: Create .env.test
|
||||||
|
run: touch .env.test
|
||||||
- name: Run tests
|
- name: Run tests
|
||||||
run: cargo test --verbose -p schedule-parser-rusted -p schedule_parser
|
run: cargo test
|
||||||
|
env:
|
||||||
|
DATABASE_URL: ${{ secrets.TEST_DATABASE_URL }}
|
||||||
|
JWT_SECRET: "test-secret-at-least-256-bits-used"
|
||||||
|
VKID_CLIENT_ID: 0
|
||||||
|
VKID_REDIRECT_URI: "vk0://vk.com/blank.html"
|
||||||
|
REQWEST_USER_AGENT: "Dalvik/2.1.0 (Linux; U; Android 6.0.1; OPPO R9s Build/MMB29M)"
|
||||||
3
.gitignore
vendored
3
.gitignore
vendored
@@ -3,4 +3,5 @@
|
|||||||
schedule.json
|
schedule.json
|
||||||
teachers.json
|
teachers.json
|
||||||
|
|
||||||
.env*
|
.env*
|
||||||
|
/*-firebase-adminsdk-*.json
|
||||||
|
|||||||
14
.idea/discord.xml
generated
Normal file
14
.idea/discord.xml
generated
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="DiscordProjectSettings">
|
||||||
|
<option name="show" value="PROJECT_FILES" />
|
||||||
|
<option name="description" value="" />
|
||||||
|
<option name="applicationTheme" value="default" />
|
||||||
|
<option name="iconsTheme" value="default" />
|
||||||
|
<option name="button1Title" value="" />
|
||||||
|
<option name="button1Url" value="" />
|
||||||
|
<option name="button2Title" value="" />
|
||||||
|
<option name="button2Url" value="" />
|
||||||
|
<option name="customApplicationId" value="" />
|
||||||
|
</component>
|
||||||
|
</project>
|
||||||
7
.idea/schedule-parser-rusted.iml
generated
7
.idea/schedule-parser-rusted.iml
generated
@@ -2,10 +2,15 @@
|
|||||||
<module type="EMPTY_MODULE" version="4">
|
<module type="EMPTY_MODULE" version="4">
|
||||||
<component name="NewModuleRootManager">
|
<component name="NewModuleRootManager">
|
||||||
<content url="file://$MODULE_DIR$">
|
<content url="file://$MODULE_DIR$">
|
||||||
<sourceFolder url="file://$MODULE_DIR$/lib/schedule_parser/benches" isTestSource="true" />
|
|
||||||
<sourceFolder url="file://$MODULE_DIR$/lib/schedule_parser/src" isTestSource="false" />
|
<sourceFolder url="file://$MODULE_DIR$/lib/schedule_parser/src" isTestSource="false" />
|
||||||
<sourceFolder url="file://$MODULE_DIR$/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" />
|
||||||
|
<excludeFolder url="file://$MODULE_DIR$/actix-macros/target" />
|
||||||
|
<excludeFolder url="file://$MODULE_DIR$/actix-test/target" />
|
||||||
<excludeFolder url="file://$MODULE_DIR$/target" />
|
<excludeFolder url="file://$MODULE_DIR$/target" />
|
||||||
|
<excludeFolder url="file://$MODULE_DIR$/.idea/dataSources" />
|
||||||
</content>
|
</content>
|
||||||
<orderEntry type="inheritedJdk" />
|
<orderEntry type="inheritedJdk" />
|
||||||
<orderEntry type="sourceFolder" forTests="false" />
|
<orderEntry type="sourceFolder" forTests="false" />
|
||||||
|
|||||||
9
.idea/sqldialects.xml
generated
9
.idea/sqldialects.xml
generated
@@ -1,9 +0,0 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
|
||||||
<project version="4">
|
|
||||||
<component name="SqlDialectMappings">
|
|
||||||
<file url="file://$PROJECT_DIR$/migrations/2025-03-21-211822_create_user_role/down.sql" dialect="PostgreSQL" />
|
|
||||||
<file url="file://$PROJECT_DIR$/migrations/2025-03-21-212111_create_users/up.sql" dialect="PostgreSQL" />
|
|
||||||
<file url="file://$PROJECT_DIR$/migrations/2025-03-21-212723_create_fcm/down.sql" dialect="PostgreSQL" />
|
|
||||||
<file url="file://$PROJECT_DIR$/migrations/2025-03-21-212723_create_fcm/up.sql" dialect="PostgreSQL" />
|
|
||||||
</component>
|
|
||||||
</project>
|
|
||||||
1629
Cargo.lock
generated
1629
Cargo.lock
generated
File diff suppressed because it is too large
Load Diff
46
Cargo.toml
46
Cargo.toml
@@ -1,19 +1,53 @@
|
|||||||
[workspace]
|
[workspace]
|
||||||
members = ["lib/schedule_parser"]
|
members = ["actix-macros", "actix-test"]
|
||||||
|
|
||||||
[package]
|
[package]
|
||||||
name = "schedule-parser-rusted"
|
name = "schedule-parser-rusted"
|
||||||
version = "0.3.0"
|
version = "1.0.3"
|
||||||
edition = "2024"
|
edition = "2024"
|
||||||
publish = false
|
publish = false
|
||||||
|
|
||||||
|
[profile.release]
|
||||||
|
debug = true
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
actix-web = "4.10.2"
|
||||||
|
actix-macros = { path = "actix-macros" }
|
||||||
|
bcrypt = "0.17.0"
|
||||||
|
calamine = "0.26.1"
|
||||||
|
chrono = { version = "0.4.40", features = ["serde"] }
|
||||||
|
derive_more = "2.0.1"
|
||||||
diesel = { version = "2.2.8", features = ["postgres"] }
|
diesel = { version = "2.2.8", features = ["postgres"] }
|
||||||
diesel-derive-enum = { git = "https://github.com/Havunen/diesel-derive-enum.git", features = ["postgres"] }
|
diesel-derive-enum = { git = "https://github.com/Havunen/diesel-derive-enum.git", features = ["postgres"] }
|
||||||
dotenvy = "0.15.7"
|
dotenvy = "0.15.7"
|
||||||
|
env_logger = "0.11.7"
|
||||||
|
firebase-messaging-rs = { git = "https://github.com/i10416/firebase-messaging-rs.git" }
|
||||||
|
futures-util = "0.3.31"
|
||||||
|
fuzzy-matcher = "0.3.7"
|
||||||
|
jsonwebtoken = { version = "9.3.1", features = ["use_pem"] }
|
||||||
|
hex = "0.4.3"
|
||||||
|
mime = "0.3.17"
|
||||||
|
objectid = "0.2.0"
|
||||||
|
regex = "1.11.1"
|
||||||
|
reqwest = { version = "0.12.15", features = ["json"] }
|
||||||
|
sentry = "0.37.0"
|
||||||
|
sentry-actix = "0.37.0"
|
||||||
serde = { version = "1.0.219", features = ["derive"] }
|
serde = { version = "1.0.219", features = ["derive"] }
|
||||||
schedule_parser = { path = "./lib/schedule_parser" }
|
serde_json = "1.0.140"
|
||||||
chrono = "0.4.40"
|
serde_with = "3.12.0"
|
||||||
reqwest = "0.12.15"
|
serde_repr = "0.1.20"
|
||||||
|
sha1 = "0.11.0-pre.5"
|
||||||
tokio = { version = "1.44.1", features = ["macros", "rt-multi-thread"] }
|
tokio = { version = "1.44.1", features = ["macros", "rt-multi-thread"] }
|
||||||
actix-web = "4.10.2"
|
rand = "0.9.0"
|
||||||
|
utoipa = { version = "5", features = ["actix_extras", "chrono"] }
|
||||||
|
utoipa-rapidoc = { version = "6.0.0", features = ["actix-web"] }
|
||||||
|
utoipa-actix-web = "0.1"
|
||||||
|
uuid = { version = "1.16.0", features = ["v4"] }
|
||||||
|
|
||||||
|
[dev-dependencies]
|
||||||
|
actix-test = { path = "actix-test" }
|
||||||
|
criterion = "0.5.1"
|
||||||
|
|
||||||
|
[[bench]]
|
||||||
|
name = "parse"
|
||||||
|
harness = false
|
||||||
14
Dockerfile
Normal file
14
Dockerfile
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
FROM debian:stable-slim
|
||||||
|
LABEL authors="n08i40k"
|
||||||
|
|
||||||
|
ARG BINARY_NAME
|
||||||
|
|
||||||
|
WORKDIR /app/
|
||||||
|
|
||||||
|
RUN apt update && \
|
||||||
|
apt install -y libpq5 ca-certificates openssl
|
||||||
|
|
||||||
|
COPY ./${BINARY_NAME} /bin/main
|
||||||
|
RUN chmod +x /bin/main
|
||||||
|
|
||||||
|
ENTRYPOINT ["main"]
|
||||||
1
actix-macros/.gitignore
vendored
Normal file
1
actix-macros/.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
|||||||
|
/target
|
||||||
7
actix-macros/Cargo.lock
generated
Normal file
7
actix-macros/Cargo.lock
generated
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
# 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"
|
||||||
12
actix-macros/Cargo.toml
Normal file
12
actix-macros/Cargo.toml
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
[package]
|
||||||
|
name = "actix-macros"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2024"
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
syn = "2.0.100"
|
||||||
|
quote = "1.0.40"
|
||||||
|
proc-macro2 = "1.0.94"
|
||||||
|
|
||||||
|
[lib]
|
||||||
|
proc-macro = true
|
||||||
209
actix-macros/src/lib.rs
Normal file
209
actix-macros/src/lib.rs
Normal file
@@ -0,0 +1,209 @@
|
|||||||
|
extern crate proc_macro;
|
||||||
|
|
||||||
|
use proc_macro::TokenStream;
|
||||||
|
|
||||||
|
mod shared {
|
||||||
|
use quote::{ToTokens, quote};
|
||||||
|
use syn::{Attribute, DeriveInput};
|
||||||
|
|
||||||
|
pub fn find_status_code(attrs: &Vec<Attribute>) -> Option<proc_macro2::TokenStream> {
|
||||||
|
attrs
|
||||||
|
.iter()
|
||||||
|
.find_map(|attr| -> Option<proc_macro2::TokenStream> {
|
||||||
|
if !attr.path().is_ident("status_code") {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
|
||||||
|
let meta = attr.meta.require_name_value().ok()?;
|
||||||
|
|
||||||
|
let code = meta.value.to_token_stream().to_string();
|
||||||
|
let trimmed_code = code.trim_matches('"');
|
||||||
|
|
||||||
|
if let Ok(numeric_code) = trimmed_code.parse::<u16>() {
|
||||||
|
Some(quote! { actix_web::http::StatusCode::from_u16(#numeric_code).unwrap() })
|
||||||
|
} else {
|
||||||
|
let string_code: proc_macro2::TokenStream =
|
||||||
|
trimmed_code.to_string().parse().unwrap();
|
||||||
|
|
||||||
|
Some(quote! { #string_code })
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_arms(ast: &DeriveInput) -> Vec<proc_macro2::TokenStream> {
|
||||||
|
let name = &ast.ident;
|
||||||
|
|
||||||
|
let variants = if let syn::Data::Enum(data) = &ast.data {
|
||||||
|
&data.variants
|
||||||
|
} else {
|
||||||
|
panic!("Only enums are supported");
|
||||||
|
};
|
||||||
|
|
||||||
|
let mut status_code_arms: Vec<proc_macro2::TokenStream> = variants
|
||||||
|
.iter()
|
||||||
|
.map(|v| -> Option<proc_macro2::TokenStream> {
|
||||||
|
let status_code = find_status_code(&v.attrs)?;
|
||||||
|
let variant_name = &v.ident;
|
||||||
|
|
||||||
|
Some(quote! { #name::#variant_name => #status_code, })
|
||||||
|
})
|
||||||
|
.filter(|v| v.is_some())
|
||||||
|
.map(|v| v.unwrap())
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
if status_code_arms.len() < variants.len() {
|
||||||
|
let status_code = find_status_code(&ast.attrs)
|
||||||
|
.unwrap_or_else(|| quote! { ::actix_web::http::StatusCode::INTERNAL_SERVER_ERROR });
|
||||||
|
|
||||||
|
status_code_arms.push(quote! { _ => #status_code });
|
||||||
|
}
|
||||||
|
|
||||||
|
status_code_arms
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
mod response_error_message {
|
||||||
|
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 ::actix_web::ResponseError for #name {
|
||||||
|
fn status_code(&self) -> ::actix_web::http::StatusCode {
|
||||||
|
match self {
|
||||||
|
#(#status_code_arms)*
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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)*
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
mod responder_json {
|
||||||
|
use proc_macro::TokenStream;
|
||||||
|
use quote::quote;
|
||||||
|
|
||||||
|
pub fn fmt(ast: &syn::DeriveInput) -> TokenStream {
|
||||||
|
let name = &ast.ident;
|
||||||
|
|
||||||
|
TokenStream::from(quote! {
|
||||||
|
impl ::actix_web::Responder for #name {
|
||||||
|
type Body = ::actix_web::body::EitherBody<::actix_web::body::BoxBody>;
|
||||||
|
|
||||||
|
fn respond_to(self, _: &::actix_web::HttpRequest) -> ::actix_web::HttpResponse<Self::Body> {
|
||||||
|
::actix_web::HttpResponse::Ok()
|
||||||
|
.json(self)
|
||||||
|
.map_into_left_body()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
mod into_response_error {
|
||||||
|
use proc_macro::TokenStream;
|
||||||
|
use quote::quote;
|
||||||
|
|
||||||
|
pub fn fmt(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 {
|
||||||
|
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 {}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[proc_macro_derive(ResponseErrorMessage, attributes(status_code))]
|
||||||
|
pub fn rem_derive(input: TokenStream) -> TokenStream {
|
||||||
|
let ast = syn::parse(input).unwrap();
|
||||||
|
|
||||||
|
response_error_message::fmt(&ast)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[proc_macro_derive(ResponderJson)]
|
||||||
|
pub fn responser_json_derive(input: TokenStream) -> TokenStream {
|
||||||
|
let ast = syn::parse(input).unwrap();
|
||||||
|
|
||||||
|
responder_json::fmt(&ast)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[proc_macro_derive(IntoResponseError)]
|
||||||
|
pub fn into_response_error_derive(input: TokenStream) -> TokenStream {
|
||||||
|
let ast = syn::parse(input).unwrap();
|
||||||
|
|
||||||
|
into_response_error::fmt(&ast)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[proc_macro_derive(IntoResponseErrorNamed)]
|
||||||
|
pub fn into_response_error_named_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)
|
||||||
|
}
|
||||||
1
actix-test/.gitignore
vendored
Normal file
1
actix-test/.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
|||||||
|
/target
|
||||||
1520
actix-test/Cargo.lock
generated
Normal file
1520
actix-test/Cargo.lock
generated
Normal file
File diff suppressed because it is too large
Load Diff
8
actix-test/Cargo.toml
Normal file
8
actix-test/Cargo.toml
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
[package]
|
||||||
|
name = "actix-test"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2024"
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
actix-http = "3.10.0"
|
||||||
|
actix-web = "4.10.2"
|
||||||
12
actix-test/src/lib.rs
Normal file
12
actix-test/src/lib.rs
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
use actix_web::dev::{HttpServiceFactory, Service, ServiceResponse};
|
||||||
|
use actix_web::{App, test, web};
|
||||||
|
|
||||||
|
pub async fn test_app<F, A: 'static>(
|
||||||
|
app_state: web::Data<A>,
|
||||||
|
factory: F,
|
||||||
|
) -> impl Service<actix_http::Request, Response = ServiceResponse, Error = actix_web::Error>
|
||||||
|
where
|
||||||
|
F: HttpServiceFactory + 'static,
|
||||||
|
{
|
||||||
|
test::init_service(App::new().app_data(app_state).service(factory)).await
|
||||||
|
}
|
||||||
@@ -1,10 +1,11 @@
|
|||||||
use criterion::{Criterion, criterion_group, criterion_main};
|
use criterion::{Criterion, criterion_group, criterion_main};
|
||||||
use schedule_parser::parse_xls;
|
|
||||||
|
use schedule_parser_rusted::parser::parse_xls;
|
||||||
|
|
||||||
pub fn bench_parse_xls(c: &mut Criterion) {
|
pub fn bench_parse_xls(c: &mut Criterion) {
|
||||||
let buffer: Vec<u8> = include_bytes!("../../../schedule.xls").to_vec();
|
let buffer: Vec<u8> = include_bytes!("../schedule.xls").to_vec();
|
||||||
|
|
||||||
c.bench_function("parse_xls", |b| b.iter(|| parse_xls(&buffer)));
|
c.bench_function("parse_xls", |b| b.iter(|| parse_xls(&buffer).unwrap()));
|
||||||
}
|
}
|
||||||
|
|
||||||
criterion_group!(benches, bench_parse_xls);
|
criterion_group!(benches, bench_parse_xls);
|
||||||
@@ -1,23 +0,0 @@
|
|||||||
[package]
|
|
||||||
name = "schedule_parser"
|
|
||||||
version = "0.2.0"
|
|
||||||
edition = "2024"
|
|
||||||
|
|
||||||
[lib]
|
|
||||||
name = "schedule_parser"
|
|
||||||
path = "src/lib/lib.rs"
|
|
||||||
|
|
||||||
[dependencies]
|
|
||||||
serde = { version = "1.0.219", features = ["derive"] }
|
|
||||||
serde_repr = "0.1.20"
|
|
||||||
chrono = { version = "0.4.40", features = ["serde"] }
|
|
||||||
calamine = "0.26.1"
|
|
||||||
regex = "1.11.1"
|
|
||||||
fuzzy-matcher = "0.3.7"
|
|
||||||
|
|
||||||
[dev-dependencies]
|
|
||||||
criterion = "0.5.1"
|
|
||||||
|
|
||||||
[[bench]]
|
|
||||||
name = "parse"
|
|
||||||
harness = false
|
|
||||||
@@ -1,107 +0,0 @@
|
|||||||
use chrono::{DateTime, Utc};
|
|
||||||
use serde::{Deserialize, Serialize};
|
|
||||||
use serde_repr::{Deserialize_repr, Serialize_repr};
|
|
||||||
use std::collections::HashMap;
|
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Debug, Clone)]
|
|
||||||
pub struct LessonTime {
|
|
||||||
pub start: DateTime<Utc>,
|
|
||||||
pub end: DateTime<Utc>,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Serialize_repr, Deserialize_repr, Debug, PartialEq, Clone)]
|
|
||||||
#[repr(u8)]
|
|
||||||
pub enum LessonType {
|
|
||||||
Default = 0, // Обычная
|
|
||||||
Additional, // Допы
|
|
||||||
Break, // Перемена
|
|
||||||
Consultation, // Консультация
|
|
||||||
IndependentWork, // Самостоятельная работа
|
|
||||||
Exam, // Зачёт
|
|
||||||
ExamWithGrade, // Зачет с оценкой
|
|
||||||
ExamDefault, // Экзамен
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Debug, Clone)]
|
|
||||||
pub struct LessonSubGroup {
|
|
||||||
pub number: u8,
|
|
||||||
|
|
||||||
pub cabinet: Option<String>,
|
|
||||||
|
|
||||||
pub teacher: String,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Debug, Clone)]
|
|
||||||
pub struct Lesson {
|
|
||||||
/**
|
|
||||||
* Тип занятия
|
|
||||||
*/
|
|
||||||
#[serde(rename = "type")]
|
|
||||||
pub lesson_type: LessonType,
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Индексы пар, если присутствуют
|
|
||||||
*/
|
|
||||||
#[serde(rename = "defaultRange")]
|
|
||||||
pub default_range: Option<[u8; 2]>,
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Название занятия
|
|
||||||
*/
|
|
||||||
pub name: Option<String>,
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Начало и конец занятия
|
|
||||||
*/
|
|
||||||
pub time: LessonTime,
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Подгруппы
|
|
||||||
*/
|
|
||||||
#[serde(rename = "subGroups")]
|
|
||||||
pub subgroups: Option<Vec<LessonSubGroup>>,
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Группа (только для расписания преподавателей)
|
|
||||||
*/
|
|
||||||
pub group: Option<String>,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Debug, Clone)]
|
|
||||||
pub struct Day {
|
|
||||||
pub name: String,
|
|
||||||
|
|
||||||
pub street: Option<String>,
|
|
||||||
|
|
||||||
pub date: DateTime<Utc>,
|
|
||||||
|
|
||||||
pub lessons: Vec<Lesson>,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Debug)]
|
|
||||||
pub struct ScheduleEntity {
|
|
||||||
pub name: String,
|
|
||||||
|
|
||||||
pub days: Vec<Day>,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Debug)]
|
|
||||||
pub struct Schedule {
|
|
||||||
#[serde(rename = "updatedAt")]
|
|
||||||
pub updated_at: DateTime<Utc>,
|
|
||||||
|
|
||||||
pub groups: HashMap<String, ScheduleEntity>,
|
|
||||||
|
|
||||||
#[serde(rename = "updatedGroups")]
|
|
||||||
pub updated_groups: Vec<Vec<usize>>,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Debug)]
|
|
||||||
pub struct TeacherSchedule {
|
|
||||||
#[serde(rename = "updatedAt")]
|
|
||||||
pub updated_at: DateTime<Utc>,
|
|
||||||
|
|
||||||
pub teacher: ScheduleEntity,
|
|
||||||
|
|
||||||
pub updated: Vec<usize>,
|
|
||||||
}
|
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
-- This file was automatically created by Diesel to setup helper functions
|
-- This file was automatically created by Diesel to set up helper functions
|
||||||
-- and other internal bookkeeping. This file is safe to edit, any future
|
-- and other internal bookkeeping. This file is safe to edit, any future
|
||||||
-- changes will be added to existing projects as new migrations.
|
-- changes will be added to existing projects as new migrations.
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
-- This file was automatically created by Diesel to setup helper functions
|
-- This file was automatically created by Diesel to set up helper functions
|
||||||
-- and other internal bookkeeping. This file is safe to edit, any future
|
-- and other internal bookkeeping. This file is safe to edit, any future
|
||||||
-- changes will be added to existing projects as new migrations.
|
-- changes will be added to existing projects as new migrations.
|
||||||
|
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ CREATE TABLE users
|
|||||||
(
|
(
|
||||||
id text PRIMARY KEY NOT NULL,
|
id text PRIMARY KEY NOT NULL,
|
||||||
username text UNIQUE NOT NULL,
|
username text UNIQUE NOT NULL,
|
||||||
"password" text NOT NULL,
|
password text NOT NULL,
|
||||||
vk_id int4 NULL,
|
vk_id int4 NULL,
|
||||||
access_token text UNIQUE NOT NULL,
|
access_token text UNIQUE NOT NULL,
|
||||||
"group" text NOT NULL,
|
"group" text NOT NULL,
|
||||||
|
|||||||
@@ -1,11 +1,6 @@
|
|||||||
CREATE TABLE fcm
|
CREATE TABLE fcm
|
||||||
(
|
(
|
||||||
user_id text PRIMARY KEY NOT NULL,
|
user_id text PRIMARY KEY NOT NULL REFERENCES users (id),
|
||||||
token text NOT NULL,
|
token text NOT NULL,
|
||||||
topics text[] NULL
|
topics text[] NOT NULL CHECK ( array_position(topics, null) is null )
|
||||||
);
|
);
|
||||||
|
|
||||||
CREATE UNIQUE INDEX fcm_user_id_key ON fcm USING btree (user_id);
|
|
||||||
|
|
||||||
ALTER TABLE fcm
|
|
||||||
ADD CONSTRAINT fcm_user_id_fkey FOREIGN KEY (user_id) REFERENCES users (id) ON DELETE RESTRICT ON UPDATE CASCADE;
|
|
||||||
88
src/app_state.rs
Normal file
88
src/app_state.rs
Normal file
@@ -0,0 +1,88 @@
|
|||||||
|
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)
|
||||||
|
}
|
||||||
163
src/database/driver.rs
Normal file
163
src/database/driver.rs
Normal file
@@ -0,0 +1,163 @@
|
|||||||
|
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 actix_web::web;
|
||||||
|
use diesel::{ExpressionMethods, QueryResult, insert_into};
|
||||||
|
use diesel::{QueryDsl, RunQueryDsl};
|
||||||
|
use diesel::{SaveChangesDsl, SelectableHelper};
|
||||||
|
|
||||||
|
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 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)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
//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)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
//noinspection DuplicatedCode
|
||||||
|
pub 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,
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
//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 fn insert(state: &web::Data<AppState>, user: &User) -> QueryResult<usize> {
|
||||||
|
state
|
||||||
|
.database
|
||||||
|
.scope(|conn| insert_into(users).values(user).execute(conn))
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Function declaration [User::save][UserSave::save].
|
||||||
|
pub trait UserSave {
|
||||||
|
/// Saves the user's changes to the database.
|
||||||
|
///
|
||||||
|
/// # Arguments
|
||||||
|
///
|
||||||
|
/// * `state`: The state of the actix-web application that stores the mutex of the [connection][diesel::PgConnection].
|
||||||
|
///
|
||||||
|
/// returns: `QueryResult<User>`
|
||||||
|
///
|
||||||
|
/// # Examples
|
||||||
|
///
|
||||||
|
/// ```
|
||||||
|
/// use crate::database::driver::users;
|
||||||
|
///
|
||||||
|
/// #[derive(Deserialize)]
|
||||||
|
/// struct Params {
|
||||||
|
/// pub username: String,
|
||||||
|
/// }
|
||||||
|
///
|
||||||
|
/// #[patch("/")]
|
||||||
|
/// async fn patch_user(
|
||||||
|
/// app_state: web::Data<AppState>,
|
||||||
|
/// user: SyncExtractor<User>,
|
||||||
|
/// web::Query(params): web::Query<Params>,
|
||||||
|
/// ) -> web::Json<User> {
|
||||||
|
/// let mut user = user.into_inner();
|
||||||
|
///
|
||||||
|
/// user.username = params.username;
|
||||||
|
///
|
||||||
|
/// match user.save(&app_state) {
|
||||||
|
/// Ok(user) => web::Json(user),
|
||||||
|
/// Err(e) => {
|
||||||
|
/// eprintln!("Failed to save user: {e}");
|
||||||
|
/// panic!();
|
||||||
|
/// }
|
||||||
|
/// }
|
||||||
|
/// }
|
||||||
|
/// ```
|
||||||
|
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))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[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,
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
#[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)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,2 +1,3 @@
|
|||||||
pub mod schema;
|
pub mod schema;
|
||||||
pub mod models;
|
pub mod models;
|
||||||
|
pub mod driver;
|
||||||
|
|||||||
@@ -1,7 +1,12 @@
|
|||||||
|
use actix_macros::ResponderJson;
|
||||||
|
use diesel::QueryId;
|
||||||
use diesel::prelude::*;
|
use diesel::prelude::*;
|
||||||
use serde::Serialize;
|
use serde::{Deserialize, Serialize};
|
||||||
|
use utoipa::ToSchema;
|
||||||
|
|
||||||
#[derive(diesel_derive_enum::DbEnum, Serialize, Debug)]
|
#[derive(
|
||||||
|
Copy, Clone, PartialEq, Debug, Serialize, Deserialize, diesel_derive_enum::DbEnum, ToSchema,
|
||||||
|
)]
|
||||||
#[ExistingTypePath = "crate::database::schema::sql_types::UserRole"]
|
#[ExistingTypePath = "crate::database::schema::sql_types::UserRole"]
|
||||||
#[DbValueStyle = "UPPERCASE"]
|
#[DbValueStyle = "UPPERCASE"]
|
||||||
#[serde(rename_all = "UPPERCASE")]
|
#[serde(rename_all = "UPPERCASE")]
|
||||||
@@ -11,16 +16,69 @@ pub enum UserRole {
|
|||||||
Admin,
|
Admin,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Queryable, Selectable, Serialize)]
|
#[derive(
|
||||||
|
Identifiable,
|
||||||
|
AsChangeset,
|
||||||
|
Queryable,
|
||||||
|
QueryId,
|
||||||
|
Selectable,
|
||||||
|
Serialize,
|
||||||
|
Insertable,
|
||||||
|
Debug,
|
||||||
|
ToSchema,
|
||||||
|
ResponderJson,
|
||||||
|
)]
|
||||||
#[diesel(table_name = crate::database::schema::users)]
|
#[diesel(table_name = crate::database::schema::users)]
|
||||||
#[diesel(check_for_backend(diesel::pg::Pg))]
|
#[diesel(treat_none_as_null = true)]
|
||||||
pub struct User {
|
pub struct User {
|
||||||
|
/// Account UUID.
|
||||||
pub id: String,
|
pub id: String,
|
||||||
|
|
||||||
|
/// User name.
|
||||||
pub username: String,
|
pub username: String,
|
||||||
|
|
||||||
|
/// BCrypt password hash.
|
||||||
pub password: String,
|
pub password: String,
|
||||||
|
|
||||||
|
/// ID of the linked VK account.
|
||||||
pub vk_id: Option<i32>,
|
pub vk_id: Option<i32>,
|
||||||
|
|
||||||
|
/// JWT access token.
|
||||||
pub access_token: String,
|
pub access_token: String,
|
||||||
|
|
||||||
|
/// Group.
|
||||||
pub group: String,
|
pub group: String,
|
||||||
|
|
||||||
|
/// Role.
|
||||||
pub role: UserRole,
|
pub role: UserRole,
|
||||||
|
|
||||||
|
/// Version of the installed Polytechnic+ application.
|
||||||
pub version: String,
|
pub version: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(
|
||||||
|
Debug,
|
||||||
|
Clone,
|
||||||
|
Serialize,
|
||||||
|
Identifiable,
|
||||||
|
Queryable,
|
||||||
|
Selectable,
|
||||||
|
Insertable,
|
||||||
|
AsChangeset,
|
||||||
|
Associations,
|
||||||
|
ToSchema,
|
||||||
|
ResponderJson,
|
||||||
|
)]
|
||||||
|
#[diesel(belongs_to(User))]
|
||||||
|
#[diesel(table_name = crate::database::schema::fcm)]
|
||||||
|
#[diesel(primary_key(user_id))]
|
||||||
|
pub struct FCM {
|
||||||
|
/// Account UUID.
|
||||||
|
pub user_id: String,
|
||||||
|
|
||||||
|
/// FCM token.
|
||||||
|
pub token: String,
|
||||||
|
|
||||||
|
/// List of topics subscribed to by the user.
|
||||||
|
pub topics: Vec<Option<String>>,
|
||||||
|
}
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ diesel::table! {
|
|||||||
fcm (user_id) {
|
fcm (user_id) {
|
||||||
user_id -> Text,
|
user_id -> Text,
|
||||||
token -> Text,
|
token -> Text,
|
||||||
topics -> Nullable<Array<Nullable<Text>>>,
|
topics -> Array<Nullable<Text>>,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
110
src/extractors/authorized_user.rs
Normal file
110
src/extractors/authorized_user.rs
Normal file
@@ -0,0 +1,110 @@
|
|||||||
|
use crate::app_state::AppState;
|
||||||
|
use crate::database::driver;
|
||||||
|
use crate::database::models::{FCM, User};
|
||||||
|
use crate::extractors::base::{FromRequestSync, SyncExtractor};
|
||||||
|
use crate::utility::jwt;
|
||||||
|
use actix_macros::ResponseErrorMessage;
|
||||||
|
use actix_web::body::BoxBody;
|
||||||
|
use actix_web::dev::Payload;
|
||||||
|
use actix_web::http::header;
|
||||||
|
use actix_web::{FromRequest, HttpRequest, web};
|
||||||
|
use derive_more::Display;
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
use std::fmt::Debug;
|
||||||
|
|
||||||
|
#[derive(Clone, Debug, Serialize, Deserialize, Display, ResponseErrorMessage)]
|
||||||
|
#[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,
|
||||||
|
|
||||||
|
/// Unknown authorization type other than Bearer.
|
||||||
|
#[display("Bearer token is required")]
|
||||||
|
UnknownAuthorizationType,
|
||||||
|
|
||||||
|
/// Invalid or expired access token.
|
||||||
|
#[display("Invalid or expired access token")]
|
||||||
|
InvalidAccessToken,
|
||||||
|
|
||||||
|
/// The user bound to the token is not found in the database.
|
||||||
|
#[display("No user associated with access token")]
|
||||||
|
NoUser,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Error {
|
||||||
|
pub fn into_err(self) -> actix_web::Error {
|
||||||
|
actix_web::Error::from(self)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// User extractor from request with Bearer access token.
|
||||||
|
impl FromRequestSync 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();
|
||||||
|
|
||||||
|
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())
|
||||||
|
.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,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
151
src/extractors/base.rs
Normal file
151
src/extractors/base.rs
Normal file
@@ -0,0 +1,151 @@
|
|||||||
|
use actix_web::dev::Payload;
|
||||||
|
use actix_web::{FromRequest, HttpRequest};
|
||||||
|
use futures_util::future::LocalBoxFuture;
|
||||||
|
use std::future::{Ready, ready};
|
||||||
|
use std::ops;
|
||||||
|
|
||||||
|
/// # Async extractor.
|
||||||
|
|
||||||
|
/// Asynchronous object extractor from a query.
|
||||||
|
pub struct AsyncExtractor<T>(T);
|
||||||
|
|
||||||
|
impl<T> AsyncExtractor<T> {
|
||||||
|
#[allow(dead_code)]
|
||||||
|
/// Retrieve the object extracted with the extractor.
|
||||||
|
pub fn into_inner(self) -> T {
|
||||||
|
self.0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T> ops::Deref for AsyncExtractor<T> {
|
||||||
|
type Target = T;
|
||||||
|
|
||||||
|
fn deref(&self) -> &Self::Target {
|
||||||
|
&self.0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T> ops::DerefMut for AsyncExtractor<T> {
|
||||||
|
fn deref_mut(&mut self) -> &mut Self::Target {
|
||||||
|
&mut self.0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub trait FromRequestAsync: Sized {
|
||||||
|
type Error: Into<actix_web::Error>;
|
||||||
|
|
||||||
|
/// Asynchronous function for extracting data from a query.
|
||||||
|
///
|
||||||
|
/// returns: Result<Self, Self::Error>
|
||||||
|
///
|
||||||
|
/// # Examples
|
||||||
|
///
|
||||||
|
/// ```
|
||||||
|
/// struct User {
|
||||||
|
/// pub id: String,
|
||||||
|
/// pub username: String,
|
||||||
|
/// }
|
||||||
|
///
|
||||||
|
/// // TODO: Я вообще этот экстрактор не использую, нахуя мне тогда писать пример, если я не ебу как его использовать. Я забыл.
|
||||||
|
///
|
||||||
|
/// #[get("/")]
|
||||||
|
/// fn get_user_async(
|
||||||
|
/// user: web::AsyncExtractor<User>,
|
||||||
|
/// ) -> web::Json<User> {
|
||||||
|
/// let user = user.into_inner();
|
||||||
|
///
|
||||||
|
/// web::Json(user)
|
||||||
|
/// }
|
||||||
|
/// ```
|
||||||
|
async fn from_request_async(req: HttpRequest, payload: 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 {
|
||||||
|
let req = req.clone();
|
||||||
|
let payload = payload.take();
|
||||||
|
Box::pin(async move {
|
||||||
|
T::from_request_async(req, payload)
|
||||||
|
.await
|
||||||
|
.map(|res| Self(res))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// # Sync extractor.
|
||||||
|
|
||||||
|
/// Synchronous object extractor from a query.
|
||||||
|
pub struct SyncExtractor<T>(T);
|
||||||
|
|
||||||
|
impl<T> SyncExtractor<T> {
|
||||||
|
/// Retrieving an object extracted with the extractor.
|
||||||
|
pub fn into_inner(self) -> T {
|
||||||
|
self.0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T> ops::Deref for SyncExtractor<T> {
|
||||||
|
type Target = T;
|
||||||
|
|
||||||
|
fn deref(&self) -> &Self::Target {
|
||||||
|
&self.0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T> ops::DerefMut for SyncExtractor<T> {
|
||||||
|
fn deref_mut(&mut self) -> &mut Self::Target {
|
||||||
|
&mut self.0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub trait FromRequestSync: Sized {
|
||||||
|
type Error: Into<actix_web::Error>;
|
||||||
|
|
||||||
|
/// Synchronous function for extracting data from a query.
|
||||||
|
///
|
||||||
|
/// returns: Result<Self, Self::Error>
|
||||||
|
///
|
||||||
|
/// # Examples
|
||||||
|
///
|
||||||
|
/// ```
|
||||||
|
/// struct User {
|
||||||
|
/// pub id: String,
|
||||||
|
/// pub username: String,
|
||||||
|
/// }
|
||||||
|
///
|
||||||
|
/// impl FromRequestSync for User {
|
||||||
|
/// type Error = actix_web::Error;
|
||||||
|
///
|
||||||
|
/// fn from_request_sync(req: &HttpRequest, _: &mut Payload) -> Result<Self, Self::Error> {
|
||||||
|
/// // do magic here.
|
||||||
|
///
|
||||||
|
/// Ok(User {
|
||||||
|
/// id: "qwerty".to_string(),
|
||||||
|
/// username: "n08i40k".to_string()
|
||||||
|
/// })
|
||||||
|
/// }
|
||||||
|
/// }
|
||||||
|
///
|
||||||
|
/// #[get("/")]
|
||||||
|
/// fn get_user_sync(
|
||||||
|
/// user: web::SyncExtractor<User>,
|
||||||
|
/// ) -> web::Json<User> {
|
||||||
|
/// let user = user.into_inner();
|
||||||
|
///
|
||||||
|
/// web::Json(user)
|
||||||
|
/// }
|
||||||
|
/// ```
|
||||||
|
fn from_request_sync(req: &HttpRequest, payload: &mut Payload) -> Result<Self, Self::Error>;
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T: FromRequestSync> FromRequest for SyncExtractor<T> {
|
||||||
|
type Error = T::Error;
|
||||||
|
type Future = Ready<Result<Self, Self::Error>>;
|
||||||
|
|
||||||
|
fn from_request(req: &HttpRequest, payload: &mut Payload) -> Self::Future {
|
||||||
|
ready(T::from_request_sync(req, payload).map(|res| Self(res)))
|
||||||
|
}
|
||||||
|
}
|
||||||
2
src/extractors/mod.rs
Normal file
2
src/extractors/mod.rs
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
pub mod authorized_user;
|
||||||
|
pub mod base;
|
||||||
1
src/lib.rs
Normal file
1
src/lib.rs
Normal file
@@ -0,0 +1 @@
|
|||||||
|
pub mod parser;
|
||||||
151
src/main.rs
151
src/main.rs
@@ -1,57 +1,124 @@
|
|||||||
use crate::routes::auth::sign_in::sign_in;
|
use crate::app_state::{AppState, app_state};
|
||||||
use crate::xls_downloader::basic_impl::BasicXlsDownloader;
|
use crate::middlewares::authorization::JWTAuthorization;
|
||||||
use actix_web::{web, App, HttpServer};
|
use crate::middlewares::content_type::ContentTypeBootstrap;
|
||||||
use chrono::{DateTime, Utc};
|
use actix_web::dev::{ServiceFactory, ServiceRequest};
|
||||||
use diesel::{Connection, PgConnection};
|
use actix_web::{App, Error, HttpServer};
|
||||||
use dotenvy::dotenv;
|
use dotenvy::dotenv;
|
||||||
use schedule_parser::schema::ScheduleEntity;
|
use std::io;
|
||||||
use std::collections::HashMap;
|
use utoipa_actix_web::AppExt;
|
||||||
use std::sync::Mutex;
|
use utoipa_actix_web::scope::Scope;
|
||||||
use std::env;
|
use utoipa_rapidoc::RapiDoc;
|
||||||
|
|
||||||
|
mod app_state;
|
||||||
|
|
||||||
mod database;
|
mod database;
|
||||||
mod routes;
|
|
||||||
|
mod parser;
|
||||||
mod xls_downloader;
|
mod xls_downloader;
|
||||||
|
|
||||||
pub struct AppState {
|
mod extractors;
|
||||||
downloader: Mutex<BasicXlsDownloader>,
|
mod middlewares;
|
||||||
schedule: Mutex<
|
mod routes;
|
||||||
Option<(
|
|
||||||
String,
|
mod utility;
|
||||||
DateTime<Utc>,
|
|
||||||
(
|
mod test_env;
|
||||||
HashMap<String, ScheduleEntity>,
|
|
||||||
HashMap<String, ScheduleEntity>,
|
pub fn get_api_scope<
|
||||||
),
|
I: Into<Scope<T>>,
|
||||||
)>,
|
T: ServiceFactory<ServiceRequest, Config = (), Error = Error, InitError = ()>,
|
||||||
>,
|
>(
|
||||||
database: Mutex<PgConnection>,
|
scope: I,
|
||||||
|
) -> Scope<T> {
|
||||||
|
let auth_scope = utoipa_actix_web::scope("/auth")
|
||||||
|
.service(routes::auth::sign_in)
|
||||||
|
.service(routes::auth::sign_in_vk)
|
||||||
|
.service(routes::auth::sign_up)
|
||||||
|
.service(routes::auth::sign_up_vk);
|
||||||
|
|
||||||
|
let users_scope = utoipa_actix_web::scope("/users")
|
||||||
|
.wrap(JWTAuthorization::default())
|
||||||
|
.service(routes::users::change_group)
|
||||||
|
.service(routes::users::change_username)
|
||||||
|
.service(routes::users::me);
|
||||||
|
|
||||||
|
let schedule_scope = utoipa_actix_web::scope("/schedule")
|
||||||
|
.wrap(JWTAuthorization {
|
||||||
|
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 vk_id_scope = utoipa_actix_web::scope("/vkid") //
|
||||||
|
.service(routes::vk_id::oauth);
|
||||||
|
|
||||||
|
utoipa_actix_web::scope(scope)
|
||||||
|
.service(auth_scope)
|
||||||
|
.service(users_scope)
|
||||||
|
.service(schedule_scope)
|
||||||
|
.service(fcm_scope)
|
||||||
|
.service(vk_id_scope)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[actix_web::main]
|
async fn async_main() -> io::Result<()> {
|
||||||
async fn main() {
|
println!("Starting server...");
|
||||||
dotenv().ok();
|
|
||||||
|
|
||||||
let database_url = env::var("DATABASE_URL").expect("DATABASE_URL must be set");
|
let app_state = app_state().await;
|
||||||
|
|
||||||
let data = web::Data::new(AppState {
|
|
||||||
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)),
|
|
||||||
),
|
|
||||||
});
|
|
||||||
|
|
||||||
HttpServer::new(move || {
|
HttpServer::new(move || {
|
||||||
let schedule_scope = web::scope("/auth").service(sign_in);
|
let (app, api) = App::new()
|
||||||
let api_scope = web::scope("/api/v1").service(schedule_scope);
|
.into_utoipa_app()
|
||||||
|
.app_data(app_state.clone())
|
||||||
|
.service(
|
||||||
|
get_api_scope("/api/v1")
|
||||||
|
.wrap(sentry_actix::Sentry::new())
|
||||||
|
.wrap(ContentTypeBootstrap),
|
||||||
|
)
|
||||||
|
.split_for_parts();
|
||||||
|
|
||||||
App::new().app_data(data.clone()).service(api_scope)
|
let rapidoc_service = RapiDoc::with_openapi("/api-docs-json", api).path("/api-docs");
|
||||||
|
|
||||||
|
// Because CORS error on non-localhost
|
||||||
|
let patched_rapidoc_html = rapidoc_service.to_html().replace(
|
||||||
|
"https://unpkg.com/rapidoc/dist/rapidoc-min.js",
|
||||||
|
"https://cdn.jsdelivr.net/npm/rapidoc/dist/rapidoc-min.min.js",
|
||||||
|
);
|
||||||
|
|
||||||
|
app.service(rapidoc_service.custom_html(patched_rapidoc_html))
|
||||||
})
|
})
|
||||||
.bind(("127.0.0.1", 8080))
|
.workers(4)
|
||||||
.unwrap()
|
.bind(("0.0.0.0", 5050))?
|
||||||
.run()
|
.run()
|
||||||
.await
|
.await
|
||||||
.unwrap();
|
}
|
||||||
|
|
||||||
|
fn main() -> io::Result<()> {
|
||||||
|
let _guard = sentry::init((
|
||||||
|
"https://9c33db76e89984b3f009b28a9f4b5954@sentry.n08i40k.ru/8",
|
||||||
|
sentry::ClientOptions {
|
||||||
|
release: sentry::release_name!(),
|
||||||
|
send_default_pii: true,
|
||||||
|
..Default::default()
|
||||||
|
},
|
||||||
|
));
|
||||||
|
|
||||||
|
unsafe { std::env::set_var("RUST_BACKTRACE", "1") };
|
||||||
|
|
||||||
|
dotenv().unwrap();
|
||||||
|
|
||||||
|
env_logger::init();
|
||||||
|
|
||||||
|
actix_web::rt::System::new().block_on(async { async_main().await })?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|||||||
116
src/middlewares/authorization.rs
Normal file
116
src/middlewares/authorization.rs
Normal file
@@ -0,0 +1,116 @@
|
|||||||
|
use crate::database::models::User;
|
||||||
|
use crate::extractors::authorized_user;
|
||||||
|
use crate::extractors::base::FromRequestSync;
|
||||||
|
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};
|
||||||
|
|
||||||
|
/// Middleware guard working with JWT tokens.
|
||||||
|
pub struct JWTAuthorization {
|
||||||
|
/// List of ignored endpoints.
|
||||||
|
pub ignore: &'static [&'static str],
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Default for JWTAuthorization {
|
||||||
|
fn default() -> Self {
|
||||||
|
Self { ignore: &[] }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<S, B> Transform<S, ServiceRequest> for JWTAuthorization
|
||||||
|
where
|
||||||
|
S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = Error>,
|
||||||
|
S::Future: 'static,
|
||||||
|
B: 'static,
|
||||||
|
{
|
||||||
|
type Response = ServiceResponse<EitherBody<B, BoxBody>>;
|
||||||
|
type Error = Error;
|
||||||
|
type Transform = JWTAuthorizationMiddleware<S>;
|
||||||
|
type InitError = ();
|
||||||
|
type Future = Ready<Result<Self::Transform, Self::InitError>>;
|
||||||
|
|
||||||
|
fn new_transform(&self, service: S) -> Self::Future {
|
||||||
|
ready(Ok(JWTAuthorizationMiddleware {
|
||||||
|
service,
|
||||||
|
ignore: self.ignore,
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct JWTAuthorizationMiddleware<S> {
|
||||||
|
service: S,
|
||||||
|
/// List of ignored endpoints.
|
||||||
|
ignore: &'static [&'static str],
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<S, B> JWTAuthorizationMiddleware<S>
|
||||||
|
where
|
||||||
|
S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = Error>,
|
||||||
|
S::Future: 'static,
|
||||||
|
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)
|
||||||
|
.map(|_| ())
|
||||||
|
.map_err(|e| e.as_error::<authorized_user::Error>().unwrap().clone())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn should_skip(&self, req: &ServiceRequest) -> bool {
|
||||||
|
let path = req.match_info().unprocessed();
|
||||||
|
|
||||||
|
self.ignore.iter().any(|ignore| {
|
||||||
|
if !path.starts_with(ignore) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if let Some(other) = path.as_bytes().iter().nth(ignore.len()) {
|
||||||
|
return ['?' as u8, '/' as u8].contains(other);
|
||||||
|
}
|
||||||
|
|
||||||
|
true
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a, S, B> Service<ServiceRequest> for JWTAuthorizationMiddleware<S>
|
||||||
|
where
|
||||||
|
S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = Error>,
|
||||||
|
S::Future: 'static,
|
||||||
|
B: 'static,
|
||||||
|
{
|
||||||
|
type Response = ServiceResponse<EitherBody<B, BoxBody>>;
|
||||||
|
type Error = Error;
|
||||||
|
type Future = LocalBoxFuture<'static, Result<Self::Response, Self::Error>>;
|
||||||
|
|
||||||
|
forward_ready!(service);
|
||||||
|
|
||||||
|
fn call(&self, req: ServiceRequest) -> Self::Future {
|
||||||
|
if self.should_skip(&req) {
|
||||||
|
let fut = self.service.call(req);
|
||||||
|
return Box::pin(async move { Ok(fut.await?.map_into_left_body()) });
|
||||||
|
}
|
||||||
|
|
||||||
|
let (http_req, mut payload) = req.into_parts();
|
||||||
|
|
||||||
|
if let Err(err) = self.check_authorization(&http_req, &mut payload) {
|
||||||
|
return Box::pin(async move {
|
||||||
|
Ok(ServiceResponse::new(
|
||||||
|
http_req,
|
||||||
|
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()) })
|
||||||
|
}
|
||||||
|
}
|
||||||
64
src/middlewares/content_type.rs
Normal file
64
src/middlewares/content_type.rs
Normal file
@@ -0,0 +1,64 @@
|
|||||||
|
use actix_web::Error;
|
||||||
|
use actix_web::body::{BoxBody, EitherBody};
|
||||||
|
use actix_web::dev::{Service, ServiceRequest, ServiceResponse, Transform, forward_ready};
|
||||||
|
use actix_web::http::header;
|
||||||
|
use actix_web::http::header::HeaderValue;
|
||||||
|
use futures_util::future::LocalBoxFuture;
|
||||||
|
use std::future::{Ready, ready};
|
||||||
|
|
||||||
|
/// Middleware to specify the encoding in the Content-Type header.
|
||||||
|
pub struct ContentTypeBootstrap;
|
||||||
|
|
||||||
|
impl<S, B> Transform<S, ServiceRequest> for ContentTypeBootstrap
|
||||||
|
where
|
||||||
|
S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = Error>,
|
||||||
|
S::Future: 'static,
|
||||||
|
B: 'static,
|
||||||
|
{
|
||||||
|
type Response = ServiceResponse<EitherBody<B, BoxBody>>;
|
||||||
|
type Error = Error;
|
||||||
|
type Transform = ContentTypeMiddleware<S>;
|
||||||
|
type InitError = ();
|
||||||
|
type Future = Ready<Result<Self::Transform, Self::InitError>>;
|
||||||
|
|
||||||
|
fn new_transform(&self, service: S) -> Self::Future {
|
||||||
|
ready(Ok(ContentTypeMiddleware { service }))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct ContentTypeMiddleware<S> {
|
||||||
|
service: S,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a, S, B> Service<ServiceRequest> for ContentTypeMiddleware<S>
|
||||||
|
where
|
||||||
|
S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = Error>,
|
||||||
|
S::Future: 'static,
|
||||||
|
B: 'static,
|
||||||
|
{
|
||||||
|
type Response = ServiceResponse<EitherBody<B, BoxBody>>;
|
||||||
|
type Error = Error;
|
||||||
|
type Future = LocalBoxFuture<'static, Result<Self::Response, Self::Error>>;
|
||||||
|
|
||||||
|
forward_ready!(service);
|
||||||
|
|
||||||
|
fn call(&self, req: ServiceRequest) -> Self::Future {
|
||||||
|
let fut = self.service.call(req);
|
||||||
|
|
||||||
|
Box::pin(async move {
|
||||||
|
let mut response = fut.await?;
|
||||||
|
|
||||||
|
let headers = response.response_mut().headers_mut();
|
||||||
|
if let Some(content_type) = headers.get("Content-Type") {
|
||||||
|
if content_type == "application/json" {
|
||||||
|
headers.insert(
|
||||||
|
header::CONTENT_TYPE,
|
||||||
|
HeaderValue::from_static("application/json; charset=utf8"),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(response.map_into_left_body())
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
2
src/middlewares/mod.rs
Normal file
2
src/middlewares/mod.rs
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
pub mod authorization;
|
||||||
|
pub mod content_type;
|
||||||
@@ -1,10 +1,13 @@
|
|||||||
use crate::schema::LessonType::Break;
|
use crate::parser::LessonParseResult::{Lessons, Street};
|
||||||
use crate::schema::{Day, Lesson, LessonSubGroup, LessonTime, LessonType, ScheduleEntity};
|
use crate::parser::schema::LessonType::Break;
|
||||||
use crate::LessonParseResult::{Lessons, Street};
|
use crate::parser::schema::{
|
||||||
use calamine::{open_workbook_from_rs, Reader, Xls};
|
Day, ErrorCell, ErrorCellPos, Lesson, LessonSubGroup, LessonTime, LessonType, ParseError,
|
||||||
use chrono::{Duration, NaiveDateTime};
|
ParseResult, ScheduleEntry,
|
||||||
use fuzzy_matcher::skim::SkimMatcherV2;
|
};
|
||||||
|
use calamine::{Reader, Xls, open_workbook_from_rs};
|
||||||
|
use chrono::{DateTime, Duration, NaiveDateTime, Utc};
|
||||||
use fuzzy_matcher::FuzzyMatcher;
|
use fuzzy_matcher::FuzzyMatcher;
|
||||||
|
use fuzzy_matcher::skim::SkimMatcherV2;
|
||||||
use regex::Regex;
|
use regex::Regex;
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::io::Cursor;
|
use std::io::Cursor;
|
||||||
@@ -12,47 +15,37 @@ use std::sync::LazyLock;
|
|||||||
|
|
||||||
pub mod schema;
|
pub mod schema;
|
||||||
|
|
||||||
|
/// Data cell storing the line.
|
||||||
struct InternalId {
|
struct InternalId {
|
||||||
/**
|
/// Line index.
|
||||||
* Индекс строки
|
|
||||||
*/
|
|
||||||
row: u32,
|
row: u32,
|
||||||
|
|
||||||
/**
|
/// Column index.
|
||||||
* Индекс столбца
|
|
||||||
*/
|
|
||||||
column: u32,
|
column: u32,
|
||||||
|
|
||||||
/**
|
/// Text in the cell.
|
||||||
* Текст в ячейке
|
|
||||||
*/
|
|
||||||
name: String,
|
name: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Data on the time of lessons from the second column of the schedule.
|
||||||
struct InternalTime {
|
struct InternalTime {
|
||||||
/**
|
/// Temporary segment of the lesson.
|
||||||
* Временной отрезок проведения пары
|
|
||||||
*/
|
|
||||||
time_range: LessonTime,
|
time_range: LessonTime,
|
||||||
|
|
||||||
/**
|
/// Type of lesson.
|
||||||
* Тип пары
|
|
||||||
*/
|
|
||||||
lesson_type: LessonType,
|
lesson_type: LessonType,
|
||||||
|
|
||||||
/**
|
/// The lesson index.
|
||||||
* Индекс пары
|
|
||||||
*/
|
|
||||||
default_index: Option<u32>,
|
default_index: Option<u32>,
|
||||||
|
|
||||||
/**
|
/// The frame of the cell.
|
||||||
* Рамка ячейки
|
|
||||||
*/
|
|
||||||
xls_range: ((u32, u32), (u32, u32)),
|
xls_range: ((u32, u32), (u32, u32)),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Working sheet type alias.
|
||||||
type WorkSheet = calamine::Range<calamine::Data>;
|
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> {
|
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)) {
|
let cell_data = if let Some(data) = worksheet.get((row as usize, col as usize)) {
|
||||||
data.to_string()
|
data.to_string()
|
||||||
@@ -64,9 +57,8 @@ fn get_string_from_cell(worksheet: &WorkSheet, row: u32, col: u32) -> Option<Str
|
|||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
|
|
||||||
static NL_RE: LazyLock<Regex, fn() -> Regex> =
|
static NL_RE: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"[\n\r]+").unwrap());
|
||||||
LazyLock::new(|| Regex::new(r"[\n\r]+").unwrap());
|
static SP_RE: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"\s+").unwrap());
|
||||||
static SP_RE: LazyLock<Regex, fn() -> Regex> = LazyLock::new(|| Regex::new(r"\s+").unwrap());
|
|
||||||
|
|
||||||
let trimmed_data = SP_RE
|
let trimmed_data = SP_RE
|
||||||
.replace_all(&NL_RE.replace_all(&cell_data, " "), " ")
|
.replace_all(&NL_RE.replace_all(&cell_data, " "), " ")
|
||||||
@@ -80,6 +72,7 @@ fn get_string_from_cell(worksheet: &WorkSheet, row: u32, col: u32) -> Option<Str
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// 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)) {
|
fn get_merge_from_start(worksheet: &WorkSheet, row: u32, column: u32) -> ((u32, u32), (u32, u32)) {
|
||||||
let worksheet_end = worksheet.end().unwrap();
|
let worksheet_end = worksheet.end().unwrap();
|
||||||
|
|
||||||
@@ -114,7 +107,8 @@ fn get_merge_from_start(worksheet: &WorkSheet, row: u32, column: u32) -> ((u32,
|
|||||||
((row, column), (row_end, column_end))
|
((row, column), (row_end, column_end))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn parse_skeleton(worksheet: &WorkSheet) -> (Vec<InternalId>, Vec<InternalId>) {
|
/// Obtaining a "skeleton" schedule from the working sheet.
|
||||||
|
fn parse_skeleton(worksheet: &WorkSheet) -> Result<(Vec<InternalId>, Vec<InternalId>), ParseError> {
|
||||||
let range = &worksheet;
|
let range = &worksheet;
|
||||||
|
|
||||||
let mut is_parsed = false;
|
let mut is_parsed = false;
|
||||||
@@ -122,8 +116,8 @@ fn parse_skeleton(worksheet: &WorkSheet) -> (Vec<InternalId>, Vec<InternalId>) {
|
|||||||
let mut groups: Vec<InternalId> = Vec::new();
|
let mut groups: Vec<InternalId> = Vec::new();
|
||||||
let mut days: Vec<InternalId> = Vec::new();
|
let mut days: Vec<InternalId> = Vec::new();
|
||||||
|
|
||||||
let start = range.start().expect("Could not find start");
|
let start = range.start().ok_or(ParseError::UnknownWorkSheetRange)?;
|
||||||
let end = range.end().expect("Could not find end");
|
let end = range.end().ok_or(ParseError::UnknownWorkSheetRange)?;
|
||||||
|
|
||||||
let mut row = start.0;
|
let mut row = start.0;
|
||||||
while row < end.0 {
|
while row < end.0 {
|
||||||
@@ -168,15 +162,23 @@ fn parse_skeleton(worksheet: &WorkSheet) -> (Vec<InternalId>, Vec<InternalId>) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
(days, groups)
|
Ok((days, groups))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// The result of obtaining a lesson from the cell.
|
||||||
enum LessonParseResult {
|
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>),
|
Lessons(Vec<Lesson>),
|
||||||
|
|
||||||
|
/// Street on which the Polytechnic Corps is located.
|
||||||
Street(String),
|
Street(String),
|
||||||
}
|
}
|
||||||
|
|
||||||
trait StringInnerSlice {
|
trait StringInnerSlice {
|
||||||
|
/// Obtaining a line from the line on the initial and final index.
|
||||||
fn inner_slice(&self, from: usize, to: usize) -> Self;
|
fn inner_slice(&self, from: usize, to: usize) -> Self;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -189,6 +191,8 @@ impl StringInnerSlice for String {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// noinspection GrazieInspection
|
||||||
|
/// Obtaining a non-standard type of lesson by name.
|
||||||
fn guess_lesson_type(name: &String) -> Option<(String, LessonType)> {
|
fn guess_lesson_type(name: &String) -> Option<(String, LessonType)> {
|
||||||
let map: HashMap<String, LessonType> = HashMap::from([
|
let map: HashMap<String, LessonType> = HashMap::from([
|
||||||
("(консультация)".to_string(), LessonType::Consultation),
|
("(консультация)".to_string(), LessonType::Consultation),
|
||||||
@@ -230,28 +234,29 @@ fn guess_lesson_type(name: &String) -> Option<(String, LessonType)> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Getting a pair or street from a cell.
|
||||||
fn parse_lesson(
|
fn parse_lesson(
|
||||||
worksheet: &WorkSheet,
|
worksheet: &WorkSheet,
|
||||||
day: &mut Day,
|
day: &mut Day,
|
||||||
day_times: &Vec<InternalTime>,
|
day_times: &Vec<InternalTime>,
|
||||||
time: &InternalTime,
|
time: &InternalTime,
|
||||||
column: u32,
|
column: u32,
|
||||||
) -> LessonParseResult {
|
) -> Result<LessonParseResult, ParseError> {
|
||||||
let row = time.xls_range.0.0;
|
let row = time.xls_range.0.0;
|
||||||
|
|
||||||
let (name, lesson_type) = {
|
let (name, lesson_type) = {
|
||||||
let raw_name_opt = get_string_from_cell(&worksheet, row, column);
|
let raw_name_opt = get_string_from_cell(&worksheet, row, column);
|
||||||
if raw_name_opt.is_none() {
|
if raw_name_opt.is_none() {
|
||||||
return Lessons(Vec::new());
|
return Ok(Lessons(Vec::new()));
|
||||||
}
|
}
|
||||||
|
|
||||||
let raw_name = raw_name_opt.unwrap();
|
let raw_name = raw_name_opt.unwrap();
|
||||||
|
|
||||||
static OTHER_STREET_RE: LazyLock<Regex, fn() -> Regex> =
|
static OTHER_STREET_RE: LazyLock<Regex> =
|
||||||
LazyLock::new(|| Regex::new(r"^[А-Я][а-я]+,?\s?[0-9]+$").unwrap());
|
LazyLock::new(|| Regex::new(r"^[А-Я][а-я]+,?\s?[0-9]+$").unwrap());
|
||||||
|
|
||||||
if OTHER_STREET_RE.is_match(&raw_name) {
|
if OTHER_STREET_RE.is_match(&raw_name) {
|
||||||
return Street(raw_name);
|
return Ok(Street(raw_name));
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Some(guess) = guess_lesson_type(&raw_name) {
|
if let Some(guess) = guess_lesson_type(&raw_name) {
|
||||||
@@ -261,7 +266,7 @@ fn parse_lesson(
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
let (default_range, lesson_time): (Option<[u8; 2]>, LessonTime) = {
|
let (default_range, lesson_time) = || -> Result<(Option<[u8; 2]>, LessonTime), ParseError> {
|
||||||
// check if multi-lesson
|
// check if multi-lesson
|
||||||
let cell_range = get_merge_from_start(worksheet, row, column);
|
let cell_range = get_merge_from_start(worksheet, row, column);
|
||||||
|
|
||||||
@@ -270,7 +275,9 @@ fn parse_lesson(
|
|||||||
.filter(|time| time.xls_range.1.0 == cell_range.1.0)
|
.filter(|time| time.xls_range.1.0 == cell_range.1.0)
|
||||||
.collect::<Vec<&InternalTime>>();
|
.collect::<Vec<&InternalTime>>();
|
||||||
|
|
||||||
let end_time = end_time_arr.first().expect("Unable to find lesson time!");
|
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 range: Option<[u8; 2]> = if time.default_index != None {
|
||||||
let default = time.default_index.unwrap() as u8;
|
let default = time.default_index.unwrap() as u8;
|
||||||
@@ -284,10 +291,10 @@ fn parse_lesson(
|
|||||||
end: end_time.time_range.end,
|
end: end_time.time_range.end,
|
||||||
};
|
};
|
||||||
|
|
||||||
(range, time)
|
Ok((range, time))
|
||||||
};
|
}()?;
|
||||||
|
|
||||||
let (name, mut subgroups) = parse_name_and_subgroups(&name);
|
let (name, mut subgroups) = parse_name_and_subgroups(&name)?;
|
||||||
|
|
||||||
{
|
{
|
||||||
let cabinets: Vec<String> = parse_cabinets(worksheet, row, column + 1);
|
let cabinets: Vec<String> = parse_cabinets(worksheet, row, column + 1);
|
||||||
@@ -343,12 +350,12 @@ fn parse_lesson(
|
|||||||
};
|
};
|
||||||
|
|
||||||
let prev_lesson = if day.lessons.len() == 0 {
|
let prev_lesson = if day.lessons.len() == 0 {
|
||||||
return Lessons(Vec::from([lesson]));
|
return Ok(Lessons(Vec::from([lesson])));
|
||||||
} else {
|
} else {
|
||||||
&day.lessons[day.lessons.len() - 1]
|
&day.lessons[day.lessons.len() - 1]
|
||||||
};
|
};
|
||||||
|
|
||||||
Lessons(Vec::from([
|
Ok(Lessons(Vec::from([
|
||||||
Lesson {
|
Lesson {
|
||||||
lesson_type: Break,
|
lesson_type: Break,
|
||||||
default_range: None,
|
default_range: None,
|
||||||
@@ -361,9 +368,10 @@ fn parse_lesson(
|
|||||||
group: None,
|
group: None,
|
||||||
},
|
},
|
||||||
lesson,
|
lesson,
|
||||||
]))
|
])))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Obtaining a list of cabinets to the right of the lesson cell.
|
||||||
fn parse_cabinets(worksheet: &WorkSheet, row: u32, column: u32) -> Vec<String> {
|
fn parse_cabinets(worksheet: &WorkSheet, row: u32, column: u32) -> Vec<String> {
|
||||||
let mut cabinets: Vec<String> = Vec::new();
|
let mut cabinets: Vec<String> = Vec::new();
|
||||||
|
|
||||||
@@ -381,15 +389,14 @@ fn parse_cabinets(worksheet: &WorkSheet, row: u32, column: u32) -> Vec<String> {
|
|||||||
cabinets
|
cabinets
|
||||||
}
|
}
|
||||||
|
|
||||||
fn parse_name_and_subgroups(name: &String) -> (String, Vec<LessonSubGroup>) {
|
/// Getting the "pure" name of the lesson and list of teachers from the text of the lesson cell.
|
||||||
static LESSON_RE: LazyLock<Regex, fn() -> Regex> =
|
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());
|
LazyLock::new(|| Regex::new(r"(?:[А-Я][а-я]+[А-Я]{2}(?:\([0-9][а-я]+\))?)+$").unwrap());
|
||||||
static TEACHER_RE: LazyLock<Regex, fn() -> Regex> =
|
static TEACHER_RE: LazyLock<Regex> =
|
||||||
LazyLock::new(|| Regex::new(r"([А-Я][а-я]+)([А-Я])([А-Я])(?:\(([0-9])[а-я]+\))?").unwrap());
|
LazyLock::new(|| Regex::new(r"([А-Я][а-я]+)([А-Я])([А-Я])(?:\(([0-9])[а-я]+\))?").unwrap());
|
||||||
static CLEAN_RE: LazyLock<Regex, fn() -> Regex> =
|
static CLEAN_RE: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"[\s.,]+").unwrap());
|
||||||
LazyLock::new(|| Regex::new(r"[\s.,]+").unwrap());
|
static END_CLEAN_RE: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"[.\s]+$").unwrap());
|
||||||
static NAME_CLEAN_RE: LazyLock<Regex, fn() -> Regex> =
|
|
||||||
LazyLock::new(|| Regex::new(r"\.\s+$").unwrap());
|
|
||||||
|
|
||||||
let (teachers, lesson_name) = {
|
let (teachers, lesson_name) = {
|
||||||
let clean_name = CLEAN_RE.replace_all(&name, "").to_string();
|
let clean_name = CLEAN_RE.replace_all(&name, "").to_string();
|
||||||
@@ -400,11 +407,13 @@ fn parse_name_and_subgroups(name: &String) -> (String, Vec<LessonSubGroup>) {
|
|||||||
let capture_name: String = capture_str.chars().take(5).collect();
|
let capture_name: String = capture_str.chars().take(5).collect();
|
||||||
|
|
||||||
(
|
(
|
||||||
NAME_CLEAN_RE.replace(&capture_str, "").to_string(),
|
END_CLEAN_RE.replace(&capture_str, "").to_string(),
|
||||||
name[0..name.find(&*capture_name).unwrap()].to_string(),
|
END_CLEAN_RE
|
||||||
|
.replace(&name[0..name.find(&*capture_name).unwrap()], "")
|
||||||
|
.to_string(),
|
||||||
)
|
)
|
||||||
} else {
|
} else {
|
||||||
return (NAME_CLEAN_RE.replace(&name, "").to_string(), Vec::new());
|
return Ok((END_CLEAN_RE.replace(&name, "").to_string(), Vec::new()));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -414,14 +423,9 @@ fn parse_name_and_subgroups(name: &String) -> (String, Vec<LessonSubGroup>) {
|
|||||||
|
|
||||||
for captures in teacher_it {
|
for captures in teacher_it {
|
||||||
subgroups.push(LessonSubGroup {
|
subgroups.push(LessonSubGroup {
|
||||||
number: if let Some(capture) = captures.get(4) {
|
number: match captures.get(4) {
|
||||||
capture
|
Some(capture) => capture.as_str().to_string().parse::<u8>().unwrap(),
|
||||||
.as_str()
|
None => 0,
|
||||||
.to_string()
|
|
||||||
.parse::<u8>()
|
|
||||||
.expect("Unable to read subgroup index!")
|
|
||||||
} else {
|
|
||||||
0
|
|
||||||
},
|
},
|
||||||
cabinet: None,
|
cabinet: None,
|
||||||
teacher: format!(
|
teacher: format!(
|
||||||
@@ -430,7 +434,7 @@ fn parse_name_and_subgroups(name: &String) -> (String, Vec<LessonSubGroup>) {
|
|||||||
captures.get(2).unwrap().as_str().to_string(),
|
captures.get(2).unwrap().as_str().to_string(),
|
||||||
captures.get(3).unwrap().as_str().to_string()
|
captures.get(3).unwrap().as_str().to_string()
|
||||||
),
|
),
|
||||||
})
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// фикс, если у кого-то отсутствует индекс подгруппы
|
// фикс, если у кого-то отсутствует индекс подгруппы
|
||||||
@@ -467,13 +471,14 @@ fn parse_name_and_subgroups(name: &String) -> (String, Vec<LessonSubGroup>) {
|
|||||||
subgroups.reverse()
|
subgroups.reverse()
|
||||||
}
|
}
|
||||||
|
|
||||||
(lesson_name, subgroups)
|
Ok((lesson_name, subgroups))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Conversion of the list of couples of groups in the list of lessons of teachers.
|
||||||
fn convert_groups_to_teachers(
|
fn convert_groups_to_teachers(
|
||||||
groups: &HashMap<String, ScheduleEntity>,
|
groups: &HashMap<String, ScheduleEntry>,
|
||||||
) -> HashMap<String, ScheduleEntity> {
|
) -> HashMap<String, ScheduleEntry> {
|
||||||
let mut teachers: HashMap<String, ScheduleEntity> = HashMap::new();
|
let mut teachers: HashMap<String, ScheduleEntry> = HashMap::new();
|
||||||
|
|
||||||
let empty_days: Vec<Day> = groups
|
let empty_days: Vec<Day> = groups
|
||||||
.values()
|
.values()
|
||||||
@@ -510,7 +515,7 @@ fn convert_groups_to_teachers(
|
|||||||
if !teachers.contains_key(&subgroup.teacher) {
|
if !teachers.contains_key(&subgroup.teacher) {
|
||||||
teachers.insert(
|
teachers.insert(
|
||||||
subgroup.teacher.clone(),
|
subgroup.teacher.clone(),
|
||||||
ScheduleEntity {
|
ScheduleEntry {
|
||||||
name: subgroup.teacher.clone(),
|
name: subgroup.teacher.clone(),
|
||||||
days: empty_days.to_vec(),
|
days: empty_days.to_vec(),
|
||||||
},
|
},
|
||||||
@@ -535,34 +540,58 @@ fn convert_groups_to_teachers(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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
|
teachers
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn parse_xls(
|
/// Reading XLS Document from the buffer and converting it into the schedule ready to use.
|
||||||
buffer: &Vec<u8>,
|
///
|
||||||
) -> (
|
/// # Arguments
|
||||||
HashMap<String, ScheduleEntity>,
|
///
|
||||||
HashMap<String, ScheduleEntity>,
|
/// * `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 cursor = Cursor::new(&buffer);
|
||||||
let mut workbook: Xls<_> = open_workbook_from_rs(cursor).expect("Can't open workbook");
|
let mut workbook: Xls<_> =
|
||||||
|
open_workbook_from_rs(cursor).map_err(|e| ParseError::BadXLS(std::sync::Arc::new(e)))?;
|
||||||
|
|
||||||
let worksheet: WorkSheet = workbook
|
let worksheet: WorkSheet = workbook
|
||||||
.worksheets()
|
.worksheets()
|
||||||
.first()
|
.first()
|
||||||
.expect("No worksheet found")
|
.ok_or(ParseError::NoWorkSheets)?
|
||||||
.1
|
.1
|
||||||
.to_owned();
|
.to_owned();
|
||||||
|
|
||||||
let (days_markup, groups_markup) = parse_skeleton(&worksheet);
|
let (days_markup, groups_markup) = parse_skeleton(&worksheet)?;
|
||||||
|
|
||||||
let mut groups: HashMap<String, ScheduleEntity> = HashMap::new();
|
let mut groups: HashMap<String, ScheduleEntry> = HashMap::new();
|
||||||
let mut days_times: Vec<Vec<InternalTime>> = Vec::new();
|
let mut days_times: Vec<Vec<InternalTime>> = Vec::new();
|
||||||
|
|
||||||
let saturday_end_row = worksheet.end().unwrap().0;
|
let saturday_end_row = worksheet.end().unwrap().0;
|
||||||
|
|
||||||
for group_markup in groups_markup {
|
for group_markup in groups_markup {
|
||||||
let mut group = ScheduleEntity {
|
let mut group = ScheduleEntry {
|
||||||
name: group_markup.name,
|
name: group_markup.name,
|
||||||
days: Vec::new(),
|
days: Vec::new(),
|
||||||
};
|
};
|
||||||
@@ -631,12 +660,12 @@ pub fn parse_xls(
|
|||||||
|
|
||||||
// time
|
// time
|
||||||
let time_range = {
|
let time_range = {
|
||||||
static TIME_RE: LazyLock<Regex, fn() -> 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 = TIME_RE
|
let parse_res = TIME_RE.captures(&time).ok_or(ParseError::GlobalTime(
|
||||||
.captures(&time)
|
ErrorCell::new(row, lesson_time_column, time.clone()),
|
||||||
.expect("Unable to obtain lesson start and end!");
|
))?;
|
||||||
|
|
||||||
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();
|
||||||
@@ -644,13 +673,15 @@ pub fn parse_xls(
|
|||||||
let end_match = parse_res.get(2).unwrap().as_str();
|
let end_match = parse_res.get(2).unwrap().as_str();
|
||||||
let end_parts: Vec<&str> = end_match.split(".").collect();
|
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 {
|
LessonTime {
|
||||||
start: day.date.clone()
|
start: GET_TIME(day.date.clone(), &start_parts),
|
||||||
+ Duration::hours(start_parts[0].parse().unwrap())
|
end: GET_TIME(day.date.clone(), &end_parts),
|
||||||
+ Duration::minutes(start_parts[1].parse().unwrap()),
|
|
||||||
end: day.date.clone()
|
|
||||||
+ Duration::hours(end_parts[0].parse().unwrap())
|
|
||||||
+ Duration::minutes(end_parts[1].parse().unwrap()),
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -674,7 +705,7 @@ pub fn parse_xls(
|
|||||||
&day_times,
|
&day_times,
|
||||||
&time,
|
&time,
|
||||||
group_markup.column,
|
group_markup.column,
|
||||||
) {
|
)? {
|
||||||
Lessons(l) => day.lessons.append(l),
|
Lessons(l) => day.lessons.append(l),
|
||||||
Street(s) => day.street = Some(s.to_owned()),
|
Street(s) => day.street = Some(s.to_owned()),
|
||||||
}
|
}
|
||||||
@@ -686,19 +717,27 @@ pub fn parse_xls(
|
|||||||
groups.insert(group.name.clone(), group);
|
groups.insert(group.name.clone(), group);
|
||||||
}
|
}
|
||||||
|
|
||||||
(convert_groups_to_teachers(&groups), groups)
|
Ok(ParseResult {
|
||||||
|
teachers: convert_groups_to_teachers(&groups),
|
||||||
|
groups,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
pub mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
|
pub fn test_result() -> Result<ParseResult, ParseError> {
|
||||||
|
parse_xls(&include_bytes!("../../schedule.xls").to_vec())
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn read() {
|
fn read() {
|
||||||
let buffer: Vec<u8> = include_bytes!("../../../../schedule.xls").to_vec();
|
let result = test_result();
|
||||||
let result = parse_xls(&buffer);
|
|
||||||
|
|
||||||
assert_ne!(result.0.len(), 0);
|
assert!(result.is_ok());
|
||||||
assert_ne!(result.1.len(), 0);
|
|
||||||
|
assert_ne!(result.as_ref().unwrap().groups.len(), 0);
|
||||||
|
assert_ne!(result.as_ref().unwrap().teachers.len(), 0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
180
src/parser/schema.rs
Normal file
180
src/parser/schema.rs
Normal file
@@ -0,0 +1,180 @@
|
|||||||
|
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"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,2 +1,8 @@
|
|||||||
pub mod sign_in;
|
mod sign_in;
|
||||||
mod schema;
|
mod sign_up;
|
||||||
|
mod shared;
|
||||||
|
|
||||||
|
pub use sign_in::*;
|
||||||
|
pub use sign_up::*;
|
||||||
|
|
||||||
|
// TODO: change-password
|
||||||
@@ -1,56 +0,0 @@
|
|||||||
use crate::database::models::User;
|
|
||||||
use serde::{Deserialize, Serialize, Serializer};
|
|
||||||
|
|
||||||
#[derive(Deserialize)]
|
|
||||||
pub struct SignInDto {
|
|
||||||
pub username: String,
|
|
||||||
pub password: String,
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct SignInResult(Result<SignInOk, SignInErr>);
|
|
||||||
|
|
||||||
#[derive(Serialize)]
|
|
||||||
#[serde(rename_all = "camelCase")]
|
|
||||||
pub struct SignInOk {
|
|
||||||
id: String,
|
|
||||||
access_token: String,
|
|
||||||
group: String,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Serialize)]
|
|
||||||
pub struct SignInErr {
|
|
||||||
code: SignInErrCode,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Serialize)]
|
|
||||||
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
|
|
||||||
pub enum SignInErrCode {
|
|
||||||
IncorrectCredentials,
|
|
||||||
InvalidVkAccessToken,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl SignInResult {
|
|
||||||
pub fn ok(user: &User) -> Self {
|
|
||||||
Self(Ok(SignInOk {
|
|
||||||
id: user.id.clone(),
|
|
||||||
access_token: user.access_token.clone(),
|
|
||||||
group: user.group.clone(),
|
|
||||||
}))
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn err(code: SignInErrCode) -> SignInResult {
|
|
||||||
Self(Err(SignInErr { code }))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Serialize for SignInResult {
|
|
||||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
|
||||||
where
|
|
||||||
S: Serializer,
|
|
||||||
{
|
|
||||||
match &self.0 {
|
|
||||||
Ok(ok) => serializer.serialize_some(&ok),
|
|
||||||
Err(err) => serializer.serialize_some(&err),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
83
src/routes/auth/shared.rs
Normal file
83
src/routes/auth/shared.rs
Normal file
@@ -0,0 +1,83 @@
|
|||||||
|
use jsonwebtoken::errors::ErrorKind;
|
||||||
|
use jsonwebtoken::{decode, Algorithm, DecodingKey, Validation};
|
||||||
|
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)]
|
||||||
|
struct Claims {
|
||||||
|
sub: i32,
|
||||||
|
iis: String,
|
||||||
|
jti: i32,
|
||||||
|
app: i32,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, PartialEq)]
|
||||||
|
pub enum Error {
|
||||||
|
JwtError(ErrorKind),
|
||||||
|
InvalidSignature,
|
||||||
|
InvalidToken,
|
||||||
|
Expired,
|
||||||
|
UnknownIssuer(String),
|
||||||
|
UnknownType(i32),
|
||||||
|
UnknownClientId(i32),
|
||||||
|
}
|
||||||
|
|
||||||
|
//noinspection SpellCheckingInspection
|
||||||
|
const VK_PUBLIC_KEY: &str = concat!(
|
||||||
|
"-----BEGIN PUBLIC KEY-----\n",
|
||||||
|
"MIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKCAgEAvsvJlhFX9Ju/pvCz1frB\n",
|
||||||
|
"DgJs592VjdwQuRAmnlJAItyHkoiDIOEocPzgcUBTbDf1plDcTyO2RCkUt0pz0WK6\n",
|
||||||
|
"6HNhpJyIfARjaWHeUlv4TpuHXAJJsBKklkU2gf1cjID+40sWWYjtq5dAkXnSJUVA\n",
|
||||||
|
"UR+sq0lJ7GmTdJtAr8hzESqGEcSP15PTs7VUdHZ1nkC2XgkuR8KmKAUb388ji1Q4\n",
|
||||||
|
"n02rJNOPQgd9r0ac4N2v/yTAFPXumO78N25bpcuWf5vcL9e8THk/U2zt7wf+aAWL\n",
|
||||||
|
"748e0pREqNluTBJNZfmhC79Xx6GHtwqHyyduiqfPmejmiujNM/rqnA4e30Tg86Yn\n",
|
||||||
|
"cNZ6vLJyF72Eva1wXchukH/aLispbY+EqNPxxn4zzCWaLKHG87gaCxpVv9Tm0jSD\n",
|
||||||
|
"2es22NjrUbtb+2pAGnXbyDp2eGUqw0RrTQFZqt/VcmmSCE45FlcZMT28otrwG1ZB\n",
|
||||||
|
"kZAb5Js3wLEch3ZfYL8sjhyNRPBmJBrAvzrd8qa3rdUjkC9sKyjGAaHu2MNmFl1Y\n",
|
||||||
|
"JFQ3J54tGpkGgJjD7Kz3w0K6OiPDlVCNQN5sqXm24fCw85Pbi8SJiaLTp/CImrs1\n",
|
||||||
|
"Z3nHW5q8hljA7OGmqfOP0nZS/5zW9GHPyepsI1rW6CympYLJ15WeNzePxYS5KEX9\n",
|
||||||
|
"EncmkSD9b45ge95hJeJZteUCAwEAAQ==\n",
|
||||||
|
"-----END PUBLIC KEY-----"
|
||||||
|
);
|
||||||
|
|
||||||
|
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(Algorithm::RS256)) {
|
||||||
|
Ok(token_data) => {
|
||||||
|
let claims = token_data.claims;
|
||||||
|
|
||||||
|
if claims.iis != "VK" {
|
||||||
|
Err(Error::UnknownIssuer(claims.iis))
|
||||||
|
} else if claims.jti != 21 {
|
||||||
|
Err(Error::UnknownType(claims.jti))
|
||||||
|
} else if claims.app != client_id {
|
||||||
|
Err(Error::UnknownClientId(claims.app))
|
||||||
|
} else {
|
||||||
|
Ok(claims.sub)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(err) => Err(match err.into_kind() {
|
||||||
|
ErrorKind::InvalidToken => Error::InvalidToken,
|
||||||
|
ErrorKind::InvalidSignature => Error::InvalidSignature,
|
||||||
|
ErrorKind::InvalidAlgorithmName => Error::InvalidToken,
|
||||||
|
ErrorKind::MissingRequiredClaim(_) => Error::InvalidToken,
|
||||||
|
ErrorKind::ExpiredSignature => Error::Expired,
|
||||||
|
ErrorKind::InvalidAlgorithm => Error::InvalidToken,
|
||||||
|
ErrorKind::MissingAlgorithm => Error::InvalidToken,
|
||||||
|
ErrorKind::Base64(_) => Error::InvalidToken,
|
||||||
|
ErrorKind::Json(_) => Error::InvalidToken,
|
||||||
|
ErrorKind::Utf8(_) => Error::InvalidToken,
|
||||||
|
kind => Error::JwtError(kind),
|
||||||
|
}),
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,26 +1,231 @@
|
|||||||
|
use self::schema::*;
|
||||||
|
use crate::database::driver;
|
||||||
use crate::database::models::User;
|
use crate::database::models::User;
|
||||||
use crate::routes::auth::schema::SignInErrCode::IncorrectCredentials;
|
use crate::routes::auth::shared::parse_vk_id;
|
||||||
use crate::routes::auth::schema::{SignInDto, SignInResult};
|
use crate::routes::auth::sign_in::schema::SignInData::{Default, Vk};
|
||||||
use crate::AppState;
|
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 actix_web::{post, web};
|
||||||
use diesel::{ExpressionMethods, QueryDsl, RunQueryDsl, SelectableHelper};
|
use diesel::SaveChangesDsl;
|
||||||
use std::ops::DerefMut;
|
|
||||||
use web::Json;
|
use web::Json;
|
||||||
|
|
||||||
#[post("/sign-in")]
|
async fn sign_in_combined(
|
||||||
pub async fn sign_in(data: Json<SignInDto>, app_state: web::Data<AppState>) -> Json<SignInResult> {
|
data: SignInData,
|
||||||
use crate::database::schema::users::dsl::*;
|
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),
|
||||||
|
};
|
||||||
|
|
||||||
match {
|
match user {
|
||||||
let mut lock = app_state.database.lock().unwrap();
|
Ok(mut user) => {
|
||||||
let connection = lock.deref_mut();
|
if let Default(data) = data {
|
||||||
|
match bcrypt::verify(&data.password, &user.password) {
|
||||||
|
Ok(result) => {
|
||||||
|
if !result {
|
||||||
|
return Err(ErrorCode::IncorrectCredentials);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(_) => {
|
||||||
|
return Err(ErrorCode::IncorrectCredentials);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
users
|
user.access_token = utility::jwt::encode(&user.id);
|
||||||
.filter(username.eq(data.username.clone()))
|
|
||||||
.select(User::as_select())
|
app_state.database.scope(|conn| {
|
||||||
.first(connection)
|
user.save_changes::<User>(conn)
|
||||||
} {
|
.expect("Failed to update user")
|
||||||
Ok(user) => Json(SignInResult::ok(&user)),
|
});
|
||||||
Err(_) => Json(SignInResult::err(IncorrectCredentials)),
|
|
||||||
|
Ok(user.into())
|
||||||
|
}
|
||||||
|
|
||||||
|
Err(_) => Err(ErrorCode::IncorrectCredentials),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[utoipa::path(responses(
|
||||||
|
(status = OK, body = UserResponse),
|
||||||
|
(status = NOT_ACCEPTABLE, body = ResponseError<ErrorCode>)
|
||||||
|
))]
|
||||||
|
#[post("/sign-in")]
|
||||||
|
pub async fn sign_in(data: Json<Request>, app_state: web::Data<AppState>) -> ServiceResponse {
|
||||||
|
sign_in_combined(Default(data.into_inner()), &app_state)
|
||||||
|
.await
|
||||||
|
.into()
|
||||||
|
}
|
||||||
|
|
||||||
|
#[utoipa::path(responses(
|
||||||
|
(status = OK, body = UserResponse),
|
||||||
|
(status = NOT_ACCEPTABLE, body = ResponseError<ErrorCode>)
|
||||||
|
))]
|
||||||
|
#[post("/sign-in-vk")]
|
||||||
|
pub async fn sign_in_vk(
|
||||||
|
data_json: Json<vk::Request>,
|
||||||
|
app_state: web::Data<AppState>,
|
||||||
|
) -> ServiceResponse {
|
||||||
|
let data = data_json.into_inner();
|
||||||
|
|
||||||
|
match parse_vk_id(&data.access_token, app_state.vk_id.client_id) {
|
||||||
|
Ok(id) => sign_in_combined(Vk(id), &app_state).await.into(),
|
||||||
|
Err(_) => ErrorCode::InvalidVkAccessToken.into_response(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
mod schema {
|
||||||
|
use crate::routes::schema::user::UserResponse;
|
||||||
|
use actix_macros::{IntoResponseError, StatusCode};
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
use utoipa::ToSchema;
|
||||||
|
|
||||||
|
#[derive(Deserialize, Serialize, ToSchema)]
|
||||||
|
#[schema(as = SignIn::Request)]
|
||||||
|
pub struct Request {
|
||||||
|
/// User name.
|
||||||
|
#[schema(examples("n08i40k"))]
|
||||||
|
pub username: String,
|
||||||
|
|
||||||
|
/// Password.
|
||||||
|
pub password: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub mod vk {
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
use utoipa::ToSchema;
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, ToSchema)]
|
||||||
|
#[serde(rename_all = "camelCase")]
|
||||||
|
#[schema(as = SignInVk::Request)]
|
||||||
|
pub struct Request {
|
||||||
|
/// VK ID token.
|
||||||
|
pub access_token: String,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub type ServiceResponse = crate::routes::schema::Response<UserResponse, ErrorCode>;
|
||||||
|
|
||||||
|
#[derive(Serialize, ToSchema, Clone, IntoResponseError, StatusCode)]
|
||||||
|
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
|
||||||
|
#[schema(as = SignIn::ErrorCode)]
|
||||||
|
#[status_code = "actix_web::http::StatusCode::NOT_ACCEPTABLE"]
|
||||||
|
pub enum ErrorCode {
|
||||||
|
/// Incorrect username or password.
|
||||||
|
IncorrectCredentials,
|
||||||
|
|
||||||
|
/// Invalid VK ID token.
|
||||||
|
InvalidVkAccessToken,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Internal
|
||||||
|
|
||||||
|
/// Type of authorization.
|
||||||
|
pub enum SignInData {
|
||||||
|
/// User and password name and password.
|
||||||
|
Default(Request),
|
||||||
|
|
||||||
|
/// Identifier of the attached account VK.
|
||||||
|
Vk(i32),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::schema::*;
|
||||||
|
use crate::database::driver;
|
||||||
|
use crate::database::models::{User, UserRole};
|
||||||
|
use crate::routes::auth::sign_in::sign_in;
|
||||||
|
use crate::test_env::tests::{static_app_state, test_app_state, test_env};
|
||||||
|
use crate::utility;
|
||||||
|
use actix_test::test_app;
|
||||||
|
use actix_web::dev::ServiceResponse;
|
||||||
|
use actix_web::http::Method;
|
||||||
|
use actix_web::http::StatusCode;
|
||||||
|
use actix_web::test;
|
||||||
|
use sha1::{Digest, Sha1};
|
||||||
|
use std::fmt::Write;
|
||||||
|
|
||||||
|
async fn sign_in_client(data: Request) -> ServiceResponse {
|
||||||
|
let app = test_app(test_app_state().await, sign_in).await;
|
||||||
|
|
||||||
|
let req = test::TestRequest::with_uri("/sign-in")
|
||||||
|
.method(Method::POST)
|
||||||
|
.set_json(data)
|
||||||
|
.to_request();
|
||||||
|
|
||||||
|
test::call_service(&app, req).await
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn prepare(username: String) {
|
||||||
|
let id = {
|
||||||
|
let mut sha = Sha1::new();
|
||||||
|
sha.update(&username);
|
||||||
|
|
||||||
|
let result = sha.finalize();
|
||||||
|
let bytes = &result[..12];
|
||||||
|
|
||||||
|
let mut hex = String::new();
|
||||||
|
for byte in bytes {
|
||||||
|
write!(&mut hex, "{:02x}", byte).unwrap();
|
||||||
|
}
|
||||||
|
|
||||||
|
hex
|
||||||
|
};
|
||||||
|
|
||||||
|
test_env();
|
||||||
|
|
||||||
|
let app_state = static_app_state().await;
|
||||||
|
driver::users::insert_or_ignore(
|
||||||
|
&app_state,
|
||||||
|
&User {
|
||||||
|
id: id.clone(),
|
||||||
|
username,
|
||||||
|
password: bcrypt::hash("example".to_string(), bcrypt::DEFAULT_COST).unwrap(),
|
||||||
|
vk_id: None,
|
||||||
|
access_token: utility::jwt::encode(&id),
|
||||||
|
group: "ИС-214/23".to_string(),
|
||||||
|
role: UserRole::Student,
|
||||||
|
version: "1.0.0".to_string(),
|
||||||
|
},
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
|
}
|
||||||
|
|
||||||
|
#[actix_web::test]
|
||||||
|
async fn sign_in_ok() {
|
||||||
|
prepare("test::sign_in_ok".to_string()).await;
|
||||||
|
|
||||||
|
let resp = sign_in_client(Request {
|
||||||
|
username: "test::sign_in_ok".to_string(),
|
||||||
|
password: "example".to_string(),
|
||||||
|
})
|
||||||
|
.await;
|
||||||
|
|
||||||
|
assert_eq!(resp.status(), StatusCode::OK);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[actix_web::test]
|
||||||
|
async fn sign_in_err() {
|
||||||
|
prepare("test::sign_in_err".to_string()).await;
|
||||||
|
|
||||||
|
let invalid_username = sign_in_client(Request {
|
||||||
|
username: "test::sign_in_err::username".to_string(),
|
||||||
|
password: "example".to_string(),
|
||||||
|
})
|
||||||
|
.await;
|
||||||
|
|
||||||
|
assert_eq!(invalid_username.status(), StatusCode::NOT_ACCEPTABLE);
|
||||||
|
|
||||||
|
let invalid_password = sign_in_client(Request {
|
||||||
|
username: "test::sign_in_err".to_string(),
|
||||||
|
password: "bad_password".to_string(),
|
||||||
|
})
|
||||||
|
.await;
|
||||||
|
|
||||||
|
assert_eq!(invalid_password.status(), StatusCode::NOT_ACCEPTABLE);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
352
src/routes/auth/sign_up.rs
Normal file
352
src/routes/auth/sign_up.rs
Normal file
@@ -0,0 +1,352 @@
|
|||||||
|
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::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(
|
||||||
|
data: SignUpData,
|
||||||
|
app_state: &web::Data<AppState>,
|
||||||
|
) -> Result<UserResponse, ErrorCode> {
|
||||||
|
// If user selected forbidden role.
|
||||||
|
if data.role == UserRole::Admin {
|
||||||
|
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 user with specified username already exists.
|
||||||
|
if driver::users::contains_by_username(&app_state, &data.username) {
|
||||||
|
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) {
|
||||||
|
return Err(ErrorCode::VkAlreadyExists);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let user = data.into();
|
||||||
|
driver::users::insert(&app_state, &user).unwrap();
|
||||||
|
|
||||||
|
Ok(UserResponse::from(&user)).into()
|
||||||
|
}
|
||||||
|
|
||||||
|
#[utoipa::path(responses(
|
||||||
|
(status = OK, body = UserResponse),
|
||||||
|
(status = NOT_ACCEPTABLE, body = ResponseError<ErrorCode>)
|
||||||
|
))]
|
||||||
|
#[post("/sign-up")]
|
||||||
|
pub async fn sign_up(data_json: Json<Request>, app_state: web::Data<AppState>) -> ServiceResponse {
|
||||||
|
let data = data_json.into_inner();
|
||||||
|
|
||||||
|
sign_up_combined(
|
||||||
|
SignUpData {
|
||||||
|
username: data.username,
|
||||||
|
password: data.password,
|
||||||
|
vk_id: None,
|
||||||
|
group: data.group,
|
||||||
|
role: data.role,
|
||||||
|
version: data.version,
|
||||||
|
},
|
||||||
|
&app_state,
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
.into()
|
||||||
|
}
|
||||||
|
|
||||||
|
#[utoipa::path(responses(
|
||||||
|
(status = OK, body = UserResponse),
|
||||||
|
(status = NOT_ACCEPTABLE, body = ResponseError<ErrorCode>)
|
||||||
|
))]
|
||||||
|
#[post("/sign-up-vk")]
|
||||||
|
pub async fn sign_up_vk(
|
||||||
|
data_json: Json<vk::Request>,
|
||||||
|
app_state: web::Data<AppState>,
|
||||||
|
) -> ServiceResponse {
|
||||||
|
let data = data_json.into_inner();
|
||||||
|
|
||||||
|
match parse_vk_id(&data.access_token, app_state.vk_id.client_id) {
|
||||||
|
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()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
mod schema {
|
||||||
|
use crate::database::models::{User, UserRole};
|
||||||
|
use crate::routes::schema::user::UserResponse;
|
||||||
|
use crate::utility;
|
||||||
|
use actix_macros::{IntoResponseError, StatusCode};
|
||||||
|
use objectid::ObjectId;
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
|
/// WEB
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, utoipa::ToSchema)]
|
||||||
|
#[schema(as = SignUp::Request)]
|
||||||
|
pub struct Request {
|
||||||
|
/// User name.
|
||||||
|
#[schema(examples("n08i40k"))]
|
||||||
|
pub username: String,
|
||||||
|
|
||||||
|
/// Password.
|
||||||
|
pub password: String,
|
||||||
|
|
||||||
|
/// Group.
|
||||||
|
#[schema(examples("ИС-214/23"))]
|
||||||
|
pub group: String,
|
||||||
|
|
||||||
|
/// Role.
|
||||||
|
pub role: UserRole,
|
||||||
|
|
||||||
|
/// Version of the installed Polytechnic+ application.
|
||||||
|
#[schema(examples("3.0.0"))]
|
||||||
|
pub version: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub mod vk {
|
||||||
|
use crate::database::models::UserRole;
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, utoipa::ToSchema)]
|
||||||
|
#[serde(rename_all = "camelCase")]
|
||||||
|
#[schema(as = SignUpVk::Request)]
|
||||||
|
pub struct Request {
|
||||||
|
/// VK ID token.
|
||||||
|
pub access_token: String,
|
||||||
|
|
||||||
|
/// User name.
|
||||||
|
#[schema(examples("n08i40k"))]
|
||||||
|
pub username: String,
|
||||||
|
|
||||||
|
/// Group.
|
||||||
|
#[schema(examples("ИС-214/23"))]
|
||||||
|
pub group: String,
|
||||||
|
|
||||||
|
/// Role.
|
||||||
|
pub role: UserRole,
|
||||||
|
|
||||||
|
/// Version of the installed Polytechnic+ application.
|
||||||
|
#[schema(examples("3.0.0"))]
|
||||||
|
pub version: String,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub type ServiceResponse = crate::routes::schema::Response<UserResponse, ErrorCode>;
|
||||||
|
|
||||||
|
#[derive(Clone, Serialize, utoipa::ToSchema, IntoResponseError, StatusCode)]
|
||||||
|
#[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.
|
||||||
|
DisallowedRole,
|
||||||
|
|
||||||
|
/// Unknown name of the group.
|
||||||
|
InvalidGroupName,
|
||||||
|
|
||||||
|
/// User with this name is already registered.
|
||||||
|
UsernameAlreadyExists,
|
||||||
|
|
||||||
|
/// Invalid VK ID token.
|
||||||
|
InvalidVkAccessToken,
|
||||||
|
|
||||||
|
/// User with such an account VK is already registered.
|
||||||
|
VkAlreadyExists,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Internal
|
||||||
|
|
||||||
|
/// Data for registration.
|
||||||
|
pub struct SignUpData {
|
||||||
|
/// User name.
|
||||||
|
pub username: String,
|
||||||
|
|
||||||
|
/// Password.
|
||||||
|
///
|
||||||
|
/// Should be present even if registration occurs using the VK ID token.
|
||||||
|
pub password: String,
|
||||||
|
|
||||||
|
/// Account identifier VK.
|
||||||
|
pub vk_id: Option<i32>,
|
||||||
|
|
||||||
|
/// Group.
|
||||||
|
pub group: String,
|
||||||
|
|
||||||
|
/// Role.
|
||||||
|
pub role: UserRole,
|
||||||
|
|
||||||
|
/// Version of the installed Polytechnic+ application.
|
||||||
|
pub version: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Into<User> for SignUpData {
|
||||||
|
fn into(self) -> User {
|
||||||
|
let id = ObjectId::new().unwrap().to_string();
|
||||||
|
let access_token = utility::jwt::encode(&id);
|
||||||
|
|
||||||
|
User {
|
||||||
|
id,
|
||||||
|
username: self.username,
|
||||||
|
password: bcrypt::hash(self.password, bcrypt::DEFAULT_COST).unwrap(),
|
||||||
|
vk_id: self.vk_id,
|
||||||
|
access_token,
|
||||||
|
group: self.group,
|
||||||
|
role: self.role,
|
||||||
|
version: self.version,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use crate::database::driver;
|
||||||
|
use crate::database::models::UserRole;
|
||||||
|
use crate::routes::auth::sign_up::schema::Request;
|
||||||
|
use crate::routes::auth::sign_up::sign_up;
|
||||||
|
use crate::test_env::tests::{static_app_state, test_app_state, test_env};
|
||||||
|
use actix_test::test_app;
|
||||||
|
use actix_web::dev::ServiceResponse;
|
||||||
|
use actix_web::http::Method;
|
||||||
|
use actix_web::http::StatusCode;
|
||||||
|
use actix_web::test;
|
||||||
|
|
||||||
|
struct SignUpPartial {
|
||||||
|
username: String,
|
||||||
|
group: String,
|
||||||
|
role: UserRole,
|
||||||
|
}
|
||||||
|
|
||||||
|
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(),
|
||||||
|
password: "example".to_string(),
|
||||||
|
group: data.group.clone(),
|
||||||
|
role: data.role.clone(),
|
||||||
|
version: "1.0.0".to_string(),
|
||||||
|
})
|
||||||
|
.to_request();
|
||||||
|
|
||||||
|
test::call_service(&app, req).await
|
||||||
|
}
|
||||||
|
|
||||||
|
#[actix_web::test]
|
||||||
|
async fn sign_up_valid() {
|
||||||
|
// prepare
|
||||||
|
|
||||||
|
test_env();
|
||||||
|
|
||||||
|
let app_state = static_app_state().await;
|
||||||
|
driver::users::delete_by_username(&app_state, &"test::sign_up_valid".to_string());
|
||||||
|
|
||||||
|
// test
|
||||||
|
|
||||||
|
let resp = sign_up_client(SignUpPartial {
|
||||||
|
username: "test::sign_up_valid".to_string(),
|
||||||
|
group: "ИС-214/23".to_string(),
|
||||||
|
role: UserRole::Student,
|
||||||
|
})
|
||||||
|
.await;
|
||||||
|
|
||||||
|
assert_eq!(resp.status(), StatusCode::OK);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[actix_web::test]
|
||||||
|
async fn sign_up_multiple() {
|
||||||
|
// prepare
|
||||||
|
|
||||||
|
test_env();
|
||||||
|
|
||||||
|
let app_state = static_app_state().await;
|
||||||
|
driver::users::delete_by_username(&app_state, &"test::sign_up_multiple".to_string());
|
||||||
|
|
||||||
|
let create = sign_up_client(SignUpPartial {
|
||||||
|
username: "test::sign_up_multiple".to_string(),
|
||||||
|
group: "ИС-214/23".to_string(),
|
||||||
|
role: UserRole::Student,
|
||||||
|
})
|
||||||
|
.await;
|
||||||
|
|
||||||
|
assert_eq!(create.status(), StatusCode::OK);
|
||||||
|
|
||||||
|
let resp = sign_up_client(SignUpPartial {
|
||||||
|
username: "test::sign_up_multiple".to_string(),
|
||||||
|
group: "ИС-214/23".to_string(),
|
||||||
|
role: UserRole::Student,
|
||||||
|
})
|
||||||
|
.await;
|
||||||
|
|
||||||
|
assert_eq!(resp.status(), StatusCode::NOT_ACCEPTABLE);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[actix_web::test]
|
||||||
|
async fn sign_up_invalid_role() {
|
||||||
|
test_env();
|
||||||
|
|
||||||
|
// test
|
||||||
|
let resp = sign_up_client(SignUpPartial {
|
||||||
|
username: "test::sign_up_invalid_role".to_string(),
|
||||||
|
group: "ИС-214/23".to_string(),
|
||||||
|
role: UserRole::Admin,
|
||||||
|
})
|
||||||
|
.await;
|
||||||
|
|
||||||
|
assert_eq!(resp.status(), StatusCode::NOT_ACCEPTABLE);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[actix_web::test]
|
||||||
|
async fn sign_up_invalid_group() {
|
||||||
|
test_env();
|
||||||
|
|
||||||
|
// test
|
||||||
|
let resp = sign_up_client(SignUpPartial {
|
||||||
|
username: "test::sign_up_invalid_group".to_string(),
|
||||||
|
group: "invalid_group".to_string(),
|
||||||
|
role: UserRole::Student,
|
||||||
|
})
|
||||||
|
.await;
|
||||||
|
|
||||||
|
assert_eq!(resp.status(), StatusCode::NOT_ACCEPTABLE);
|
||||||
|
}
|
||||||
|
}
|
||||||
5
src/routes/fcm/mod.rs
Normal file
5
src/routes/fcm/mod.rs
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
mod update_callback;
|
||||||
|
mod set_token;
|
||||||
|
|
||||||
|
pub use update_callback::*;
|
||||||
|
pub use set_token::*;
|
||||||
114
src/routes/fcm/set_token.rs
Normal file
114
src/routes/fcm/set_token.rs
Normal file
@@ -0,0 +1,114 @@
|
|||||||
|
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()
|
||||||
|
}
|
||||||
32
src/routes/fcm/update_callback.rs
Normal file
32
src/routes/fcm/update_callback.rs
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
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()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1 +1,6 @@
|
|||||||
pub mod auth;
|
pub mod auth;
|
||||||
|
pub mod fcm;
|
||||||
|
pub mod schedule;
|
||||||
|
mod schema;
|
||||||
|
pub mod users;
|
||||||
|
pub mod vk_id;
|
||||||
|
|||||||
23
src/routes/schedule/cache_status.rs
Normal file
23
src/routes/schedule/cache_status.rs
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
use crate::AppState;
|
||||||
|
use crate::routes::schedule::schema::CacheStatus;
|
||||||
|
use actix_web::{get, web};
|
||||||
|
|
||||||
|
#[utoipa::path(responses(
|
||||||
|
(status = OK, body = CacheStatus),
|
||||||
|
))]
|
||||||
|
#[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()
|
||||||
|
}
|
||||||
96
src/routes/schedule/group.rs
Normal file
96
src/routes/schedule/group.rs
Normal file
@@ -0,0 +1,96 @@
|
|||||||
|
use self::schema::*;
|
||||||
|
use crate::AppState;
|
||||||
|
use crate::database::models::User;
|
||||||
|
use crate::extractors::base::SyncExtractor;
|
||||||
|
use crate::routes::schema::{IntoResponseAsError, ResponseError};
|
||||||
|
use actix_web::{get, web};
|
||||||
|
|
||||||
|
#[utoipa::path(responses(
|
||||||
|
(status = OK, body = Response),
|
||||||
|
(
|
||||||
|
status = SERVICE_UNAVAILABLE,
|
||||||
|
body = ResponseError<ErrorCode>,
|
||||||
|
example = json!({
|
||||||
|
"code": "NO_SCHEDULE",
|
||||||
|
"message": "Schedule not parsed yet."
|
||||||
|
})
|
||||||
|
),
|
||||||
|
(
|
||||||
|
status = NOT_FOUND,
|
||||||
|
body = ResponseError<ErrorCode>,
|
||||||
|
example = json!({
|
||||||
|
"code": "NOT_FOUND",
|
||||||
|
"message": "Required group not found."
|
||||||
|
})
|
||||||
|
),
|
||||||
|
))]
|
||||||
|
#[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();
|
||||||
|
|
||||||
|
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(),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
mod schema {
|
||||||
|
use crate::parser::schema::ScheduleEntry;
|
||||||
|
use actix_macros::{IntoResponseErrorNamed, StatusCode};
|
||||||
|
use chrono::{DateTime, NaiveDateTime, Utc};
|
||||||
|
use derive_more::Display;
|
||||||
|
use serde::Serialize;
|
||||||
|
use utoipa::ToSchema;
|
||||||
|
|
||||||
|
pub type ServiceResponse = crate::routes::schema::Response<Response, 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)]
|
||||||
|
#[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,
|
||||||
|
|
||||||
|
/// Group not found.
|
||||||
|
#[status_code = "actix_web::http::StatusCode::NOT_FOUND"]
|
||||||
|
#[display("Required group not found.")]
|
||||||
|
NotFound,
|
||||||
|
}
|
||||||
|
}
|
||||||
48
src/routes/schedule/group_names.rs
Normal file
48
src/routes/schedule/group_names.rs
Normal file
@@ -0,0 +1,48 @@
|
|||||||
|
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>),
|
||||||
|
))]
|
||||||
|
#[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();
|
||||||
|
|
||||||
|
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();
|
||||||
|
|
||||||
|
Ok(names.into()).into()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
.into()
|
||||||
|
}
|
||||||
|
|
||||||
|
mod schema {
|
||||||
|
use crate::routes::schedule::schema::ErrorCode;
|
||||||
|
use serde::Serialize;
|
||||||
|
use utoipa::ToSchema;
|
||||||
|
|
||||||
|
pub type ServiceResponse = crate::routes::schema::Response<Response, ErrorCode>;
|
||||||
|
|
||||||
|
#[derive(Serialize, ToSchema)]
|
||||||
|
#[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 }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
16
src/routes/schedule/mod.rs
Normal file
16
src/routes/schedule/mod.rs
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
mod cache_status;
|
||||||
|
mod group;
|
||||||
|
mod group_names;
|
||||||
|
mod schedule;
|
||||||
|
mod teacher;
|
||||||
|
mod teacher_names;
|
||||||
|
mod schema;
|
||||||
|
mod update_download_url;
|
||||||
|
|
||||||
|
pub use cache_status::*;
|
||||||
|
pub use group::*;
|
||||||
|
pub use group_names::*;
|
||||||
|
pub use schedule::*;
|
||||||
|
pub use teacher::*;
|
||||||
|
pub use teacher_names::*;
|
||||||
|
pub use update_download_url::*;
|
||||||
25
src/routes/schedule/schedule.rs
Normal file
25
src/routes/schedule/schedule.rs
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
use self::schema::*;
|
||||||
|
use crate::app_state::AppState;
|
||||||
|
use crate::routes::schedule::schema::{ErrorCode, ScheduleView};
|
||||||
|
use crate::routes::schema::{IntoResponseAsError, ResponseError};
|
||||||
|
use actix_web::{get, web};
|
||||||
|
|
||||||
|
#[utoipa::path(responses(
|
||||||
|
(status = OK, body = ScheduleView),
|
||||||
|
(status = SERVICE_UNAVAILABLE, body = ResponseError<ErrorCode>)
|
||||||
|
))]
|
||||||
|
#[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>;
|
||||||
|
}
|
||||||
107
src/routes/schedule/schema.rs
Normal file
107
src/routes/schedule/schema.rs
Normal file
@@ -0,0 +1,107 @@
|
|||||||
|
use crate::app_state::{AppState, Schedule};
|
||||||
|
use crate::parser::schema::ScheduleEntry;
|
||||||
|
use actix_macros::{IntoResponseErrorNamed, ResponderJson, StatusCode};
|
||||||
|
use actix_web::web;
|
||||||
|
use chrono::{DateTime, Duration, Utc};
|
||||||
|
use derive_more::Display;
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
use std::collections::HashMap;
|
||||||
|
use utoipa::ToSchema;
|
||||||
|
|
||||||
|
/// Response from schedule server.
|
||||||
|
#[derive(Serialize, ToSchema)]
|
||||||
|
#[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>,
|
||||||
|
|
||||||
|
/// Groups schedule.
|
||||||
|
groups: HashMap<String, ScheduleEntry>,
|
||||||
|
|
||||||
|
/// Teachers schedule.
|
||||||
|
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,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl TryFrom<&web::Data<AppState>> for ScheduleView {
|
||||||
|
type Error = ErrorCode;
|
||||||
|
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Cached schedule status.
|
||||||
|
#[derive(Serialize, Deserialize, ToSchema, ResponderJson)]
|
||||||
|
#[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,
|
||||||
|
|
||||||
|
/// Last cache update date.
|
||||||
|
pub last_cache_update: i64,
|
||||||
|
|
||||||
|
/// Cached schedule update date.
|
||||||
|
///
|
||||||
|
/// Determined by the polytechnic's server.
|
||||||
|
pub last_schedule_update: 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,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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 {
|
||||||
|
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(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
97
src/routes/schedule/teacher.rs
Normal file
97
src/routes/schedule/teacher.rs
Normal file
@@ -0,0 +1,97 @@
|
|||||||
|
use self::schema::*;
|
||||||
|
use crate::routes::schema::{IntoResponseAsError, ResponseError};
|
||||||
|
use crate::AppState;
|
||||||
|
use actix_web::{get, web};
|
||||||
|
|
||||||
|
#[utoipa::path(responses(
|
||||||
|
(status = OK, body = Response),
|
||||||
|
(
|
||||||
|
status = SERVICE_UNAVAILABLE,
|
||||||
|
body = ResponseError<ErrorCode>,
|
||||||
|
example = json!({
|
||||||
|
"code": "NO_SCHEDULE",
|
||||||
|
"message": "Schedule not parsed yet."
|
||||||
|
})
|
||||||
|
),
|
||||||
|
(
|
||||||
|
status = NOT_FOUND,
|
||||||
|
body = ResponseError<ErrorCode>,
|
||||||
|
example = json!({
|
||||||
|
"code": "NOT_FOUND",
|
||||||
|
"message": "Required teacher not found."
|
||||||
|
})
|
||||||
|
),
|
||||||
|
))]
|
||||||
|
#[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();
|
||||||
|
|
||||||
|
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(),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
mod schema {
|
||||||
|
use crate::parser::schema::ScheduleEntry;
|
||||||
|
use actix_macros::{IntoResponseErrorNamed, StatusCode};
|
||||||
|
use chrono::{DateTime, NaiveDateTime, Utc};
|
||||||
|
use derive_more::Display;
|
||||||
|
use serde::Serialize;
|
||||||
|
use utoipa::ToSchema;
|
||||||
|
|
||||||
|
pub type ServiceResponse = crate::routes::schema::Response<Response, 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)]
|
||||||
|
#[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.")]
|
||||||
|
NotFound,
|
||||||
|
}
|
||||||
|
}
|
||||||
48
src/routes/schedule/teacher_names.rs
Normal file
48
src/routes/schedule/teacher_names.rs
Normal file
@@ -0,0 +1,48 @@
|
|||||||
|
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>),
|
||||||
|
))]
|
||||||
|
#[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();
|
||||||
|
|
||||||
|
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();
|
||||||
|
|
||||||
|
Ok(names.into()).into()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
.into()
|
||||||
|
}
|
||||||
|
|
||||||
|
mod schema {
|
||||||
|
use crate::routes::schedule::schema::ErrorCode;
|
||||||
|
use serde::Serialize;
|
||||||
|
use utoipa::ToSchema;
|
||||||
|
|
||||||
|
pub type ServiceResponse = crate::routes::schema::Response<Response, ErrorCode>;
|
||||||
|
|
||||||
|
#[derive(Serialize, ToSchema)]
|
||||||
|
#[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 }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
140
src/routes/schedule/update_download_url.rs
Normal file
140
src/routes/schedule/update_download_url.rs
Normal file
@@ -0,0 +1,140 @@
|
|||||||
|
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::{FetchError, 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) => {
|
||||||
|
if let FetchError::Unknown(error) = &error {
|
||||||
|
sentry::capture_error(&error);
|
||||||
|
}
|
||||||
|
|
||||||
|
ErrorCode::DownloadFailed(error).into_response()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(error) => {
|
||||||
|
if let FetchError::Unknown(error) = &error {
|
||||||
|
sentry::capture_error(&error);
|
||||||
|
}
|
||||||
|
|
||||||
|
ErrorCode::FetchFailed(error).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;
|
||||||
|
use crate::xls_downloader::interface::FetchError;
|
||||||
|
|
||||||
|
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: {_0}")]
|
||||||
|
FetchFailed(FetchError),
|
||||||
|
|
||||||
|
/// Failed to download the file.
|
||||||
|
#[display("Unable to retrieve data from the specified URL: {_0}")]
|
||||||
|
DownloadFailed(FetchError),
|
||||||
|
|
||||||
|
/// 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"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
174
src/routes/schema.rs
Normal file
174
src/routes/schema.rs
Normal file
@@ -0,0 +1,174 @@
|
|||||||
|
use actix_web::body::EitherBody;
|
||||||
|
use actix_web::error::JsonPayloadError;
|
||||||
|
use actix_web::http::StatusCode;
|
||||||
|
use actix_web::{HttpRequest, HttpResponse, Responder};
|
||||||
|
use serde::{Serialize, Serializer};
|
||||||
|
use std::convert::Into;
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 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,
|
||||||
|
{
|
||||||
|
fn into(self) -> Result<T, E> {
|
||||||
|
self.0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 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,
|
||||||
|
{
|
||||||
|
fn from(value: Result<T, E>) -> Self {
|
||||||
|
Response(value)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Serialize Response<T, E>
|
||||||
|
impl<T, E> Serialize for Response<T, E>
|
||||||
|
where
|
||||||
|
T: Serialize + PartialSchema,
|
||||||
|
E: Serialize + PartialSchema + Clone + PartialStatusCode + 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())),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 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>>,
|
||||||
|
{
|
||||||
|
type Body = EitherBody<String>;
|
||||||
|
|
||||||
|
fn respond_to(self, _: &HttpRequest) -> HttpResponse<Self::Body> {
|
||||||
|
match serde_json::to_string(&self) {
|
||||||
|
Ok(body) => {
|
||||||
|
let code = match &self.0 {
|
||||||
|
Ok(_) => StatusCode::OK,
|
||||||
|
Err(e) => e.status_code(),
|
||||||
|
};
|
||||||
|
|
||||||
|
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(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Err(err) => {
|
||||||
|
HttpResponse::from_error(JsonPayloadError::Serialize(err)).map_into_right_body()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 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 IntoResponseAsError<T>
|
||||||
|
where
|
||||||
|
T: Serialize + PartialSchema,
|
||||||
|
Self: Serialize + PartialSchema + Clone + PartialStatusCode + Into<ResponseError<Self>>,
|
||||||
|
{
|
||||||
|
fn into_response(self) -> Response<T, Self> {
|
||||||
|
Response(Err(self))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub mod user {
|
||||||
|
use crate::database::models::{User, UserRole};
|
||||||
|
use actix_macros::ResponderJson;
|
||||||
|
use serde::Serialize;
|
||||||
|
|
||||||
|
//noinspection SpellCheckingInspection
|
||||||
|
/// Используется для скрытия чувствительных полей, таких как хеш пароля или FCM
|
||||||
|
#[derive(Serialize, utoipa::ToSchema, ResponderJson)]
|
||||||
|
#[serde(rename_all = "camelCase")]
|
||||||
|
pub struct UserResponse {
|
||||||
|
/// UUID
|
||||||
|
#[schema(examples("67dcc9a9507b0000772744a2"))]
|
||||||
|
id: String,
|
||||||
|
|
||||||
|
/// Имя пользователя
|
||||||
|
#[schema(examples("n08i40k"))]
|
||||||
|
username: String,
|
||||||
|
|
||||||
|
/// Группа
|
||||||
|
#[schema(examples("ИС-214/23"))]
|
||||||
|
group: String,
|
||||||
|
|
||||||
|
/// Роль
|
||||||
|
role: UserRole,
|
||||||
|
|
||||||
|
/// Идентификатор привязанного аккаунта VK
|
||||||
|
#[schema(examples(498094647, json!(null)))]
|
||||||
|
vk_id: Option<i32>,
|
||||||
|
|
||||||
|
/// JWT токен доступа
|
||||||
|
#[schema(examples(
|
||||||
|
"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6IjY3ZGNjOWE5NTA3YjAwMDA3NzI3NDRhMiIsImlhdCI6IjE3NDMxMDgwOTkiLCJleHAiOiIxODY5MjUyMDk5In0.rMgXRb3JbT9AvLK4eiY9HMB5LxgUudkpQyoWKOypZFY"
|
||||||
|
))]
|
||||||
|
access_token: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Create UserResponse from User ref.
|
||||||
|
impl From<&User> for UserResponse {
|
||||||
|
fn from(user: &User) -> Self {
|
||||||
|
UserResponse {
|
||||||
|
id: user.id.clone(),
|
||||||
|
username: user.username.clone(),
|
||||||
|
group: user.group.clone(),
|
||||||
|
role: user.role.clone(),
|
||||||
|
vk_id: user.vk_id.clone(),
|
||||||
|
access_token: user.access_token.clone(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Transform User to UserResponse.
|
||||||
|
impl From<User> for UserResponse {
|
||||||
|
fn from(user: User) -> Self {
|
||||||
|
UserResponse {
|
||||||
|
id: user.id,
|
||||||
|
username: user.username,
|
||||||
|
group: user.group,
|
||||||
|
role: user.role,
|
||||||
|
vk_id: user.vk_id,
|
||||||
|
access_token: user.access_token,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
85
src/routes/users/change_group.rs
Normal file
85
src/routes/users/change_group.rs
Normal file
@@ -0,0 +1,85 @@
|
|||||||
|
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 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>,
|
||||||
|
data: web::Json<Request>,
|
||||||
|
) -> ServiceResponse {
|
||||||
|
let mut user = user.into_inner();
|
||||||
|
|
||||||
|
if user.group == data.group {
|
||||||
|
return ErrorCode::SameGroup.into_response();
|
||||||
|
}
|
||||||
|
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(()).into()
|
||||||
|
}
|
||||||
|
|
||||||
|
mod schema {
|
||||||
|
use actix_macros::{IntoResponseErrorNamed, StatusCode};
|
||||||
|
use derive_more::Display;
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
use utoipa::ToSchema;
|
||||||
|
|
||||||
|
pub type ServiceResponse = crate::routes::schema::Response<(), ErrorCode>;
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, ToSchema)]
|
||||||
|
#[schema(as = ChangeGroup::Request)]
|
||||||
|
pub struct Request {
|
||||||
|
/// Group name.
|
||||||
|
pub group: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Serialize, ToSchema, StatusCode, Display, IntoResponseErrorNamed)]
|
||||||
|
#[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,
|
||||||
|
}
|
||||||
|
}
|
||||||
70
src/routes/users/change_username.rs
Normal file
70
src/routes/users/change_username.rs
Normal file
@@ -0,0 +1,70 @@
|
|||||||
|
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 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>,
|
||||||
|
data: web::Json<Request>,
|
||||||
|
) -> ServiceResponse {
|
||||||
|
let mut user = user.into_inner();
|
||||||
|
|
||||||
|
if user.username == data.username {
|
||||||
|
return ErrorCode::SameUsername.into_response();
|
||||||
|
}
|
||||||
|
|
||||||
|
if driver::users::get_by_username(&app_state, &data.username).is_ok() {
|
||||||
|
return ErrorCode::AlreadyExists.into_response();
|
||||||
|
}
|
||||||
|
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(()).into()
|
||||||
|
}
|
||||||
|
|
||||||
|
mod schema {
|
||||||
|
use actix_macros::{IntoResponseErrorNamed, StatusCode};
|
||||||
|
use derive_more::Display;
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
use utoipa::ToSchema;
|
||||||
|
|
||||||
|
pub type ServiceResponse = crate::routes::schema::Response<(), ErrorCode>;
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, ToSchema)]
|
||||||
|
#[schema(as = ChangeUsername::Request)]
|
||||||
|
pub struct Request {
|
||||||
|
/// User name.
|
||||||
|
pub username: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Serialize, ToSchema, StatusCode, Display, IntoResponseErrorNamed)]
|
||||||
|
#[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,
|
||||||
|
}
|
||||||
|
}
|
||||||
10
src/routes/users/me.rs
Normal file
10
src/routes/users/me.rs
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
use crate::database::models::User;
|
||||||
|
use crate::extractors::base::SyncExtractor;
|
||||||
|
use actix_web::get;
|
||||||
|
use crate::routes::schema::user::UserResponse;
|
||||||
|
|
||||||
|
#[utoipa::path(responses((status = OK, body = UserResponse)))]
|
||||||
|
#[get("/me")]
|
||||||
|
pub async fn me(user: SyncExtractor<User>) -> UserResponse {
|
||||||
|
user.into_inner().into()
|
||||||
|
}
|
||||||
7
src/routes/users/mod.rs
Normal file
7
src/routes/users/mod.rs
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
mod change_group;
|
||||||
|
mod change_username;
|
||||||
|
mod me;
|
||||||
|
|
||||||
|
pub use change_group::*;
|
||||||
|
pub use change_username::*;
|
||||||
|
pub use me::*;
|
||||||
3
src/routes/vk_id/mod.rs
Normal file
3
src/routes/vk_id/mod.rs
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
mod oauth;
|
||||||
|
|
||||||
|
pub use oauth::*;
|
||||||
117
src/routes/vk_id/oauth.rs
Normal file
117
src/routes/vk_id/oauth.rs
Normal file
@@ -0,0 +1,117 @@
|
|||||||
|
use self::schema::*;
|
||||||
|
use crate::app_state::AppState;
|
||||||
|
use crate::routes::schema::{IntoResponseAsError, ResponseError};
|
||||||
|
use actix_web::{post, web};
|
||||||
|
use serde::Deserialize;
|
||||||
|
use std::collections::HashMap;
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
#[allow(dead_code)]
|
||||||
|
#[derive(Deserialize)]
|
||||||
|
struct VkIdAuthResponse {
|
||||||
|
refresh_token: String,
|
||||||
|
access_token: String,
|
||||||
|
id_token: String,
|
||||||
|
token_type: String,
|
||||||
|
expires_in: i32,
|
||||||
|
user_id: i32,
|
||||||
|
state: String,
|
||||||
|
scope: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[utoipa::path(responses(
|
||||||
|
(status = OK, body = Response),
|
||||||
|
(
|
||||||
|
status = NOT_ACCEPTABLE,
|
||||||
|
body = ResponseError<ErrorCode>,
|
||||||
|
example = json!({
|
||||||
|
"code": "VK_ID_ERROR",
|
||||||
|
"message": "VK server returned an error"
|
||||||
|
})
|
||||||
|
),
|
||||||
|
))]
|
||||||
|
#[post("/oauth")]
|
||||||
|
async fn oauth(data: web::Json<Request>, app_state: web::Data<AppState>) -> ServiceResponse {
|
||||||
|
let data = data.into_inner();
|
||||||
|
let state = Uuid::new_v4().simple().to_string();
|
||||||
|
|
||||||
|
let vk_id = &app_state.vk_id;
|
||||||
|
let client_id = vk_id.client_id.clone().to_string();
|
||||||
|
|
||||||
|
let mut params = HashMap::new();
|
||||||
|
params.insert("grant_type", "authorization_code");
|
||||||
|
params.insert("client_id", client_id.as_str());
|
||||||
|
params.insert("state", state.as_str());
|
||||||
|
params.insert("code_verifier", data.code_verifier.as_str());
|
||||||
|
params.insert("code", data.code.as_str());
|
||||||
|
params.insert("device_id", data.device_id.as_str());
|
||||||
|
params.insert("redirect_uri", vk_id.redirect_url.as_str());
|
||||||
|
|
||||||
|
let client = reqwest::Client::new();
|
||||||
|
match client
|
||||||
|
.post("https://id.vk.com/oauth2/auth")
|
||||||
|
.form(¶ms)
|
||||||
|
.send()
|
||||||
|
.await
|
||||||
|
{
|
||||||
|
Ok(res) => {
|
||||||
|
if !res.status().is_success() {
|
||||||
|
return ErrorCode::VkIdError.into_response();
|
||||||
|
}
|
||||||
|
|
||||||
|
match res.json::<VkIdAuthResponse>().await {
|
||||||
|
Ok(auth_data) =>
|
||||||
|
Ok(Response {
|
||||||
|
access_token: auth_data.id_token,
|
||||||
|
}).into(),
|
||||||
|
Err(error) => {
|
||||||
|
sentry::capture_error(&error);
|
||||||
|
|
||||||
|
ErrorCode::VkIdError.into_response()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(_) => ErrorCode::VkIdError.into_response(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
mod schema {
|
||||||
|
use actix_macros::{IntoResponseErrorNamed, StatusCode};
|
||||||
|
use derive_more::Display;
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
use utoipa::ToSchema;
|
||||||
|
|
||||||
|
pub type ServiceResponse = crate::routes::schema::Response<Response, ErrorCode>;
|
||||||
|
|
||||||
|
#[derive(Deserialize, ToSchema)]
|
||||||
|
#[serde(rename_all = "camelCase")]
|
||||||
|
#[schema(as = VkIdOAuth::Request)]
|
||||||
|
pub struct Request {
|
||||||
|
/// Код подтверждения authorization_code.
|
||||||
|
pub code: String,
|
||||||
|
|
||||||
|
/// Parameter to protect transmitted data.
|
||||||
|
pub code_verifier: String,
|
||||||
|
|
||||||
|
/// Device ID.
|
||||||
|
pub device_id: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, ToSchema)]
|
||||||
|
#[serde(rename_all = "camelCase")]
|
||||||
|
#[schema(as = VkIdOAuth::Response)]
|
||||||
|
pub struct Response {
|
||||||
|
/// ID token.
|
||||||
|
pub access_token: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Serialize, ToSchema, IntoResponseErrorNamed, StatusCode, Display)]
|
||||||
|
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
|
||||||
|
#[schema(as = VkIdOAuth::ErrorCode)]
|
||||||
|
#[status_code = "actix_web::http::StatusCode::NOT_ACCEPTABLE"]
|
||||||
|
pub enum ErrorCode {
|
||||||
|
/// VK server returned an error.
|
||||||
|
#[display("VK server returned an error")]
|
||||||
|
VkIdError,
|
||||||
|
}
|
||||||
|
}
|
||||||
32
src/test_env.rs
Normal file
32
src/test_env.rs
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
#[cfg(test)]
|
||||||
|
pub(crate) mod tests {
|
||||||
|
use crate::app_state::{AppState, Schedule, app_state};
|
||||||
|
use crate::parser::tests::test_result;
|
||||||
|
use actix_web::web;
|
||||||
|
use tokio::sync::OnceCell;
|
||||||
|
|
||||||
|
pub fn test_env() {
|
||||||
|
dotenvy::from_path(".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(),
|
||||||
|
});
|
||||||
|
|
||||||
|
state.clone()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn static_app_state() -> web::Data<AppState> {
|
||||||
|
static STATE: OnceCell<web::Data<AppState>> = OnceCell::const_new();
|
||||||
|
|
||||||
|
STATE.get_or_init(|| test_app_state()).await.clone()
|
||||||
|
}
|
||||||
|
}
|
||||||
19
src/utility/error.rs
Normal file
19
src/utility/error.rs
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
use std::fmt::{Write};
|
||||||
|
use std::fmt::Display;
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
|
/// Server response to errors within Middleware.
|
||||||
|
#[derive(Serialize, Deserialize)]
|
||||||
|
pub struct ResponseErrorMessage<T: Display> {
|
||||||
|
code: T,
|
||||||
|
message: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T: Display + Serialize> ResponseErrorMessage<T> {
|
||||||
|
pub fn new(code: T) -> Self {
|
||||||
|
let mut message = String::new();
|
||||||
|
write!(&mut message, "{}", code).unwrap();
|
||||||
|
|
||||||
|
Self { code, message }
|
||||||
|
}
|
||||||
|
}
|
||||||
38
src/utility/hasher.rs
Normal file
38
src/utility/hasher.rs
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
use sha1::Digest;
|
||||||
|
use std::hash::Hasher;
|
||||||
|
|
||||||
|
/// Hesher returning hash from the algorithm implementing Digest
|
||||||
|
pub struct DigestHasher<D: Digest> {
|
||||||
|
digest: D,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<D> DigestHasher<D>
|
||||||
|
where
|
||||||
|
D: Digest,
|
||||||
|
{
|
||||||
|
/// Obtain hash.
|
||||||
|
pub fn finalize(self) -> String {
|
||||||
|
hex::encode(self.digest.finalize().0)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<D> From<D> for DigestHasher<D>
|
||||||
|
where
|
||||||
|
D: Digest,
|
||||||
|
{
|
||||||
|
/// Creating a hash from an algorithm implementing Digest.
|
||||||
|
fn from(digest: D) -> Self {
|
||||||
|
DigestHasher { digest }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<D: Digest> Hasher for DigestHasher<D> {
|
||||||
|
/// Stopper to prevent calling the standard Hasher result.
|
||||||
|
fn finish(&self) -> u64 {
|
||||||
|
unimplemented!("Do not call finish()");
|
||||||
|
}
|
||||||
|
|
||||||
|
fn write(&mut self, bytes: &[u8]) {
|
||||||
|
self.digest.update(bytes);
|
||||||
|
}
|
||||||
|
}
|
||||||
169
src/utility/jwt.rs
Normal file
169
src/utility/jwt.rs
Normal file
@@ -0,0 +1,169 @@
|
|||||||
|
use chrono::Duration;
|
||||||
|
use chrono::Utc;
|
||||||
|
use jsonwebtoken::errors::ErrorKind;
|
||||||
|
use jsonwebtoken::{Algorithm, DecodingKey, EncodingKey, Header, Validation, decode};
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
use serde_with::DisplayFromStr;
|
||||||
|
use serde_with::serde_as;
|
||||||
|
use std::env;
|
||||||
|
use std::mem::discriminant;
|
||||||
|
use std::sync::LazyLock;
|
||||||
|
|
||||||
|
/// Key for token verification.
|
||||||
|
static DECODING_KEY: LazyLock<DecodingKey> = LazyLock::new(|| {
|
||||||
|
let secret = env::var("JWT_SECRET").expect("JWT_SECRET must be set");
|
||||||
|
|
||||||
|
DecodingKey::from_secret(secret.as_bytes())
|
||||||
|
});
|
||||||
|
|
||||||
|
/// Key for creating a signed token.
|
||||||
|
static ENCODING_KEY: LazyLock<EncodingKey> = LazyLock::new(|| {
|
||||||
|
let secret = env::var("JWT_SECRET").expect("JWT_SECRET must be set");
|
||||||
|
|
||||||
|
EncodingKey::from_secret(secret.as_bytes())
|
||||||
|
});
|
||||||
|
|
||||||
|
/// Token verification errors.
|
||||||
|
#[allow(dead_code)]
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub enum Error {
|
||||||
|
/// The token has a different signature.
|
||||||
|
InvalidSignature,
|
||||||
|
|
||||||
|
/// Token reading error.
|
||||||
|
InvalidToken(ErrorKind),
|
||||||
|
|
||||||
|
/// Token expired.
|
||||||
|
Expired,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl PartialEq for Error {
|
||||||
|
fn eq(&self, other: &Self) -> bool {
|
||||||
|
discriminant(self) == discriminant(other)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The data the token holds.
|
||||||
|
#[serde_as]
|
||||||
|
#[derive(Debug, Serialize, Deserialize)]
|
||||||
|
struct Claims {
|
||||||
|
/// User account UUID.
|
||||||
|
id: String,
|
||||||
|
|
||||||
|
/// Token creation date.
|
||||||
|
#[serde_as(as = "DisplayFromStr")]
|
||||||
|
iat: u64,
|
||||||
|
|
||||||
|
/// Token expiry date.
|
||||||
|
#[serde_as(as = "DisplayFromStr")]
|
||||||
|
exp: u64,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Token signing algorithm.
|
||||||
|
pub(crate) const DEFAULT_ALGORITHM: Algorithm = Algorithm::HS256;
|
||||||
|
|
||||||
|
/// Checking the token and extracting the UUID of the user account from it.
|
||||||
|
pub fn verify_and_decode(token: &String) -> Result<String, Error> {
|
||||||
|
let mut validation = Validation::new(DEFAULT_ALGORITHM);
|
||||||
|
|
||||||
|
validation.required_spec_claims.remove("exp");
|
||||||
|
validation.validate_exp = false;
|
||||||
|
|
||||||
|
let result = decode::<Claims>(&token, &*DECODING_KEY, &validation);
|
||||||
|
|
||||||
|
match result {
|
||||||
|
Ok(token_data) => {
|
||||||
|
if token_data.claims.exp < Utc::now().timestamp().unsigned_abs() {
|
||||||
|
Err(Error::Expired)
|
||||||
|
} else {
|
||||||
|
Ok(token_data.claims.id)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(err) => Err(match err.into_kind() {
|
||||||
|
ErrorKind::InvalidSignature => Error::InvalidSignature,
|
||||||
|
ErrorKind::ExpiredSignature => Error::Expired,
|
||||||
|
kind => Error::InvalidToken(kind),
|
||||||
|
}),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Creating a user token.
|
||||||
|
pub fn encode(id: &String) -> String {
|
||||||
|
let header = Header {
|
||||||
|
typ: Some(String::from("JWT")),
|
||||||
|
..Default::default()
|
||||||
|
};
|
||||||
|
|
||||||
|
let iat = Utc::now();
|
||||||
|
let exp = iat + Duration::days(365 * 4);
|
||||||
|
|
||||||
|
let claims = Claims {
|
||||||
|
id: id.clone(),
|
||||||
|
iat: iat.timestamp().unsigned_abs(),
|
||||||
|
exp: exp.timestamp().unsigned_abs(),
|
||||||
|
};
|
||||||
|
|
||||||
|
jsonwebtoken::encode(&header, &claims, &*ENCODING_KEY).unwrap()
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
use crate::test_env::tests::test_env;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_encode() {
|
||||||
|
test_env();
|
||||||
|
|
||||||
|
assert_eq!(encode(&"test".to_string()).is_empty(), false);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_decode_invalid_token() {
|
||||||
|
test_env();
|
||||||
|
|
||||||
|
let token = "".to_string();
|
||||||
|
let result = verify_and_decode(&token);
|
||||||
|
|
||||||
|
assert!(result.is_err());
|
||||||
|
assert_eq!(
|
||||||
|
result.err().unwrap(),
|
||||||
|
Error::InvalidToken(ErrorKind::InvalidToken)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
//noinspection SpellCheckingInspection
|
||||||
|
#[test]
|
||||||
|
fn test_decode_invalid_signature() {
|
||||||
|
test_env();
|
||||||
|
|
||||||
|
let token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOiIxNjE2NTI2Mzc2IiwiaWF0IjoiMTQ5MDM4MjM3NiIsImlkIjoiNjdkY2M5YTk1MDdiMDAwMDc3Mjc0NGEyIn0.Qc2LbMJTvl2hWzDM2XyQv4m9lIqR84COAESQAieUxz8".to_string();
|
||||||
|
let result = verify_and_decode(&token);
|
||||||
|
|
||||||
|
assert!(result.is_err());
|
||||||
|
assert_eq!(result.err().unwrap(), Error::InvalidSignature);
|
||||||
|
}
|
||||||
|
|
||||||
|
//noinspection SpellCheckingInspection
|
||||||
|
#[test]
|
||||||
|
fn test_decode_expired() {
|
||||||
|
test_env();
|
||||||
|
|
||||||
|
let token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjY3ZGNjOWE5NTA3YjAwMDA3NzI3NDRhMiIsImlhdCI6IjAiLCJleHAiOiIwIn0.GBsVYvnZIfHXt00t-qmAdUMyHSyWOBtC0Mrxwg1HQOM".to_string();
|
||||||
|
let result = verify_and_decode(&token);
|
||||||
|
|
||||||
|
assert!(result.is_err());
|
||||||
|
assert_eq!(result.err().unwrap(), Error::Expired);
|
||||||
|
}
|
||||||
|
|
||||||
|
//noinspection SpellCheckingInspection
|
||||||
|
#[test]
|
||||||
|
fn test_decode_ok() {
|
||||||
|
test_env();
|
||||||
|
|
||||||
|
let token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjY3ZGNjOWE5NTA3YjAwMDA3NzI3NDRhMiIsImlhdCI6Ijk5OTk5OTk5OTkiLCJleHAiOiI5OTk5OTk5OTk5In0.o1vN-ze5iaJrnlHqe7WARXMBhhzjxTjTKkjlmTGEnOI".to_string();
|
||||||
|
let result = verify_and_decode(&token);
|
||||||
|
|
||||||
|
assert!(result.is_ok());
|
||||||
|
}
|
||||||
|
}
|
||||||
4
src/utility/mod.rs
Normal file
4
src/utility/mod.rs
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
pub mod jwt;
|
||||||
|
pub mod error;
|
||||||
|
pub mod hasher;
|
||||||
|
pub mod mutex;
|
||||||
77
src/utility/mutex.rs
Normal file
77
src/utility/mutex.rs
Normal file
@@ -0,0 +1,77 @@
|
|||||||
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,11 +1,14 @@
|
|||||||
use crate::xls_downloader::interface::{FetchError, FetchOk, FetchResult, XLSDownloader};
|
use crate::xls_downloader::interface::{FetchError, FetchOk, FetchResult, XLSDownloader};
|
||||||
use chrono::{DateTime, Utc};
|
use chrono::{DateTime, Utc};
|
||||||
|
use std::env;
|
||||||
|
use std::sync::Arc;
|
||||||
|
|
||||||
pub struct BasicXlsDownloader {
|
pub struct BasicXlsDownloader {
|
||||||
url: Option<String>,
|
pub url: Option<String>,
|
||||||
|
user_agent: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn fetch_specified(url: &String, user_agent: String, head: bool) -> FetchResult {
|
async fn fetch_specified(url: &String, user_agent: &String, head: bool) -> FetchResult {
|
||||||
let client = reqwest::Client::new();
|
let client = reqwest::Client::new();
|
||||||
|
|
||||||
let response = if head {
|
let response = if head {
|
||||||
@@ -13,14 +16,14 @@ async fn fetch_specified(url: &String, user_agent: String, head: bool) -> FetchR
|
|||||||
} else {
|
} else {
|
||||||
client.get(url)
|
client.get(url)
|
||||||
}
|
}
|
||||||
.header("User-Agent", user_agent)
|
.header("User-Agent", user_agent.clone())
|
||||||
.send()
|
.send()
|
||||||
.await;
|
.await;
|
||||||
|
|
||||||
match response {
|
match response {
|
||||||
Ok(r) => {
|
Ok(r) => {
|
||||||
if r.status().as_u16() != 200 {
|
if r.status().as_u16() != 200 {
|
||||||
return Err(FetchError::BadStatusCode);
|
return Err(FetchError::BadStatusCode(r.status().as_u16()));
|
||||||
}
|
}
|
||||||
|
|
||||||
let headers = r.headers();
|
let headers = r.headers();
|
||||||
@@ -30,11 +33,18 @@ async fn fetch_specified(url: &String, user_agent: String, head: bool) -> FetchR
|
|||||||
let last_modified = headers.get("last-modified");
|
let last_modified = headers.get("last-modified");
|
||||||
let date = headers.get("date");
|
let date = headers.get("date");
|
||||||
|
|
||||||
if content_type.is_none() || etag.is_none() || last_modified.is_none() || date.is_none()
|
if content_type.is_none() {
|
||||||
{
|
Err(FetchError::BadHeaders("Content-Type".to_string()))
|
||||||
Err(FetchError::BadHeaders)
|
} else if etag.is_none() {
|
||||||
|
Err(FetchError::BadHeaders("ETag".to_string()))
|
||||||
|
} else if last_modified.is_none() {
|
||||||
|
Err(FetchError::BadHeaders("Last-Modified".to_string()))
|
||||||
|
} else if date.is_none() {
|
||||||
|
Err(FetchError::BadHeaders("Date".to_string()))
|
||||||
} else if content_type.unwrap() != "application/vnd.ms-excel" {
|
} else if content_type.unwrap() != "application/vnd.ms-excel" {
|
||||||
Err(FetchError::BadContentType)
|
Err(FetchError::BadContentType(
|
||||||
|
content_type.unwrap().to_str().unwrap().to_string(),
|
||||||
|
))
|
||||||
} else {
|
} else {
|
||||||
let etag = etag.unwrap().to_str().unwrap().to_string();
|
let etag = etag.unwrap().to_str().unwrap().to_string();
|
||||||
let last_modified =
|
let last_modified =
|
||||||
@@ -49,13 +59,16 @@ async fn fetch_specified(url: &String, user_agent: String, head: bool) -> FetchR
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Err(_) => Err(FetchError::Unknown),
|
Err(error) => Err(FetchError::Unknown(Arc::new(error))),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl BasicXlsDownloader {
|
impl BasicXlsDownloader {
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
BasicXlsDownloader { url: None }
|
BasicXlsDownloader {
|
||||||
|
url: None,
|
||||||
|
user_agent: env::var("REQWEST_USER_AGENT").expect("USER_AGENT must be set"),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -64,29 +77,24 @@ impl XLSDownloader for BasicXlsDownloader {
|
|||||||
if self.url.is_none() {
|
if self.url.is_none() {
|
||||||
Err(FetchError::NoUrlProvided)
|
Err(FetchError::NoUrlProvided)
|
||||||
} else {
|
} else {
|
||||||
fetch_specified(
|
fetch_specified(self.url.as_ref().unwrap(), &self.user_agent, head).await
|
||||||
self.url.as_ref().unwrap(),
|
|
||||||
"t.me/polytechnic_next".to_string(),
|
|
||||||
head,
|
|
||||||
)
|
|
||||||
.await
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn set_url(&mut self, url: String) -> Result<(), FetchError> {
|
async fn set_url(&mut self, url: String) -> FetchResult {
|
||||||
let result = fetch_specified(&url, "t.me/polytechnic_next".to_string(), true).await;
|
let result = fetch_specified(&url, &self.user_agent, true).await;
|
||||||
|
|
||||||
if let Ok(_) = result {
|
if let Ok(_) = result {
|
||||||
Ok(self.url = Some(url))
|
self.url = Some(url);
|
||||||
} else {
|
|
||||||
Err(result.err().unwrap())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
result
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use crate::xls_downloader::basic_impl::{BasicXlsDownloader, fetch_specified};
|
use crate::xls_downloader::basic_impl::{fetch_specified, BasicXlsDownloader};
|
||||||
use crate::xls_downloader::interface::{FetchError, XLSDownloader};
|
use crate::xls_downloader::interface::{FetchError, XLSDownloader};
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
@@ -95,8 +103,8 @@ mod tests {
|
|||||||
let user_agent = String::new();
|
let user_agent = String::new();
|
||||||
|
|
||||||
let results = [
|
let results = [
|
||||||
fetch_specified(&url, user_agent.clone(), true).await,
|
fetch_specified(&url, &user_agent, true).await,
|
||||||
fetch_specified(&url, user_agent.clone(), false).await,
|
fetch_specified(&url, &user_agent, false).await,
|
||||||
];
|
];
|
||||||
|
|
||||||
assert!(results[0].is_err());
|
assert!(results[0].is_err());
|
||||||
@@ -109,21 +117,17 @@ mod tests {
|
|||||||
let user_agent = String::new();
|
let user_agent = String::new();
|
||||||
|
|
||||||
let results = [
|
let results = [
|
||||||
fetch_specified(&url, user_agent.clone(), true).await,
|
fetch_specified(&url, &user_agent, true).await,
|
||||||
fetch_specified(&url, user_agent.clone(), false).await,
|
fetch_specified(&url, &user_agent, false).await,
|
||||||
];
|
];
|
||||||
|
|
||||||
assert!(results[0].is_err());
|
assert!(results[0].is_err());
|
||||||
assert!(results[1].is_err());
|
assert!(results[1].is_err());
|
||||||
|
|
||||||
assert_eq!(
|
let expected_error = FetchError::BadStatusCode(404);
|
||||||
*results[0].as_ref().err().unwrap(),
|
|
||||||
FetchError::BadStatusCode
|
assert_eq!(*results[0].as_ref().err().unwrap(), expected_error);
|
||||||
);
|
assert_eq!(*results[1].as_ref().err().unwrap(), expected_error);
|
||||||
assert_eq!(
|
|
||||||
*results[1].as_ref().err().unwrap(),
|
|
||||||
FetchError::BadStatusCode
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
@@ -132,15 +136,17 @@ mod tests {
|
|||||||
let user_agent = String::new();
|
let user_agent = String::new();
|
||||||
|
|
||||||
let results = [
|
let results = [
|
||||||
fetch_specified(&url, user_agent.clone(), true).await,
|
fetch_specified(&url, &user_agent, true).await,
|
||||||
fetch_specified(&url, user_agent.clone(), false).await,
|
fetch_specified(&url, &user_agent, false).await,
|
||||||
];
|
];
|
||||||
|
|
||||||
assert!(results[0].is_err());
|
assert!(results[0].is_err());
|
||||||
assert!(results[1].is_err());
|
assert!(results[1].is_err());
|
||||||
|
|
||||||
assert_eq!(*results[0].as_ref().err().unwrap(), FetchError::BadHeaders);
|
let expected_error = FetchError::BadHeaders("ETag".to_string());
|
||||||
assert_eq!(*results[1].as_ref().err().unwrap(), FetchError::BadHeaders);
|
|
||||||
|
assert_eq!(*results[0].as_ref().err().unwrap(), expected_error);
|
||||||
|
assert_eq!(*results[1].as_ref().err().unwrap(), expected_error);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
@@ -149,21 +155,12 @@ mod tests {
|
|||||||
let user_agent = String::new();
|
let user_agent = String::new();
|
||||||
|
|
||||||
let results = [
|
let results = [
|
||||||
fetch_specified(&url, user_agent.clone(), true).await,
|
fetch_specified(&url, &user_agent, true).await,
|
||||||
fetch_specified(&url, user_agent.clone(), false).await,
|
fetch_specified(&url, &user_agent, false).await,
|
||||||
];
|
];
|
||||||
|
|
||||||
assert!(results[0].is_err());
|
assert!(results[0].is_err());
|
||||||
assert!(results[1].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]
|
#[tokio::test]
|
||||||
@@ -172,8 +169,8 @@ mod tests {
|
|||||||
let user_agent = String::new();
|
let user_agent = String::new();
|
||||||
|
|
||||||
let results = [
|
let results = [
|
||||||
fetch_specified(&url, user_agent.clone(), true).await,
|
fetch_specified(&url, &user_agent, true).await,
|
||||||
fetch_specified(&url, user_agent.clone(), false).await,
|
fetch_specified(&url, &user_agent, false).await,
|
||||||
];
|
];
|
||||||
|
|
||||||
assert!(results[0].is_ok());
|
assert!(results[0].is_ok());
|
||||||
|
|||||||
@@ -1,22 +1,57 @@
|
|||||||
use chrono::{DateTime, Utc};
|
use chrono::{DateTime, Utc};
|
||||||
|
use derive_more::Display;
|
||||||
|
use std::mem::discriminant;
|
||||||
|
use std::sync::Arc;
|
||||||
|
use utoipa::ToSchema;
|
||||||
|
|
||||||
#[derive(PartialEq, Debug)]
|
/// XLS data retrieval errors.
|
||||||
|
#[derive(Clone, Debug, ToSchema, Display)]
|
||||||
pub enum FetchError {
|
pub enum FetchError {
|
||||||
|
/// File url is not set.
|
||||||
|
#[display("The link to the timetable was not provided earlier.")]
|
||||||
NoUrlProvided,
|
NoUrlProvided,
|
||||||
Unknown,
|
|
||||||
BadStatusCode,
|
/// Unknown error.
|
||||||
BadContentType,
|
#[display("An unknown error occurred while downloading the file.")]
|
||||||
BadHeaders,
|
#[schema(value_type = String)]
|
||||||
|
Unknown(Arc<reqwest::Error>),
|
||||||
|
|
||||||
|
/// Server returned a status code different from 200.
|
||||||
|
#[display("Server returned a status code {_0}.")]
|
||||||
|
BadStatusCode(u16),
|
||||||
|
|
||||||
|
/// The url leads to a file of a different type.
|
||||||
|
#[display("The link leads to a file of type '{_0}'.")]
|
||||||
|
BadContentType(String),
|
||||||
|
|
||||||
|
/// Server doesn't return expected headers.
|
||||||
|
#[display("Server doesn't return expected header(s) '{_0}'.")]
|
||||||
|
BadHeaders(String),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl PartialEq for FetchError {
|
||||||
|
fn eq(&self, other: &Self) -> bool {
|
||||||
|
discriminant(self) == discriminant(other)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Result of XLS data retrieval.
|
||||||
pub struct FetchOk {
|
pub struct FetchOk {
|
||||||
|
/// ETag object.
|
||||||
pub etag: String,
|
pub etag: String,
|
||||||
|
|
||||||
|
/// File upload date.
|
||||||
pub uploaded_at: DateTime<Utc>,
|
pub uploaded_at: DateTime<Utc>,
|
||||||
|
|
||||||
|
/// Date data received.
|
||||||
pub requested_at: DateTime<Utc>,
|
pub requested_at: DateTime<Utc>,
|
||||||
|
|
||||||
|
/// File data.
|
||||||
pub data: Option<Vec<u8>>,
|
pub data: Option<Vec<u8>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl FetchOk {
|
impl FetchOk {
|
||||||
|
/// Result without file content.
|
||||||
pub fn head(etag: String, uploaded_at: DateTime<Utc>) -> Self {
|
pub fn head(etag: String, uploaded_at: DateTime<Utc>) -> Self {
|
||||||
FetchOk {
|
FetchOk {
|
||||||
etag,
|
etag,
|
||||||
@@ -26,6 +61,7 @@ impl FetchOk {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Full result.
|
||||||
pub fn get(etag: String, uploaded_at: DateTime<Utc>, data: Vec<u8>) -> Self {
|
pub fn get(etag: String, uploaded_at: DateTime<Utc>, data: Vec<u8>) -> Self {
|
||||||
FetchOk {
|
FetchOk {
|
||||||
etag,
|
etag,
|
||||||
@@ -39,6 +75,9 @@ impl FetchOk {
|
|||||||
pub type FetchResult = Result<FetchOk, FetchError>;
|
pub type FetchResult = Result<FetchOk, FetchError>;
|
||||||
|
|
||||||
pub trait XLSDownloader {
|
pub trait XLSDownloader {
|
||||||
|
/// Get data about the file, and optionally its content.
|
||||||
async fn fetch(&self, head: bool) -> FetchResult;
|
async fn fetch(&self, head: bool) -> FetchResult;
|
||||||
async fn set_url(&mut self, url: String) -> Result<(), FetchError>;
|
|
||||||
|
/// Setting the file link.
|
||||||
|
async fn set_url(&mut self, url: String) -> FetchResult;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user