mirror of
https://github.com/n08i40k/schedule-parser-rusted.git
synced 2025-12-06 17:57:47 +03:00
Compare commits
1 Commits
master
...
0e4d34c284
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0e4d34c284 |
26
.env.test
26
.env.test
@@ -1,26 +0,0 @@
|
|||||||
# Schedule
|
|
||||||
# SCHEDULE_INIT_URL=
|
|
||||||
SCHEDULE_DISABLE_AUTO_UPDATE=1
|
|
||||||
|
|
||||||
# Basic authorization
|
|
||||||
JWT_SECRET="test-secret-at-least-256-bits-used"
|
|
||||||
|
|
||||||
# VKID
|
|
||||||
VK_ID_CLIENT_ID=0
|
|
||||||
VK_ID_REDIRECT_URI="vk0://vk.com/blank.html"
|
|
||||||
|
|
||||||
# Telegram Mini-App
|
|
||||||
TELEGRAM_BOT_ID=0
|
|
||||||
TELEGRAM_MINI_APP_HOST=example.com
|
|
||||||
TELEGRAM_TEST_DC=false
|
|
||||||
|
|
||||||
# Yandex Cloud
|
|
||||||
YANDEX_CLOUD_API_KEY=""
|
|
||||||
YANDEX_CLOUD_FUNC_ID=""
|
|
||||||
|
|
||||||
# Firebase
|
|
||||||
# GOOGLE_APPLICATION_CREDENTIALS=
|
|
||||||
|
|
||||||
# LOGGING
|
|
||||||
RUST_BACKTRACE=1
|
|
||||||
# RUST_LOG=debug
|
|
||||||
145
.github/workflows/build.yml
vendored
145
.github/workflows/build.yml
vendored
@@ -1,145 +0,0 @@
|
|||||||
name: build
|
|
||||||
|
|
||||||
on:
|
|
||||||
push:
|
|
||||||
branches: [ "master" ]
|
|
||||||
tags-ignore: [ "release/v*" ]
|
|
||||||
|
|
||||||
permissions:
|
|
||||||
contents: write
|
|
||||||
|
|
||||||
env:
|
|
||||||
CARGO_TERM_COLOR: always
|
|
||||||
|
|
||||||
BINARY_NAME: schedule-parser-rusted
|
|
||||||
|
|
||||||
TEST_DB: ${{ secrets.TEST_DATABASE_URL }}
|
|
||||||
|
|
||||||
SENTRY_AUTH_TOKEN: ${{ secrets.SENTRY_AUTH_TOKEN }}
|
|
||||||
SENTRY_ORG: ${{ secrets.SENTRY_ORG }}
|
|
||||||
SENTRY_PROJECT: ${{ secrets.SENTRY_PROJECT }}
|
|
||||||
|
|
||||||
DOCKER_IMAGE_NAME: ${{ github.repository }}
|
|
||||||
|
|
||||||
DOCKER_REGISTRY_HOST: registry.n08i40k.ru
|
|
||||||
DOCKER_REGISTRY_USERNAME: ${{ github.repository_owner }}
|
|
||||||
DOCKER_REGISTRY_PASSWORD: ${{ secrets.DOCKER_REGISTRY_PASSWORD }}
|
|
||||||
|
|
||||||
jobs:
|
|
||||||
test:
|
|
||||||
name: Test
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
steps:
|
|
||||||
- uses: actions/checkout@v4
|
|
||||||
with:
|
|
||||||
fetch-depth: 0
|
|
||||||
|
|
||||||
- name: Setup Rust
|
|
||||||
uses: actions-rust-lang/setup-rust-toolchain@v1.11.0
|
|
||||||
with:
|
|
||||||
toolchain: stable
|
|
||||||
|
|
||||||
- name: Test
|
|
||||||
run: |
|
|
||||||
cargo test
|
|
||||||
env:
|
|
||||||
DATABASE_URL: ${{ env.TEST_DB }}
|
|
||||||
SCHEDULE_DISABLE_AUTO_UPDATE: 1
|
|
||||||
JWT_SECRET: "test-secret-at-least-256-bits-used"
|
|
||||||
VK_ID_CLIENT_ID: 0
|
|
||||||
VK_ID_REDIRECT_URI: "vk0://vk.com/blank.html"
|
|
||||||
TELEGRAM_BOT_ID: 0
|
|
||||||
TELEGRAM_MINI_APP_HOST: example.com
|
|
||||||
TELEGRAM_TEST_DC: false
|
|
||||||
YANDEX_CLOUD_API_KEY: ""
|
|
||||||
YANDEX_CLOUD_FUNC_ID: ""
|
|
||||||
build:
|
|
||||||
name: Build
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
needs: test
|
|
||||||
steps:
|
|
||||||
- uses: actions/checkout@v4
|
|
||||||
with:
|
|
||||||
fetch-depth: 0
|
|
||||||
|
|
||||||
- name: Setup Rust
|
|
||||||
uses: actions-rust-lang/setup-rust-toolchain@v1.11.0
|
|
||||||
with:
|
|
||||||
toolchain: stable
|
|
||||||
|
|
||||||
- name: Build
|
|
||||||
run: cargo build --release
|
|
||||||
|
|
||||||
- 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 }}"
|
|
||||||
|
|
||||||
- name: Deploy
|
|
||||||
run: curl ${{ secrets.DEPLOY_URL }}
|
|
||||||
173
.github/workflows/release.yml
vendored
173
.github/workflows/release.yml
vendored
@@ -1,173 +0,0 @@
|
|||||||
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: |
|
|
||||||
cargo test --verbose
|
|
||||||
env:
|
|
||||||
DATABASE_URL: ${{ env.TEST_DB }}
|
|
||||||
SCHEDULE_DISABLE_AUTO_UPDATE: 1
|
|
||||||
JWT_SECRET: "test-secret-at-least-256-bits-used"
|
|
||||||
VK_ID_CLIENT_ID: 0
|
|
||||||
VK_ID_REDIRECT_URI: "vk0://vk.com/blank.html"
|
|
||||||
TELEGRAM_BOT_ID: 0
|
|
||||||
TELEGRAM_MINI_APP_HOST: example.com
|
|
||||||
TELEGRAM_TEST_DC: false
|
|
||||||
YANDEX_CLOUD_API_KEY: ""
|
|
||||||
YANDEX_CLOUD_FUNC_ID: ""
|
|
||||||
build:
|
|
||||||
name: Build
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
needs: test
|
|
||||||
steps:
|
|
||||||
- uses: actions/checkout@v4
|
|
||||||
with:
|
|
||||||
fetch-depth: 0
|
|
||||||
|
|
||||||
- name: Setup Rust
|
|
||||||
uses: actions-rust-lang/setup-rust-toolchain@v1.11.0
|
|
||||||
with:
|
|
||||||
toolchain: stable
|
|
||||||
|
|
||||||
- name: Build
|
|
||||||
run: cargo build --release --verbose
|
|
||||||
|
|
||||||
- name: Extract debug symbols
|
|
||||||
run: |
|
|
||||||
objcopy --only-keep-debug target/release/${{ env.BINARY_NAME }}{,.d}
|
|
||||||
objcopy --strip-debug --strip-unneeded target/release/${{ env.BINARY_NAME }}
|
|
||||||
objcopy --add-gnu-debuglink target/release/${{ env.BINARY_NAME }}{.d,}
|
|
||||||
|
|
||||||
- name: Setup sentry-cli
|
|
||||||
uses: matbour/setup-sentry-cli@v2.0.0
|
|
||||||
with:
|
|
||||||
version: latest
|
|
||||||
token: ${{ env.SENTRY_AUTH_TOKEN }}
|
|
||||||
organization: ${{ env.SENTRY_ORG }}
|
|
||||||
project: ${{ env.SENTRY_PROJECT }}
|
|
||||||
|
|
||||||
- name: Upload debug symbols to Sentry
|
|
||||||
run: |
|
|
||||||
sentry-cli debug-files upload --include-sources .
|
|
||||||
|
|
||||||
- name: Upload build binary artifact
|
|
||||||
uses: actions/upload-artifact@v4
|
|
||||||
with:
|
|
||||||
name: release-binary
|
|
||||||
path: target/release/${{ env.BINARY_NAME }}
|
|
||||||
|
|
||||||
- name: Upload build debug symbols artifact
|
|
||||||
uses: actions/upload-artifact@v4
|
|
||||||
with:
|
|
||||||
name: release-symbols
|
|
||||||
path: target/release/${{ env.BINARY_NAME }}.d
|
|
||||||
|
|
||||||
docker:
|
|
||||||
name: Build & Push Docker Image
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
needs: build
|
|
||||||
steps:
|
|
||||||
- uses: actions/checkout@v4
|
|
||||||
|
|
||||||
- name: Download build artifacts
|
|
||||||
uses: actions/download-artifact@v4
|
|
||||||
with:
|
|
||||||
name: release-binary
|
|
||||||
|
|
||||||
- name: Setup Docker Buildx
|
|
||||||
uses: docker/setup-buildx-action@v3.10.0
|
|
||||||
|
|
||||||
- name: Login to Registry
|
|
||||||
uses: docker/login-action@v3.4.0
|
|
||||||
with:
|
|
||||||
registry: ${{ env.DOCKER_REGISTRY_HOST }}
|
|
||||||
username: ${{ env.DOCKER_REGISTRY_USERNAME }}
|
|
||||||
password: ${{ env.DOCKER_REGISTRY_PASSWORD }}
|
|
||||||
|
|
||||||
- name: Extract Docker metadata
|
|
||||||
id: meta
|
|
||||||
uses: docker/metadata-action@v5.7.0
|
|
||||||
with:
|
|
||||||
images: ${{ env.DOCKER_REGISTRY_HOST }}/${{ env.DOCKER_IMAGE_NAME }}
|
|
||||||
|
|
||||||
- name: Build and push Docker image
|
|
||||||
id: build-and-push
|
|
||||||
uses: docker/build-push-action@v6.15.0
|
|
||||||
with:
|
|
||||||
context: .
|
|
||||||
push: true
|
|
||||||
tags: ${{ steps.meta.outputs.tags }}
|
|
||||||
labels: ${{ steps.meta.outputs.labels }}
|
|
||||||
cache-from: type=gha
|
|
||||||
cache-to: type=gha,mode=max
|
|
||||||
build-args: |
|
|
||||||
"BINARY_NAME=${{ env.BINARY_NAME }}"
|
|
||||||
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
|
|
||||||
21
.github/workflows/test.yml
vendored
21
.github/workflows/test.yml
vendored
@@ -1,9 +1,10 @@
|
|||||||
name: cargo test
|
name: Tests
|
||||||
|
|
||||||
on:
|
on:
|
||||||
push:
|
push:
|
||||||
branches: [ "development" ]
|
branches: [ "master" ]
|
||||||
tags-ignore: [ "release/v*" ]
|
pull_request:
|
||||||
|
branches: [ "master" ]
|
||||||
|
|
||||||
permissions:
|
permissions:
|
||||||
contents: read
|
contents: read
|
||||||
@@ -18,20 +19,12 @@ jobs:
|
|||||||
|
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v4
|
- uses: actions/checkout@v4
|
||||||
|
|
||||||
- name: Build
|
- name: Build
|
||||||
run: cargo build
|
run: cargo build
|
||||||
|
- name: Create .env.test
|
||||||
|
run: touch .env.test
|
||||||
- name: Run tests
|
- name: Run tests
|
||||||
run: cargo test
|
run: cargo test
|
||||||
env:
|
env:
|
||||||
DATABASE_URL: ${{ secrets.TEST_DATABASE_URL }}
|
DATABASE_URL: ${{ secrets.TEST_DATABASE_URL }}
|
||||||
SCHEDULE_DISABLE_AUTO_UPDATE: 1
|
JWT_SECRET: "test-secret-at-least-256-bits-used"
|
||||||
JWT_SECRET: "test-secret-at-least-256-bits-used"
|
|
||||||
VK_ID_CLIENT_ID: 0
|
|
||||||
VK_ID_REDIRECT_URI: "vk0://vk.com/blank.html"
|
|
||||||
TELEGRAM_BOT_ID: 0
|
|
||||||
TELEGRAM_MINI_APP_HOST: example.com
|
|
||||||
TELEGRAM_TEST_DC: false
|
|
||||||
YANDEX_CLOUD_API_KEY: ""
|
|
||||||
YANDEX_CLOUD_FUNC_ID: ""
|
|
||||||
3
.gitignore
vendored
3
.gitignore
vendored
@@ -3,5 +3,4 @@
|
|||||||
schedule.json
|
schedule.json
|
||||||
teachers.json
|
teachers.json
|
||||||
|
|
||||||
.env*
|
.env*
|
||||||
/*-firebase-adminsdk-*.json
|
|
||||||
7
.idea/schedule-parser-rusted.iml
generated
7
.idea/schedule-parser-rusted.iml
generated
@@ -4,17 +4,12 @@
|
|||||||
<content url="file://$MODULE_DIR$">
|
<content url="file://$MODULE_DIR$">
|
||||||
<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-macros/src" isTestSource="false" />
|
||||||
<sourceFolder url="file://$MODULE_DIR$/actix-test/src" isTestSource="false" />
|
<sourceFolder url="file://$MODULE_DIR$/actix-test/src" isTestSource="false" />
|
||||||
<sourceFolder url="file://$MODULE_DIR$/schedule-parser/benches" isTestSource="true" />
|
|
||||||
<sourceFolder url="file://$MODULE_DIR$/schedule-parser/src" isTestSource="false" />
|
|
||||||
<sourceFolder url="file://$MODULE_DIR$/providers/base/src" isTestSource="false" />
|
|
||||||
<sourceFolder url="file://$MODULE_DIR$/providers/provider-engels-polytechnic/src" isTestSource="false" />
|
|
||||||
<sourceFolder url="file://$MODULE_DIR$/providers/src" isTestSource="false" />
|
|
||||||
<excludeFolder url="file://$MODULE_DIR$/actix-macros/target" />
|
<excludeFolder url="file://$MODULE_DIR$/actix-macros/target" />
|
||||||
<excludeFolder url="file://$MODULE_DIR$/actix-test/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
Normal file
9
.idea/sqldialects.xml
generated
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
<?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>
|
||||||
3973
Cargo.lock
generated
3973
Cargo.lock
generated
File diff suppressed because it is too large
Load Diff
103
Cargo.toml
103
Cargo.toml
@@ -1,81 +1,46 @@
|
|||||||
[workspace]
|
[workspace]
|
||||||
members = ["actix-macros", "actix-test", "providers"]
|
members = ["actix-macros", "actix-test"]
|
||||||
|
|
||||||
[package]
|
[package]
|
||||||
name = "schedule-parser-rusted"
|
name = "schedule-parser-rusted"
|
||||||
version = "1.3.1"
|
version = "0.8.0"
|
||||||
edition = "2024"
|
edition = "2024"
|
||||||
publish = false
|
publish = false
|
||||||
|
|
||||||
[profile.release]
|
|
||||||
debug = true
|
|
||||||
|
|
||||||
[features]
|
|
||||||
trace = ["tracing", "console-subscriber"]
|
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
providers = { path = "providers" }
|
actix-web = "4.10.2"
|
||||||
actix-macros = { path = "actix-macros" }
|
actix-macros = { path = "actix-macros" }
|
||||||
|
bcrypt = "0.17.0"
|
||||||
# serve api
|
calamine = "0.26.1"
|
||||||
actix-web = "4"
|
chrono = { version = "0.4.40", features = ["serde"] }
|
||||||
|
derive_more = "2.0.1"
|
||||||
# basic
|
diesel = { version = "2.2.8", features = ["postgres"] }
|
||||||
chrono = { version = "0", features = ["serde"] }
|
diesel-derive-enum = { git = "https://github.com/Havunen/diesel-derive-enum.git", features = ["postgres"] }
|
||||||
derive_more = { version = "2", features = ["full"] }
|
dotenvy = "0.15.7"
|
||||||
dotenvy = "0"
|
env_logger = "0.11.8"
|
||||||
|
futures-util = "0.3.31"
|
||||||
# sql
|
fuzzy-matcher = "0.3.7"
|
||||||
database = { path = "database" }
|
jsonwebtoken = { version = "9.3.1", features = ["use_pem"] }
|
||||||
|
hex = "0.4.3"
|
||||||
# logging
|
mime = "0.3.17"
|
||||||
env_logger = "0"
|
objectid = "0.2.0"
|
||||||
|
regex = "1.11.1"
|
||||||
# async
|
reqwest = "0.12.15"
|
||||||
tokio = { version = "1", features = ["macros", "rt-multi-thread"] }
|
serde = { version = "1.0.219", features = ["derive"] }
|
||||||
tokio-util = "0"
|
serde_json = "1.0.140"
|
||||||
futures-util = "0"
|
serde_with = "3.12.0"
|
||||||
|
serde_repr = "0.1.20"
|
||||||
# authorization
|
sha1 = "0.11.0-pre.5"
|
||||||
bcrypt = "0"
|
tokio = { version = "1.44.1", features = ["macros", "rt-multi-thread"] }
|
||||||
jsonwebtoken = { version = "9", features = ["use_pem"] }
|
rand = "0.9.0"
|
||||||
|
|
||||||
# creating users
|
|
||||||
objectid = "0"
|
|
||||||
|
|
||||||
# schedule downloader
|
|
||||||
reqwest = { version = "0", features = ["json"] }
|
|
||||||
mime = "0"
|
|
||||||
|
|
||||||
# error handling
|
|
||||||
sentry = "0"
|
|
||||||
sentry-actix = "0"
|
|
||||||
|
|
||||||
# [de]serializing
|
|
||||||
serde = { version = "1", features = ["derive"] }
|
|
||||||
serde_json = "1"
|
|
||||||
serde_with = "3"
|
|
||||||
|
|
||||||
sha1 = "0.11.0-rc.2"
|
|
||||||
|
|
||||||
# documentation
|
|
||||||
utoipa = { version = "5", features = ["actix_extras", "chrono"] }
|
utoipa = { version = "5", features = ["actix_extras", "chrono"] }
|
||||||
utoipa-rapidoc = { version = "6", features = ["actix-web"] }
|
utoipa-rapidoc = { version = "6.0.0", features = ["actix-web"] }
|
||||||
utoipa-actix-web = "0"
|
utoipa-actix-web = "0.1"
|
||||||
|
|
||||||
uuid = { version = "1", features = ["v4"] }
|
|
||||||
hex-literal = "1"
|
|
||||||
log = "0"
|
|
||||||
|
|
||||||
# telegram webdata deciding and verify
|
|
||||||
base64 = "0"
|
|
||||||
percent-encoding = "2"
|
|
||||||
ed25519-dalek = "3.0.0-pre.1"
|
|
||||||
|
|
||||||
# development tracing
|
|
||||||
console-subscriber = { version = "0", optional = true }
|
|
||||||
tracing = { version = "0", optional = true }
|
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
providers = { path = "providers", features = ["test"] }
|
actix-test = { path = "actix-test" }
|
||||||
actix-test = { path = "actix-test" }
|
criterion = "0.5.1"
|
||||||
|
|
||||||
|
[[bench]]
|
||||||
|
name = "parse"
|
||||||
|
harness = false
|
||||||
14
Dockerfile
14
Dockerfile
@@ -1,14 +0,0 @@
|
|||||||
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"]
|
|
||||||
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"
|
||||||
@@ -4,9 +4,9 @@ version = "0.1.0"
|
|||||||
edition = "2024"
|
edition = "2024"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
syn = "2.0.106"
|
syn = "2.0.100"
|
||||||
quote = "1.0.40"
|
quote = "1.0.40"
|
||||||
proc-macro2 = "1.0.101"
|
proc-macro2 = "1.0.94"
|
||||||
|
|
||||||
[lib]
|
[lib]
|
||||||
proc-macro = true
|
proc-macro = true
|
||||||
@@ -6,7 +6,7 @@ mod shared {
|
|||||||
use quote::{ToTokens, quote};
|
use quote::{ToTokens, quote};
|
||||||
use syn::{Attribute, DeriveInput};
|
use syn::{Attribute, DeriveInput};
|
||||||
|
|
||||||
pub fn find_status_code(attrs: &[Attribute]) -> Option<proc_macro2::TokenStream> {
|
pub fn find_status_code(attrs: &Vec<Attribute>) -> Option<proc_macro2::TokenStream> {
|
||||||
attrs
|
attrs
|
||||||
.iter()
|
.iter()
|
||||||
.find_map(|attr| -> Option<proc_macro2::TokenStream> {
|
.find_map(|attr| -> Option<proc_macro2::TokenStream> {
|
||||||
@@ -41,12 +41,14 @@ mod shared {
|
|||||||
|
|
||||||
let mut status_code_arms: Vec<proc_macro2::TokenStream> = variants
|
let mut status_code_arms: Vec<proc_macro2::TokenStream> = variants
|
||||||
.iter()
|
.iter()
|
||||||
.filter_map(|v| -> Option<proc_macro2::TokenStream> {
|
.map(|v| -> Option<proc_macro2::TokenStream> {
|
||||||
let status_code = find_status_code(&v.attrs)?;
|
let status_code = find_status_code(&v.attrs)?;
|
||||||
let variant_name = &v.ident;
|
let variant_name = &v.ident;
|
||||||
|
|
||||||
Some(quote! { #name::#variant_name => #status_code, })
|
Some(quote! { #name::#variant_name => #status_code, })
|
||||||
})
|
})
|
||||||
|
.filter(|v| v.is_some())
|
||||||
|
.map(|v| v.unwrap())
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
if status_code_arms.len() < variants.len() {
|
if status_code_arms.len() < variants.len() {
|
||||||
@@ -60,7 +62,7 @@ mod shared {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
mod middleware_error {
|
mod response_error_message {
|
||||||
use proc_macro::TokenStream;
|
use proc_macro::TokenStream;
|
||||||
use quote::quote;
|
use quote::quote;
|
||||||
|
|
||||||
@@ -79,7 +81,28 @@ mod middleware_error {
|
|||||||
|
|
||||||
fn error_response(&self) -> ::actix_web::HttpResponse<BoxBody> {
|
fn error_response(&self) -> ::actix_web::HttpResponse<BoxBody> {
|
||||||
::actix_web::HttpResponse::build(self.status_code())
|
::actix_web::HttpResponse::build(self.status_code())
|
||||||
.json(crate::middlewares::error::MiddlewareError::new(self.clone()))
|
.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)*
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
@@ -107,7 +130,7 @@ mod responder_json {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
mod ok_response {
|
mod into_response_error {
|
||||||
use proc_macro::TokenStream;
|
use proc_macro::TokenStream;
|
||||||
use quote::quote;
|
use quote::quote;
|
||||||
|
|
||||||
@@ -115,37 +138,46 @@ mod ok_response {
|
|||||||
let name = &ast.ident;
|
let name = &ast.ident;
|
||||||
|
|
||||||
TokenStream::from(quote! {
|
TokenStream::from(quote! {
|
||||||
impl crate::routes::schema::PartialOkResponse for #name {}
|
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,
|
||||||
mod err_response {
|
|
||||||
use proc_macro::TokenStream;
|
|
||||||
use quote::quote;
|
|
||||||
|
|
||||||
pub fn fmt(ast: &syn::DeriveInput) -> TokenStream {
|
|
||||||
let name = &ast.ident;
|
|
||||||
|
|
||||||
let status_code_arms = super::shared::get_arms(ast);
|
|
||||||
|
|
||||||
TokenStream::from(quote! {
|
|
||||||
impl crate::routes::schema::PartialErrResponse for #name {
|
|
||||||
fn status_code(&self) -> ::actix_web::http::StatusCode {
|
|
||||||
match self {
|
|
||||||
#(#status_code_arms)*
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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(MiddlewareError, attributes(status_code))]
|
#[proc_macro_derive(ResponseErrorMessage, attributes(status_code))]
|
||||||
pub fn moddleware_error_derive(input: TokenStream) -> TokenStream {
|
pub fn rem_derive(input: TokenStream) -> TokenStream {
|
||||||
let ast = syn::parse(input).unwrap();
|
let ast = syn::parse(input).unwrap();
|
||||||
|
|
||||||
middleware_error::fmt(&ast)
|
response_error_message::fmt(&ast)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[proc_macro_derive(ResponderJson)]
|
#[proc_macro_derive(ResponderJson)]
|
||||||
@@ -155,16 +187,23 @@ pub fn responser_json_derive(input: TokenStream) -> TokenStream {
|
|||||||
responder_json::fmt(&ast)
|
responder_json::fmt(&ast)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[proc_macro_derive(OkResponse)]
|
#[proc_macro_derive(IntoResponseError)]
|
||||||
pub fn ok_response_derive(input: TokenStream) -> TokenStream {
|
pub fn into_response_error_derive(input: TokenStream) -> TokenStream {
|
||||||
let ast = syn::parse(input).unwrap();
|
let ast = syn::parse(input).unwrap();
|
||||||
|
|
||||||
ok_response::fmt(&ast)
|
into_response_error::fmt(&ast)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[proc_macro_derive(ErrResponse, attributes(status_code))]
|
#[proc_macro_derive(IntoResponseErrorNamed)]
|
||||||
pub fn err_response_derive(input: TokenStream) -> TokenStream {
|
pub fn into_response_error_named_derive(input: TokenStream) -> TokenStream {
|
||||||
let ast = syn::parse(input).unwrap();
|
let ast = syn::parse(input).unwrap();
|
||||||
|
|
||||||
err_response::fmt(&ast)
|
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)
|
||||||
}
|
}
|
||||||
|
|||||||
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
@@ -4,5 +4,5 @@ version = "0.1.0"
|
|||||||
edition = "2024"
|
edition = "2024"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
actix-http = "3.11.1"
|
actix-http = "3.10.0"
|
||||||
actix-web = "4.11.0"
|
actix-web = "4.10.2"
|
||||||
12
benches/parse.rs
Normal file
12
benches/parse.rs
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
use criterion::{Criterion, criterion_group, criterion_main};
|
||||||
|
|
||||||
|
use schedule_parser_rusted::parser::parse_xls;
|
||||||
|
|
||||||
|
pub fn bench_parse_xls(c: &mut Criterion) {
|
||||||
|
let buffer: Vec<u8> = include_bytes!("../schedule.xls").to_vec();
|
||||||
|
|
||||||
|
c.bench_function("parse_xls", |b| b.iter(|| parse_xls(&buffer).unwrap()));
|
||||||
|
}
|
||||||
|
|
||||||
|
criterion_group!(benches, bench_parse_xls);
|
||||||
|
criterion_main!(benches);
|
||||||
@@ -1,12 +0,0 @@
|
|||||||
[package]
|
|
||||||
name = "database"
|
|
||||||
version = "0.1.0"
|
|
||||||
edition = "2024"
|
|
||||||
|
|
||||||
[dependencies]
|
|
||||||
migration = { path = "migration" }
|
|
||||||
entity = { path = "entity" }
|
|
||||||
sea-orm = { version = "2.0.0-rc.15", features = ["sqlx-postgres", "runtime-tokio"] }
|
|
||||||
|
|
||||||
paste = "1"
|
|
||||||
serde = { version = "1", features = ["derive"] }
|
|
||||||
1
database/entity/.gitignore
vendored
1
database/entity/.gitignore
vendored
@@ -1 +0,0 @@
|
|||||||
/target
|
|
||||||
@@ -1,9 +0,0 @@
|
|||||||
[package]
|
|
||||||
name = "entity"
|
|
||||||
version = "0.1.0"
|
|
||||||
edition = "2024"
|
|
||||||
|
|
||||||
[dependencies]
|
|
||||||
sea-orm = "2.0.0-rc.6"
|
|
||||||
serde = { version = "1.0.219", features = ["derive"] }
|
|
||||||
utoipa = "5.4.0"
|
|
||||||
@@ -1,7 +0,0 @@
|
|||||||
//! `SeaORM` Entity, @generated by sea-orm-codegen 1.1.12
|
|
||||||
|
|
||||||
pub mod prelude;
|
|
||||||
|
|
||||||
pub mod sea_orm_active_enums;
|
|
||||||
pub mod service_user;
|
|
||||||
pub mod user;
|
|
||||||
@@ -1,4 +0,0 @@
|
|||||||
//! `SeaORM` Entity, @generated by sea-orm-codegen 1.1.12
|
|
||||||
|
|
||||||
pub use super::service_user::Entity as ServiceUser;
|
|
||||||
pub use super::user::Entity as User;
|
|
||||||
@@ -1,25 +0,0 @@
|
|||||||
//! `SeaORM` Entity, @generated by sea-orm-codegen 1.1.12
|
|
||||||
|
|
||||||
use sea_orm::entity::prelude::*;
|
|
||||||
|
|
||||||
#[derive(
|
|
||||||
Debug,
|
|
||||||
Clone,
|
|
||||||
PartialEq,
|
|
||||||
Eq,
|
|
||||||
EnumIter,
|
|
||||||
DeriveActiveEnum,
|
|
||||||
:: serde :: Serialize,
|
|
||||||
:: serde :: Deserialize,
|
|
||||||
:: utoipa :: ToSchema,
|
|
||||||
)]
|
|
||||||
#[sea_orm(rs_type = "String", db_type = "Enum", enum_name = "user_role")]
|
|
||||||
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
|
|
||||||
pub enum UserRole {
|
|
||||||
#[sea_orm(string_value = "student")]
|
|
||||||
Student,
|
|
||||||
#[sea_orm(string_value = "teacher")]
|
|
||||||
Teacher,
|
|
||||||
#[sea_orm(string_value = "admin")]
|
|
||||||
Admin,
|
|
||||||
}
|
|
||||||
@@ -1,16 +0,0 @@
|
|||||||
//! `SeaORM` Entity, @generated by sea-orm-codegen 1.1.12
|
|
||||||
|
|
||||||
use sea_orm::entity::prelude::*;
|
|
||||||
|
|
||||||
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)]
|
|
||||||
#[sea_orm(table_name = "service_user")]
|
|
||||||
pub struct Model {
|
|
||||||
#[sea_orm(primary_key, auto_increment = false)]
|
|
||||||
pub id: String,
|
|
||||||
pub name: String,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
|
|
||||||
pub enum Relation {}
|
|
||||||
|
|
||||||
impl ActiveModelBehavior for ActiveModel {}
|
|
||||||
@@ -1,25 +0,0 @@
|
|||||||
//! `SeaORM` Entity, @generated by sea-orm-codegen 1.1.12
|
|
||||||
|
|
||||||
use super::sea_orm_active_enums::UserRole;
|
|
||||||
use sea_orm::entity::prelude::*;
|
|
||||||
|
|
||||||
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)]
|
|
||||||
#[sea_orm(table_name = "user")]
|
|
||||||
pub struct Model {
|
|
||||||
#[sea_orm(primary_key, auto_increment = false)]
|
|
||||||
pub id: String,
|
|
||||||
#[sea_orm(unique)]
|
|
||||||
pub username: String,
|
|
||||||
pub password: Option<String>,
|
|
||||||
pub vk_id: Option<i32>,
|
|
||||||
pub group: Option<String>,
|
|
||||||
pub role: UserRole,
|
|
||||||
pub android_version: Option<String>,
|
|
||||||
#[sea_orm(unique)]
|
|
||||||
pub telegram_id: Option<i64>,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
|
|
||||||
pub enum Relation {}
|
|
||||||
|
|
||||||
impl ActiveModelBehavior for ActiveModel {}
|
|
||||||
1
database/migration/.gitignore
vendored
1
database/migration/.gitignore
vendored
@@ -1 +0,0 @@
|
|||||||
/target
|
|
||||||
@@ -1,22 +0,0 @@
|
|||||||
[package]
|
|
||||||
name = "migration"
|
|
||||||
version = "0.1.0"
|
|
||||||
edition = "2021"
|
|
||||||
publish = false
|
|
||||||
|
|
||||||
[lib]
|
|
||||||
name = "migration"
|
|
||||||
path = "src/lib.rs"
|
|
||||||
|
|
||||||
[dependencies]
|
|
||||||
async-std = { version = "1", features = ["attributes", "tokio1"] }
|
|
||||||
|
|
||||||
[dependencies.sea-orm-migration]
|
|
||||||
version = "2.0.0-rc.15"
|
|
||||||
features = [
|
|
||||||
# Enable at least one `ASYNC_RUNTIME` and `DATABASE_DRIVER` feature if you want to run migration via CLI.
|
|
||||||
# View the list of supported features at https://www.sea-ql.org/SeaORM/docs/install-and-config/database-and-async-runtime.
|
|
||||||
# e.g.
|
|
||||||
"runtime-tokio", # `ASYNC_RUNTIME` feature
|
|
||||||
"sqlx-postgres", # `DATABASE_DRIVER` feature
|
|
||||||
]
|
|
||||||
@@ -1,18 +0,0 @@
|
|||||||
pub use sea_orm_migration::prelude::MigratorTrait;
|
|
||||||
|
|
||||||
use sea_orm_migration::prelude::*;
|
|
||||||
|
|
||||||
mod m20250904_024854_init;
|
|
||||||
mod m20251027_230335_add_service_users;
|
|
||||||
|
|
||||||
pub struct Migrator;
|
|
||||||
|
|
||||||
#[async_trait::async_trait]
|
|
||||||
impl MigratorTrait for Migrator {
|
|
||||||
fn migrations() -> Vec<Box<dyn MigrationTrait>> {
|
|
||||||
vec![
|
|
||||||
Box::new(m20250904_024854_init::Migration),
|
|
||||||
Box::new(m20251027_230335_add_service_users::Migration),
|
|
||||||
]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,70 +0,0 @@
|
|||||||
use sea_orm_migration::prelude::extension::postgres::Type;
|
|
||||||
use sea_orm_migration::sea_orm::{EnumIter, Iterable};
|
|
||||||
use sea_orm_migration::{prelude::*, schema::*};
|
|
||||||
|
|
||||||
#[derive(DeriveMigrationName)]
|
|
||||||
pub struct Migration;
|
|
||||||
|
|
||||||
#[async_trait::async_trait]
|
|
||||||
impl MigrationTrait for Migration {
|
|
||||||
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
|
|
||||||
manager
|
|
||||||
.create_type(
|
|
||||||
Type::create()
|
|
||||||
.as_enum(UserRole)
|
|
||||||
.values(UserRoleVariants::iter())
|
|
||||||
.to_owned(),
|
|
||||||
)
|
|
||||||
.await?;
|
|
||||||
|
|
||||||
manager
|
|
||||||
.create_table(
|
|
||||||
Table::create()
|
|
||||||
.table(User::Table)
|
|
||||||
.if_not_exists()
|
|
||||||
.col(string_uniq(User::Id).primary_key().not_null())
|
|
||||||
.col(string_uniq(User::Username).not_null())
|
|
||||||
.col(string_null(User::Password))
|
|
||||||
.col(integer_null(User::VkId))
|
|
||||||
.col(string_null(User::Group))
|
|
||||||
.col(enumeration(User::Role, UserRole, UserRoleVariants::iter()))
|
|
||||||
.col(string_null(User::AndroidVersion))
|
|
||||||
.col(big_integer_null(User::TelegramId).unique_key())
|
|
||||||
.to_owned(),
|
|
||||||
)
|
|
||||||
.await
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
|
|
||||||
manager
|
|
||||||
.drop_table(Table::drop().table(User::Table).to_owned())
|
|
||||||
.await?;
|
|
||||||
|
|
||||||
manager
|
|
||||||
.drop_type(Type::drop().name(UserRole).to_owned())
|
|
||||||
.await
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(DeriveIden)]
|
|
||||||
struct UserRole;
|
|
||||||
|
|
||||||
#[derive(DeriveIden, EnumIter)]
|
|
||||||
enum UserRoleVariants {
|
|
||||||
Student,
|
|
||||||
Teacher,
|
|
||||||
Admin,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(DeriveIden)]
|
|
||||||
enum User {
|
|
||||||
Table,
|
|
||||||
Id,
|
|
||||||
Username,
|
|
||||||
Password,
|
|
||||||
VkId,
|
|
||||||
Group,
|
|
||||||
Role,
|
|
||||||
AndroidVersion,
|
|
||||||
TelegramId,
|
|
||||||
}
|
|
||||||
@@ -1,33 +0,0 @@
|
|||||||
use sea_orm_migration::{prelude::*, schema::*};
|
|
||||||
|
|
||||||
#[derive(DeriveMigrationName)]
|
|
||||||
pub struct Migration;
|
|
||||||
|
|
||||||
#[async_trait::async_trait]
|
|
||||||
impl MigrationTrait for Migration {
|
|
||||||
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
|
|
||||||
manager
|
|
||||||
.create_table(
|
|
||||||
Table::create()
|
|
||||||
.table(ServiceUser::Table)
|
|
||||||
.if_not_exists()
|
|
||||||
.col(string_uniq(ServiceUser::Id).primary_key().not_null())
|
|
||||||
.col(string(ServiceUser::Name))
|
|
||||||
.to_owned(),
|
|
||||||
)
|
|
||||||
.await
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
|
|
||||||
manager
|
|
||||||
.drop_table(Table::drop().table(ServiceUser::Table).to_owned())
|
|
||||||
.await
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(DeriveIden)]
|
|
||||||
enum ServiceUser {
|
|
||||||
Table,
|
|
||||||
Id,
|
|
||||||
Name,
|
|
||||||
}
|
|
||||||
@@ -1,6 +0,0 @@
|
|||||||
use sea_orm_migration::prelude::*;
|
|
||||||
|
|
||||||
#[async_std::main]
|
|
||||||
async fn main() {
|
|
||||||
cli::run_cli(migration::Migrator).await;
|
|
||||||
}
|
|
||||||
@@ -1,31 +0,0 @@
|
|||||||
pub mod query;
|
|
||||||
|
|
||||||
pub use migration;
|
|
||||||
pub use sea_orm;
|
|
||||||
|
|
||||||
pub mod entity {
|
|
||||||
use serde::{Deserialize, Serialize};
|
|
||||||
|
|
||||||
pub use entity::*;
|
|
||||||
|
|
||||||
pub use entity::user::{
|
|
||||||
ActiveModel as ActiveUser, //
|
|
||||||
Column as UserColumn, //
|
|
||||||
Entity as UserEntity, //
|
|
||||||
Model as User, //
|
|
||||||
};
|
|
||||||
|
|
||||||
pub use entity::service_user::{
|
|
||||||
ActiveModel as ActiveServiceUser, //
|
|
||||||
Column as ServiceUserColumn, //
|
|
||||||
Entity as ServiceUserEntity, //
|
|
||||||
Model as ServiceUser, //
|
|
||||||
};
|
|
||||||
|
|
||||||
#[derive(Debug, Serialize, Deserialize, PartialEq)]
|
|
||||||
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
|
|
||||||
pub enum UserType {
|
|
||||||
Default,
|
|
||||||
Service,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,73 +0,0 @@
|
|||||||
use paste::paste;
|
|
||||||
use sea_orm::ColumnTrait;
|
|
||||||
use sea_orm::EntityTrait;
|
|
||||||
use sea_orm::QueryFilter;
|
|
||||||
|
|
||||||
pub struct Query;
|
|
||||||
|
|
||||||
macro_rules! ref_type {
|
|
||||||
(String) => {
|
|
||||||
&String
|
|
||||||
};
|
|
||||||
(str) => {
|
|
||||||
&str
|
|
||||||
};
|
|
||||||
($other:ty) => {
|
|
||||||
$other
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
macro_rules! define_is_exists {
|
|
||||||
($entity: ident, $by: ident, $by_type: ident, $by_column: ident) => {
|
|
||||||
paste! {
|
|
||||||
pub async fn [<is_ $entity _exists_by_ $by>](
|
|
||||||
db: &::sea_orm::DbConn,
|
|
||||||
$by: ref_type!($by_type)
|
|
||||||
) -> Result<bool, ::sea_orm::DbErr> {
|
|
||||||
::entity::$entity::Entity::find()
|
|
||||||
.filter(::entity::$entity::Column::$by_column.eq($by))
|
|
||||||
.one(db)
|
|
||||||
.await
|
|
||||||
.map(|x| x.is_some())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
macro_rules! define_find_by {
|
|
||||||
($entity: ident, $by: ident, $by_type: ident, $by_column: ident) => {
|
|
||||||
paste! {
|
|
||||||
pub async fn [<find_ $entity _by_ $by>](
|
|
||||||
db: &::sea_orm::DbConn,
|
|
||||||
$by: ref_type!($by_type)
|
|
||||||
) -> Result<Option<::entity::$entity::Model>, ::sea_orm::DbErr> {
|
|
||||||
::entity::$entity::Entity::find()
|
|
||||||
.filter(::entity::$entity::Column::$by_column.eq($by))
|
|
||||||
.one(db)
|
|
||||||
.await
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Query {
|
|
||||||
// User
|
|
||||||
|
|
||||||
define_find_by!(user, id, str, Id);
|
|
||||||
define_find_by!(user, telegram_id, i64, TelegramId);
|
|
||||||
define_find_by!(user, vk_id, i32, VkId);
|
|
||||||
define_find_by!(user, username, str, Username);
|
|
||||||
|
|
||||||
define_is_exists!(user, id, str, Id);
|
|
||||||
define_is_exists!(user, username, str, Username);
|
|
||||||
define_is_exists!(user, telegram_id, i64, TelegramId);
|
|
||||||
define_is_exists!(user, vk_id, i32, VkId);
|
|
||||||
|
|
||||||
// Service user
|
|
||||||
|
|
||||||
define_find_by!(service_user, id, str, Id);
|
|
||||||
define_find_by!(service_user, name, str, Name);
|
|
||||||
|
|
||||||
define_is_exists!(service_user, id, str, Id);
|
|
||||||
define_is_exists!(service_user, name, str, Name);
|
|
||||||
}
|
|
||||||
9
diesel.toml
Normal file
9
diesel.toml
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
# For documentation on how to configure this file,
|
||||||
|
# see https://diesel.rs/guides/configuring-diesel-cli
|
||||||
|
|
||||||
|
[print_schema]
|
||||||
|
file = "src/database/schema.rs"
|
||||||
|
custom_type_derives = ["diesel::query_builder::QueryId", "Clone"]
|
||||||
|
|
||||||
|
[migrations_directory]
|
||||||
|
dir = "./migrations"
|
||||||
0
migrations/.keep
Normal file
0
migrations/.keep
Normal file
6
migrations/00000000000000_diesel_initial_setup/down.sql
Normal file
6
migrations/00000000000000_diesel_initial_setup/down.sql
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
-- This file was automatically created by Diesel to setup helper functions
|
||||||
|
-- and other internal bookkeeping. This file is safe to edit, any future
|
||||||
|
-- changes will be added to existing projects as new migrations.
|
||||||
|
|
||||||
|
DROP FUNCTION IF EXISTS diesel_manage_updated_at(_tbl regclass);
|
||||||
|
DROP FUNCTION IF EXISTS diesel_set_updated_at();
|
||||||
36
migrations/00000000000000_diesel_initial_setup/up.sql
Normal file
36
migrations/00000000000000_diesel_initial_setup/up.sql
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
-- This file was automatically created by Diesel to setup helper functions
|
||||||
|
-- and other internal bookkeeping. This file is safe to edit, any future
|
||||||
|
-- changes will be added to existing projects as new migrations.
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
-- Sets up a trigger for the given table to automatically set a column called
|
||||||
|
-- `updated_at` whenever the row is modified (unless `updated_at` was included
|
||||||
|
-- in the modified columns)
|
||||||
|
--
|
||||||
|
-- # Example
|
||||||
|
--
|
||||||
|
-- ```sql
|
||||||
|
-- CREATE TABLE users (id SERIAL PRIMARY KEY, updated_at TIMESTAMP NOT NULL DEFAULT NOW());
|
||||||
|
--
|
||||||
|
-- SELECT diesel_manage_updated_at('users');
|
||||||
|
-- ```
|
||||||
|
CREATE OR REPLACE FUNCTION diesel_manage_updated_at(_tbl regclass) RETURNS VOID AS $$
|
||||||
|
BEGIN
|
||||||
|
EXECUTE format('CREATE TRIGGER set_updated_at BEFORE UPDATE ON %s
|
||||||
|
FOR EACH ROW EXECUTE PROCEDURE diesel_set_updated_at()', _tbl);
|
||||||
|
END;
|
||||||
|
$$ LANGUAGE plpgsql;
|
||||||
|
|
||||||
|
CREATE OR REPLACE FUNCTION diesel_set_updated_at() RETURNS trigger AS $$
|
||||||
|
BEGIN
|
||||||
|
IF (
|
||||||
|
NEW IS DISTINCT FROM OLD AND
|
||||||
|
NEW.updated_at IS NOT DISTINCT FROM OLD.updated_at
|
||||||
|
) THEN
|
||||||
|
NEW.updated_at := current_timestamp;
|
||||||
|
END IF;
|
||||||
|
RETURN NEW;
|
||||||
|
END;
|
||||||
|
$$ LANGUAGE plpgsql;
|
||||||
1
migrations/2025-03-21-211822_create_user_role/down.sql
Normal file
1
migrations/2025-03-21-211822_create_user_role/down.sql
Normal file
@@ -0,0 +1 @@
|
|||||||
|
DROP TYPE user_role;
|
||||||
4
migrations/2025-03-21-211822_create_user_role/up.sql
Normal file
4
migrations/2025-03-21-211822_create_user_role/up.sql
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
CREATE TYPE user_role AS ENUM (
|
||||||
|
'STUDENT',
|
||||||
|
'TEACHER',
|
||||||
|
'ADMIN');
|
||||||
1
migrations/2025-03-21-212111_create_users/down.sql
Normal file
1
migrations/2025-03-21-212111_create_users/down.sql
Normal file
@@ -0,0 +1 @@
|
|||||||
|
DROP TABLE users;
|
||||||
11
migrations/2025-03-21-212111_create_users/up.sql
Normal file
11
migrations/2025-03-21-212111_create_users/up.sql
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
CREATE TABLE users
|
||||||
|
(
|
||||||
|
id text PRIMARY KEY NOT NULL,
|
||||||
|
username text UNIQUE NOT NULL,
|
||||||
|
"password" text NOT NULL,
|
||||||
|
vk_id int4 NULL,
|
||||||
|
access_token text UNIQUE NOT NULL,
|
||||||
|
"group" text NOT NULL,
|
||||||
|
role user_role NOT NULL,
|
||||||
|
version text NOT NULL
|
||||||
|
);
|
||||||
1
migrations/2025-03-21-212723_create_fcm/down.sql
Normal file
1
migrations/2025-03-21-212723_create_fcm/down.sql
Normal file
@@ -0,0 +1 @@
|
|||||||
|
DROP TABLE fcm;
|
||||||
11
migrations/2025-03-21-212723_create_fcm/up.sql
Normal file
11
migrations/2025-03-21-212723_create_fcm/up.sql
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
CREATE TABLE fcm
|
||||||
|
(
|
||||||
|
user_id text PRIMARY KEY NOT NULL,
|
||||||
|
token text NOT NULL,
|
||||||
|
topics text[] 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;
|
||||||
@@ -1,12 +0,0 @@
|
|||||||
[package]
|
|
||||||
name = "providers"
|
|
||||||
version = "0.1.0"
|
|
||||||
edition = "2024"
|
|
||||||
|
|
||||||
[features]
|
|
||||||
test = ["provider-engels-polytechnic/test"]
|
|
||||||
|
|
||||||
[dependencies]
|
|
||||||
base = { path = "base" }
|
|
||||||
|
|
||||||
provider-engels-polytechnic = { path = "provider-engels-polytechnic" }
|
|
||||||
@@ -1,17 +0,0 @@
|
|||||||
[package]
|
|
||||||
name = "base"
|
|
||||||
version = "0.1.0"
|
|
||||||
edition = "2024"
|
|
||||||
|
|
||||||
[dependencies]
|
|
||||||
tokio-util = "0.7.16"
|
|
||||||
async-trait = "0.1.89"
|
|
||||||
|
|
||||||
chrono = { version = "0.4.41", features = ["serde"] }
|
|
||||||
|
|
||||||
serde = { version = "1.0.219", features = ["derive"] }
|
|
||||||
serde_repr = "0.1.20"
|
|
||||||
|
|
||||||
utoipa = { version = "5.4.0", features = ["macros", "chrono"] }
|
|
||||||
|
|
||||||
sha1 = "0.11.0-rc.2"
|
|
||||||
@@ -1,53 +0,0 @@
|
|||||||
use sha1::Digest;
|
|
||||||
use sha1::digest::OutputSizeUser;
|
|
||||||
use sha1::digest::typenum::Unsigned;
|
|
||||||
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 {
|
|
||||||
static ALPHABET: [char; 16] = [
|
|
||||||
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F',
|
|
||||||
];
|
|
||||||
|
|
||||||
let mut hex = String::with_capacity(<D as OutputSizeUser>::OutputSize::USIZE * 2);
|
|
||||||
|
|
||||||
for byte in self.digest.finalize().0.into_iter() {
|
|
||||||
let byte: u8 = byte;
|
|
||||||
|
|
||||||
hex.push(ALPHABET[(byte >> 4) as usize]);
|
|
||||||
hex.push(ALPHABET[(byte & 0xF) as usize]);
|
|
||||||
}
|
|
||||||
|
|
||||||
hex
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,231 +0,0 @@
|
|||||||
use crate::hasher::DigestHasher;
|
|
||||||
use async_trait::async_trait;
|
|
||||||
use chrono::{DateTime, Utc};
|
|
||||||
use serde::{Deserialize, Serialize};
|
|
||||||
use serde_repr::{Deserialize_repr, Serialize_repr};
|
|
||||||
use sha1::{Digest, Sha1};
|
|
||||||
use std::collections::HashMap;
|
|
||||||
use std::hash::Hash;
|
|
||||||
use std::sync::Arc;
|
|
||||||
use tokio_util::sync::CancellationToken;
|
|
||||||
use utoipa::ToSchema;
|
|
||||||
|
|
||||||
mod hasher;
|
|
||||||
|
|
||||||
// pub(crate) mod internal {
|
|
||||||
// use super::{LessonBoundaries, LessonType};
|
|
||||||
// use chrono::{DateTime, Utc};
|
|
||||||
//
|
|
||||||
// /// Data cell storing the group name.
|
|
||||||
// pub struct GroupCellInfo {
|
|
||||||
// /// Column index.
|
|
||||||
// pub column: u32,
|
|
||||||
//
|
|
||||||
// /// Text in the cell.
|
|
||||||
// pub name: String,
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// /// Data cell storing the line.
|
|
||||||
// pub struct DayCellInfo {
|
|
||||||
// /// Line index.
|
|
||||||
// pub row: u32,
|
|
||||||
//
|
|
||||||
// /// Column index.
|
|
||||||
// pub column: u32,
|
|
||||||
//
|
|
||||||
// /// Day name.
|
|
||||||
// pub name: String,
|
|
||||||
//
|
|
||||||
// /// Date of the day.
|
|
||||||
// pub date: DateTime<Utc>,
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// /// Data on the time of lessons from the second column of the schedule.
|
|
||||||
// pub struct BoundariesCellInfo {
|
|
||||||
// /// Temporary segment of the lesson.
|
|
||||||
// pub time_range: LessonBoundaries,
|
|
||||||
//
|
|
||||||
// /// Type of lesson.
|
|
||||||
// pub lesson_type: LessonType,
|
|
||||||
//
|
|
||||||
// /// The lesson index.
|
|
||||||
// pub default_index: Option<u32>,
|
|
||||||
//
|
|
||||||
// /// The frame of the cell.
|
|
||||||
// pub xls_range: ((u32, u32), (u32, u32)),
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
/// The beginning and end of the lesson.
|
|
||||||
#[derive(Clone, Hash, Debug, Serialize, Deserialize, ToSchema)]
|
|
||||||
pub struct LessonBoundaries {
|
|
||||||
/// The beginning of a lesson.
|
|
||||||
pub start: DateTime<Utc>,
|
|
||||||
|
|
||||||
/// The end of the lesson.
|
|
||||||
pub end: DateTime<Utc>,
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Type of lesson.
|
|
||||||
#[derive(Clone, Hash, PartialEq, Debug, Serialize_repr, Deserialize_repr, ToSchema)]
|
|
||||||
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
|
|
||||||
#[repr(u8)]
|
|
||||||
pub enum LessonType {
|
|
||||||
/// Обычная.
|
|
||||||
Default = 0,
|
|
||||||
|
|
||||||
/// Допы.
|
|
||||||
Additional,
|
|
||||||
|
|
||||||
/// Перемена.
|
|
||||||
Break,
|
|
||||||
|
|
||||||
/// Консультация.
|
|
||||||
Consultation,
|
|
||||||
|
|
||||||
/// Самостоятельная работа.
|
|
||||||
IndependentWork,
|
|
||||||
|
|
||||||
/// Зачёт.
|
|
||||||
Exam,
|
|
||||||
|
|
||||||
/// Зачёт с оценкой.
|
|
||||||
ExamWithGrade,
|
|
||||||
|
|
||||||
/// Экзамен.
|
|
||||||
ExamDefault,
|
|
||||||
|
|
||||||
/// Курсовой проект.
|
|
||||||
CourseProject,
|
|
||||||
|
|
||||||
/// Защита курсового проекта.
|
|
||||||
CourseProjectDefense,
|
|
||||||
|
|
||||||
/// Практическое занятие.
|
|
||||||
Practice,
|
|
||||||
|
|
||||||
/// Дифференцированный зачёт.
|
|
||||||
DifferentiatedExam,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Clone, Hash, Debug, Serialize, Deserialize, ToSchema)]
|
|
||||||
pub struct LessonSubGroup {
|
|
||||||
/// Cabinet, if present.
|
|
||||||
pub cabinet: Option<String>,
|
|
||||||
|
|
||||||
/// Full name of the teacher.
|
|
||||||
pub teacher: Option<String>,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Clone, Hash, Debug, Serialize, Deserialize, ToSchema)]
|
|
||||||
#[serde(rename_all = "camelCase")]
|
|
||||||
pub struct Lesson {
|
|
||||||
/// Type.
|
|
||||||
#[serde(rename = "type")]
|
|
||||||
pub lesson_type: LessonType,
|
|
||||||
|
|
||||||
/// Lesson indexes, if present.
|
|
||||||
pub range: Option<[u8; 2]>,
|
|
||||||
|
|
||||||
/// Name.
|
|
||||||
pub name: Option<String>,
|
|
||||||
|
|
||||||
/// The beginning and end.
|
|
||||||
pub time: LessonBoundaries,
|
|
||||||
|
|
||||||
/// List of subgroups.
|
|
||||||
#[serde(rename = "subgroups")]
|
|
||||||
pub subgroups: Option<Vec<Option<LessonSubGroup>>>,
|
|
||||||
|
|
||||||
/// Group name, if this is a schedule for teachers.
|
|
||||||
pub group: Option<String>,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Clone, Hash, Debug, Serialize, Deserialize, ToSchema)]
|
|
||||||
pub struct Day {
|
|
||||||
/// Day of the week.
|
|
||||||
pub name: String,
|
|
||||||
|
|
||||||
/// Address of another corps.
|
|
||||||
pub street: Option<String>,
|
|
||||||
|
|
||||||
/// Date.
|
|
||||||
pub date: DateTime<Utc>,
|
|
||||||
|
|
||||||
/// List of lessons on this day.
|
|
||||||
pub lessons: Vec<Lesson>,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Clone, Hash, Debug, Serialize, Deserialize, ToSchema)]
|
|
||||||
pub struct ScheduleEntry {
|
|
||||||
/// The name of the group or name of the teacher.
|
|
||||||
pub name: String,
|
|
||||||
|
|
||||||
/// List of six days.
|
|
||||||
pub days: Vec<Day>,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Clone)]
|
|
||||||
pub struct ParsedSchedule {
|
|
||||||
/// List of groups.
|
|
||||||
pub groups: HashMap<String, ScheduleEntry>,
|
|
||||||
|
|
||||||
/// List of teachers.
|
|
||||||
pub teachers: HashMap<String, ScheduleEntry>,
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Represents a snapshot of the schedule parsed from an XLS file.
|
|
||||||
#[derive(Clone)]
|
|
||||||
pub struct ScheduleSnapshot {
|
|
||||||
/// Timestamp when the Polytechnic website was queried for the schedule.
|
|
||||||
pub fetched_at: DateTime<Utc>,
|
|
||||||
|
|
||||||
/// Timestamp indicating when the schedule was last updated on the Polytechnic website.
|
|
||||||
///
|
|
||||||
/// <note>
|
|
||||||
/// This value is determined by the website's content and does not depend on the application.
|
|
||||||
/// </note>
|
|
||||||
pub updated_at: DateTime<Utc>,
|
|
||||||
|
|
||||||
/// URL pointing to the XLS file containing the source schedule data.
|
|
||||||
pub url: String,
|
|
||||||
|
|
||||||
/// Parsed schedule data in the application's internal representation.
|
|
||||||
pub data: ParsedSchedule,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl ScheduleSnapshot {
|
|
||||||
/// Converting the schedule data into a hash.
|
|
||||||
/// ### Important!
|
|
||||||
/// The hash does not depend on the dates.
|
|
||||||
/// If the application is restarted, but the file with source schedule will remain unchanged, then the hash will not change.
|
|
||||||
pub fn hash(&self) -> String {
|
|
||||||
let mut hasher = DigestHasher::from(Sha1::new());
|
|
||||||
|
|
||||||
self.data.teachers.iter().for_each(|e| e.hash(&mut hasher));
|
|
||||||
self.data.groups.iter().for_each(|e| e.hash(&mut hasher));
|
|
||||||
|
|
||||||
hasher.finalize()
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Simply updates the value of [`ScheduleSnapshot::fetched_at`].
|
|
||||||
/// Used for auto-updates.
|
|
||||||
pub fn update(&mut self) {
|
|
||||||
self.fetched_at = Utc::now();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[async_trait]
|
|
||||||
pub trait ScheduleProvider
|
|
||||||
where
|
|
||||||
Self: Sync + Send,
|
|
||||||
{
|
|
||||||
/// Returns ok when task has been canceled.
|
|
||||||
/// Returns err when error appeared while trying to parse or download schedule
|
|
||||||
async fn start_auto_update_task(
|
|
||||||
&self,
|
|
||||||
cancellation_token: CancellationToken,
|
|
||||||
) -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>>;
|
|
||||||
|
|
||||||
async fn get_schedule(&self) -> Arc<ScheduleSnapshot>;
|
|
||||||
}
|
|
||||||
@@ -1,31 +0,0 @@
|
|||||||
[package]
|
|
||||||
name = "provider-engels-polytechnic"
|
|
||||||
version = "0.2.3"
|
|
||||||
edition = "2024"
|
|
||||||
|
|
||||||
[features]
|
|
||||||
test = []
|
|
||||||
|
|
||||||
[dependencies]
|
|
||||||
base = { path = "../base" }
|
|
||||||
|
|
||||||
tokio = { version = "1", features = ["sync", "macros", "time"] }
|
|
||||||
tokio-util = "0"
|
|
||||||
|
|
||||||
chrono = { version = "0", features = ["serde"] }
|
|
||||||
|
|
||||||
derive_more = { version = "2", features = ["error", "display", "from"] }
|
|
||||||
|
|
||||||
utoipa = { version = "5", features = ["macros", "chrono"] }
|
|
||||||
|
|
||||||
calamine = "0"
|
|
||||||
async-trait = "0"
|
|
||||||
|
|
||||||
reqwest = "0"
|
|
||||||
ua_generator = "0"
|
|
||||||
regex = "1"
|
|
||||||
strsim = "0"
|
|
||||||
log = "0"
|
|
||||||
sentry = "0"
|
|
||||||
fancy-regex = "0"
|
|
||||||
|
|
||||||
@@ -1,84 +0,0 @@
|
|||||||
pub use crate::updater::{UpdateSource, Updater};
|
|
||||||
use async_trait::async_trait;
|
|
||||||
use base::{ScheduleProvider, ScheduleSnapshot};
|
|
||||||
use std::ops::DerefMut;
|
|
||||||
use std::sync::Arc;
|
|
||||||
use std::time::Duration;
|
|
||||||
use tokio::sync::RwLock;
|
|
||||||
use tokio::time::interval;
|
|
||||||
use tokio_util::sync::CancellationToken;
|
|
||||||
|
|
||||||
mod parser;
|
|
||||||
mod updater;
|
|
||||||
mod xls_downloader;
|
|
||||||
|
|
||||||
#[cfg(feature = "test")]
|
|
||||||
pub mod test_utils {
|
|
||||||
pub use crate::parser::test_utils::test_result;
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct EngelsPolytechnicProvider {
|
|
||||||
updater: Updater,
|
|
||||||
snapshot: Arc<ScheduleSnapshot>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl EngelsPolytechnicProvider {
|
|
||||||
pub async fn get(
|
|
||||||
update_source: UpdateSource,
|
|
||||||
) -> Result<Arc<dyn ScheduleProvider>, crate::updater::Error> {
|
|
||||||
let (updater, snapshot) = Updater::new(update_source).await?;
|
|
||||||
|
|
||||||
Ok(Arc::new(Wrapper {
|
|
||||||
inner: RwLock::new(Self {
|
|
||||||
updater,
|
|
||||||
snapshot: Arc::new(snapshot),
|
|
||||||
}),
|
|
||||||
}))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct Wrapper {
|
|
||||||
inner: RwLock<EngelsPolytechnicProvider>,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[async_trait]
|
|
||||||
impl ScheduleProvider for Wrapper {
|
|
||||||
async fn start_auto_update_task(
|
|
||||||
&self,
|
|
||||||
cancellation_token: CancellationToken,
|
|
||||||
) -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> {
|
|
||||||
let mut ticker = interval(Duration::from_secs(60 * 30));
|
|
||||||
ticker.tick().await; // bc we already have the latest schedule, when instantiating provider
|
|
||||||
|
|
||||||
loop {
|
|
||||||
tokio::select! {
|
|
||||||
_ = ticker.tick() => {
|
|
||||||
let mut lock = self.inner.write().await;
|
|
||||||
let this= lock.deref_mut();
|
|
||||||
|
|
||||||
log::info!("Updating schedule...");
|
|
||||||
|
|
||||||
match this.updater.update(&this.snapshot).await {
|
|
||||||
Ok(snapshot) => {
|
|
||||||
this.snapshot = Arc::new(snapshot);
|
|
||||||
},
|
|
||||||
|
|
||||||
Err(updater::Error::EmptyUri) => {},
|
|
||||||
|
|
||||||
Err(err) => {
|
|
||||||
sentry::capture_error(&err);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
_ = cancellation_token.cancelled() => {
|
|
||||||
return Ok(());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn get_schedule(&self) -> Arc<ScheduleSnapshot> {
|
|
||||||
self.inner.read().await.snapshot.clone()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,25 +0,0 @@
|
|||||||
use crate::parser::worksheet::CellPos;
|
|
||||||
use derive_more::{Display, Error, From};
|
|
||||||
|
|
||||||
#[derive(Debug, Display, Error, From)]
|
|
||||||
pub enum Error {
|
|
||||||
#[from]
|
|
||||||
BadXls(calamine::XlsError),
|
|
||||||
|
|
||||||
#[display("No work sheets found.")]
|
|
||||||
NoWorkSheets,
|
|
||||||
|
|
||||||
#[display("There is no data on work sheet boundaries.")]
|
|
||||||
UnknownWorkSheetRange,
|
|
||||||
|
|
||||||
#[display("Failed to read lesson start and end of lesson at {_0}.")]
|
|
||||||
NoLessonBoundaries(CellPos),
|
|
||||||
|
|
||||||
#[display("No start and end times matching the lesson (at {_0}) was found.")]
|
|
||||||
LessonTimeNotFound(CellPos),
|
|
||||||
|
|
||||||
#[display("Unknown lesson type `{type}` at {pos}")]
|
|
||||||
UnknownLessonType { pos: CellPos, r#type: String },
|
|
||||||
}
|
|
||||||
|
|
||||||
pub type Result<T> = core::result::Result<T, Error>;
|
|
||||||
@@ -1,21 +0,0 @@
|
|||||||
#[macro_export]
|
|
||||||
macro_rules! or_continue {
|
|
||||||
( $e:expr ) => {{
|
|
||||||
if let Some(x) = $e {
|
|
||||||
x
|
|
||||||
} else {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
}};
|
|
||||||
}
|
|
||||||
|
|
||||||
#[macro_export]
|
|
||||||
macro_rules! or_break {
|
|
||||||
( $e:expr ) => {{
|
|
||||||
if let Some(x) = $e {
|
|
||||||
x
|
|
||||||
} else {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}};
|
|
||||||
}
|
|
||||||
@@ -1,859 +0,0 @@
|
|||||||
pub use self::error::{Error, Result};
|
|
||||||
use crate::or_continue;
|
|
||||||
use crate::parser::worksheet::{CellPos, CellRange, WorkSheet};
|
|
||||||
use crate::parser::LessonParseResult::{Lessons, Street};
|
|
||||||
use base::LessonType::Break;
|
|
||||||
use base::{
|
|
||||||
Day, Lesson, LessonBoundaries, LessonSubGroup, LessonType, ParsedSchedule, ScheduleEntry,
|
|
||||||
};
|
|
||||||
use calamine::{open_workbook_from_rs, Reader, Xls};
|
|
||||||
use chrono::{DateTime, Duration, NaiveDate, NaiveTime, Utc};
|
|
||||||
use regex::Regex;
|
|
||||||
use std::collections::HashMap;
|
|
||||||
use std::io::Cursor;
|
|
||||||
use std::sync::LazyLock;
|
|
||||||
|
|
||||||
mod error;
|
|
||||||
mod macros;
|
|
||||||
mod worksheet;
|
|
||||||
|
|
||||||
/// Data cell storing the group name.
|
|
||||||
pub struct GroupMarkup {
|
|
||||||
/// Column index.
|
|
||||||
pub column: u32,
|
|
||||||
|
|
||||||
/// Text in the cell.
|
|
||||||
pub name: String,
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Data cell storing the line.
|
|
||||||
pub struct DayMarkup {
|
|
||||||
/// Line index.
|
|
||||||
pub row: u32,
|
|
||||||
|
|
||||||
/// Column index.
|
|
||||||
pub column: u32,
|
|
||||||
|
|
||||||
/// Day name.
|
|
||||||
pub name: String,
|
|
||||||
|
|
||||||
/// Date of the day.
|
|
||||||
pub date: DateTime<Utc>,
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct WorkSheetMarkup {
|
|
||||||
days: Box<[DayMarkup]>,
|
|
||||||
groups: Box<[GroupMarkup]>,
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Data on the time of lessons from the second column of the schedule.
|
|
||||||
pub struct BoundariesData {
|
|
||||||
/// Temporary segment of the lesson.
|
|
||||||
pub time_range: LessonBoundaries,
|
|
||||||
|
|
||||||
/// Type of lesson.
|
|
||||||
pub lesson_type: LessonType,
|
|
||||||
|
|
||||||
/// The lesson index.
|
|
||||||
pub default_index: Option<u32>,
|
|
||||||
|
|
||||||
/// The frame of the cell.
|
|
||||||
pub range: CellRange,
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Obtaining a "skeleton" schedule from the working sheet.
|
|
||||||
fn parse_markup(worksheet: &WorkSheet) -> Result<WorkSheetMarkup> {
|
|
||||||
struct PartialDayMarkup {
|
|
||||||
row: u32,
|
|
||||||
name: String,
|
|
||||||
date: Option<DateTime<Utc>>,
|
|
||||||
}
|
|
||||||
|
|
||||||
let mut groups: Vec<GroupMarkup> = Vec::new();
|
|
||||||
let mut days: Vec<PartialDayMarkup> = Vec::new();
|
|
||||||
|
|
||||||
let (start_row, start_col) = worksheet.start().ok_or(Error::UnknownWorkSheetRange)?;
|
|
||||||
let (end_row, end_col) = worksheet.end().ok_or(Error::UnknownWorkSheetRange)?;
|
|
||||||
|
|
||||||
let mut row = start_row;
|
|
||||||
|
|
||||||
while row < end_row {
|
|
||||||
row += 1;
|
|
||||||
|
|
||||||
let day_full_name = or_continue!(worksheet.get_string_from_cell(row, 0));
|
|
||||||
|
|
||||||
// parse groups row when days column will found
|
|
||||||
if groups.is_empty() {
|
|
||||||
// переход на предыдущую строку
|
|
||||||
row -= 1;
|
|
||||||
|
|
||||||
for column in (start_col + 2)..=end_col {
|
|
||||||
groups.push(GroupMarkup {
|
|
||||||
column,
|
|
||||||
name: or_continue!(worksheet.get_string_from_cell(row, column))
|
|
||||||
.replace(" ", ""),
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
// возврат на текущую строку
|
|
||||||
row += 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
let (day_name, day_date) = {
|
|
||||||
let space_index = match day_full_name.find(' ') {
|
|
||||||
Some(index) => {
|
|
||||||
if index < 10 {
|
|
||||||
break;
|
|
||||||
} else {
|
|
||||||
index
|
|
||||||
}
|
|
||||||
}
|
|
||||||
None => break,
|
|
||||||
};
|
|
||||||
|
|
||||||
let name = day_full_name[..space_index].to_string();
|
|
||||||
|
|
||||||
let date_slice = &day_full_name[space_index + 1..];
|
|
||||||
let date = NaiveDate::parse_from_str(date_slice, "%d.%m.%Y")
|
|
||||||
.map(|date| date.and_time(NaiveTime::default()).and_utc())
|
|
||||||
.ok();
|
|
||||||
|
|
||||||
(name, date)
|
|
||||||
};
|
|
||||||
|
|
||||||
days.push(PartialDayMarkup {
|
|
||||||
row,
|
|
||||||
name: day_name,
|
|
||||||
date: day_date,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
// fix unparsable day dates
|
|
||||||
let days_max = days.len().min(5);
|
|
||||||
|
|
||||||
for i in 0..days_max {
|
|
||||||
if days[i].date.is_none() && days[i + 1].date.is_some() {
|
|
||||||
days[i].date = Some(days[i + 1].date.unwrap() - Duration::days(1));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for i in 0..days_max {
|
|
||||||
let i = days_max - i;
|
|
||||||
|
|
||||||
if days[i - 1].date.is_none() && days[i].date.is_some() {
|
|
||||||
days[i - 1].date = Some(days[i].date.unwrap() - Duration::days(1));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
let days = days
|
|
||||||
.into_iter()
|
|
||||||
.map(|day| DayMarkup {
|
|
||||||
row: day.row,
|
|
||||||
column: 0,
|
|
||||||
name: day.name,
|
|
||||||
date: day.date.unwrap(),
|
|
||||||
})
|
|
||||||
.collect();
|
|
||||||
|
|
||||||
Ok(WorkSheetMarkup {
|
|
||||||
days,
|
|
||||||
groups: groups.into_boxed_slice(),
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
/// The result of obtaining a lesson from the cell.
|
|
||||||
enum LessonParseResult {
|
|
||||||
/// List of lessons long from one to two.
|
|
||||||
///
|
|
||||||
/// The number of lessons will be equal to one if the couple is the first in the day,
|
|
||||||
/// otherwise the list from the change template and the lesson itself will be returned.
|
|
||||||
Lessons(Vec<Lesson>),
|
|
||||||
|
|
||||||
/// Street on which the Polytechnic Corps is located.
|
|
||||||
Street(String),
|
|
||||||
}
|
|
||||||
|
|
||||||
// noinspection GrazieInspection
|
|
||||||
/// Obtaining a non-standard type of lesson by name.
|
|
||||||
fn guess_lesson_type(text: &str) -> Option<LessonType> {
|
|
||||||
static MAP: LazyLock<HashMap<&str, LessonType>> = LazyLock::new(|| {
|
|
||||||
HashMap::from([
|
|
||||||
("о важном", LessonType::Additional),
|
|
||||||
("консультация", LessonType::Consultation),
|
|
||||||
("самостоятельная работа", LessonType::IndependentWork),
|
|
||||||
("зачет", LessonType::Exam),
|
|
||||||
("зачет с оценкой", LessonType::ExamWithGrade),
|
|
||||||
("экзамен", LessonType::ExamDefault),
|
|
||||||
("курсовой проект", LessonType::CourseProject),
|
|
||||||
("защита курсового проекта", LessonType::CourseProjectDefense),
|
|
||||||
("практическое занятие", LessonType::Practice),
|
|
||||||
("дифференцированный зачет", LessonType::DifferentiatedExam),
|
|
||||||
])
|
|
||||||
});
|
|
||||||
|
|
||||||
let name_lower = text.to_lowercase();
|
|
||||||
|
|
||||||
MAP.iter()
|
|
||||||
.map(|(text, lesson_type)| (lesson_type, strsim::levenshtein(text, &name_lower)))
|
|
||||||
.filter(|x| x.1 <= 4)
|
|
||||||
.min_by_key(|(_, score)| *score)
|
|
||||||
.map(|v| v.0.clone())
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Getting a pair or street from a cell.
|
|
||||||
fn parse_lesson(
|
|
||||||
worksheet: &WorkSheet,
|
|
||||||
day: &Day,
|
|
||||||
day_boundaries: &[BoundariesData],
|
|
||||||
lesson_boundaries: &BoundariesData,
|
|
||||||
group_column: u32,
|
|
||||||
) -> Result<LessonParseResult> {
|
|
||||||
let row = lesson_boundaries.range.start.row;
|
|
||||||
|
|
||||||
let name = {
|
|
||||||
let cell_data = match worksheet.get_string_from_cell(row, group_column) {
|
|
||||||
Some(x) => x,
|
|
||||||
None => return Ok(Lessons(Vec::new())),
|
|
||||||
};
|
|
||||||
|
|
||||||
static OTHER_STREET_RE: LazyLock<Regex> =
|
|
||||||
LazyLock::new(|| Regex::new(r"^[А-Я][а-я]+[,\s]+д\.\s\d+$").unwrap());
|
|
||||||
|
|
||||||
if OTHER_STREET_RE.is_match(&cell_data) {
|
|
||||||
return Ok(Street(cell_data));
|
|
||||||
}
|
|
||||||
|
|
||||||
cell_data
|
|
||||||
};
|
|
||||||
|
|
||||||
let lesson_cell_range = worksheet.get_merge_from_start(row, group_column);
|
|
||||||
|
|
||||||
let (default_range, lesson_time) = {
|
|
||||||
let end_time_arr = day_boundaries
|
|
||||||
.iter()
|
|
||||||
.filter(
|
|
||||||
|BoundariesData {
|
|
||||||
range: CellRange { end, .. },
|
|
||||||
..
|
|
||||||
}| { lesson_cell_range.end.row <= end.row },
|
|
||||||
)
|
|
||||||
.collect::<Vec<&BoundariesData>>();
|
|
||||||
|
|
||||||
let end_time = end_time_arr
|
|
||||||
.first()
|
|
||||||
.ok_or(Error::LessonTimeNotFound(CellPos::new(row, group_column)))?;
|
|
||||||
|
|
||||||
let range: Option<[u8; 2]> = if lesson_boundaries.default_index.is_some() {
|
|
||||||
let default = lesson_boundaries.default_index.unwrap() as u8;
|
|
||||||
Some([default, end_time.default_index.unwrap() as u8])
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
};
|
|
||||||
|
|
||||||
let time = LessonBoundaries {
|
|
||||||
start: lesson_boundaries.time_range.start,
|
|
||||||
end: end_time.time_range.end,
|
|
||||||
};
|
|
||||||
|
|
||||||
(range, time)
|
|
||||||
};
|
|
||||||
|
|
||||||
let ParsedLessonName {
|
|
||||||
name,
|
|
||||||
mut subgroups,
|
|
||||||
r#type: lesson_type,
|
|
||||||
} = parse_name_and_subgroups(&name, row, group_column)?;
|
|
||||||
|
|
||||||
{
|
|
||||||
let cabinets: Vec<String> = parse_cabinets(
|
|
||||||
worksheet,
|
|
||||||
(lesson_cell_range.start.row, lesson_cell_range.end.row),
|
|
||||||
group_column + 1,
|
|
||||||
);
|
|
||||||
|
|
||||||
let cab_count = cabinets.len();
|
|
||||||
|
|
||||||
if cab_count == 1 {
|
|
||||||
// Назначаем этот кабинет всем подгруппам
|
|
||||||
let cab = Some(cabinets.first().unwrap().clone());
|
|
||||||
|
|
||||||
for subgroup in subgroups.iter_mut().flatten() {
|
|
||||||
subgroup.cabinet = cab.clone()
|
|
||||||
}
|
|
||||||
} else if cab_count == 2 {
|
|
||||||
while subgroups.len() < cab_count {
|
|
||||||
subgroups.push(subgroups.last().unwrap_or(&None).clone());
|
|
||||||
}
|
|
||||||
|
|
||||||
for i in 0..cab_count {
|
|
||||||
let subgroup = subgroups.get_mut(i).unwrap();
|
|
||||||
let cabinet = Some(cabinets.get(i).unwrap().clone());
|
|
||||||
|
|
||||||
match subgroup {
|
|
||||||
None => {
|
|
||||||
let _ = subgroup.insert(LessonSubGroup {
|
|
||||||
teacher: None,
|
|
||||||
cabinet,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
Some(subgroup) => {
|
|
||||||
subgroup.cabinet = cabinet;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
let lesson = Lesson {
|
|
||||||
lesson_type: lesson_type.unwrap_or(lesson_boundaries.lesson_type.clone()),
|
|
||||||
range: default_range,
|
|
||||||
name: Some(name),
|
|
||||||
time: lesson_time,
|
|
||||||
subgroups: if subgroups.len() == 2 && subgroups.iter().all(|x| x.is_none()) {
|
|
||||||
None
|
|
||||||
} else {
|
|
||||||
Some(subgroups)
|
|
||||||
},
|
|
||||||
group: None,
|
|
||||||
};
|
|
||||||
|
|
||||||
let prev_lesson = if day.lessons.is_empty() {
|
|
||||||
return Ok(Lessons(Vec::from([lesson])));
|
|
||||||
} else {
|
|
||||||
&day.lessons[day.lessons.len() - 1]
|
|
||||||
};
|
|
||||||
|
|
||||||
Ok(Lessons(Vec::from([
|
|
||||||
Lesson {
|
|
||||||
lesson_type: Break,
|
|
||||||
range: None,
|
|
||||||
name: None,
|
|
||||||
time: LessonBoundaries {
|
|
||||||
start: prev_lesson.time.end,
|
|
||||||
end: lesson.time.start,
|
|
||||||
},
|
|
||||||
subgroups: Some(Vec::new()),
|
|
||||||
group: None,
|
|
||||||
},
|
|
||||||
lesson,
|
|
||||||
])))
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Obtaining a list of cabinets to the right of the lesson cell.
|
|
||||||
fn parse_cabinets(worksheet: &WorkSheet, row_range: (u32, u32), column: u32) -> Vec<String> {
|
|
||||||
let mut cabinets: Vec<String> = Vec::new();
|
|
||||||
|
|
||||||
for row in row_range.0..row_range.1 {
|
|
||||||
let raw = or_continue!(worksheet.get_string_from_cell(row, column));
|
|
||||||
|
|
||||||
let clean = raw.replace("\n", " ");
|
|
||||||
let parts: Vec<&str> = clean.split(" ").collect();
|
|
||||||
|
|
||||||
parts.iter().take(2).for_each(|part| {
|
|
||||||
let clean_part = part.to_string().trim().to_string();
|
|
||||||
|
|
||||||
cabinets.push(clean_part);
|
|
||||||
});
|
|
||||||
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
cabinets
|
|
||||||
}
|
|
||||||
|
|
||||||
struct ParsedLessonName {
|
|
||||||
name: String,
|
|
||||||
subgroups: Vec<Option<LessonSubGroup>>,
|
|
||||||
r#type: Option<LessonType>,
|
|
||||||
}
|
|
||||||
|
|
||||||
//noinspection GrazieInspection
|
|
||||||
/// Getting the "pure" name of the lesson and list of teachers from the text of the lesson cell.
|
|
||||||
fn parse_name_and_subgroups(text: &str, row: u32, column: u32) -> Result<ParsedLessonName> {
|
|
||||||
// Части названия пары:
|
|
||||||
// 1. Само название.
|
|
||||||
// 2. Список преподавателей и подгрупп.
|
|
||||||
// 3. "Модификатор" (чаще всего).
|
|
||||||
//
|
|
||||||
// Регулярное выражение для получения ФИО преподавателей и номеров подгрупп (aka. второй части).
|
|
||||||
static NAME_RE: LazyLock<fancy_regex::Regex> = LazyLock::new(|| {
|
|
||||||
fancy_regex::Regex::new(
|
|
||||||
r"([А-Я][а-я]+(?:[\s.]*[А-Я]){1,2})(?=[^А-Яа-я])[.\s]*(?:\(?(\d)[\sа-я]*\)?)?",
|
|
||||||
)
|
|
||||||
.unwrap()
|
|
||||||
});
|
|
||||||
|
|
||||||
let text = text
|
|
||||||
.chars()
|
|
||||||
.filter(|c: &char| {
|
|
||||||
c.is_whitespace()
|
|
||||||
|| c.is_ascii_digit()
|
|
||||||
|| (*c >= 'а' && *c <= 'я')
|
|
||||||
|| (*c >= 'А' && *c <= 'Я')
|
|
||||||
|| *c == '.'
|
|
||||||
|| *c == '-'
|
|
||||||
})
|
|
||||||
.collect::<String>()
|
|
||||||
.replace(r"\s+", " ");
|
|
||||||
|
|
||||||
let mut lesson_name: Option<&str> = None;
|
|
||||||
let mut extra: Option<&str> = None;
|
|
||||||
|
|
||||||
let mut shared_subgroup = true;
|
|
||||||
let mut subgroups: [Option<LessonSubGroup>; 2] = [None, None];
|
|
||||||
|
|
||||||
for capture in NAME_RE.captures_iter(&text).take(2) {
|
|
||||||
let capture = capture.unwrap();
|
|
||||||
|
|
||||||
if lesson_name.is_none() {
|
|
||||||
lesson_name = Some(&text[..capture.get(0).unwrap().start()]);
|
|
||||||
}
|
|
||||||
|
|
||||||
extra = Some(&text[capture.get(0).unwrap().end()..]);
|
|
||||||
|
|
||||||
let teacher_name = {
|
|
||||||
let clean = capture
|
|
||||||
.get(1)
|
|
||||||
.unwrap()
|
|
||||||
.as_str()
|
|
||||||
.chars()
|
|
||||||
.filter(|c| c.is_alphabetic())
|
|
||||||
.collect::<Vec<char>>();
|
|
||||||
|
|
||||||
if clean.get(clean.len() - 2).is_some_and(|c| c.is_uppercase()) {
|
|
||||||
let (name, remaining) = clean.split_at(clean.len() - 2);
|
|
||||||
format!(
|
|
||||||
"{} {}.{}.",
|
|
||||||
name.iter().collect::<String>(),
|
|
||||||
remaining[0],
|
|
||||||
remaining[1]
|
|
||||||
)
|
|
||||||
} else {
|
|
||||||
let (remaining, name) = clean.split_last().unwrap();
|
|
||||||
format!("{} {}.", name.iter().collect::<String>(), remaining)
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
let subgroup_index = capture.get(2).map(|m| m.as_str().parse::<u32>().unwrap());
|
|
||||||
|
|
||||||
let subgroup = Some(LessonSubGroup {
|
|
||||||
cabinet: None,
|
|
||||||
teacher: Some(teacher_name),
|
|
||||||
});
|
|
||||||
|
|
||||||
match subgroup_index {
|
|
||||||
None => {
|
|
||||||
// we have only 2 matches max so more than 2 subgroups we cant have 100%
|
|
||||||
*subgroups.iter_mut().find(|x| x.is_none()).unwrap() = subgroup;
|
|
||||||
}
|
|
||||||
Some(num) => {
|
|
||||||
// bc we have indexed subgroup
|
|
||||||
shared_subgroup = false;
|
|
||||||
|
|
||||||
// 1 - 1 = 0 | 2 - 1 = 1 | 3 - 1 = 2 (schedule index to array index)
|
|
||||||
// 0 % 2 = 0 | 1 % 2 = 1 | 2 % 2 = 0 (clamp)
|
|
||||||
let subgroup_index = ((num - 1) % 2) as usize;
|
|
||||||
|
|
||||||
// if we have subgroup in that index (probably non-indexed, we change it index to free)
|
|
||||||
if subgroups[subgroup_index].is_some() {
|
|
||||||
subgroups.swap(0, 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
subgroups[subgroup_index] = subgroup;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
let subgroups = if lesson_name.is_none() {
|
|
||||||
Vec::new()
|
|
||||||
} else if shared_subgroup {
|
|
||||||
Vec::from([subgroups.into_iter().next().unwrap()])
|
|
||||||
} else {
|
|
||||||
Vec::from(subgroups)
|
|
||||||
};
|
|
||||||
|
|
||||||
if extra.is_none() {
|
|
||||||
extra = text
|
|
||||||
.rfind(" ")
|
|
||||||
.and_then(|i| text[..i].rfind(" "))
|
|
||||||
.map(|i| &text[i + 1..]);
|
|
||||||
}
|
|
||||||
|
|
||||||
let lesson_type = if let Some(extra) = extra
|
|
||||||
&& extra.len() > 4
|
|
||||||
{
|
|
||||||
let result = guess_lesson_type(extra);
|
|
||||||
|
|
||||||
if result.is_none() {
|
|
||||||
#[cfg(not(debug_assertions))]
|
|
||||||
sentry::capture_error(&Error::UnknownLessonType {
|
|
||||||
r#type: extra.to_string(),
|
|
||||||
pos: CellPos::new(row, column),
|
|
||||||
});
|
|
||||||
|
|
||||||
#[cfg(debug_assertions)]
|
|
||||||
log::warn!(
|
|
||||||
"{}",
|
|
||||||
Error::UnknownLessonType {
|
|
||||||
r#type: extra.to_string(),
|
|
||||||
pos: CellPos::new(row, column),
|
|
||||||
}
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
result
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
};
|
|
||||||
|
|
||||||
Ok(ParsedLessonName {
|
|
||||||
name: lesson_name.unwrap_or(&text).to_string(),
|
|
||||||
subgroups,
|
|
||||||
r#type: lesson_type,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Getting the start and end of a pair from a cell in the first column of a document.
|
|
||||||
///
|
|
||||||
/// # Arguments
|
|
||||||
///
|
|
||||||
/// * `cell_data`: text in cell.
|
|
||||||
/// * `date`: date of the current day.
|
|
||||||
fn parse_lesson_boundaries_cell(cell_data: &str, date: DateTime<Utc>) -> Option<LessonBoundaries> {
|
|
||||||
static TIME_RE: LazyLock<Regex> =
|
|
||||||
LazyLock::new(|| Regex::new(r"(\d+\.\d+)-(\d+\.\d+)").unwrap());
|
|
||||||
|
|
||||||
let parse_res = TIME_RE.captures(cell_data)?;
|
|
||||||
|
|
||||||
let start_match = parse_res.get(1).unwrap().as_str();
|
|
||||||
let start_parts: Vec<&str> = start_match.split(".").collect();
|
|
||||||
|
|
||||||
let end_match = parse_res.get(2).unwrap().as_str();
|
|
||||||
let end_parts: Vec<&str> = end_match.split(".").collect();
|
|
||||||
|
|
||||||
static GET_TIME: fn(DateTime<Utc>, &Vec<&str>) -> DateTime<Utc> = |date, parts| {
|
|
||||||
date + Duration::hours(parts[0].parse::<i64>().unwrap() - 4)
|
|
||||||
+ Duration::minutes(parts[1].parse::<i64>().unwrap())
|
|
||||||
};
|
|
||||||
|
|
||||||
Some(LessonBoundaries {
|
|
||||||
start: GET_TIME(date, &start_parts),
|
|
||||||
end: GET_TIME(date, &end_parts),
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Parse the column of the document to obtain a list of day's lesson boundaries.
|
|
||||||
///
|
|
||||||
/// # Arguments
|
|
||||||
///
|
|
||||||
/// * `worksheet`: document.
|
|
||||||
/// * `date`: date of the current day.
|
|
||||||
/// * `row_range`: row boundaries of the current day.
|
|
||||||
/// * `column`: column with the required data.
|
|
||||||
fn parse_day_boundaries(
|
|
||||||
worksheet: &WorkSheet,
|
|
||||||
date: DateTime<Utc>,
|
|
||||||
row_range: (u32, u32),
|
|
||||||
column: u32,
|
|
||||||
) -> Result<Vec<BoundariesData>> {
|
|
||||||
let mut day_times: Vec<BoundariesData> = Vec::new();
|
|
||||||
|
|
||||||
for row in row_range.0..row_range.1 {
|
|
||||||
let time_cell = if let Some(str) = worksheet.get_string_from_cell(row, column) {
|
|
||||||
str
|
|
||||||
} else {
|
|
||||||
continue;
|
|
||||||
};
|
|
||||||
|
|
||||||
let lesson_time = parse_lesson_boundaries_cell(&time_cell, date)
|
|
||||||
.ok_or(Error::NoLessonBoundaries(CellPos::new(row, column)))?;
|
|
||||||
|
|
||||||
// type
|
|
||||||
let lesson_type = if time_cell.contains("пара") {
|
|
||||||
LessonType::Default
|
|
||||||
} else {
|
|
||||||
LessonType::Additional
|
|
||||||
};
|
|
||||||
|
|
||||||
// lesson index
|
|
||||||
let default_index = if lesson_type == LessonType::Default {
|
|
||||||
Some(
|
|
||||||
time_cell
|
|
||||||
.chars()
|
|
||||||
.next()
|
|
||||||
.unwrap()
|
|
||||||
.to_string()
|
|
||||||
.parse::<u32>()
|
|
||||||
.unwrap(),
|
|
||||||
)
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
};
|
|
||||||
|
|
||||||
day_times.push(BoundariesData {
|
|
||||||
time_range: lesson_time,
|
|
||||||
lesson_type,
|
|
||||||
default_index,
|
|
||||||
range: worksheet.get_merge_from_start(row, column),
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(day_times)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Parse the column of the document to obtain a list of week's lesson boundaries.
|
|
||||||
///
|
|
||||||
/// # Arguments
|
|
||||||
///
|
|
||||||
/// * `worksheet`: document.
|
|
||||||
/// * `week_markup`: markup of the current week.
|
|
||||||
fn parse_week_boundaries(
|
|
||||||
worksheet: &WorkSheet,
|
|
||||||
week_markup: &[DayMarkup],
|
|
||||||
) -> Result<Vec<Vec<BoundariesData>>> {
|
|
||||||
let mut result: Vec<Vec<BoundariesData>> = Vec::new();
|
|
||||||
|
|
||||||
let worksheet_end_row = worksheet.end().unwrap().0;
|
|
||||||
let lesson_time_column = week_markup[0].column + 1;
|
|
||||||
|
|
||||||
for day_index in 0..week_markup.len() {
|
|
||||||
let day_markup = &week_markup[day_index];
|
|
||||||
|
|
||||||
// Если текущий день не последнему, то индекс строки следующего дня.
|
|
||||||
// Если текущий день - последний, то индекс последней строки документа.
|
|
||||||
let end_row = if day_index != week_markup.len() - 1 {
|
|
||||||
week_markup[day_index + 1].row
|
|
||||||
} else {
|
|
||||||
worksheet_end_row
|
|
||||||
};
|
|
||||||
|
|
||||||
let day_boundaries = parse_day_boundaries(
|
|
||||||
worksheet,
|
|
||||||
day_markup.date,
|
|
||||||
(day_markup.row, end_row),
|
|
||||||
lesson_time_column,
|
|
||||||
)?;
|
|
||||||
|
|
||||||
result.push(day_boundaries);
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(result)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Conversion of the list of couples of groups in the list of lessons of teachers.
|
|
||||||
fn convert_groups_to_teachers(
|
|
||||||
groups: &HashMap<String, ScheduleEntry>,
|
|
||||||
) -> HashMap<String, ScheduleEntry> {
|
|
||||||
let mut teachers: HashMap<String, ScheduleEntry> = HashMap::new();
|
|
||||||
|
|
||||||
let empty_days: Vec<Day> = groups
|
|
||||||
.values()
|
|
||||||
.next()
|
|
||||||
.unwrap()
|
|
||||||
.days
|
|
||||||
.iter()
|
|
||||||
.map(|day| Day {
|
|
||||||
name: day.name.clone(),
|
|
||||||
street: day.street.clone(),
|
|
||||||
date: day.date,
|
|
||||||
lessons: vec![],
|
|
||||||
})
|
|
||||||
.collect();
|
|
||||||
|
|
||||||
for group in groups.values() {
|
|
||||||
for (index, day) in group.days.iter().enumerate() {
|
|
||||||
for group_lesson in &day.lessons {
|
|
||||||
if group_lesson.lesson_type == Break {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
if group_lesson.subgroups.is_none() {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
let subgroups = group_lesson.subgroups.as_ref().unwrap();
|
|
||||||
|
|
||||||
for subgroup in subgroups {
|
|
||||||
let teacher = match subgroup {
|
|
||||||
None => continue,
|
|
||||||
Some(subgroup) => match &subgroup.teacher {
|
|
||||||
None => continue,
|
|
||||||
Some(teacher) => teacher,
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
if teacher == "Ошибка в расписании" {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
if !teachers.contains_key(teacher) {
|
|
||||||
teachers.insert(
|
|
||||||
teacher.clone(),
|
|
||||||
ScheduleEntry {
|
|
||||||
name: teacher.clone(),
|
|
||||||
days: empty_days.to_vec(),
|
|
||||||
},
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
let teacher_day = teachers
|
|
||||||
.get_mut(teacher)
|
|
||||||
.unwrap()
|
|
||||||
.days
|
|
||||||
.get_mut(index)
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
teacher_day.lessons.push({
|
|
||||||
let mut lesson = group_lesson.clone();
|
|
||||||
lesson.group = Some(group.name.clone());
|
|
||||||
|
|
||||||
lesson
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
teachers.iter_mut().for_each(|(_, teacher)| {
|
|
||||||
teacher.days.iter_mut().for_each(|day| {
|
|
||||||
day.lessons
|
|
||||||
.sort_by(|a, b| a.range.as_ref().unwrap()[1].cmp(&b.range.as_ref().unwrap()[1]))
|
|
||||||
})
|
|
||||||
});
|
|
||||||
|
|
||||||
teachers
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Reading XLS Document from the buffer and converting it into the schedule ready to use.
|
|
||||||
///
|
|
||||||
/// # Arguments
|
|
||||||
///
|
|
||||||
/// * `buffer`: XLS data containing schedule.
|
|
||||||
///
|
|
||||||
/// returns: Result<ParseResult, Error>
|
|
||||||
pub fn parse_xls(buffer: &Vec<u8>) -> Result<ParsedSchedule> {
|
|
||||||
let cursor = Cursor::new(&buffer);
|
|
||||||
let mut workbook: Xls<_> = open_workbook_from_rs(cursor)?;
|
|
||||||
|
|
||||||
let worksheet = {
|
|
||||||
let (worksheet_name, worksheet) = workbook
|
|
||||||
.worksheets()
|
|
||||||
.first()
|
|
||||||
.ok_or(Error::NoWorkSheets)?
|
|
||||||
.clone();
|
|
||||||
|
|
||||||
let worksheet_merges = workbook
|
|
||||||
.worksheet_merge_cells(&worksheet_name)
|
|
||||||
.ok_or(Error::NoWorkSheets)?;
|
|
||||||
|
|
||||||
WorkSheet {
|
|
||||||
data: worksheet,
|
|
||||||
merges: worksheet_merges,
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
let WorkSheetMarkup {
|
|
||||||
days: week_markup,
|
|
||||||
groups: groups_markup,
|
|
||||||
} = parse_markup(&worksheet)?;
|
|
||||||
|
|
||||||
let week_boundaries = parse_week_boundaries(&worksheet, &week_markup)?;
|
|
||||||
|
|
||||||
let mut groups: HashMap<String, ScheduleEntry> = HashMap::new();
|
|
||||||
|
|
||||||
for group_markup in groups_markup {
|
|
||||||
let mut group = ScheduleEntry {
|
|
||||||
name: group_markup.name,
|
|
||||||
days: Vec::new(),
|
|
||||||
};
|
|
||||||
|
|
||||||
for day_index in 0..week_markup.len() {
|
|
||||||
let day_markup = &week_markup[day_index];
|
|
||||||
|
|
||||||
let mut day = Day {
|
|
||||||
name: day_markup.name.clone(),
|
|
||||||
street: None,
|
|
||||||
date: day_markup.date,
|
|
||||||
lessons: Vec::new(),
|
|
||||||
};
|
|
||||||
|
|
||||||
let day_boundaries = &week_boundaries[day_index];
|
|
||||||
|
|
||||||
for lesson_boundaries in day_boundaries {
|
|
||||||
match &mut parse_lesson(
|
|
||||||
&worksheet,
|
|
||||||
&day,
|
|
||||||
day_boundaries,
|
|
||||||
lesson_boundaries,
|
|
||||||
group_markup.column,
|
|
||||||
)? {
|
|
||||||
Lessons(lesson) => day.lessons.append(lesson),
|
|
||||||
Street(street) => day.street = Some(street.to_owned()),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
group.days.push(day);
|
|
||||||
}
|
|
||||||
|
|
||||||
groups.insert(group.name.clone(), group);
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(ParsedSchedule {
|
|
||||||
teachers: convert_groups_to_teachers(&groups),
|
|
||||||
groups,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(any(test, feature = "test"))]
|
|
||||||
pub mod test_utils {
|
|
||||||
use super::*;
|
|
||||||
use base::ParsedSchedule;
|
|
||||||
|
|
||||||
pub fn test_result() -> Result<ParsedSchedule> {
|
|
||||||
parse_xls(&include_bytes!("../../../../test-data/engels-polytechnic.xls").to_vec())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(test)]
|
|
||||||
pub mod tests {
|
|
||||||
#[test]
|
|
||||||
fn read() {
|
|
||||||
let result = super::test_utils::test_result();
|
|
||||||
|
|
||||||
assert!(result.is_ok(), "{}", result.err().unwrap());
|
|
||||||
|
|
||||||
assert_ne!(result.as_ref().unwrap().groups.len(), 0);
|
|
||||||
assert_ne!(result.as_ref().unwrap().teachers.len(), 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_split_lesson() {
|
|
||||||
let result = super::test_utils::test_result();
|
|
||||||
assert!(result.is_ok(), "{}", result.err().unwrap());
|
|
||||||
|
|
||||||
let result = result.unwrap();
|
|
||||||
assert!(result.groups.contains_key("ИС-214/23"));
|
|
||||||
|
|
||||||
let group = result.groups.get("ИС-214/23").unwrap();
|
|
||||||
|
|
||||||
let thursday = group.days.get(3).unwrap();
|
|
||||||
assert_eq!(thursday.lessons.len(), 1);
|
|
||||||
|
|
||||||
let lesson = &thursday.lessons[0];
|
|
||||||
assert_eq!(lesson.range.unwrap()[1], 3);
|
|
||||||
assert!(lesson.subgroups.is_some());
|
|
||||||
|
|
||||||
let subgroups = lesson.subgroups.as_ref().unwrap();
|
|
||||||
assert_eq!(subgroups.len(), 2);
|
|
||||||
|
|
||||||
assert_eq!(
|
|
||||||
subgroups[0].as_ref().unwrap().cabinet,
|
|
||||||
Some("44".to_string())
|
|
||||||
);
|
|
||||||
|
|
||||||
assert_eq!(
|
|
||||||
subgroups[1].as_ref().unwrap().cabinet,
|
|
||||||
Some("43".to_string())
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,105 +0,0 @@
|
|||||||
use regex::Regex;
|
|
||||||
use std::fmt::{Display, Formatter};
|
|
||||||
use std::ops::Deref;
|
|
||||||
use std::sync::LazyLock;
|
|
||||||
|
|
||||||
/// XLS WorkSheet data.
|
|
||||||
pub struct WorkSheet {
|
|
||||||
pub data: calamine::Range<calamine::Data>,
|
|
||||||
pub merges: Vec<calamine::Dimensions>,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Clone, Debug, derive_more::Error)]
|
|
||||||
pub struct CellPos {
|
|
||||||
pub row: u32,
|
|
||||||
pub column: u32,
|
|
||||||
}
|
|
||||||
|
|
||||||
fn format_column_index(index: u32) -> String {
|
|
||||||
// https://stackoverflow.com/a/297214
|
|
||||||
let quotient = index / 26;
|
|
||||||
|
|
||||||
let char = char::from((65 + (index % 26)) as u8);
|
|
||||||
|
|
||||||
if quotient > 0 {
|
|
||||||
return format!("{}{}", format_column_index(quotient - 1), char);
|
|
||||||
}
|
|
||||||
|
|
||||||
char.to_string()
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Display for CellPos {
|
|
||||||
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
|
||||||
f.write_fmt(format_args!(
|
|
||||||
"column {}, row {}",
|
|
||||||
format_column_index(self.column),
|
|
||||||
self.row + 1,
|
|
||||||
))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct CellRange {
|
|
||||||
pub start: CellPos,
|
|
||||||
pub end: CellPos,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Deref for WorkSheet {
|
|
||||||
type Target = calamine::Range<calamine::Data>;
|
|
||||||
|
|
||||||
fn deref(&self) -> &Self::Target {
|
|
||||||
&self.data
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl WorkSheet {
|
|
||||||
/// Getting a line from the required cell.
|
|
||||||
pub fn get_string_from_cell(&self, row: u32, col: u32) -> Option<String> {
|
|
||||||
let cell_data = if let Some(data) = self.get((row as usize, col as usize)) {
|
|
||||||
data.to_string()
|
|
||||||
} else {
|
|
||||||
return None;
|
|
||||||
};
|
|
||||||
|
|
||||||
if cell_data.trim().is_empty() {
|
|
||||||
return None;
|
|
||||||
}
|
|
||||||
|
|
||||||
static NL_RE: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"[\n\r]+").unwrap());
|
|
||||||
static SP_RE: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"\s+").unwrap());
|
|
||||||
|
|
||||||
let trimmed_data = SP_RE
|
|
||||||
.replace_all(&NL_RE.replace_all(&cell_data, " "), " ")
|
|
||||||
.trim()
|
|
||||||
.to_string();
|
|
||||||
|
|
||||||
if trimmed_data.is_empty() {
|
|
||||||
None
|
|
||||||
} else {
|
|
||||||
Some(trimmed_data)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Obtaining the boundaries of the cell along its upper left coordinate.
|
|
||||||
pub fn get_merge_from_start(&self, row: u32, column: u32) -> CellRange {
|
|
||||||
match self
|
|
||||||
.merges
|
|
||||||
.iter()
|
|
||||||
.find(|merge| merge.start.0 == row && merge.start.1 == column)
|
|
||||||
{
|
|
||||||
Some(merge) => CellRange {
|
|
||||||
start: CellPos::new(merge.start.0, merge.start.1),
|
|
||||||
end: CellPos::new(merge.end.0 + 1, merge.end.1 + 1),
|
|
||||||
},
|
|
||||||
None => CellRange {
|
|
||||||
start: CellPos::new(row, column),
|
|
||||||
end: CellPos::new(row + 1, column + 1),
|
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl CellPos {
|
|
||||||
pub fn new(row: u32, column: u32) -> Self {
|
|
||||||
Self { row, column }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,33 +0,0 @@
|
|||||||
use crate::xls_downloader::FetchError;
|
|
||||||
use derive_more::{Display, Error, From};
|
|
||||||
|
|
||||||
#[derive(Debug, Display, Error, From)]
|
|
||||||
pub enum Error {
|
|
||||||
/// Occurs when the request to the Yandex Cloud API fails.
|
|
||||||
///
|
|
||||||
/// This may be due to network issues, invalid API key, incorrect function ID, or other
|
|
||||||
/// problems with the Yandex Cloud Function invocation.
|
|
||||||
#[display("An error occurred during the request to the Yandex Cloud API: {_0}")]
|
|
||||||
Reqwest(reqwest::Error),
|
|
||||||
|
|
||||||
#[display("Unable to get URI in 3 retries")]
|
|
||||||
EmptyUri,
|
|
||||||
|
|
||||||
/// The ETag is the same (no update needed).
|
|
||||||
#[display("The ETag is the same.")]
|
|
||||||
SameETag,
|
|
||||||
|
|
||||||
/// The URL query for the XLS file failed to execute, either due to network issues or invalid API parameters.
|
|
||||||
#[display("Failed to fetch URL: {_0}")]
|
|
||||||
ScheduleFetchFailed(FetchError),
|
|
||||||
|
|
||||||
/// Downloading the XLS file content failed after successfully obtaining the URL.
|
|
||||||
#[display("Download failed: {_0}")]
|
|
||||||
ScheduleDownloadFailed(FetchError),
|
|
||||||
|
|
||||||
/// The XLS file could not be parsed into a valid schedule format.
|
|
||||||
#[from]
|
|
||||||
InvalidSchedule(crate::parser::Error),
|
|
||||||
}
|
|
||||||
|
|
||||||
pub type Result<T> = core::result::Result<T, Error>;
|
|
||||||
@@ -1,225 +0,0 @@
|
|||||||
pub use self::error::{Error, Result};
|
|
||||||
use crate::parser::parse_xls;
|
|
||||||
use crate::xls_downloader::{FetchError, XlsDownloader};
|
|
||||||
use base::ScheduleSnapshot;
|
|
||||||
mod error;
|
|
||||||
|
|
||||||
pub enum UpdateSource {
|
|
||||||
Prepared(ScheduleSnapshot),
|
|
||||||
|
|
||||||
Url(String),
|
|
||||||
|
|
||||||
GrabFromSite {
|
|
||||||
yandex_api_key: String,
|
|
||||||
yandex_func_id: String,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct Updater {
|
|
||||||
downloader: XlsDownloader,
|
|
||||||
update_source: UpdateSource,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Updater {
|
|
||||||
/// Constructs a new `ScheduleSnapshot` by downloading and parsing schedule data from the specified URL.
|
|
||||||
///
|
|
||||||
/// This method first checks if the provided URL is the same as the one already configured in the downloader.
|
|
||||||
/// If different, it updates the downloader's URL, fetches the XLS content, parses it, and creates a snapshot.
|
|
||||||
/// Errors are returned for URL conflicts, network issues, download failures, or invalid data.
|
|
||||||
///
|
|
||||||
/// # Arguments
|
|
||||||
///
|
|
||||||
/// * `downloader`: A mutable reference to an `XLSDownloader` implementation used to fetch and parse the schedule data.
|
|
||||||
/// * `url`: The source URL pointing to the XLS file containing schedule data.
|
|
||||||
///
|
|
||||||
/// returns: Result<ScheduleSnapshot, SnapshotCreationError>
|
|
||||||
async fn new_snapshot(downloader: &mut XlsDownloader, url: String) -> Result<ScheduleSnapshot> {
|
|
||||||
let head_result = downloader.set_url(&url).await.map_err(|error| {
|
|
||||||
if let FetchError::Reqwest(error) = &error {
|
|
||||||
sentry::capture_error(&error);
|
|
||||||
}
|
|
||||||
|
|
||||||
Error::ScheduleFetchFailed(error)
|
|
||||||
})?;
|
|
||||||
|
|
||||||
if downloader.etag == Some(head_result.etag) {
|
|
||||||
return Err(Error::SameETag);
|
|
||||||
}
|
|
||||||
|
|
||||||
let xls_data = downloader
|
|
||||||
.fetch(false)
|
|
||||||
.await
|
|
||||||
.map_err(|error| {
|
|
||||||
if let FetchError::Reqwest(error) = &error {
|
|
||||||
sentry::capture_error(&error);
|
|
||||||
}
|
|
||||||
|
|
||||||
Error::ScheduleDownloadFailed(error)
|
|
||||||
})?
|
|
||||||
.data
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
let parse_result = parse_xls(&xls_data)?;
|
|
||||||
|
|
||||||
Ok(ScheduleSnapshot {
|
|
||||||
fetched_at: head_result.requested_at,
|
|
||||||
updated_at: head_result.uploaded_at,
|
|
||||||
url,
|
|
||||||
data: parse_result,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Queries the Yandex Cloud Function (FaaS) to obtain a URL for the schedule file.
|
|
||||||
///
|
|
||||||
/// This sends a POST request to the specified Yandex Cloud Function endpoint,
|
|
||||||
/// using the provided API key for authentication. The returned URI is combined
|
|
||||||
/// with the "https://politehnikum-eng.ru" base domain to form the complete URL.
|
|
||||||
///
|
|
||||||
/// # Arguments
|
|
||||||
///
|
|
||||||
/// * `api_key` - Authentication token for Yandex Cloud API
|
|
||||||
/// * `func_id` - ID of the target Yandex Cloud Function to invoke
|
|
||||||
///
|
|
||||||
/// # Returns
|
|
||||||
///
|
|
||||||
/// Result containing:
|
|
||||||
/// - `Ok(String)` - Complete URL constructed from the Function's response
|
|
||||||
/// - `Err(QueryUrlError)` - If the request or response processing fails
|
|
||||||
async fn query_url(api_key: &str, func_id: &str) -> Result<String> {
|
|
||||||
let client = reqwest::Client::new();
|
|
||||||
|
|
||||||
let uri = {
|
|
||||||
// вот бы добавили named-scopes как в котлине,
|
|
||||||
// чтоб мне не пришлось такой хуйнёй страдать.
|
|
||||||
#[allow(unused_assignments)]
|
|
||||||
let mut uri = String::new();
|
|
||||||
let mut counter = 0;
|
|
||||||
|
|
||||||
loop {
|
|
||||||
if counter == 3 {
|
|
||||||
return Err(Error::EmptyUri);
|
|
||||||
}
|
|
||||||
|
|
||||||
counter += 1;
|
|
||||||
|
|
||||||
uri = client
|
|
||||||
.post(format!(
|
|
||||||
"https://functions.yandexcloud.net/{}?integration=raw",
|
|
||||||
func_id
|
|
||||||
))
|
|
||||||
.header("Authorization", format!("Api-Key {}", api_key))
|
|
||||||
.send()
|
|
||||||
.await
|
|
||||||
.map_err(Error::Reqwest)?
|
|
||||||
.text()
|
|
||||||
.await
|
|
||||||
.map_err(Error::Reqwest)?;
|
|
||||||
|
|
||||||
if uri.is_empty() {
|
|
||||||
log::warn!("[{}] Unable to get uri! Retrying in 5 seconds...", counter);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
uri
|
|
||||||
};
|
|
||||||
|
|
||||||
Ok(format!("https://politehnikum-eng.ru{}", uri.trim()))
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Initializes the schedule by fetching the URL from the environment or Yandex Cloud Function (FaaS)
|
|
||||||
/// and creating a [`ScheduleSnapshot`] with the downloaded data.
|
|
||||||
///
|
|
||||||
/// # Arguments
|
|
||||||
///
|
|
||||||
/// * `downloader`: Mutable reference to an `XLSDownloader` implementation used to fetch and parse the schedule
|
|
||||||
/// * `app_env`: Reference to the application environment containing either a predefined URL or Yandex Cloud credentials
|
|
||||||
///
|
|
||||||
/// # Returns
|
|
||||||
///
|
|
||||||
/// Returns `Ok(())` if the snapshot was successfully initialized, or an `Error` if:
|
|
||||||
/// - URL query to Yandex Cloud failed ([`QueryUrlError`])
|
|
||||||
/// - Schedule snapshot creation failed ([`SnapshotCreationError`])
|
|
||||||
pub async fn new(update_source: UpdateSource) -> Result<(Self, ScheduleSnapshot)> {
|
|
||||||
let mut this = Updater {
|
|
||||||
downloader: XlsDownloader::new(),
|
|
||||||
update_source,
|
|
||||||
};
|
|
||||||
|
|
||||||
if let UpdateSource::Prepared(snapshot) = &this.update_source {
|
|
||||||
let snapshot = snapshot.clone();
|
|
||||||
return Ok((this, snapshot));
|
|
||||||
}
|
|
||||||
|
|
||||||
let url = match &this.update_source {
|
|
||||||
UpdateSource::Url(url) => {
|
|
||||||
log::info!("The default link {} will be used", url);
|
|
||||||
url.clone()
|
|
||||||
}
|
|
||||||
UpdateSource::GrabFromSite {
|
|
||||||
yandex_api_key,
|
|
||||||
yandex_func_id,
|
|
||||||
} => {
|
|
||||||
log::info!("Obtaining a link using FaaS...");
|
|
||||||
Self::query_url(yandex_api_key, yandex_func_id).await?
|
|
||||||
}
|
|
||||||
_ => unreachable!(),
|
|
||||||
};
|
|
||||||
|
|
||||||
log::info!("For the initial setup, a link {} will be used", url);
|
|
||||||
|
|
||||||
let snapshot = Self::new_snapshot(&mut this.downloader, url).await?;
|
|
||||||
log::info!("Schedule snapshot successfully created!");
|
|
||||||
|
|
||||||
Ok((this, snapshot))
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Updates the schedule snapshot by querying the latest URL from FaaS and checking for changes.
|
|
||||||
/// If the URL hasn't changed, only updates the [`fetched_at`] timestamp. If changed, downloads
|
|
||||||
/// and parses the new schedule data.
|
|
||||||
///
|
|
||||||
/// # Arguments
|
|
||||||
///
|
|
||||||
/// * `downloader`: XLS file downloader used to fetch and parse the schedule data
|
|
||||||
/// * `app_env`: Application environment containing Yandex Cloud configuration and auto-update settings
|
|
||||||
///
|
|
||||||
/// returns: `Result<(), Error>` - Returns error if URL query fails or schedule parsing encounters issues
|
|
||||||
///
|
|
||||||
/// # Safety
|
|
||||||
///
|
|
||||||
/// Use `unsafe` to access the initialized snapshot, guaranteed valid by prior `init()` call
|
|
||||||
pub async fn update(
|
|
||||||
&mut self,
|
|
||||||
current_snapshot: &ScheduleSnapshot,
|
|
||||||
) -> Result<ScheduleSnapshot> {
|
|
||||||
if let UpdateSource::Prepared(snapshot) = &self.update_source {
|
|
||||||
let mut snapshot = snapshot.clone();
|
|
||||||
snapshot.update();
|
|
||||||
return Ok(snapshot);
|
|
||||||
}
|
|
||||||
|
|
||||||
let url = match &self.update_source {
|
|
||||||
UpdateSource::Url(url) => url.clone(),
|
|
||||||
UpdateSource::GrabFromSite {
|
|
||||||
yandex_api_key,
|
|
||||||
yandex_func_id,
|
|
||||||
} => Self::query_url(yandex_api_key.as_str(), yandex_func_id.as_str()).await?,
|
|
||||||
_ => unreachable!(),
|
|
||||||
};
|
|
||||||
|
|
||||||
let snapshot = match Self::new_snapshot(&mut self.downloader, url).await {
|
|
||||||
Ok(snapshot) => snapshot,
|
|
||||||
Err(Error::SameETag) => {
|
|
||||||
let mut clone = current_snapshot.clone();
|
|
||||||
clone.update();
|
|
||||||
|
|
||||||
clone
|
|
||||||
}
|
|
||||||
Err(error) => return Err(error),
|
|
||||||
};
|
|
||||||
|
|
||||||
Ok(snapshot)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,253 +0,0 @@
|
|||||||
use chrono::{DateTime, Utc};
|
|
||||||
use derive_more::{Display, Error};
|
|
||||||
use std::mem::discriminant;
|
|
||||||
use std::sync::Arc;
|
|
||||||
use utoipa::ToSchema;
|
|
||||||
|
|
||||||
/// XLS data retrieval errors.
|
|
||||||
#[derive(Clone, Debug, ToSchema, Display, Error)]
|
|
||||||
pub enum FetchError {
|
|
||||||
/// File url is not set.
|
|
||||||
#[display("The link to the timetable was not provided earlier.")]
|
|
||||||
NoUrlProvided,
|
|
||||||
|
|
||||||
/// Unknown error.
|
|
||||||
#[display("An unknown error occurred while downloading the file.")]
|
|
||||||
#[schema(value_type = String)]
|
|
||||||
Reqwest(Arc<reqwest::Error>),
|
|
||||||
|
|
||||||
/// Server returned a status code different from 200.
|
|
||||||
#[display("Server returned a status code {status_code}.")]
|
|
||||||
BadStatusCode { status_code: u16 },
|
|
||||||
|
|
||||||
/// The url leads to a file of a different type.
|
|
||||||
#[display("The link leads to a file of type '{content_type}'.")]
|
|
||||||
BadContentType { content_type: String },
|
|
||||||
|
|
||||||
/// Server doesn't return expected headers.
|
|
||||||
#[display("Server doesn't return expected header(s) '{expected_header}'.")]
|
|
||||||
BadHeaders { expected_header: String },
|
|
||||||
}
|
|
||||||
|
|
||||||
impl FetchError {
|
|
||||||
pub fn unknown(error: Arc<reqwest::Error>) -> Self {
|
|
||||||
Self::Reqwest(error)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn bad_status_code(status_code: u16) -> Self {
|
|
||||||
Self::BadStatusCode { status_code }
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn bad_content_type(content_type: &str) -> Self {
|
|
||||||
Self::BadContentType {
|
|
||||||
content_type: content_type.to_string(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn bad_headers(expected_header: &str) -> Self {
|
|
||||||
Self::BadHeaders {
|
|
||||||
expected_header: expected_header.to_string(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl PartialEq for FetchError {
|
|
||||||
fn eq(&self, other: &Self) -> bool {
|
|
||||||
discriminant(self) == discriminant(other)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Result of XLS data retrieval.
|
|
||||||
#[derive(Debug, PartialEq)]
|
|
||||||
pub struct FetchOk {
|
|
||||||
/// File upload date.
|
|
||||||
pub uploaded_at: DateTime<Utc>,
|
|
||||||
|
|
||||||
/// Date data received.
|
|
||||||
pub requested_at: DateTime<Utc>,
|
|
||||||
|
|
||||||
/// Etag.
|
|
||||||
pub etag: String,
|
|
||||||
|
|
||||||
/// File data.
|
|
||||||
pub data: Option<Vec<u8>>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl FetchOk {
|
|
||||||
/// Result without file content.
|
|
||||||
pub fn head(uploaded_at: DateTime<Utc>, etag: String) -> Self {
|
|
||||||
FetchOk {
|
|
||||||
uploaded_at,
|
|
||||||
requested_at: Utc::now(),
|
|
||||||
etag,
|
|
||||||
data: None,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Full result.
|
|
||||||
pub fn get(uploaded_at: DateTime<Utc>, etag: String, data: Vec<u8>) -> Self {
|
|
||||||
FetchOk {
|
|
||||||
uploaded_at,
|
|
||||||
requested_at: Utc::now(),
|
|
||||||
etag,
|
|
||||||
data: Some(data),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub type FetchResult = Result<FetchOk, FetchError>;
|
|
||||||
|
|
||||||
pub struct XlsDownloader {
|
|
||||||
pub url: Option<String>,
|
|
||||||
pub etag: Option<String>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl XlsDownloader {
|
|
||||||
pub fn new() -> Self {
|
|
||||||
XlsDownloader {
|
|
||||||
url: None,
|
|
||||||
etag: None,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn fetch_specified(url: &str, head: bool) -> FetchResult {
|
|
||||||
let client = reqwest::Client::new();
|
|
||||||
|
|
||||||
let response = if head {
|
|
||||||
client.head(url)
|
|
||||||
} else {
|
|
||||||
client.get(url)
|
|
||||||
}
|
|
||||||
.header("User-Agent", ua_generator::ua::spoof_chrome_ua())
|
|
||||||
.send()
|
|
||||||
.await
|
|
||||||
.map_err(|e| FetchError::unknown(Arc::new(e)))?;
|
|
||||||
|
|
||||||
if response.status().as_u16() != 200 {
|
|
||||||
return Err(FetchError::bad_status_code(response.status().as_u16()));
|
|
||||||
}
|
|
||||||
|
|
||||||
let headers = response.headers();
|
|
||||||
|
|
||||||
let content_type = headers
|
|
||||||
.get("Content-Type")
|
|
||||||
.ok_or(FetchError::bad_headers("Content-Type"))?;
|
|
||||||
|
|
||||||
let etag = headers
|
|
||||||
.get("etag")
|
|
||||||
.ok_or(FetchError::bad_headers("etag"))?
|
|
||||||
.to_str()
|
|
||||||
.or(Err(FetchError::bad_headers("etag")))?
|
|
||||||
.to_string();
|
|
||||||
|
|
||||||
let last_modified = headers
|
|
||||||
.get("last-modified")
|
|
||||||
.ok_or(FetchError::bad_headers("last-modified"))?;
|
|
||||||
|
|
||||||
if content_type != "application/vnd.ms-excel" {
|
|
||||||
return Err(FetchError::bad_content_type(content_type.to_str().unwrap()));
|
|
||||||
}
|
|
||||||
|
|
||||||
let last_modified = DateTime::parse_from_rfc2822(last_modified.to_str().unwrap())
|
|
||||||
.unwrap()
|
|
||||||
.with_timezone(&Utc);
|
|
||||||
|
|
||||||
Ok(if head {
|
|
||||||
FetchOk::head(last_modified, etag)
|
|
||||||
} else {
|
|
||||||
FetchOk::get(
|
|
||||||
last_modified,
|
|
||||||
etag,
|
|
||||||
response.bytes().await.unwrap().to_vec(),
|
|
||||||
)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
pub async fn fetch(&self, head: bool) -> FetchResult {
|
|
||||||
if self.url.is_none() {
|
|
||||||
Err(FetchError::NoUrlProvided)
|
|
||||||
} else {
|
|
||||||
Self::fetch_specified(self.url.as_ref().unwrap(), head).await
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub async fn set_url(&mut self, url: &str) -> FetchResult {
|
|
||||||
let result = Self::fetch_specified(url, true).await;
|
|
||||||
|
|
||||||
if result.is_ok() {
|
|
||||||
self.url = Some(url.to_string());
|
|
||||||
}
|
|
||||||
|
|
||||||
result
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(test)]
|
|
||||||
mod tests {
|
|
||||||
use crate::xls_downloader::{FetchError, XlsDownloader};
|
|
||||||
|
|
||||||
#[tokio::test]
|
|
||||||
async fn bad_url() {
|
|
||||||
let url = "bad_url";
|
|
||||||
|
|
||||||
let mut downloader = XlsDownloader::new();
|
|
||||||
assert!(downloader.set_url(url).await.is_err());
|
|
||||||
}
|
|
||||||
|
|
||||||
#[tokio::test]
|
|
||||||
async fn bad_status_code() {
|
|
||||||
let url = "https://www.google.com/not-found";
|
|
||||||
|
|
||||||
let mut downloader = XlsDownloader::new();
|
|
||||||
assert_eq!(
|
|
||||||
downloader.set_url(url).await,
|
|
||||||
Err(FetchError::bad_status_code(404))
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[tokio::test]
|
|
||||||
async fn bad_headers() {
|
|
||||||
let url = "https://www.google.com/favicon.ico";
|
|
||||||
|
|
||||||
let mut downloader = XlsDownloader::new();
|
|
||||||
assert_eq!(
|
|
||||||
downloader.set_url(url).await,
|
|
||||||
Err(FetchError::BadHeaders {
|
|
||||||
expected_header: "ETag".to_string(),
|
|
||||||
})
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[tokio::test]
|
|
||||||
async fn bad_content_type() {
|
|
||||||
let url = "https://s3.aero-storage.ldragol.ru/679e5d1145a6ad00843ad3f1/67ddb59fd46303008396ac96%2Fexample.txt";
|
|
||||||
|
|
||||||
let mut downloader = XlsDownloader::new();
|
|
||||||
assert!(downloader.set_url(url).await.is_err());
|
|
||||||
}
|
|
||||||
|
|
||||||
#[tokio::test]
|
|
||||||
async fn ok() {
|
|
||||||
let url = "https://s3.aero-storage.ldragol.ru/679e5d1145a6ad00843ad3f1/67ddb5fad46303008396ac97%2Fschedule.xls";
|
|
||||||
|
|
||||||
let mut downloader = XlsDownloader::new();
|
|
||||||
assert!(downloader.set_url(url).await.is_ok());
|
|
||||||
}
|
|
||||||
|
|
||||||
#[tokio::test]
|
|
||||||
async fn downloader_ok() {
|
|
||||||
let url = "https://s3.aero-storage.ldragol.ru/679e5d1145a6ad00843ad3f1/67ddb5fad46303008396ac97%2Fschedule.xls";
|
|
||||||
|
|
||||||
let mut downloader = XlsDownloader::new();
|
|
||||||
assert!(downloader.set_url(url).await.is_ok());
|
|
||||||
assert!(downloader.fetch(false).await.is_ok());
|
|
||||||
}
|
|
||||||
|
|
||||||
#[tokio::test]
|
|
||||||
async fn downloader_no_url_provided() {
|
|
||||||
let downloader = XlsDownloader::new();
|
|
||||||
|
|
||||||
let result = downloader.fetch(false).await;
|
|
||||||
assert_eq!(result, Err(FetchError::NoUrlProvided));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,9 +0,0 @@
|
|||||||
pub use base;
|
|
||||||
|
|
||||||
pub use provider_engels_polytechnic::EngelsPolytechnicProvider;
|
|
||||||
pub use provider_engels_polytechnic::UpdateSource as EngelsPolytechnicUpdateSource;
|
|
||||||
|
|
||||||
#[cfg(feature = "test")]
|
|
||||||
pub mod test_utils {
|
|
||||||
pub use provider_engels_polytechnic::test_utils as engels_polytechnic;
|
|
||||||
}
|
|
||||||
BIN
schedule.xls
Normal file
BIN
schedule.xls
Normal file
Binary file not shown.
60
src/app_state.rs
Normal file
60
src/app_state.rs
Normal file
@@ -0,0 +1,60 @@
|
|||||||
|
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 sha1::{Digest, Sha1};
|
||||||
|
use std::env;
|
||||||
|
use std::hash::Hash;
|
||||||
|
use std::sync::{Mutex, MutexGuard};
|
||||||
|
|
||||||
|
#[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,
|
||||||
|
}
|
||||||
|
|
||||||
|
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()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Общие данные передаваемые в эндпоинты
|
||||||
|
pub struct AppState {
|
||||||
|
pub downloader: Mutex<BasicXlsDownloader>,
|
||||||
|
pub schedule: Mutex<Option<Schedule>>,
|
||||||
|
pub database: Mutex<PgConnection>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl AppState {
|
||||||
|
/// Получение объекта соединения с базой данных PostgreSQL
|
||||||
|
pub fn connection(&self) -> MutexGuard<PgConnection> {
|
||||||
|
self.database.lock().unwrap()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Создание нового объекта web::Data<AppState>
|
||||||
|
pub fn app_state() -> web::Data<AppState> {
|
||||||
|
let database_url = env::var("DATABASE_URL").expect("DATABASE_URL must be set");
|
||||||
|
|
||||||
|
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)),
|
||||||
|
),
|
||||||
|
})
|
||||||
|
}
|
||||||
103
src/database/driver.rs
Normal file
103
src/database/driver.rs
Normal file
@@ -0,0 +1,103 @@
|
|||||||
|
pub mod users {
|
||||||
|
use crate::database::models::User;
|
||||||
|
use crate::database::schema::users::dsl::users;
|
||||||
|
use crate::database::schema::users::dsl::*;
|
||||||
|
use diesel::{ExpressionMethods, QueryResult, insert_into};
|
||||||
|
use diesel::{PgConnection, SelectableHelper};
|
||||||
|
use diesel::{QueryDsl, RunQueryDsl};
|
||||||
|
use std::ops::DerefMut;
|
||||||
|
use std::sync::Mutex;
|
||||||
|
|
||||||
|
pub fn get(connection: &Mutex<PgConnection>, _id: &String) -> QueryResult<User> {
|
||||||
|
let mut lock = connection.lock().unwrap();
|
||||||
|
let con = lock.deref_mut();
|
||||||
|
|
||||||
|
users
|
||||||
|
.filter(id.eq(_id))
|
||||||
|
.select(User::as_select())
|
||||||
|
.first(con)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_by_username(
|
||||||
|
connection: &Mutex<PgConnection>,
|
||||||
|
_username: &String,
|
||||||
|
) -> QueryResult<User> {
|
||||||
|
let mut lock = connection.lock().unwrap();
|
||||||
|
let con = lock.deref_mut();
|
||||||
|
|
||||||
|
users
|
||||||
|
.filter(username.eq(_username))
|
||||||
|
.select(User::as_select())
|
||||||
|
.first(con)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_by_vk_id(
|
||||||
|
connection: &Mutex<PgConnection>,
|
||||||
|
_vk_id: i32,
|
||||||
|
) -> QueryResult<User> {
|
||||||
|
let mut lock = connection.lock().unwrap();
|
||||||
|
let con = lock.deref_mut();
|
||||||
|
|
||||||
|
users
|
||||||
|
.filter(vk_id.eq(_vk_id))
|
||||||
|
.select(User::as_select())
|
||||||
|
.first(con)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn contains_by_username(connection: &Mutex<PgConnection>, _username: &String) -> bool {
|
||||||
|
let mut lock = connection.lock().unwrap();
|
||||||
|
let con = lock.deref_mut();
|
||||||
|
|
||||||
|
match users
|
||||||
|
.filter(username.eq(_username))
|
||||||
|
.count()
|
||||||
|
.get_result::<i64>(con)
|
||||||
|
{
|
||||||
|
Ok(count) => count > 0,
|
||||||
|
Err(_) => false,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn contains_by_vk_id(connection: &Mutex<PgConnection>, _vk_id: i32) -> bool {
|
||||||
|
let mut lock = connection.lock().unwrap();
|
||||||
|
let con = lock.deref_mut();
|
||||||
|
|
||||||
|
match users
|
||||||
|
.filter(vk_id.eq(_vk_id))
|
||||||
|
.count()
|
||||||
|
.get_result::<i64>(con)
|
||||||
|
{
|
||||||
|
Ok(count) => count > 0,
|
||||||
|
Err(_) => false,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn insert(connection: &Mutex<PgConnection>, user: &User) -> QueryResult<usize> {
|
||||||
|
let mut lock = connection.lock().unwrap();
|
||||||
|
let con = lock.deref_mut();
|
||||||
|
|
||||||
|
insert_into(users).values(user).execute(con)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
pub fn delete_by_username(connection: &Mutex<PgConnection>, _username: &String) -> bool {
|
||||||
|
let mut lock = connection.lock().unwrap();
|
||||||
|
let con = lock.deref_mut();
|
||||||
|
|
||||||
|
match diesel::delete(users.filter(username.eq(_username))).execute(con) {
|
||||||
|
Ok(count) => count > 0,
|
||||||
|
Err(_) => false,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
pub fn insert_or_ignore(connection: &Mutex<PgConnection>, user: &User) -> QueryResult<usize> {
|
||||||
|
let mut lock = connection.lock().unwrap();
|
||||||
|
let con = lock.deref_mut();
|
||||||
|
|
||||||
|
insert_into(users)
|
||||||
|
.values(user)
|
||||||
|
.on_conflict_do_nothing()
|
||||||
|
.execute(con)
|
||||||
|
}
|
||||||
|
}
|
||||||
3
src/database/mod.rs
Normal file
3
src/database/mod.rs
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
pub mod schema;
|
||||||
|
pub mod models;
|
||||||
|
pub mod driver;
|
||||||
61
src/database/models.rs
Normal file
61
src/database/models.rs
Normal file
@@ -0,0 +1,61 @@
|
|||||||
|
use actix_macros::ResponderJson;
|
||||||
|
use diesel::prelude::*;
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
|
#[derive(
|
||||||
|
diesel_derive_enum::DbEnum,
|
||||||
|
Serialize,
|
||||||
|
Deserialize,
|
||||||
|
Debug,
|
||||||
|
Clone,
|
||||||
|
Copy,
|
||||||
|
PartialEq,
|
||||||
|
utoipa::ToSchema,
|
||||||
|
)]
|
||||||
|
#[ExistingTypePath = "crate::database::schema::sql_types::UserRole"]
|
||||||
|
#[DbValueStyle = "UPPERCASE"]
|
||||||
|
#[serde(rename_all = "UPPERCASE")]
|
||||||
|
pub enum UserRole {
|
||||||
|
Student,
|
||||||
|
Teacher,
|
||||||
|
Admin,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(
|
||||||
|
Identifiable,
|
||||||
|
AsChangeset,
|
||||||
|
Queryable,
|
||||||
|
Selectable,
|
||||||
|
Serialize,
|
||||||
|
Insertable,
|
||||||
|
Debug,
|
||||||
|
utoipa::ToSchema,
|
||||||
|
ResponderJson,
|
||||||
|
)]
|
||||||
|
#[diesel(table_name = crate::database::schema::users)]
|
||||||
|
#[diesel(treat_none_as_null = true)]
|
||||||
|
pub struct User {
|
||||||
|
/// UUID аккаунта
|
||||||
|
pub id: String,
|
||||||
|
|
||||||
|
/// Имя пользователя
|
||||||
|
pub username: String,
|
||||||
|
|
||||||
|
/// BCrypt хеш пароля
|
||||||
|
pub password: String,
|
||||||
|
|
||||||
|
/// Идентификатор привязанного аккаунта VK
|
||||||
|
pub vk_id: Option<i32>,
|
||||||
|
|
||||||
|
/// JWT токен доступа
|
||||||
|
pub access_token: String,
|
||||||
|
|
||||||
|
/// Группа
|
||||||
|
pub group: String,
|
||||||
|
|
||||||
|
/// Роль
|
||||||
|
pub role: UserRole,
|
||||||
|
|
||||||
|
/// Версия установленного приложения Polytechnic+
|
||||||
|
pub version: String,
|
||||||
|
}
|
||||||
38
src/database/schema.rs
Normal file
38
src/database/schema.rs
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
// @generated automatically by Diesel CLI.
|
||||||
|
|
||||||
|
pub mod sql_types {
|
||||||
|
#[derive(diesel::query_builder::QueryId, Clone, diesel::sql_types::SqlType)]
|
||||||
|
#[diesel(postgres_type(name = "user_role"))]
|
||||||
|
pub struct UserRole;
|
||||||
|
}
|
||||||
|
|
||||||
|
diesel::table! {
|
||||||
|
fcm (user_id) {
|
||||||
|
user_id -> Text,
|
||||||
|
token -> Text,
|
||||||
|
topics -> Nullable<Array<Nullable<Text>>>,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
diesel::table! {
|
||||||
|
use diesel::sql_types::*;
|
||||||
|
use super::sql_types::UserRole;
|
||||||
|
|
||||||
|
users (id) {
|
||||||
|
id -> Text,
|
||||||
|
username -> Text,
|
||||||
|
password -> Text,
|
||||||
|
vk_id -> Nullable<Int4>,
|
||||||
|
access_token -> Text,
|
||||||
|
group -> Text,
|
||||||
|
role -> UserRole,
|
||||||
|
version -> Text,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
diesel::joinable!(fcm -> users (user_id));
|
||||||
|
|
||||||
|
diesel::allow_tables_to_appear_in_same_query!(
|
||||||
|
fcm,
|
||||||
|
users,
|
||||||
|
);
|
||||||
@@ -1,80 +1,68 @@
|
|||||||
use crate::extractors::base::FromRequestAsync;
|
use crate::app_state::AppState;
|
||||||
use crate::state::AppState;
|
use crate::database::driver;
|
||||||
use crate::utility::req_auth;
|
use crate::database::models::User;
|
||||||
use crate::utility::req_auth::get_claims_from_req;
|
use crate::extractors::base::FromRequestSync;
|
||||||
use actix_macros::MiddlewareError;
|
use crate::utility::jwt;
|
||||||
|
use actix_macros::ResponseErrorMessage;
|
||||||
use actix_web::body::BoxBody;
|
use actix_web::body::BoxBody;
|
||||||
use actix_web::dev::Payload;
|
use actix_web::dev::Payload;
|
||||||
use actix_web::{web, HttpRequest};
|
use actix_web::http::header;
|
||||||
use database::entity::{User, UserType};
|
use actix_web::{HttpRequest, web};
|
||||||
use database::query::Query;
|
|
||||||
use derive_more::Display;
|
use derive_more::Display;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use std::fmt::Debug;
|
use std::fmt::Debug;
|
||||||
|
|
||||||
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, Display, MiddlewareError)]
|
#[derive(Clone, Debug, Serialize, Deserialize, Display, ResponseErrorMessage)]
|
||||||
#[status_code = "actix_web::http::StatusCode::UNAUTHORIZED"]
|
#[status_code = "actix_web::http::StatusCode::UNAUTHORIZED"]
|
||||||
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
|
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
|
||||||
pub enum Error {
|
pub enum Error {
|
||||||
/// There is no Authorization header or cookie in the request.
|
/// В запросе отсутствует заголовок Authorization
|
||||||
#[display("No Authorization header or cookie found")]
|
#[display("No Authorization header found")]
|
||||||
NoHeaderOrCookieFound,
|
NoHeader,
|
||||||
|
|
||||||
/// Unknown authorization type other than Bearer.
|
/// Неизвестный тип авторизации, отличающийся от Bearer
|
||||||
#[display("Bearer token is required")]
|
#[display("Bearer token is required")]
|
||||||
UnknownAuthorizationType,
|
UnknownAuthorizationType,
|
||||||
|
|
||||||
/// Invalid or expired access token.
|
/// Токен не действителен
|
||||||
#[display("Invalid or expired access token")]
|
#[display("Invalid or expired access token")]
|
||||||
InvalidAccessToken,
|
InvalidAccessToken,
|
||||||
|
|
||||||
/// Default user is required.
|
/// Пользователь привязанный к токену не найден в базе данных
|
||||||
#[display("Non-default user type is owning this access token")]
|
|
||||||
#[status_code = "actix_web::http::StatusCode::FORBIDDEN"]
|
|
||||||
NonDefaultUserType,
|
|
||||||
|
|
||||||
/// The user bound to the token is not found in the database.
|
|
||||||
#[display("No user associated with access token")]
|
#[display("No user associated with access token")]
|
||||||
NoUser,
|
NoUser,
|
||||||
|
|
||||||
/// User doesn't have required role.
|
|
||||||
#[display("You don't have sufficient rights")]
|
|
||||||
#[status_code = "actix_web::http::StatusCode::FORBIDDEN"]
|
|
||||||
InsufficientRights,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<req_auth::Error> for Error {
|
impl Error {
|
||||||
fn from(value: req_auth::Error) -> Self {
|
pub fn into_err(self) -> actix_web::Error {
|
||||||
match value {
|
actix_web::Error::from(self)
|
||||||
req_auth::Error::NoHeaderOrCookieFound => Error::NoHeaderOrCookieFound,
|
|
||||||
req_auth::Error::UnknownAuthorizationType => Error::UnknownAuthorizationType,
|
|
||||||
req_auth::Error::InvalidAccessToken => Error::InvalidAccessToken,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// User extractor from request with Bearer access token.
|
/// Экстрактор пользователя из запроса с токеном
|
||||||
impl FromRequestAsync for User {
|
impl FromRequestSync for User {
|
||||||
type Error = Error;
|
type Error = actix_web::Error;
|
||||||
|
|
||||||
async fn from_request_async(
|
fn from_request_sync(req: &HttpRequest, _: &mut Payload) -> Result<Self, Self::Error> {
|
||||||
req: &HttpRequest,
|
let authorization = req
|
||||||
_payload: &mut Payload,
|
.headers()
|
||||||
) -> Result<Self, Self::Error> {
|
.get(header::AUTHORIZATION)
|
||||||
let claims = get_claims_from_req(req).map_err(Error::from)?;
|
.ok_or(Error::NoHeader.into_err())?
|
||||||
|
.to_str()
|
||||||
|
.map_err(|_| Error::NoHeader.into_err())?
|
||||||
|
.to_string();
|
||||||
|
|
||||||
if claims.user_type.unwrap_or(UserType::Default) != UserType::Default {
|
let parts: Vec<&str> = authorization.split(' ').collect();
|
||||||
return Err(Error::NonDefaultUserType);
|
|
||||||
|
if parts.len() != 2 || parts[0] != "Bearer" {
|
||||||
|
return Err(Error::UnknownAuthorizationType.into_err());
|
||||||
}
|
}
|
||||||
|
|
||||||
let db = req
|
let user_id = jwt::verify_and_decode(&parts[1].to_string())
|
||||||
.app_data::<web::Data<AppState>>()
|
.map_err(|_| Error::InvalidAccessToken.into_err())?;
|
||||||
.unwrap()
|
|
||||||
.get_database();
|
|
||||||
|
|
||||||
match Query::find_user_by_id(db, &claims.id).await {
|
let app_state = req.app_data::<web::Data<AppState>>().unwrap();
|
||||||
Ok(Some(user)) => Ok(user),
|
|
||||||
_ => Err(Error::NoUser),
|
driver::users::get(&app_state.database, &user_id).map_err(|_| Error::NoUser.into())
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,148 +2,59 @@ use actix_web::dev::Payload;
|
|||||||
use actix_web::{FromRequest, HttpRequest};
|
use actix_web::{FromRequest, HttpRequest};
|
||||||
use futures_util::future::LocalBoxFuture;
|
use futures_util::future::LocalBoxFuture;
|
||||||
use std::future::{Ready, ready};
|
use std::future::{Ready, ready};
|
||||||
use std::ops;
|
|
||||||
|
|
||||||
/// # Async extractor.
|
/// Асинхронный экстрактор объектов из запроса
|
||||||
/// Asynchronous object extractor from a query.
|
|
||||||
pub struct AsyncExtractor<T>(T);
|
pub struct AsyncExtractor<T>(T);
|
||||||
|
|
||||||
impl<T> AsyncExtractor<T> {
|
impl<T> AsyncExtractor<T> {
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
/// Retrieve the object extracted with the extractor.
|
/// Получение объекта, извлечённого с помощью экстрактора
|
||||||
pub fn into_inner(self) -> T {
|
pub fn into_inner(self) -> T {
|
||||||
self.0
|
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 {
|
pub trait FromRequestAsync: Sized {
|
||||||
type Error: Into<actix_web::Error>;
|
type Error: Into<actix_web::Error>;
|
||||||
|
|
||||||
/// Asynchronous function for extracting data from a query.
|
/// Асинхронная функция для извлечения данных из запроса
|
||||||
///
|
async fn from_request_async(req: HttpRequest, payload: Payload) -> Result<Self, Self::Error>;
|
||||||
/// 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: &mut Payload,
|
|
||||||
) -> Result<Self, Self::Error>;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Реализация треита FromRequest для всех асинхронных экстракторов
|
||||||
impl<T: FromRequestAsync> FromRequest for AsyncExtractor<T> {
|
impl<T: FromRequestAsync> FromRequest for AsyncExtractor<T> {
|
||||||
type Error = T::Error;
|
type Error = T::Error;
|
||||||
type Future = LocalBoxFuture<'static, Result<Self, Self::Error>>;
|
type Future = LocalBoxFuture<'static, Result<Self, Self::Error>>;
|
||||||
|
|
||||||
fn from_request(req: &HttpRequest, _payload: &mut Payload) -> Self::Future {
|
fn from_request(req: &HttpRequest, payload: &mut Payload) -> Self::Future {
|
||||||
let req = req.clone();
|
let req = req.clone();
|
||||||
let mut payload = Payload::None;
|
let payload = payload.take();
|
||||||
|
|
||||||
Box::pin(async move {
|
Box::pin(async move {
|
||||||
T::from_request_async(&req, &mut payload)
|
T::from_request_async(req, payload)
|
||||||
.await
|
.await
|
||||||
.map(|res| Self(res))
|
.map(|res| Self(res))
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// # Sync extractor.
|
/// Синхронный экстрактор объектов из запроса
|
||||||
/// Synchronous object extractor from a query.
|
|
||||||
pub struct SyncExtractor<T>(T);
|
pub struct SyncExtractor<T>(T);
|
||||||
|
|
||||||
impl<T> SyncExtractor<T> {
|
impl<T> SyncExtractor<T> {
|
||||||
/// Retrieving an object extracted with the extractor.
|
/// Получение объекта, извлечённого с помощью экстрактора
|
||||||
#[allow(unused)]
|
|
||||||
pub fn into_inner(self) -> T {
|
pub fn into_inner(self) -> T {
|
||||||
self.0
|
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 {
|
pub trait FromRequestSync: Sized {
|
||||||
type Error: Into<actix_web::Error>;
|
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>;
|
fn from_request_sync(req: &HttpRequest, payload: &mut Payload) -> Result<Self, Self::Error>;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Реализация треита FromRequest для всех синхронных экстракторов
|
||||||
impl<T: FromRequestSync> FromRequest for SyncExtractor<T> {
|
impl<T: FromRequestSync> FromRequest for SyncExtractor<T> {
|
||||||
type Error = T::Error;
|
type Error = T::Error;
|
||||||
type Future = Ready<Result<Self, Self::Error>>;
|
type Future = Ready<Result<Self, Self::Error>>;
|
||||||
|
|||||||
@@ -1,2 +1,2 @@
|
|||||||
pub mod authorized_user;
|
pub mod authorized_user;
|
||||||
pub mod base;
|
pub mod base;
|
||||||
1
src/lib.rs
Normal file
1
src/lib.rs
Normal file
@@ -0,0 +1 @@
|
|||||||
|
pub mod parser;
|
||||||
193
src/main.rs
193
src/main.rs
@@ -1,17 +1,26 @@
|
|||||||
use crate::middlewares::authorization::{JWTAuthorizationBuilder, ServiceConfig};
|
use crate::app_state::{AppState, app_state};
|
||||||
use crate::middlewares::content_type::ContentTypeBootstrap;
|
use crate::middlewares::authorization::JWTAuthorization;
|
||||||
use crate::state::{new_app_state, AppState};
|
use crate::routes::auth::sign_in::{sign_in_default, sign_in_vk};
|
||||||
use actix_web::dev::{ServiceFactory, ServiceRequest};
|
use crate::routes::auth::sign_up::{sign_up_default, sign_up_vk};
|
||||||
use actix_web::{App, Error, HttpServer};
|
use crate::routes::schedule::get_cache_status::get_cache_status;
|
||||||
use database::entity::sea_orm_active_enums::UserRole;
|
use crate::routes::schedule::get_group::get_group;
|
||||||
|
use crate::routes::schedule::get_group_names::get_group_names;
|
||||||
|
use crate::routes::schedule::get_schedule::get_schedule;
|
||||||
|
use crate::routes::schedule::get_teacher::get_teacher;
|
||||||
|
use crate::routes::schedule::get_teacher_names::get_teacher_names;
|
||||||
|
use crate::routes::schedule::update_download_url::update_download_url;
|
||||||
|
use crate::routes::users::me::me;
|
||||||
|
use actix_web::{App, HttpServer};
|
||||||
use dotenvy::dotenv;
|
use dotenvy::dotenv;
|
||||||
use log::info;
|
|
||||||
use std::io;
|
|
||||||
use utoipa_actix_web::scope::Scope;
|
|
||||||
use utoipa_actix_web::AppExt;
|
use utoipa_actix_web::AppExt;
|
||||||
use utoipa_rapidoc::RapiDoc;
|
use utoipa_rapidoc::RapiDoc;
|
||||||
|
|
||||||
mod state;
|
mod app_state;
|
||||||
|
|
||||||
|
mod database;
|
||||||
|
|
||||||
|
mod parser;
|
||||||
|
mod xls_downloader;
|
||||||
|
|
||||||
mod extractors;
|
mod extractors;
|
||||||
mod middlewares;
|
mod middlewares;
|
||||||
@@ -21,122 +30,45 @@ mod utility;
|
|||||||
|
|
||||||
mod test_env;
|
mod test_env;
|
||||||
|
|
||||||
pub fn get_api_scope<
|
#[actix_web::main]
|
||||||
I: Into<Scope<T>>,
|
async fn main() {
|
||||||
T: ServiceFactory<ServiceRequest, Config = (), Error = Error, InitError = ()>,
|
dotenv().ok();
|
||||||
>(
|
|
||||||
scope: I,
|
|
||||||
) -> Scope<T> {
|
|
||||||
let admin_scope = {
|
|
||||||
let service_user_scope =
|
|
||||||
utoipa_actix_web::scope("/service-users").service(routes::admin::service_users::create);
|
|
||||||
|
|
||||||
utoipa_actix_web::scope("/admin")
|
unsafe { std::env::set_var("RUST_LOG", "debug") };
|
||||||
.wrap(
|
env_logger::init();
|
||||||
JWTAuthorizationBuilder::new()
|
|
||||||
.with_default(Some(ServiceConfig {
|
|
||||||
allow_service: false,
|
|
||||||
user_roles: Some(&[UserRole::Admin]),
|
|
||||||
}))
|
|
||||||
.build(),
|
|
||||||
)
|
|
||||||
.service(service_user_scope)
|
|
||||||
};
|
|
||||||
|
|
||||||
let auth_scope = utoipa_actix_web::scope("/auth")
|
let app_state = app_state();
|
||||||
.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(
|
|
||||||
JWTAuthorizationBuilder::new()
|
|
||||||
.add_paths(
|
|
||||||
["/by/id/{id}", "/by/telegram-id/{id}"],
|
|
||||||
Some(ServiceConfig {
|
|
||||||
allow_service: true,
|
|
||||||
user_roles: Some(&[UserRole::Admin]),
|
|
||||||
}),
|
|
||||||
)
|
|
||||||
.build(),
|
|
||||||
)
|
|
||||||
.service(
|
|
||||||
utoipa_actix_web::scope("/by")
|
|
||||||
.service(routes::users::by::by_id)
|
|
||||||
.service(routes::users::by::by_telegram_id),
|
|
||||||
)
|
|
||||||
.service(routes::users::change_group)
|
|
||||||
.service(routes::users::change_username)
|
|
||||||
.service(routes::users::me);
|
|
||||||
|
|
||||||
let schedule_scope = utoipa_actix_web::scope("/schedule")
|
|
||||||
.wrap(
|
|
||||||
JWTAuthorizationBuilder::new()
|
|
||||||
.with_default(Some(ServiceConfig {
|
|
||||||
allow_service: true,
|
|
||||||
user_roles: None,
|
|
||||||
}))
|
|
||||||
.add_paths(["/group-names", "/teacher-names"], None)
|
|
||||||
.add_paths(
|
|
||||||
["/"],
|
|
||||||
Some(ServiceConfig {
|
|
||||||
allow_service: true,
|
|
||||||
user_roles: Some(&[UserRole::Admin]),
|
|
||||||
}),
|
|
||||||
)
|
|
||||||
.add_paths(
|
|
||||||
["/group"],
|
|
||||||
Some(ServiceConfig {
|
|
||||||
allow_service: false,
|
|
||||||
user_roles: None,
|
|
||||||
}),
|
|
||||||
)
|
|
||||||
.build(),
|
|
||||||
)
|
|
||||||
.service(routes::schedule::cache_status)
|
|
||||||
.service(routes::schedule::schedule)
|
|
||||||
.service(routes::schedule::group)
|
|
||||||
.service(routes::schedule::group_by_name)
|
|
||||||
.service(routes::schedule::group_names)
|
|
||||||
.service(routes::schedule::teacher)
|
|
||||||
.service(routes::schedule::teacher_names);
|
|
||||||
|
|
||||||
let flow_scope = utoipa_actix_web::scope("/flow")
|
|
||||||
.wrap(
|
|
||||||
JWTAuthorizationBuilder::new()
|
|
||||||
.add_paths(["/telegram-auth"], None)
|
|
||||||
.build(),
|
|
||||||
)
|
|
||||||
.service(routes::flow::telegram_auth)
|
|
||||||
.service(routes::flow::telegram_complete);
|
|
||||||
|
|
||||||
let vk_id_scope = utoipa_actix_web::scope("/vkid") //
|
|
||||||
.service(routes::vk_id::oauth);
|
|
||||||
|
|
||||||
utoipa_actix_web::scope(scope)
|
|
||||||
.service(admin_scope)
|
|
||||||
.service(auth_scope)
|
|
||||||
.service(users_scope)
|
|
||||||
.service(schedule_scope)
|
|
||||||
.service(flow_scope)
|
|
||||||
.service(vk_id_scope)
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn async_main() -> io::Result<()> {
|
|
||||||
info!("Запуск сервера...");
|
|
||||||
|
|
||||||
let app_state = new_app_state(None).await.unwrap();
|
|
||||||
|
|
||||||
HttpServer::new(move || {
|
HttpServer::new(move || {
|
||||||
|
let auth_scope = utoipa_actix_web::scope("/auth")
|
||||||
|
.service(sign_in_default)
|
||||||
|
.service(sign_in_vk)
|
||||||
|
.service(sign_up_default)
|
||||||
|
.service(sign_up_vk);
|
||||||
|
|
||||||
|
let users_scope = utoipa_actix_web::scope("/users")
|
||||||
|
.wrap(JWTAuthorization)
|
||||||
|
.service(me);
|
||||||
|
|
||||||
|
let schedule_scope = utoipa_actix_web::scope("/schedule")
|
||||||
|
.wrap(JWTAuthorization)
|
||||||
|
.service(get_schedule)
|
||||||
|
.service(update_download_url)
|
||||||
|
.service(get_cache_status)
|
||||||
|
.service(get_group)
|
||||||
|
.service(get_group_names)
|
||||||
|
.service(get_teacher)
|
||||||
|
.service(get_teacher_names);
|
||||||
|
|
||||||
|
let api_scope = utoipa_actix_web::scope("/api/v1")
|
||||||
|
.service(auth_scope)
|
||||||
|
.service(users_scope)
|
||||||
|
.service(schedule_scope);
|
||||||
|
|
||||||
let (app, api) = App::new()
|
let (app, api) = App::new()
|
||||||
.into_utoipa_app()
|
.into_utoipa_app()
|
||||||
.app_data(app_state.clone())
|
.app_data(app_state.clone())
|
||||||
.service(
|
.service(api_scope)
|
||||||
get_api_scope("/api/v1")
|
|
||||||
.wrap(sentry_actix::Sentry::new())
|
|
||||||
.wrap(ContentTypeBootstrap),
|
|
||||||
)
|
|
||||||
.split_for_parts();
|
.split_for_parts();
|
||||||
|
|
||||||
let rapidoc_service = RapiDoc::with_openapi("/api-docs-json", api).path("/api-docs");
|
let rapidoc_service = RapiDoc::with_openapi("/api-docs-json", api).path("/api-docs");
|
||||||
@@ -150,26 +82,9 @@ async fn async_main() -> io::Result<()> {
|
|||||||
app.service(rapidoc_service.custom_html(patched_rapidoc_html))
|
app.service(rapidoc_service.custom_html(patched_rapidoc_html))
|
||||||
})
|
})
|
||||||
.workers(4)
|
.workers(4)
|
||||||
.bind(("0.0.0.0", 5050))?
|
.bind(("0.0.0.0", 8080))
|
||||||
|
.unwrap()
|
||||||
.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()
|
|
||||||
},
|
|
||||||
));
|
|
||||||
|
|
||||||
let _ = dotenv();
|
|
||||||
|
|
||||||
env_logger::init();
|
|
||||||
|
|
||||||
actix_web::rt::System::new().block_on(async { async_main().await })?;
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,73 +1,18 @@
|
|||||||
|
use crate::database::models::User;
|
||||||
use crate::extractors::authorized_user;
|
use crate::extractors::authorized_user;
|
||||||
use crate::state::AppState;
|
use crate::extractors::base::FromRequestSync;
|
||||||
use crate::utility::req_auth::get_claims_from_req;
|
|
||||||
use actix_web::body::{BoxBody, EitherBody};
|
use actix_web::body::{BoxBody, EitherBody};
|
||||||
use actix_web::dev::{forward_ready, Service, ServiceRequest, ServiceResponse, Transform};
|
use actix_web::dev::{Payload, Service, ServiceRequest, ServiceResponse, Transform, forward_ready};
|
||||||
use actix_web::{web, Error, HttpRequest, ResponseError};
|
use actix_web::{Error, HttpRequest, ResponseError};
|
||||||
use database::entity::sea_orm_active_enums::UserRole;
|
|
||||||
use database::entity::UserType;
|
|
||||||
use database::query::Query;
|
|
||||||
use futures_util::future::LocalBoxFuture;
|
use futures_util::future::LocalBoxFuture;
|
||||||
use std::future::{ready, Ready};
|
use std::future::{Ready, ready};
|
||||||
use std::ops::Deref;
|
|
||||||
use std::rc::Rc;
|
|
||||||
use std::sync::Arc;
|
|
||||||
|
|
||||||
#[derive(Default, Clone)]
|
/// Middleware guard работающий с токенами JWT
|
||||||
pub struct ServiceConfig {
|
pub struct JWTAuthorization;
|
||||||
/// Allow service users to access endpoints.
|
|
||||||
pub allow_service: bool,
|
|
||||||
|
|
||||||
/// List of required roles to access endpoints.
|
|
||||||
pub user_roles: Option<&'static [UserRole]>,
|
|
||||||
}
|
|
||||||
|
|
||||||
type ServiceKV = (Arc<[&'static str]>, Option<ServiceConfig>);
|
|
||||||
|
|
||||||
pub struct JWTAuthorizationBuilder {
|
|
||||||
pub default_config: Option<ServiceConfig>,
|
|
||||||
pub path_configs: Vec<ServiceKV>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl JWTAuthorizationBuilder {
|
|
||||||
pub fn new() -> Self {
|
|
||||||
JWTAuthorizationBuilder {
|
|
||||||
default_config: Some(ServiceConfig::default()),
|
|
||||||
path_configs: vec![],
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn with_default(mut self, default: Option<ServiceConfig>) -> Self {
|
|
||||||
self.default_config = default;
|
|
||||||
self
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn add_paths(
|
|
||||||
mut self,
|
|
||||||
paths: impl AsRef<[&'static str]>,
|
|
||||||
config: Option<ServiceConfig>,
|
|
||||||
) -> Self {
|
|
||||||
self.path_configs.push((Arc::from(paths.as_ref()), config));
|
|
||||||
self
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn build(self) -> JWTAuthorization {
|
|
||||||
JWTAuthorization {
|
|
||||||
default_config: Arc::new(self.default_config),
|
|
||||||
path_configs: Arc::from(self.path_configs),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Middleware guard working with JWT tokens.
|
|
||||||
pub struct JWTAuthorization {
|
|
||||||
pub default_config: Arc<Option<ServiceConfig>>,
|
|
||||||
pub path_configs: Arc<[ServiceKV]>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<S, B> Transform<S, ServiceRequest> for JWTAuthorization
|
impl<S, B> Transform<S, ServiceRequest> for JWTAuthorization
|
||||||
where
|
where
|
||||||
S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = Error> + 'static,
|
S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = Error>,
|
||||||
S::Future: 'static,
|
S::Future: 'static,
|
||||||
B: 'static,
|
B: 'static,
|
||||||
{
|
{
|
||||||
@@ -78,96 +23,35 @@ where
|
|||||||
type Future = Ready<Result<Self::Transform, Self::InitError>>;
|
type Future = Ready<Result<Self::Transform, Self::InitError>>;
|
||||||
|
|
||||||
fn new_transform(&self, service: S) -> Self::Future {
|
fn new_transform(&self, service: S) -> Self::Future {
|
||||||
ready(Ok(JWTAuthorizationMiddleware {
|
ready(Ok(JWTAuthorizationMiddleware { service }))
|
||||||
service: Rc::new(service),
|
|
||||||
default_config: self.default_config.clone(),
|
|
||||||
path_configs: self.path_configs.clone(),
|
|
||||||
}))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct JWTAuthorizationMiddleware<S> {
|
pub struct JWTAuthorizationMiddleware<S> {
|
||||||
service: Rc<S>,
|
service: S,
|
||||||
|
|
||||||
default_config: Arc<Option<ServiceConfig>>,
|
|
||||||
path_configs: Arc<[ServiceKV]>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Функция для проверки наличия и действительности токена в запросе, а так же существования пользователя к которому он привязан
|
||||||
impl<S, B> JWTAuthorizationMiddleware<S>
|
impl<S, B> JWTAuthorizationMiddleware<S>
|
||||||
where
|
where
|
||||||
S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = Error>,
|
S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = Error>,
|
||||||
S::Future: 'static,
|
S::Future: 'static,
|
||||||
B: 'static,
|
B: 'static,
|
||||||
{
|
{
|
||||||
/// Checking the validity of the token.
|
pub fn check_authorization(
|
||||||
async fn check_authorization(
|
&self,
|
||||||
req: &HttpRequest,
|
req: &HttpRequest,
|
||||||
allow_service_user: bool,
|
payload: &mut Payload,
|
||||||
required_user_roles: Option<&'static [UserRole]>,
|
|
||||||
) -> Result<(), authorized_user::Error> {
|
) -> Result<(), authorized_user::Error> {
|
||||||
let claims = get_claims_from_req(req).map_err(authorized_user::Error::from)?;
|
User::from_request_sync(req, payload)
|
||||||
|
.map(|_| ())
|
||||||
let db = req
|
.map_err(|e| e.as_error::<authorized_user::Error>().unwrap().clone())
|
||||||
.app_data::<web::Data<AppState>>()
|
|
||||||
.unwrap()
|
|
||||||
.get_database();
|
|
||||||
|
|
||||||
let user_type = claims.user_type.unwrap_or(UserType::Default);
|
|
||||||
|
|
||||||
match user_type {
|
|
||||||
UserType::Default => {
|
|
||||||
if let Some(required_user_roles) = required_user_roles {
|
|
||||||
let Ok(Some(user)) = Query::find_user_by_id(db, &claims.id).await else {
|
|
||||||
return Err(authorized_user::Error::NoUser);
|
|
||||||
};
|
|
||||||
|
|
||||||
if !required_user_roles.contains(&user.role) {
|
|
||||||
return Err(authorized_user::Error::InsufficientRights);
|
|
||||||
}
|
|
||||||
|
|
||||||
return Ok(());
|
|
||||||
}
|
|
||||||
|
|
||||||
match Query::is_user_exists_by_id(db, &claims.id).await {
|
|
||||||
Ok(true) => Ok(()),
|
|
||||||
_ => Err(authorized_user::Error::NoUser),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
UserType::Service => {
|
|
||||||
if !allow_service_user {
|
|
||||||
return Err(authorized_user::Error::NonDefaultUserType);
|
|
||||||
}
|
|
||||||
|
|
||||||
match Query::is_service_user_exists_by_id(db, &claims.id).await {
|
|
||||||
Ok(true) => Ok(()),
|
|
||||||
_ => Err(authorized_user::Error::NoUser),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn find_config(
|
|
||||||
current_path: &str,
|
|
||||||
per_route: &[ServiceKV],
|
|
||||||
default: &Option<ServiceConfig>,
|
|
||||||
) -> Option<ServiceConfig> {
|
|
||||||
for (service_paths, config) in per_route {
|
|
||||||
for service_path in service_paths.deref() {
|
|
||||||
if !service_path.eq(¤t_path) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
return config.clone();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
default.clone()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<S, B> Service<ServiceRequest> for JWTAuthorizationMiddleware<S>
|
impl<S, B> Service<ServiceRequest> for JWTAuthorizationMiddleware<S>
|
||||||
where
|
where
|
||||||
S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = Error> + 'static,
|
S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = Error>,
|
||||||
S::Future: 'static,
|
S::Future: 'static,
|
||||||
B: 'static,
|
B: 'static,
|
||||||
{
|
{
|
||||||
@@ -178,42 +62,20 @@ where
|
|||||||
forward_ready!(service);
|
forward_ready!(service);
|
||||||
|
|
||||||
fn call(&self, req: ServiceRequest) -> Self::Future {
|
fn call(&self, req: ServiceRequest) -> Self::Future {
|
||||||
let service = Rc::clone(&self.service);
|
let (http_req, mut payload) = req.into_parts();
|
||||||
|
|
||||||
let match_info = req.match_info();
|
if let Err(err) = self.check_authorization(&http_req, &mut payload) {
|
||||||
let path = if let Some(pattern) = req.match_pattern() {
|
return Box::pin(async move {
|
||||||
let scope_start_idx = match_info
|
Ok(ServiceResponse::new(
|
||||||
.as_str()
|
http_req,
|
||||||
.find(match_info.unprocessed())
|
|
||||||
.unwrap_or(0);
|
|
||||||
|
|
||||||
pattern.as_str().split_at(scope_start_idx).1.to_owned()
|
|
||||||
} else {
|
|
||||||
match_info.unprocessed().to_owned()
|
|
||||||
};
|
|
||||||
|
|
||||||
let Some(config) = Self::find_config(&path, &self.path_configs, &self.default_config)
|
|
||||||
else {
|
|
||||||
let fut = self.service.call(req);
|
|
||||||
return Box::pin(async move { Ok(fut.await?.map_into_left_body()) });
|
|
||||||
};
|
|
||||||
|
|
||||||
let allow_service_user = config.allow_service;
|
|
||||||
let required_user_roles = config.user_roles;
|
|
||||||
|
|
||||||
Box::pin(async move {
|
|
||||||
match Self::check_authorization(req.request(), allow_service_user, required_user_roles)
|
|
||||||
.await
|
|
||||||
{
|
|
||||||
Ok(_) => {
|
|
||||||
let fut = service.call(req).await?;
|
|
||||||
Ok(fut.map_into_left_body())
|
|
||||||
}
|
|
||||||
Err(err) => Ok(ServiceResponse::new(
|
|
||||||
req.into_parts().0,
|
|
||||||
err.error_response().map_into_right_body(),
|
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()) })
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,65 +0,0 @@
|
|||||||
use actix_web::body::{BoxBody, EitherBody};
|
|
||||||
use actix_web::dev::{forward_ready, Service, ServiceRequest, ServiceResponse, Transform};
|
|
||||||
use actix_web::http::header;
|
|
||||||
use actix_web::http::header::HeaderValue;
|
|
||||||
use actix_web::Error;
|
|
||||||
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<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")
|
|
||||||
&& content_type == "application/json"
|
|
||||||
{
|
|
||||||
headers.insert(
|
|
||||||
header::CONTENT_TYPE,
|
|
||||||
HeaderValue::from_static("application/json; charset=utf8"),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(response.map_into_left_body())
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,4 +1 @@
|
|||||||
pub mod error;
|
pub mod authorization;
|
||||||
|
|
||||||
pub mod authorization;
|
|
||||||
pub mod content_type;
|
|
||||||
725
src/parser/mod.rs
Normal file
725
src/parser/mod.rs
Normal file
@@ -0,0 +1,725 @@
|
|||||||
|
use crate::parser::LessonParseResult::{Lessons, Street};
|
||||||
|
use crate::parser::schema::LessonType::Break;
|
||||||
|
use crate::parser::schema::{
|
||||||
|
Day, Lesson, LessonSubGroup, LessonTime, LessonType, ParseError, ParseResult, ScheduleEntry,
|
||||||
|
};
|
||||||
|
use calamine::{Reader, Xls, open_workbook_from_rs};
|
||||||
|
use chrono::{Duration, NaiveDateTime};
|
||||||
|
use fuzzy_matcher::FuzzyMatcher;
|
||||||
|
use fuzzy_matcher::skim::SkimMatcherV2;
|
||||||
|
use regex::Regex;
|
||||||
|
use std::collections::HashMap;
|
||||||
|
use std::io::Cursor;
|
||||||
|
use std::sync::LazyLock;
|
||||||
|
|
||||||
|
pub mod schema;
|
||||||
|
|
||||||
|
/// Данные ячейке хранящей строку
|
||||||
|
struct InternalId {
|
||||||
|
/// Индекс строки
|
||||||
|
row: u32,
|
||||||
|
|
||||||
|
/// Индекс столбца
|
||||||
|
column: u32,
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Текст в ячейке
|
||||||
|
*/
|
||||||
|
name: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Данные о времени проведения пар из второй колонки расписания
|
||||||
|
struct InternalTime {
|
||||||
|
/// Временной отрезок проведения пары
|
||||||
|
time_range: LessonTime,
|
||||||
|
|
||||||
|
/// Тип пары
|
||||||
|
lesson_type: LessonType,
|
||||||
|
|
||||||
|
/// Индекс пары
|
||||||
|
default_index: Option<u32>,
|
||||||
|
|
||||||
|
/// Рамка ячейки
|
||||||
|
xls_range: ((u32, u32), (u32, u32)),
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Сокращение типа рабочего листа
|
||||||
|
type WorkSheet = calamine::Range<calamine::Data>;
|
||||||
|
|
||||||
|
/// Получение строки из требуемой ячейки
|
||||||
|
fn get_string_from_cell(worksheet: &WorkSheet, row: u32, col: u32) -> Option<String> {
|
||||||
|
let cell_data = if let Some(data) = worksheet.get((row as usize, col as usize)) {
|
||||||
|
data.to_string()
|
||||||
|
} else {
|
||||||
|
return None;
|
||||||
|
};
|
||||||
|
|
||||||
|
if cell_data.trim().is_empty() {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
|
||||||
|
static NL_RE: LazyLock<Regex, fn() -> Regex> =
|
||||||
|
LazyLock::new(|| Regex::new(r"[\n\r]+").unwrap());
|
||||||
|
static SP_RE: LazyLock<Regex, fn() -> Regex> = LazyLock::new(|| Regex::new(r"\s+").unwrap());
|
||||||
|
|
||||||
|
let trimmed_data = SP_RE
|
||||||
|
.replace_all(&NL_RE.replace_all(&cell_data, " "), " ")
|
||||||
|
.trim()
|
||||||
|
.to_string();
|
||||||
|
|
||||||
|
if trimmed_data.is_empty() {
|
||||||
|
None
|
||||||
|
} else {
|
||||||
|
Some(trimmed_data)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Получение границ ячейки по её верхней левой координате
|
||||||
|
fn get_merge_from_start(worksheet: &WorkSheet, row: u32, column: u32) -> ((u32, u32), (u32, u32)) {
|
||||||
|
let worksheet_end = worksheet.end().unwrap();
|
||||||
|
|
||||||
|
let row_end: u32 = {
|
||||||
|
let mut r: u32 = 0;
|
||||||
|
|
||||||
|
for _r in (row + 1)..worksheet_end.0 {
|
||||||
|
r = _r;
|
||||||
|
|
||||||
|
if let Some(_) = worksheet.get((_r as usize, column as usize)) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
r
|
||||||
|
};
|
||||||
|
|
||||||
|
let column_end: u32 = {
|
||||||
|
let mut c: u32 = 0;
|
||||||
|
|
||||||
|
for _c in (column + 1)..worksheet_end.1 {
|
||||||
|
c = _c;
|
||||||
|
|
||||||
|
if let Some(_) = worksheet.get((row as usize, _c as usize)) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
c
|
||||||
|
};
|
||||||
|
|
||||||
|
((row, column), (row_end, column_end))
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Получение "скелета" расписания из рабочего листа
|
||||||
|
fn parse_skeleton(worksheet: &WorkSheet) -> Result<(Vec<InternalId>, Vec<InternalId>), ParseError> {
|
||||||
|
let range = &worksheet;
|
||||||
|
|
||||||
|
let mut is_parsed = false;
|
||||||
|
|
||||||
|
let mut groups: Vec<InternalId> = Vec::new();
|
||||||
|
let mut days: Vec<InternalId> = Vec::new();
|
||||||
|
|
||||||
|
let start = range.start().ok_or(ParseError::UnknownWorkSheetRange)?;
|
||||||
|
let end = range.end().ok_or(ParseError::UnknownWorkSheetRange)?;
|
||||||
|
|
||||||
|
let mut row = start.0;
|
||||||
|
while row < end.0 {
|
||||||
|
row += 1;
|
||||||
|
|
||||||
|
let day_name_opt = get_string_from_cell(&worksheet, row, 0);
|
||||||
|
if day_name_opt.is_none() {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
let day_name = day_name_opt.unwrap();
|
||||||
|
|
||||||
|
if !is_parsed {
|
||||||
|
is_parsed = true;
|
||||||
|
|
||||||
|
row -= 1;
|
||||||
|
|
||||||
|
for column in (start.1 + 2)..=end.1 {
|
||||||
|
let group_name = get_string_from_cell(&worksheet, row, column);
|
||||||
|
if group_name.is_none() {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
groups.push(InternalId {
|
||||||
|
row,
|
||||||
|
column,
|
||||||
|
name: group_name.unwrap(),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
row += 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
days.push(InternalId {
|
||||||
|
row,
|
||||||
|
column: 0,
|
||||||
|
name: day_name.clone(),
|
||||||
|
});
|
||||||
|
|
||||||
|
if days.len() > 2 && day_name.starts_with("Суббота") {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok((days, groups))
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Результат получения пары из ячейки
|
||||||
|
enum LessonParseResult {
|
||||||
|
/// Список пар длинной от одного до двух
|
||||||
|
///
|
||||||
|
/// Количество пар будет равно одному, если пара первая за день, иначе будет возвращен список из шаблона перемены и самой пары
|
||||||
|
Lessons(Vec<Lesson>),
|
||||||
|
|
||||||
|
/// Улица на которой находится корпус политехникума
|
||||||
|
Street(String),
|
||||||
|
}
|
||||||
|
|
||||||
|
trait StringInnerSlice {
|
||||||
|
/// Получения отрезка строки из строки по начальному и конечному индексу
|
||||||
|
fn inner_slice(&self, from: usize, to: usize) -> Self;
|
||||||
|
}
|
||||||
|
|
||||||
|
impl StringInnerSlice for String {
|
||||||
|
fn inner_slice(&self, from: usize, to: usize) -> Self {
|
||||||
|
self.chars()
|
||||||
|
.take(from)
|
||||||
|
.chain(self.chars().skip(to))
|
||||||
|
.collect()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Получение нестандартного типа пары по названию
|
||||||
|
fn guess_lesson_type(name: &String) -> Option<(String, LessonType)> {
|
||||||
|
let map: HashMap<String, LessonType> = HashMap::from([
|
||||||
|
("(консультация)".to_string(), LessonType::Consultation),
|
||||||
|
(
|
||||||
|
"самостоятельная работа".to_string(),
|
||||||
|
LessonType::IndependentWork,
|
||||||
|
),
|
||||||
|
("зачет".to_string(), LessonType::Exam),
|
||||||
|
("зачет с оценкой".to_string(), LessonType::ExamWithGrade),
|
||||||
|
("экзамен".to_string(), LessonType::ExamDefault),
|
||||||
|
]);
|
||||||
|
|
||||||
|
let matcher = SkimMatcherV2::default();
|
||||||
|
let name_lower = name.to_lowercase();
|
||||||
|
|
||||||
|
type SearchResult<'a> = (&'a LessonType, i64, Vec<usize>);
|
||||||
|
|
||||||
|
let mut search_results: Vec<SearchResult> = map
|
||||||
|
.iter()
|
||||||
|
.map(|entry| -> SearchResult {
|
||||||
|
if let Some((score, indices)) = matcher.fuzzy_indices(&*name_lower, entry.0) {
|
||||||
|
return (entry.1, score, indices);
|
||||||
|
}
|
||||||
|
|
||||||
|
(entry.1, 0, Vec::new())
|
||||||
|
})
|
||||||
|
.collect();
|
||||||
|
search_results.sort_by(|a, b| b.1.cmp(&a.1));
|
||||||
|
|
||||||
|
let guessed_type = search_results.first().unwrap();
|
||||||
|
|
||||||
|
if guessed_type.1 > 80 {
|
||||||
|
Some((
|
||||||
|
name.inner_slice(guessed_type.2[0], guessed_type.2[guessed_type.2.len() - 1]),
|
||||||
|
guessed_type.0.clone(),
|
||||||
|
))
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Получение пары или улицы из ячейки
|
||||||
|
fn parse_lesson(
|
||||||
|
worksheet: &WorkSheet,
|
||||||
|
day: &mut Day,
|
||||||
|
day_times: &Vec<InternalTime>,
|
||||||
|
time: &InternalTime,
|
||||||
|
column: u32,
|
||||||
|
) -> Result<LessonParseResult, ParseError> {
|
||||||
|
let row = time.xls_range.0.0;
|
||||||
|
|
||||||
|
let (name, lesson_type) = {
|
||||||
|
let raw_name_opt = get_string_from_cell(&worksheet, row, column);
|
||||||
|
if raw_name_opt.is_none() {
|
||||||
|
return Ok(Lessons(Vec::new()));
|
||||||
|
}
|
||||||
|
|
||||||
|
let raw_name = raw_name_opt.unwrap();
|
||||||
|
|
||||||
|
static OTHER_STREET_RE: LazyLock<Regex, fn() -> Regex> =
|
||||||
|
LazyLock::new(|| Regex::new(r"^[А-Я][а-я]+,?\s?[0-9]+$").unwrap());
|
||||||
|
|
||||||
|
if OTHER_STREET_RE.is_match(&raw_name) {
|
||||||
|
return Ok(Street(raw_name));
|
||||||
|
}
|
||||||
|
|
||||||
|
if let Some(guess) = guess_lesson_type(&raw_name) {
|
||||||
|
guess
|
||||||
|
} else {
|
||||||
|
(raw_name, time.lesson_type.clone())
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
let (default_range, lesson_time) = || -> Result<(Option<[u8; 2]>, LessonTime), ParseError> {
|
||||||
|
// check if multi-lesson
|
||||||
|
let cell_range = get_merge_from_start(worksheet, row, column);
|
||||||
|
|
||||||
|
let end_time_arr = day_times
|
||||||
|
.iter()
|
||||||
|
.filter(|time| time.xls_range.1.0 == cell_range.1.0)
|
||||||
|
.collect::<Vec<&InternalTime>>();
|
||||||
|
|
||||||
|
let end_time = end_time_arr.first().ok_or(ParseError::LessonTimeNotFound)?;
|
||||||
|
|
||||||
|
let range: Option<[u8; 2]> = if time.default_index != None {
|
||||||
|
let default = time.default_index.unwrap() as u8;
|
||||||
|
Some([default, end_time.default_index.unwrap() as u8])
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
};
|
||||||
|
|
||||||
|
let time = LessonTime {
|
||||||
|
start: time.time_range.start,
|
||||||
|
end: end_time.time_range.end,
|
||||||
|
};
|
||||||
|
|
||||||
|
Ok((range, time))
|
||||||
|
}()?;
|
||||||
|
|
||||||
|
let (name, mut subgroups) = parse_name_and_subgroups(&name)?;
|
||||||
|
|
||||||
|
{
|
||||||
|
let cabinets: Vec<String> = parse_cabinets(worksheet, row, column + 1);
|
||||||
|
|
||||||
|
// Если количество кабинетов равно 1, назначаем этот кабинет всем подгруппам
|
||||||
|
if cabinets.len() == 1 {
|
||||||
|
for subgroup in &mut subgroups {
|
||||||
|
subgroup.cabinet = Some(cabinets.get(0).or(Some(&String::new())).unwrap().clone())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Если количество кабинетов совпадает с количеством подгрупп, назначаем кабинеты по порядку
|
||||||
|
else if cabinets.len() == subgroups.len() {
|
||||||
|
for subgroup in &mut subgroups {
|
||||||
|
subgroup.cabinet = Some(
|
||||||
|
cabinets
|
||||||
|
.get((subgroup.number - 1) as usize)
|
||||||
|
.unwrap()
|
||||||
|
.clone(),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Если количество кабинетов больше количества подгрупп, делаем ещё одну подгруппу.
|
||||||
|
else if cabinets.len() > subgroups.len() {
|
||||||
|
for index in 0..subgroups.len() {
|
||||||
|
subgroups[index].cabinet = Some(cabinets[index].clone());
|
||||||
|
}
|
||||||
|
|
||||||
|
while cabinets.len() > subgroups.len() {
|
||||||
|
subgroups.push(LessonSubGroup {
|
||||||
|
number: (subgroups.len() + 1) as u8,
|
||||||
|
cabinet: Some(cabinets[subgroups.len()].clone()),
|
||||||
|
teacher: "Ошибка в расписании".to_string(),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Если кабинетов нет, но есть подгруппы, назначаем им значение "??"
|
||||||
|
else {
|
||||||
|
for subgroup in &mut subgroups {
|
||||||
|
subgroup.cabinet = Some("??".to_string());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
cabinets
|
||||||
|
};
|
||||||
|
|
||||||
|
let lesson = Lesson {
|
||||||
|
lesson_type,
|
||||||
|
default_range,
|
||||||
|
name: Some(name),
|
||||||
|
time: lesson_time,
|
||||||
|
subgroups: Some(subgroups),
|
||||||
|
group: None,
|
||||||
|
};
|
||||||
|
|
||||||
|
let prev_lesson = if day.lessons.len() == 0 {
|
||||||
|
return Ok(Lessons(Vec::from([lesson])));
|
||||||
|
} else {
|
||||||
|
&day.lessons[day.lessons.len() - 1]
|
||||||
|
};
|
||||||
|
|
||||||
|
Ok(Lessons(Vec::from([
|
||||||
|
Lesson {
|
||||||
|
lesson_type: Break,
|
||||||
|
default_range: None,
|
||||||
|
name: None,
|
||||||
|
time: LessonTime {
|
||||||
|
start: prev_lesson.time.end,
|
||||||
|
end: lesson.time.start,
|
||||||
|
},
|
||||||
|
subgroups: Some(Vec::new()),
|
||||||
|
group: None,
|
||||||
|
},
|
||||||
|
lesson,
|
||||||
|
])))
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Получение списка кабинетов справа от ячейки пары
|
||||||
|
fn parse_cabinets(worksheet: &WorkSheet, row: u32, column: u32) -> Vec<String> {
|
||||||
|
let mut cabinets: Vec<String> = Vec::new();
|
||||||
|
|
||||||
|
if let Some(raw) = get_string_from_cell(&worksheet, row, column) {
|
||||||
|
let clean = raw.replace("\n", " ");
|
||||||
|
let parts: Vec<&str> = clean.split(" ").collect();
|
||||||
|
|
||||||
|
for part in parts {
|
||||||
|
let clean_part = part.to_string().trim().to_string();
|
||||||
|
|
||||||
|
cabinets.push(clean_part);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
cabinets
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Получение "чистого" названия пары и списка преподавателей из текста ячейки пары
|
||||||
|
fn parse_name_and_subgroups(name: &String) -> Result<(String, Vec<LessonSubGroup>), ParseError> {
|
||||||
|
static LESSON_RE: LazyLock<Regex, fn() -> Regex> =
|
||||||
|
LazyLock::new(|| Regex::new(r"(?:[А-Я][а-я]+[А-Я]{2}(?:\([0-9][а-я]+\))?)+$").unwrap());
|
||||||
|
static TEACHER_RE: LazyLock<Regex, fn() -> Regex> =
|
||||||
|
LazyLock::new(|| Regex::new(r"([А-Я][а-я]+)([А-Я])([А-Я])(?:\(([0-9])[а-я]+\))?").unwrap());
|
||||||
|
static CLEAN_RE: LazyLock<Regex, fn() -> Regex> =
|
||||||
|
LazyLock::new(|| Regex::new(r"[\s.,]+").unwrap());
|
||||||
|
static END_CLEAN_RE: LazyLock<Regex, fn() -> Regex> =
|
||||||
|
LazyLock::new(|| Regex::new(r"[.\s]+$").unwrap());
|
||||||
|
|
||||||
|
let (teachers, lesson_name) = {
|
||||||
|
let clean_name = CLEAN_RE.replace_all(&name, "").to_string();
|
||||||
|
|
||||||
|
if let Some(captures) = LESSON_RE.captures(&clean_name) {
|
||||||
|
let capture = captures.get(0).unwrap();
|
||||||
|
let capture_str = capture.as_str().to_string();
|
||||||
|
let capture_name: String = capture_str.chars().take(5).collect();
|
||||||
|
|
||||||
|
(
|
||||||
|
END_CLEAN_RE.replace(&capture_str, "").to_string(),
|
||||||
|
END_CLEAN_RE
|
||||||
|
.replace(&name[0..name.find(&*capture_name).unwrap()], "")
|
||||||
|
.to_string(),
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
return Ok((END_CLEAN_RE.replace(&name, "").to_string(), Vec::new()));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
let mut subgroups: Vec<LessonSubGroup> = Vec::new();
|
||||||
|
|
||||||
|
let teacher_it = TEACHER_RE.captures_iter(&teachers);
|
||||||
|
|
||||||
|
for captures in teacher_it {
|
||||||
|
subgroups.push(LessonSubGroup {
|
||||||
|
number: if let Some(capture) = captures.get(4) {
|
||||||
|
capture
|
||||||
|
.as_str()
|
||||||
|
.to_string()
|
||||||
|
.parse::<u8>()
|
||||||
|
.map_err(|_| ParseError::SubgroupIndexParsingFailed)?
|
||||||
|
} else {
|
||||||
|
0
|
||||||
|
},
|
||||||
|
cabinet: None,
|
||||||
|
teacher: format!(
|
||||||
|
"{} {}.{}.",
|
||||||
|
captures.get(1).unwrap().as_str().to_string(),
|
||||||
|
captures.get(2).unwrap().as_str().to_string(),
|
||||||
|
captures.get(3).unwrap().as_str().to_string()
|
||||||
|
),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// фикс, если у кого-то отсутствует индекс подгруппы
|
||||||
|
|
||||||
|
if subgroups.len() == 1 {
|
||||||
|
let index = subgroups[0].number;
|
||||||
|
|
||||||
|
if index == 0 {
|
||||||
|
subgroups[0].number = 1u8;
|
||||||
|
} else {
|
||||||
|
subgroups.push(LessonSubGroup {
|
||||||
|
number: if index == 1 { 2 } else { 1 },
|
||||||
|
cabinet: None,
|
||||||
|
teacher: "Только у другой".to_string(),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
} else if subgroups.len() == 2 {
|
||||||
|
// если индексы отсутствуют у обоих, ставим поочерёдно
|
||||||
|
if subgroups[0].number == 0 && subgroups[1].number == 0 {
|
||||||
|
subgroups[0].number = 1;
|
||||||
|
subgroups[1].number = 2;
|
||||||
|
}
|
||||||
|
// если индекс отсутствует у первого, ставим 2, если у второго индекс 1 и наоборот
|
||||||
|
else if subgroups[0].number == 0 {
|
||||||
|
subgroups[0].number = if subgroups[1].number == 1 { 2 } else { 1 };
|
||||||
|
}
|
||||||
|
// если индекс отсутствует у второго, ставим 2, если у первого индекс 1 и наоборот
|
||||||
|
else if subgroups[1].number == 0 {
|
||||||
|
subgroups[1].number = if subgroups[0].number == 1 { 2 } else { 1 };
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if subgroups.len() == 2 && subgroups[0].number == 2 && subgroups[1].number == 1 {
|
||||||
|
subgroups.reverse()
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok((lesson_name, subgroups))
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Конвертация списка пар групп в список пар преподавателей
|
||||||
|
fn convert_groups_to_teachers(
|
||||||
|
groups: &HashMap<String, ScheduleEntry>,
|
||||||
|
) -> HashMap<String, ScheduleEntry> {
|
||||||
|
let mut teachers: HashMap<String, ScheduleEntry> = HashMap::new();
|
||||||
|
|
||||||
|
let empty_days: Vec<Day> = groups
|
||||||
|
.values()
|
||||||
|
.next()
|
||||||
|
.unwrap()
|
||||||
|
.days
|
||||||
|
.iter()
|
||||||
|
.map(|day| Day {
|
||||||
|
name: day.name.clone(),
|
||||||
|
street: day.street.clone(),
|
||||||
|
date: day.date.clone(),
|
||||||
|
lessons: vec![],
|
||||||
|
})
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
for group in groups.values() {
|
||||||
|
for (index, day) in group.days.iter().enumerate() {
|
||||||
|
for group_lesson in &day.lessons {
|
||||||
|
if group_lesson.lesson_type == Break {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if group_lesson.subgroups.is_none() {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
let subgroups = group_lesson.subgroups.as_ref().unwrap();
|
||||||
|
|
||||||
|
for subgroup in subgroups {
|
||||||
|
if subgroup.teacher == "Ошибка в расписании" {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if !teachers.contains_key(&subgroup.teacher) {
|
||||||
|
teachers.insert(
|
||||||
|
subgroup.teacher.clone(),
|
||||||
|
ScheduleEntry {
|
||||||
|
name: subgroup.teacher.clone(),
|
||||||
|
days: empty_days.to_vec(),
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
let teacher_day = teachers
|
||||||
|
.get_mut(&subgroup.teacher)
|
||||||
|
.unwrap()
|
||||||
|
.days
|
||||||
|
.get_mut(index)
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
teacher_day.lessons.push({
|
||||||
|
let mut lesson = group_lesson.clone();
|
||||||
|
lesson.group = Some(group.name.clone());
|
||||||
|
|
||||||
|
lesson
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
teachers.iter_mut().for_each(|(_, teacher)| {
|
||||||
|
teacher.days.iter_mut().for_each(|day| {
|
||||||
|
day.lessons.sort_by(|a, b| {
|
||||||
|
a.default_range.as_ref().unwrap()[1].cmp(&b.default_range.as_ref().unwrap()[1])
|
||||||
|
})
|
||||||
|
})
|
||||||
|
});
|
||||||
|
|
||||||
|
teachers
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Чтение XLS документа из буфера и преобразование его в готовые к использованию расписания
|
||||||
|
pub fn parse_xls(buffer: &Vec<u8>) -> Result<ParseResult, ParseError> {
|
||||||
|
let cursor = Cursor::new(&buffer);
|
||||||
|
let mut workbook: Xls<_> =
|
||||||
|
open_workbook_from_rs(cursor).map_err(|e| ParseError::BadXLS(std::sync::Arc::new(e)))?;
|
||||||
|
|
||||||
|
let worksheet: WorkSheet = workbook
|
||||||
|
.worksheets()
|
||||||
|
.first()
|
||||||
|
.ok_or(ParseError::NoWorkSheets)?
|
||||||
|
.1
|
||||||
|
.to_owned();
|
||||||
|
|
||||||
|
let (days_markup, groups_markup) = parse_skeleton(&worksheet)?;
|
||||||
|
|
||||||
|
let mut groups: HashMap<String, ScheduleEntry> = HashMap::new();
|
||||||
|
let mut days_times: Vec<Vec<InternalTime>> = Vec::new();
|
||||||
|
|
||||||
|
let saturday_end_row = worksheet.end().unwrap().0;
|
||||||
|
|
||||||
|
for group_markup in groups_markup {
|
||||||
|
let mut group = ScheduleEntry {
|
||||||
|
name: group_markup.name,
|
||||||
|
days: Vec::new(),
|
||||||
|
};
|
||||||
|
|
||||||
|
for day_index in 0..(&days_markup).len() {
|
||||||
|
let day_markup = &days_markup[day_index];
|
||||||
|
|
||||||
|
let mut day = {
|
||||||
|
let space_index = day_markup.name.find(' ').unwrap();
|
||||||
|
|
||||||
|
let name = day_markup.name[..space_index].to_string();
|
||||||
|
|
||||||
|
let date_raw = day_markup.name[space_index + 1..].to_string();
|
||||||
|
let date_add = format!("{} 00:00:00", date_raw);
|
||||||
|
|
||||||
|
let date = NaiveDateTime::parse_from_str(&*date_add, "%d.%m.%Y %H:%M:%S");
|
||||||
|
|
||||||
|
Day {
|
||||||
|
name,
|
||||||
|
street: None,
|
||||||
|
date: date.unwrap().and_utc(),
|
||||||
|
lessons: Vec::new(),
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
let lesson_time_column = days_markup[0].column + 1;
|
||||||
|
|
||||||
|
let row_distance = if day_index != days_markup.len() - 1 {
|
||||||
|
days_markup[day_index + 1].row
|
||||||
|
} else {
|
||||||
|
saturday_end_row
|
||||||
|
} - day_markup.row;
|
||||||
|
|
||||||
|
if days_times.len() != 6 {
|
||||||
|
let mut day_times: Vec<InternalTime> = Vec::new();
|
||||||
|
|
||||||
|
for row in day_markup.row..(day_markup.row + row_distance) {
|
||||||
|
// time
|
||||||
|
let time_opt = get_string_from_cell(&worksheet, row, lesson_time_column);
|
||||||
|
if time_opt.is_none() {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
let time = time_opt.unwrap();
|
||||||
|
|
||||||
|
// type
|
||||||
|
let lesson_type = if time.contains("пара") {
|
||||||
|
LessonType::Default
|
||||||
|
} else {
|
||||||
|
LessonType::Additional
|
||||||
|
};
|
||||||
|
|
||||||
|
// lesson index
|
||||||
|
let default_index = if lesson_type == LessonType::Default {
|
||||||
|
Some(
|
||||||
|
time.chars()
|
||||||
|
.next()
|
||||||
|
.unwrap()
|
||||||
|
.to_string()
|
||||||
|
.parse::<u32>()
|
||||||
|
.unwrap(),
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
};
|
||||||
|
|
||||||
|
// time
|
||||||
|
let time_range = {
|
||||||
|
static TIME_RE: LazyLock<Regex, fn() -> Regex> =
|
||||||
|
LazyLock::new(|| Regex::new(r"(\d+\.\d+)-(\d+\.\d+)").unwrap());
|
||||||
|
|
||||||
|
let parse_res = TIME_RE.captures(&time).ok_or(ParseError::GlobalTime)?;
|
||||||
|
|
||||||
|
let start_match = parse_res.get(1).unwrap().as_str();
|
||||||
|
let start_parts: Vec<&str> = start_match.split(".").collect();
|
||||||
|
|
||||||
|
let end_match = parse_res.get(2).unwrap().as_str();
|
||||||
|
let end_parts: Vec<&str> = end_match.split(".").collect();
|
||||||
|
|
||||||
|
LessonTime {
|
||||||
|
start: day.date.clone()
|
||||||
|
+ Duration::hours(start_parts[0].parse().unwrap())
|
||||||
|
+ 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()),
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
day_times.push(InternalTime {
|
||||||
|
time_range,
|
||||||
|
lesson_type,
|
||||||
|
default_index,
|
||||||
|
xls_range: get_merge_from_start(&worksheet, row, lesson_time_column),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
days_times.push(day_times);
|
||||||
|
}
|
||||||
|
|
||||||
|
let day_times = &days_times[day_index];
|
||||||
|
|
||||||
|
for time in day_times {
|
||||||
|
match &mut parse_lesson(
|
||||||
|
&worksheet,
|
||||||
|
&mut day,
|
||||||
|
&day_times,
|
||||||
|
&time,
|
||||||
|
group_markup.column,
|
||||||
|
)? {
|
||||||
|
Lessons(l) => day.lessons.append(l),
|
||||||
|
Street(s) => day.street = Some(s.to_owned()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
group.days.push(day);
|
||||||
|
}
|
||||||
|
|
||||||
|
groups.insert(group.name.clone(), group);
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(ParseResult {
|
||||||
|
teachers: convert_groups_to_teachers(&groups),
|
||||||
|
groups,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
pub mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
pub fn test_result() -> Result<ParseResult, ParseError> {
|
||||||
|
parse_xls(&include_bytes!("../../schedule.xls").to_vec())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn read() {
|
||||||
|
let result = test_result();
|
||||||
|
|
||||||
|
assert!(result.is_ok());
|
||||||
|
|
||||||
|
assert_ne!(result.as_ref().unwrap().groups.len(), 0);
|
||||||
|
assert_ne!(result.as_ref().unwrap().teachers.len(), 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
162
src/parser/schema.rs
Normal file
162
src/parser/schema.rs
Normal file
@@ -0,0 +1,162 @@
|
|||||||
|
use chrono::{DateTime, Utc};
|
||||||
|
use derive_more::Display;
|
||||||
|
use serde::{Deserialize, Serialize, Serializer};
|
||||||
|
use serde_repr::{Deserialize_repr, Serialize_repr};
|
||||||
|
use std::collections::HashMap;
|
||||||
|
use std::sync::Arc;
|
||||||
|
use utoipa::ToSchema;
|
||||||
|
|
||||||
|
#[derive(Clone, Hash, Debug, Serialize, Deserialize, ToSchema)]
|
||||||
|
pub struct LessonTime {
|
||||||
|
/// Начало пары
|
||||||
|
pub start: DateTime<Utc>,
|
||||||
|
|
||||||
|
/// Конец пары
|
||||||
|
pub end: DateTime<Utc>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[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 {
|
||||||
|
/// Номер подгруппы
|
||||||
|
pub number: u8,
|
||||||
|
|
||||||
|
/// Кабинет, если присутствует
|
||||||
|
pub cabinet: Option<String>,
|
||||||
|
|
||||||
|
/// Фио преподавателя
|
||||||
|
pub teacher: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Hash, Debug, Serialize, Deserialize, ToSchema)]
|
||||||
|
#[serde(rename_all = "camelCase")]
|
||||||
|
pub struct Lesson {
|
||||||
|
/// Тип занятия
|
||||||
|
#[serde(rename = "type")]
|
||||||
|
pub lesson_type: LessonType,
|
||||||
|
|
||||||
|
/// Индексы пар, если присутствуют
|
||||||
|
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(Clone, Hash, Debug, Serialize, Deserialize, ToSchema)]
|
||||||
|
pub struct Day {
|
||||||
|
/// День недели
|
||||||
|
pub name: String,
|
||||||
|
|
||||||
|
/// Адрес другого корпуса
|
||||||
|
pub street: Option<String>,
|
||||||
|
|
||||||
|
/// Дата
|
||||||
|
pub date: DateTime<Utc>,
|
||||||
|
|
||||||
|
/// Список пар в этот день
|
||||||
|
pub lessons: Vec<Lesson>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Hash, Debug, Serialize, Deserialize, ToSchema)]
|
||||||
|
pub struct ScheduleEntry {
|
||||||
|
/// Название группы или ФИО преподавателя
|
||||||
|
pub name: String,
|
||||||
|
|
||||||
|
/// Список из шести дней
|
||||||
|
pub days: Vec<Day>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone)]
|
||||||
|
pub struct ParseResult {
|
||||||
|
/// Список групп
|
||||||
|
pub groups: HashMap<String, ScheduleEntry>,
|
||||||
|
|
||||||
|
/// Список преподавателей
|
||||||
|
pub teachers: HashMap<String, ScheduleEntry>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Display, Clone, ToSchema)]
|
||||||
|
pub enum ParseError {
|
||||||
|
/// Ошибки связанные с чтением XLS файла.
|
||||||
|
#[display("{}: Failed to read XLS file.", "_0")]
|
||||||
|
#[schema(value_type = String)]
|
||||||
|
BadXLS(Arc<calamine::XlsError>),
|
||||||
|
|
||||||
|
/// Не найдено ни одного листа
|
||||||
|
#[display("No work sheets found.")]
|
||||||
|
NoWorkSheets,
|
||||||
|
|
||||||
|
/// Отсутствуют данные об границах листа
|
||||||
|
#[display("There is no data on work sheet boundaries.")]
|
||||||
|
UnknownWorkSheetRange,
|
||||||
|
|
||||||
|
/// Не удалось прочитать начало и конец пары из строки
|
||||||
|
#[display("Failed to read lesson start and end times from string.")]
|
||||||
|
GlobalTime,
|
||||||
|
|
||||||
|
/// Не найдены начало и конец соответствующее паре
|
||||||
|
#[display("No start and end times matching the lesson was found.")]
|
||||||
|
LessonTimeNotFound,
|
||||||
|
|
||||||
|
/// Не удалось прочитать индекс подгруппы
|
||||||
|
#[display("Failed to read subgroup index.")]
|
||||||
|
SubgroupIndexParsingFailed,
|
||||||
|
}
|
||||||
|
|
||||||
|
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"),
|
||||||
|
ParseError::SubgroupIndexParsingFailed => {
|
||||||
|
serializer.serialize_str("SUBGROUP_INDEX_PARSING_FAILED")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1 +0,0 @@
|
|||||||
pub mod service_users;
|
|
||||||
@@ -1,75 +0,0 @@
|
|||||||
use self::schema::*;
|
|
||||||
use crate::{utility, AppState};
|
|
||||||
use actix_web::{post, web};
|
|
||||||
use database::entity::{ActiveServiceUser, UserType};
|
|
||||||
use database::query::Query;
|
|
||||||
use database::sea_orm::{ActiveModelTrait, Set};
|
|
||||||
use objectid::ObjectId;
|
|
||||||
use web::Json;
|
|
||||||
|
|
||||||
#[utoipa::path(responses(
|
|
||||||
(status = OK, body = Response),
|
|
||||||
))]
|
|
||||||
#[post("/create")]
|
|
||||||
pub async fn create(data_json: Json<Request>, app_state: web::Data<AppState>) -> ServiceResponse {
|
|
||||||
let service_user =
|
|
||||||
match Query::find_service_user_by_id(app_state.get_database(), &data_json.name)
|
|
||||||
.await
|
|
||||||
.expect("Failed to find service user by name")
|
|
||||||
{
|
|
||||||
Some(_) => return Err(ErrorCode::AlreadyExists).into(),
|
|
||||||
None => {
|
|
||||||
let new_user = ActiveServiceUser {
|
|
||||||
id: Set(ObjectId::new().unwrap().to_string()),
|
|
||||||
name: Set(data_json.name.clone()),
|
|
||||||
};
|
|
||||||
|
|
||||||
new_user
|
|
||||||
.insert(app_state.get_database())
|
|
||||||
.await
|
|
||||||
.expect("Failed to insert service user")
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
let access_token = utility::jwt::encode(UserType::Service, &service_user.id);
|
|
||||||
Ok(Response::new(access_token)).into()
|
|
||||||
}
|
|
||||||
|
|
||||||
mod schema {
|
|
||||||
use actix_macros::{ErrResponse, OkResponse};
|
|
||||||
use derive_more::Display;
|
|
||||||
use serde::{Deserialize, Serialize};
|
|
||||||
use utoipa::ToSchema;
|
|
||||||
|
|
||||||
#[derive(Debug, Deserialize, Serialize, ToSchema)]
|
|
||||||
#[serde(rename_all = "camelCase")]
|
|
||||||
#[schema(as = ServiceUser::Create::Request)]
|
|
||||||
pub struct Request {
|
|
||||||
/// Service username.
|
|
||||||
pub name: String,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Serialize, ToSchema, OkResponse)]
|
|
||||||
#[serde(rename_all = "camelCase")]
|
|
||||||
#[schema(as = ServiceUser::Create::Response)]
|
|
||||||
pub struct Response {
|
|
||||||
access_token: String,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Response {
|
|
||||||
pub fn new(access_token: String) -> Self {
|
|
||||||
Self { access_token }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub type ServiceResponse = crate::routes::schema::Response<Response, ErrorCode>;
|
|
||||||
|
|
||||||
#[derive(Clone, ToSchema, Display, ErrResponse, Serialize)]
|
|
||||||
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
|
|
||||||
#[status_code = "actix_web::http::StatusCode::UNAUTHORIZED"]
|
|
||||||
#[schema(as = ServiceUser::Create::ErrorCode)]
|
|
||||||
pub enum ErrorCode {
|
|
||||||
#[display("Service user with that name already exists.")]
|
|
||||||
AlreadyExists,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,3 +0,0 @@
|
|||||||
mod create;
|
|
||||||
|
|
||||||
pub use create::*;
|
|
||||||
@@ -1,8 +1,3 @@
|
|||||||
|
pub mod sign_in;
|
||||||
|
pub mod sign_up;
|
||||||
mod shared;
|
mod shared;
|
||||||
mod sign_in;
|
|
||||||
mod sign_up;
|
|
||||||
|
|
||||||
pub use sign_in::*;
|
|
||||||
pub use sign_up::*;
|
|
||||||
|
|
||||||
// TODO: change-password
|
|
||||||
|
|||||||
@@ -1,10 +1,23 @@
|
|||||||
|
use crate::utility::jwt::DEFAULT_ALGORITHM;
|
||||||
use jsonwebtoken::errors::ErrorKind;
|
use jsonwebtoken::errors::ErrorKind;
|
||||||
use jsonwebtoken::{Algorithm, DecodingKey, Validation, decode};
|
use jsonwebtoken::{decode, DecodingKey, Validation};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
use std::env;
|
||||||
|
use std::sync::LazyLock;
|
||||||
|
|
||||||
|
#[derive(Deserialize, Serialize)]
|
||||||
|
struct TokenData {
|
||||||
|
iis: String,
|
||||||
|
sub: i32,
|
||||||
|
app: i32,
|
||||||
|
exp: i32,
|
||||||
|
iat: i32,
|
||||||
|
jti: i32,
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug, Serialize, Deserialize)]
|
#[derive(Debug, Serialize, Deserialize)]
|
||||||
struct Claims {
|
struct Claims {
|
||||||
sub: i32,
|
sub: String,
|
||||||
iis: String,
|
iis: String,
|
||||||
jti: i32,
|
jti: i32,
|
||||||
app: i32,
|
app: i32,
|
||||||
@@ -12,7 +25,7 @@ struct Claims {
|
|||||||
|
|
||||||
#[derive(Debug, PartialEq)]
|
#[derive(Debug, PartialEq)]
|
||||||
pub enum Error {
|
pub enum Error {
|
||||||
Jwt(ErrorKind),
|
JwtError(ErrorKind),
|
||||||
InvalidSignature,
|
InvalidSignature,
|
||||||
InvalidToken,
|
InvalidToken,
|
||||||
Expired,
|
Expired,
|
||||||
@@ -39,10 +52,17 @@ const VK_PUBLIC_KEY: &str = concat!(
|
|||||||
"-----END PUBLIC KEY-----"
|
"-----END PUBLIC KEY-----"
|
||||||
);
|
);
|
||||||
|
|
||||||
pub fn parse_vk_id(token_str: &str, client_id: i32) -> Result<i32, Error> {
|
static VK_ID_CLIENT_ID: LazyLock<i32> = LazyLock::new(|| {
|
||||||
|
env::var("VK_ID_CLIENT_ID")
|
||||||
|
.expect("VK_ID_CLIENT_ID must be set")
|
||||||
|
.parse::<i32>()
|
||||||
|
.expect("VK_ID_CLIENT_ID must be i32")
|
||||||
|
});
|
||||||
|
|
||||||
|
pub fn parse_vk_id(token_str: &String) -> Result<i32, Error> {
|
||||||
let dkey = DecodingKey::from_rsa_pem(VK_PUBLIC_KEY.as_bytes()).unwrap();
|
let dkey = DecodingKey::from_rsa_pem(VK_PUBLIC_KEY.as_bytes()).unwrap();
|
||||||
|
|
||||||
match decode::<Claims>(token_str, &dkey, &Validation::new(Algorithm::RS256)) {
|
match decode::<Claims>(&token_str, &dkey, &Validation::new(DEFAULT_ALGORITHM)) {
|
||||||
Ok(token_data) => {
|
Ok(token_data) => {
|
||||||
let claims = token_data.claims;
|
let claims = token_data.claims;
|
||||||
|
|
||||||
@@ -50,10 +70,13 @@ pub fn parse_vk_id(token_str: &str, client_id: i32) -> Result<i32, Error> {
|
|||||||
Err(Error::UnknownIssuer(claims.iis))
|
Err(Error::UnknownIssuer(claims.iis))
|
||||||
} else if claims.jti != 21 {
|
} else if claims.jti != 21 {
|
||||||
Err(Error::UnknownType(claims.jti))
|
Err(Error::UnknownType(claims.jti))
|
||||||
} else if claims.app != client_id {
|
} else if claims.app != *VK_ID_CLIENT_ID {
|
||||||
Err(Error::UnknownClientId(claims.app))
|
Err(Error::UnknownClientId(claims.app))
|
||||||
} else {
|
} else {
|
||||||
Ok(claims.sub)
|
match claims.sub.parse::<i32>() {
|
||||||
|
Ok(sub) => Ok(sub),
|
||||||
|
Err(_) => Err(Error::InvalidToken),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Err(err) => Err(match err.into_kind() {
|
Err(err) => Err(match err.into_kind() {
|
||||||
@@ -67,7 +90,7 @@ pub fn parse_vk_id(token_str: &str, client_id: i32) -> Result<i32, Error> {
|
|||||||
ErrorKind::Base64(_) => Error::InvalidToken,
|
ErrorKind::Base64(_) => Error::InvalidToken,
|
||||||
ErrorKind::Json(_) => Error::InvalidToken,
|
ErrorKind::Json(_) => Error::InvalidToken,
|
||||||
ErrorKind::Utf8(_) => Error::InvalidToken,
|
ErrorKind::Utf8(_) => Error::InvalidToken,
|
||||||
kind => Error::Jwt(kind),
|
kind => Error::JwtError(kind),
|
||||||
}),
|
}),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,35 +1,29 @@
|
|||||||
use self::schema::*;
|
use self::schema::*;
|
||||||
|
use crate::database::driver;
|
||||||
|
use crate::database::models::User;
|
||||||
use crate::routes::auth::shared::parse_vk_id;
|
use crate::routes::auth::shared::parse_vk_id;
|
||||||
use crate::routes::auth::sign_in::schema::SignInData::{Default, VkOAuth};
|
use crate::routes::auth::sign_in::schema::SignInData::{Default, Vk};
|
||||||
use crate::routes::schema::user::UserResponse;
|
use crate::routes::schema::user::UserResponse;
|
||||||
use crate::routes::schema::ResponseError;
|
use crate::routes::schema::{IntoResponseAsError, ResponseError};
|
||||||
use crate::{utility, AppState};
|
use crate::{utility, AppState};
|
||||||
use actix_web::{post, web};
|
use actix_web::{post, web};
|
||||||
use database::query::Query;
|
use diesel::SaveChangesDsl;
|
||||||
|
use std::ops::DerefMut;
|
||||||
use web::Json;
|
use web::Json;
|
||||||
use database::entity::UserType;
|
|
||||||
|
|
||||||
async fn sign_in_combined(
|
async fn sign_in(
|
||||||
data: SignInData,
|
data: SignInData,
|
||||||
app_state: &web::Data<AppState>,
|
app_state: &web::Data<AppState>,
|
||||||
) -> Result<UserResponse, ErrorCode> {
|
) -> Result<UserResponse, ErrorCode> {
|
||||||
let db = app_state.get_database();
|
|
||||||
|
|
||||||
let user = match &data {
|
let user = match &data {
|
||||||
Default(data) => Query::find_user_by_username(db, &data.username).await,
|
Default(data) => driver::users::get_by_username(&app_state.database, &data.username),
|
||||||
VkOAuth(id) => Query::find_user_by_vk_id(db, *id).await,
|
Vk(id) => driver::users::get_by_vk_id(&app_state.database, *id),
|
||||||
}
|
};
|
||||||
.ok()
|
|
||||||
.flatten();
|
|
||||||
|
|
||||||
match user {
|
match user {
|
||||||
Some(user) => {
|
Ok(mut user) => {
|
||||||
if let Default(data) = data {
|
if let Default(data) = data {
|
||||||
if user.password.is_none() {
|
match bcrypt::verify(&data.password, &user.password) {
|
||||||
return Err(ErrorCode::IncorrectCredentials);
|
|
||||||
}
|
|
||||||
|
|
||||||
match bcrypt::verify(&data.password, user.password.as_ref().unwrap()) {
|
|
||||||
Ok(result) => {
|
Ok(result) => {
|
||||||
if !result {
|
if !result {
|
||||||
return Err(ErrorCode::IncorrectCredentials);
|
return Err(ErrorCode::IncorrectCredentials);
|
||||||
@@ -41,11 +35,18 @@ async fn sign_in_combined(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let access_token = utility::jwt::encode(UserType::Default, &user.id);
|
let mut lock = app_state.connection();
|
||||||
Ok(UserResponse::from_user_with_token(user, access_token))
|
let conn = lock.deref_mut();
|
||||||
|
|
||||||
|
user.access_token = utility::jwt::encode(&user.id);
|
||||||
|
|
||||||
|
user.save_changes::<User>(conn)
|
||||||
|
.expect("Failed to update user");
|
||||||
|
|
||||||
|
Ok(user.into())
|
||||||
}
|
}
|
||||||
|
|
||||||
None => Err(ErrorCode::IncorrectCredentials),
|
Err(_) => Err(ErrorCode::IncorrectCredentials),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -54,10 +55,8 @@ async fn sign_in_combined(
|
|||||||
(status = NOT_ACCEPTABLE, body = ResponseError<ErrorCode>)
|
(status = NOT_ACCEPTABLE, body = ResponseError<ErrorCode>)
|
||||||
))]
|
))]
|
||||||
#[post("/sign-in")]
|
#[post("/sign-in")]
|
||||||
pub async fn sign_in(data: Json<Request>, app_state: web::Data<AppState>) -> ServiceResponse {
|
pub async fn sign_in_default(data: Json<Request>, app_state: web::Data<AppState>) -> ServiceResponse {
|
||||||
sign_in_combined(Default(data.into_inner()), &app_state)
|
sign_in(Default(data.into_inner()), &app_state).await.into()
|
||||||
.await
|
|
||||||
.into()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[utoipa::path(responses(
|
#[utoipa::path(responses(
|
||||||
@@ -65,34 +64,29 @@ pub async fn sign_in(data: Json<Request>, app_state: web::Data<AppState>) -> Ser
|
|||||||
(status = NOT_ACCEPTABLE, body = ResponseError<ErrorCode>)
|
(status = NOT_ACCEPTABLE, body = ResponseError<ErrorCode>)
|
||||||
))]
|
))]
|
||||||
#[post("/sign-in-vk")]
|
#[post("/sign-in-vk")]
|
||||||
pub async fn sign_in_vk(
|
pub async fn sign_in_vk(data_json: Json<vk::Request>, app_state: web::Data<AppState>) -> ServiceResponse {
|
||||||
data_json: Json<vk::Request>,
|
|
||||||
app_state: web::Data<AppState>,
|
|
||||||
) -> ServiceResponse {
|
|
||||||
let data = data_json.into_inner();
|
let data = data_json.into_inner();
|
||||||
|
|
||||||
match parse_vk_id(&data.access_token, app_state.get_env().vk_id.client_id) {
|
match parse_vk_id(&data.access_token) {
|
||||||
Ok(id) => sign_in_combined(VkOAuth(id), &app_state).await,
|
Ok(id) => sign_in(Vk(id), &app_state).await.into(),
|
||||||
Err(_) => Err(ErrorCode::InvalidVkAccessToken),
|
Err(_) => ErrorCode::InvalidVkAccessToken.into_response(),
|
||||||
}
|
}
|
||||||
.into()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
mod schema {
|
mod schema {
|
||||||
use crate::routes::schema::user::UserResponse;
|
use crate::routes::schema::user::UserResponse;
|
||||||
use actix_macros::ErrResponse;
|
use actix_macros::{IntoResponseError, StatusCode};
|
||||||
use derive_more::Display;
|
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use utoipa::ToSchema;
|
use utoipa::ToSchema;
|
||||||
|
|
||||||
#[derive(Deserialize, Serialize, ToSchema)]
|
#[derive(Deserialize, Serialize, ToSchema)]
|
||||||
#[schema(as = SignIn::Request)]
|
#[schema(as = SignIn::Request)]
|
||||||
pub struct Request {
|
pub struct Request {
|
||||||
/// User name.
|
/// Имя пользователя
|
||||||
#[schema(examples("n08i40k"))]
|
#[schema(examples("n08i40k"))]
|
||||||
pub username: String,
|
pub username: String,
|
||||||
|
|
||||||
/// Password.
|
/// Пароль
|
||||||
pub password: String,
|
pub password: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -104,55 +98,55 @@ mod schema {
|
|||||||
#[serde(rename_all = "camelCase")]
|
#[serde(rename_all = "camelCase")]
|
||||||
#[schema(as = SignInVk::Request)]
|
#[schema(as = SignInVk::Request)]
|
||||||
pub struct Request {
|
pub struct Request {
|
||||||
/// VK ID token.
|
/// Токен VK ID
|
||||||
pub access_token: String,
|
pub access_token: String,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub type ServiceResponse = crate::routes::schema::Response<UserResponse, ErrorCode>;
|
pub type ServiceResponse = crate::routes::schema::Response<UserResponse, ErrorCode>;
|
||||||
|
|
||||||
#[derive(Clone, Serialize, Display, ToSchema, ErrResponse)]
|
#[derive(Serialize, ToSchema, Clone, IntoResponseError, StatusCode)]
|
||||||
#[schema(as = SignIn::ErrorCode)]
|
|
||||||
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
|
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
|
||||||
|
#[schema(as = SignIn::ErrorCode)]
|
||||||
#[status_code = "actix_web::http::StatusCode::NOT_ACCEPTABLE"]
|
#[status_code = "actix_web::http::StatusCode::NOT_ACCEPTABLE"]
|
||||||
pub enum ErrorCode {
|
pub enum ErrorCode {
|
||||||
/// Incorrect username or password.
|
/// Некорректное имя пользователя или пароль
|
||||||
#[display("Incorrect username or password.")]
|
|
||||||
IncorrectCredentials,
|
IncorrectCredentials,
|
||||||
|
|
||||||
/// Invalid VK ID token.
|
/// Недействительный токен VK ID
|
||||||
#[display("Invalid VK ID token.")]
|
|
||||||
InvalidVkAccessToken,
|
InvalidVkAccessToken,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Type of authorization.
|
/// Internal
|
||||||
pub enum SignInData {
|
|
||||||
/// User and password name and password.
|
|
||||||
Default(Request),
|
|
||||||
|
|
||||||
/// Identifier of the attached account VK.
|
/// Тип авторизации
|
||||||
VkOAuth(i32),
|
pub enum SignInData {
|
||||||
|
/// Имя пользователя и пароль
|
||||||
|
Default(Request),
|
||||||
|
|
||||||
|
/// Идентификатор привязанного аккаунта VK
|
||||||
|
Vk(i32),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::schema::*;
|
use super::schema::*;
|
||||||
use crate::routes::auth::sign_in::sign_in;
|
use crate::database::driver;
|
||||||
|
use crate::database::models::{User, UserRole};
|
||||||
|
use crate::routes::auth::sign_in::sign_in_default;
|
||||||
use crate::test_env::tests::{static_app_state, test_app_state, test_env};
|
use crate::test_env::tests::{static_app_state, test_app_state, test_env};
|
||||||
|
use crate::utility;
|
||||||
use actix_test::test_app;
|
use actix_test::test_app;
|
||||||
use actix_web::dev::ServiceResponse;
|
use actix_web::dev::ServiceResponse;
|
||||||
use actix_web::http::Method;
|
use actix_web::http::Method;
|
||||||
use actix_web::http::StatusCode;
|
use actix_web::http::StatusCode;
|
||||||
use actix_web::test;
|
use actix_web::test;
|
||||||
use database::entity::sea_orm_active_enums::UserRole;
|
|
||||||
use database::entity::ActiveUser;
|
|
||||||
use database::sea_orm::{ActiveModelTrait, Set};
|
|
||||||
use sha1::{Digest, Sha1};
|
use sha1::{Digest, Sha1};
|
||||||
use std::fmt::Write;
|
use std::fmt::Write;
|
||||||
|
|
||||||
async fn sign_in_client(data: Request) -> ServiceResponse {
|
async fn sign_in_client(data: Request) -> ServiceResponse {
|
||||||
let app = test_app(test_app_state().await, sign_in).await;
|
let app = test_app(test_app_state(), sign_in_default).await;
|
||||||
|
|
||||||
let req = test::TestRequest::with_uri("/sign-in")
|
let req = test::TestRequest::with_uri("/sign-in")
|
||||||
.method(Method::POST)
|
.method(Method::POST)
|
||||||
@@ -162,7 +156,7 @@ mod tests {
|
|||||||
test::call_service(&app, req).await
|
test::call_service(&app, req).await
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn prepare(username: String) {
|
fn prepare(username: String) {
|
||||||
let id = {
|
let id = {
|
||||||
let mut sha = Sha1::new();
|
let mut sha = Sha1::new();
|
||||||
sha.update(&username);
|
sha.update(&username);
|
||||||
@@ -180,28 +174,26 @@ mod tests {
|
|||||||
|
|
||||||
test_env();
|
test_env();
|
||||||
|
|
||||||
let app_state = static_app_state().await;
|
let app_state = static_app_state();
|
||||||
|
driver::users::insert_or_ignore(
|
||||||
let active_user = ActiveUser {
|
&app_state.database,
|
||||||
id: Set(id.clone()),
|
&User {
|
||||||
username: Set(username),
|
id: id.clone(),
|
||||||
password: Set(Some(bcrypt::hash("example", bcrypt::DEFAULT_COST).unwrap())),
|
username,
|
||||||
vk_id: Set(None),
|
password: bcrypt::hash("example".to_string(), bcrypt::DEFAULT_COST).unwrap(),
|
||||||
telegram_id: Set(None),
|
vk_id: None,
|
||||||
group: Set(Some("ИС-214/23".to_string())),
|
access_token: utility::jwt::encode(&id),
|
||||||
role: Set(UserRole::Student),
|
group: "ИС-214/23".to_string(),
|
||||||
android_version: Set(None),
|
role: UserRole::Student,
|
||||||
};
|
version: "1.0.0".to_string(),
|
||||||
|
},
|
||||||
active_user
|
)
|
||||||
.save(app_state.get_database())
|
.unwrap();
|
||||||
.await
|
|
||||||
.expect("Failed to save user");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[actix_web::test]
|
#[actix_web::test]
|
||||||
async fn sign_in_ok() {
|
async fn sign_in_ok() {
|
||||||
prepare("test::sign_in_ok".to_string()).await;
|
prepare("test::sign_in_ok".to_string());
|
||||||
|
|
||||||
let resp = sign_in_client(Request {
|
let resp = sign_in_client(Request {
|
||||||
username: "test::sign_in_ok".to_string(),
|
username: "test::sign_in_ok".to_string(),
|
||||||
@@ -214,7 +206,7 @@ mod tests {
|
|||||||
|
|
||||||
#[actix_web::test]
|
#[actix_web::test]
|
||||||
async fn sign_in_err() {
|
async fn sign_in_err() {
|
||||||
prepare("test::sign_in_err".to_string()).await;
|
prepare("test::sign_in_err".to_string());
|
||||||
|
|
||||||
let invalid_username = sign_in_client(Request {
|
let invalid_username = sign_in_client(Request {
|
||||||
username: "test::sign_in_err::username".to_string(),
|
username: "test::sign_in_err::username".to_string(),
|
||||||
|
|||||||
@@ -1,16 +1,15 @@
|
|||||||
use self::schema::*;
|
use self::schema::*;
|
||||||
use crate::routes::auth::shared::parse_vk_id;
|
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::user::UserResponse;
|
||||||
use crate::routes::schema::ResponseError;
|
use crate::routes::schema::{IntoResponseAsError, ResponseError};
|
||||||
use crate::{utility, AppState};
|
|
||||||
use actix_web::{post, web};
|
use actix_web::{post, web};
|
||||||
use database::entity::sea_orm_active_enums::UserRole;
|
use rand::{Rng, rng};
|
||||||
use database::entity::{ActiveUser, UserType};
|
|
||||||
use database::query::Query;
|
|
||||||
use database::sea_orm::ActiveModelTrait;
|
|
||||||
use web::Json;
|
use web::Json;
|
||||||
|
|
||||||
async fn sign_up_combined(
|
async fn sign_up(
|
||||||
data: SignUpData,
|
data: SignUpData,
|
||||||
app_state: &web::Data<AppState>,
|
app_state: &web::Data<AppState>,
|
||||||
) -> Result<UserResponse, ErrorCode> {
|
) -> Result<UserResponse, ErrorCode> {
|
||||||
@@ -19,41 +18,31 @@ async fn sign_up_combined(
|
|||||||
return Err(ErrorCode::DisallowedRole);
|
return Err(ErrorCode::DisallowedRole);
|
||||||
}
|
}
|
||||||
|
|
||||||
if !app_state
|
// If specified group doesn't exist in schedule.
|
||||||
.get_schedule_snapshot("eng_polytechnic")
|
let schedule_opt = app_state.schedule.lock().unwrap();
|
||||||
.await
|
|
||||||
.unwrap()
|
if let Some(schedule) = &*schedule_opt {
|
||||||
.data
|
if !schedule.data.groups.contains_key(&data.group) {
|
||||||
.groups
|
return Err(ErrorCode::InvalidGroupName);
|
||||||
.contains_key(&data.group)
|
}
|
||||||
{
|
|
||||||
return Err(ErrorCode::InvalidGroupName);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let db = app_state.get_database();
|
// If user with specified username already exists.
|
||||||
|
if driver::users::contains_by_username(&app_state.database, &data.username) {
|
||||||
// If user with specified username already exists.O
|
|
||||||
if Query::find_user_by_username(db, &data.username)
|
|
||||||
.await
|
|
||||||
.is_ok_and(|user| user.is_some())
|
|
||||||
{
|
|
||||||
return Err(ErrorCode::UsernameAlreadyExists);
|
return Err(ErrorCode::UsernameAlreadyExists);
|
||||||
}
|
}
|
||||||
|
|
||||||
// If user with specified VKID already exists.
|
// If user with specified VKID already exists.
|
||||||
if let Some(id) = data.vk_id
|
if let Some(id) = data.vk_id {
|
||||||
&& Query::is_user_exists_by_vk_id(db, id)
|
if driver::users::contains_by_vk_id(&app_state.database, id) {
|
||||||
.await
|
return Err(ErrorCode::VkAlreadyExists);
|
||||||
.expect("Failed to check user existence")
|
}
|
||||||
{
|
|
||||||
return Err(ErrorCode::VkAlreadyExists);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let active_user: ActiveUser = data.into();
|
let user = data.into();
|
||||||
let user = active_user.insert(db).await.unwrap();
|
driver::users::insert(&app_state.database, &user).unwrap();
|
||||||
let access_token = utility::jwt::encode(UserType::Default, &user.id);
|
|
||||||
|
|
||||||
Ok(UserResponse::from_user_with_token(user, access_token))
|
Ok(UserResponse::from(&user)).into()
|
||||||
}
|
}
|
||||||
|
|
||||||
#[utoipa::path(responses(
|
#[utoipa::path(responses(
|
||||||
@@ -61,13 +50,16 @@ async fn sign_up_combined(
|
|||||||
(status = NOT_ACCEPTABLE, body = ResponseError<ErrorCode>)
|
(status = NOT_ACCEPTABLE, body = ResponseError<ErrorCode>)
|
||||||
))]
|
))]
|
||||||
#[post("/sign-up")]
|
#[post("/sign-up")]
|
||||||
pub async fn sign_up(data_json: Json<Request>, app_state: web::Data<AppState>) -> ServiceResponse {
|
pub async fn sign_up_default(
|
||||||
|
data_json: Json<Request>,
|
||||||
|
app_state: web::Data<AppState>,
|
||||||
|
) -> ServiceResponse {
|
||||||
let data = data_json.into_inner();
|
let data = data_json.into_inner();
|
||||||
|
|
||||||
sign_up_combined(
|
sign_up(
|
||||||
SignUpData {
|
SignUpData {
|
||||||
username: data.username,
|
username: data.username,
|
||||||
password: Some(data.password),
|
password: data.password,
|
||||||
vk_id: None,
|
vk_id: None,
|
||||||
group: data.group,
|
group: data.group,
|
||||||
role: data.role,
|
role: data.role,
|
||||||
@@ -90,33 +82,40 @@ pub async fn sign_up_vk(
|
|||||||
) -> ServiceResponse {
|
) -> ServiceResponse {
|
||||||
let data = data_json.into_inner();
|
let data = data_json.into_inner();
|
||||||
|
|
||||||
match parse_vk_id(&data.access_token, app_state.get_env().vk_id.client_id) {
|
match parse_vk_id(&data.access_token) {
|
||||||
Ok(id) => {
|
Ok(id) => sign_up(
|
||||||
sign_up_combined(
|
SignUpData {
|
||||||
SignUpData {
|
username: data.username,
|
||||||
username: data.username,
|
password: rng()
|
||||||
password: None,
|
.sample_iter(&rand::distr::Alphanumeric)
|
||||||
vk_id: Some(id),
|
.take(16)
|
||||||
group: data.group,
|
.map(char::from)
|
||||||
role: data.role,
|
.collect(),
|
||||||
version: data.version,
|
vk_id: Some(id),
|
||||||
},
|
group: data.group,
|
||||||
&app_state,
|
role: data.role,
|
||||||
)
|
version: data.version,
|
||||||
.await
|
},
|
||||||
|
&app_state,
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
.into(),
|
||||||
|
Err(err) => {
|
||||||
|
if err != Error::Expired {
|
||||||
|
eprintln!("Failed to parse vk id token!");
|
||||||
|
eprintln!("{:?}", err);
|
||||||
|
}
|
||||||
|
|
||||||
|
ErrorCode::InvalidVkAccessToken.into_response()
|
||||||
}
|
}
|
||||||
Err(_) => Err(ErrorCode::InvalidVkAccessToken),
|
|
||||||
}
|
}
|
||||||
.into()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
mod schema {
|
mod schema {
|
||||||
|
use crate::database::models::{User, UserRole};
|
||||||
use crate::routes::schema::user::UserResponse;
|
use crate::routes::schema::user::UserResponse;
|
||||||
use actix_macros::ErrResponse;
|
use crate::utility;
|
||||||
use database::entity::sea_orm_active_enums::UserRole;
|
use actix_macros::{IntoResponseError, StatusCode};
|
||||||
use database::entity::ActiveUser;
|
|
||||||
use database::sea_orm::Set;
|
|
||||||
use derive_more::Display;
|
|
||||||
use objectid::ObjectId;
|
use objectid::ObjectId;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
@@ -125,48 +124,48 @@ mod schema {
|
|||||||
#[derive(Serialize, Deserialize, utoipa::ToSchema)]
|
#[derive(Serialize, Deserialize, utoipa::ToSchema)]
|
||||||
#[schema(as = SignUp::Request)]
|
#[schema(as = SignUp::Request)]
|
||||||
pub struct Request {
|
pub struct Request {
|
||||||
/// User name.
|
/// Имя пользователя
|
||||||
#[schema(examples("n08i40k"))]
|
#[schema(examples("n08i40k"))]
|
||||||
pub username: String,
|
pub username: String,
|
||||||
|
|
||||||
/// Password.
|
/// Пароль
|
||||||
pub password: String,
|
pub password: String,
|
||||||
|
|
||||||
/// Group.
|
/// Группа
|
||||||
#[schema(examples("ИС-214/23"))]
|
#[schema(examples("ИС-214/23"))]
|
||||||
pub group: String,
|
pub group: String,
|
||||||
|
|
||||||
/// Role.
|
/// Роль
|
||||||
pub role: UserRole,
|
pub role: UserRole,
|
||||||
|
|
||||||
/// Version of the installed Polytechnic+ application.
|
/// Версия установленного приложения Polytechnic+
|
||||||
#[schema(examples("3.0.0"))]
|
#[schema(examples("3.0.0"))]
|
||||||
pub version: String,
|
pub version: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub mod vk {
|
pub mod vk {
|
||||||
use database::entity::sea_orm_active_enums::UserRole;
|
use crate::database::models::UserRole;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, utoipa::ToSchema)]
|
#[derive(Serialize, Deserialize, utoipa::ToSchema)]
|
||||||
#[serde(rename_all = "camelCase")]
|
#[serde(rename_all = "camelCase")]
|
||||||
#[schema(as = SignUpVk::Request)]
|
#[schema(as = SignUpVk::Request)]
|
||||||
pub struct Request {
|
pub struct Request {
|
||||||
/// VK ID token.
|
/// Токен VK ID
|
||||||
pub access_token: String,
|
pub access_token: String,
|
||||||
|
|
||||||
/// User name.
|
/// Имя пользователя
|
||||||
#[schema(examples("n08i40k"))]
|
#[schema(examples("n08i40k"))]
|
||||||
pub username: String,
|
pub username: String,
|
||||||
|
|
||||||
/// Group.
|
/// Группа
|
||||||
#[schema(examples("ИС-214/23"))]
|
#[schema(examples("ИС-214/23"))]
|
||||||
pub group: String,
|
pub group: String,
|
||||||
|
|
||||||
/// Role.
|
/// Роль
|
||||||
pub role: UserRole,
|
pub role: UserRole,
|
||||||
|
|
||||||
/// Version of the installed Polytechnic+ application.
|
/// Версия установленного приложения Polytechnic+
|
||||||
#[schema(examples("3.0.0"))]
|
#[schema(examples("3.0.0"))]
|
||||||
pub version: String,
|
pub version: String,
|
||||||
}
|
}
|
||||||
@@ -174,71 +173,66 @@ mod schema {
|
|||||||
|
|
||||||
pub type ServiceResponse = crate::routes::schema::Response<UserResponse, ErrorCode>;
|
pub type ServiceResponse = crate::routes::schema::Response<UserResponse, ErrorCode>;
|
||||||
|
|
||||||
#[derive(Clone, Serialize, Display, utoipa::ToSchema, ErrResponse)]
|
#[derive(Clone, Serialize, utoipa::ToSchema, IntoResponseError, StatusCode)]
|
||||||
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
|
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
|
||||||
#[schema(as = SignUp::ErrorCode)]
|
#[schema(as = SignUp::ErrorCode)]
|
||||||
#[status_code = "actix_web::http::StatusCode::NOT_ACCEPTABLE"]
|
#[status_code = "actix_web::http::StatusCode::NOT_ACCEPTABLE"]
|
||||||
pub enum ErrorCode {
|
pub enum ErrorCode {
|
||||||
/// Conveyed the role of Admin.
|
/// Передана роль ADMIN
|
||||||
#[display("Conveyed the role of Admin.")]
|
|
||||||
DisallowedRole,
|
DisallowedRole,
|
||||||
|
|
||||||
/// Unknown name of the group.
|
/// Неизвестное название группы
|
||||||
#[display("Unknown name of the group.")]
|
|
||||||
InvalidGroupName,
|
InvalidGroupName,
|
||||||
|
|
||||||
/// User with this name is already registered.
|
/// Пользователь с таким именем уже зарегистрирован
|
||||||
#[display("User with this name is already registered.")]
|
|
||||||
UsernameAlreadyExists,
|
UsernameAlreadyExists,
|
||||||
|
|
||||||
/// Invalid VK ID token.
|
/// Недействительный токен VK ID
|
||||||
#[display("Invalid VK ID token.")]
|
|
||||||
InvalidVkAccessToken,
|
InvalidVkAccessToken,
|
||||||
|
|
||||||
/// User with such an account VK is already registered.
|
/// Пользователь с таким аккаунтом VK уже зарегистрирован
|
||||||
#[display("User with such an account VK is already registered.")]
|
|
||||||
VkAlreadyExists,
|
VkAlreadyExists,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Data for registration.
|
/// Internal
|
||||||
|
|
||||||
|
/// Данные для регистрации
|
||||||
pub struct SignUpData {
|
pub struct SignUpData {
|
||||||
// TODO: сделать ограничение на минимальную и максимальную длину при регистрации и смене.
|
/// Имя пользователя
|
||||||
/// User name.
|
|
||||||
pub username: String,
|
pub username: String,
|
||||||
|
|
||||||
/// Password.
|
/// Пароль
|
||||||
///
|
///
|
||||||
/// Should be present even if registration occurs using the VK ID token.
|
/// Должен присутствовать даже если регистрация происходит с помощью токена VK ID
|
||||||
pub password: Option<String>,
|
pub password: String,
|
||||||
|
|
||||||
/// Account identifier VK.
|
/// Идентификатор аккаунта VK
|
||||||
pub vk_id: Option<i32>,
|
pub vk_id: Option<i32>,
|
||||||
|
|
||||||
/// Group.
|
/// Группа
|
||||||
pub group: String,
|
pub group: String,
|
||||||
|
|
||||||
/// Role.
|
/// Роль
|
||||||
pub role: UserRole,
|
pub role: UserRole,
|
||||||
|
|
||||||
/// Version of the installed Polytechnic+ application.
|
/// Версия установленного приложения Polytechnic+
|
||||||
pub version: String,
|
pub version: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<SignUpData> for ActiveUser {
|
impl Into<User> for SignUpData {
|
||||||
fn from(value: SignUpData) -> Self {
|
fn into(self) -> User {
|
||||||
assert_ne!(value.password.is_some(), value.vk_id.is_some());
|
let id = ObjectId::new().unwrap().to_string();
|
||||||
|
let access_token = utility::jwt::encode(&id);
|
||||||
|
|
||||||
ActiveUser {
|
User {
|
||||||
id: Set(ObjectId::new().unwrap().to_string()),
|
id,
|
||||||
username: Set(value.username),
|
username: self.username,
|
||||||
password: Set(value
|
password: bcrypt::hash(self.password, bcrypt::DEFAULT_COST).unwrap(),
|
||||||
.password
|
vk_id: self.vk_id,
|
||||||
.map(|x| bcrypt::hash(x, bcrypt::DEFAULT_COST).unwrap())),
|
access_token,
|
||||||
vk_id: Set(value.vk_id),
|
group: self.group,
|
||||||
telegram_id: Set(None),
|
role: self.role,
|
||||||
group: Set(Some(value.group)),
|
version: self.version,
|
||||||
role: Set(value.role),
|
|
||||||
android_version: Set(Some(value.version)),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -246,34 +240,32 @@ mod schema {
|
|||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
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::schema::Request;
|
||||||
use crate::routes::auth::sign_up::sign_up;
|
use crate::routes::auth::sign_up::sign_up_default;
|
||||||
use crate::test_env::tests::{static_app_state, test_app_state, test_env};
|
use crate::test_env::tests::{static_app_state, test_app_state, test_env};
|
||||||
use actix_test::test_app;
|
use actix_test::test_app;
|
||||||
use actix_web::dev::ServiceResponse;
|
use actix_web::dev::ServiceResponse;
|
||||||
use actix_web::http::Method;
|
use actix_web::http::Method;
|
||||||
use actix_web::http::StatusCode;
|
use actix_web::http::StatusCode;
|
||||||
use actix_web::test;
|
use actix_web::test;
|
||||||
use database::entity::sea_orm_active_enums::UserRole;
|
|
||||||
use database::entity::{UserColumn, UserEntity};
|
|
||||||
use database::sea_orm::ColumnTrait;
|
|
||||||
use database::sea_orm::{EntityTrait, QueryFilter};
|
|
||||||
|
|
||||||
struct SignUpPartial<'a> {
|
struct SignUpPartial {
|
||||||
username: &'a str,
|
username: String,
|
||||||
group: &'a str,
|
group: String,
|
||||||
role: UserRole,
|
role: UserRole,
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn sign_up_client(data: SignUpPartial<'_>) -> ServiceResponse {
|
async fn sign_up_client(data: SignUpPartial) -> ServiceResponse {
|
||||||
let app = test_app(test_app_state().await, sign_up).await;
|
let app = test_app(test_app_state(), sign_up_default).await;
|
||||||
|
|
||||||
let req = test::TestRequest::with_uri("/sign-up")
|
let req = test::TestRequest::with_uri("/sign-up")
|
||||||
.method(Method::POST)
|
.method(Method::POST)
|
||||||
.set_json(Request {
|
.set_json(Request {
|
||||||
username: data.username.to_string(),
|
username: data.username.clone(),
|
||||||
password: "example".to_string(),
|
password: "example".to_string(),
|
||||||
group: data.group.to_string(),
|
group: data.group.clone(),
|
||||||
role: data.role.clone(),
|
role: data.role.clone(),
|
||||||
version: "1.0.0".to_string(),
|
version: "1.0.0".to_string(),
|
||||||
})
|
})
|
||||||
@@ -288,19 +280,14 @@ mod tests {
|
|||||||
|
|
||||||
test_env();
|
test_env();
|
||||||
|
|
||||||
let app_state = static_app_state().await;
|
let app_state = static_app_state();
|
||||||
|
driver::users::delete_by_username(&app_state.database, &"test::sign_up_valid".to_string());
|
||||||
UserEntity::delete_many()
|
|
||||||
.filter(UserColumn::Username.eq("test::sign_up_valid"))
|
|
||||||
.exec(app_state.get_database())
|
|
||||||
.await
|
|
||||||
.expect("Failed to delete user");
|
|
||||||
|
|
||||||
// test
|
// test
|
||||||
|
|
||||||
let resp = sign_up_client(SignUpPartial {
|
let resp = sign_up_client(SignUpPartial {
|
||||||
username: "test::sign_up_valid",
|
username: "test::sign_up_valid".to_string(),
|
||||||
group: "ИС-214/23",
|
group: "ИС-214/23".to_string(),
|
||||||
role: UserRole::Student,
|
role: UserRole::Student,
|
||||||
})
|
})
|
||||||
.await;
|
.await;
|
||||||
@@ -314,17 +301,15 @@ mod tests {
|
|||||||
|
|
||||||
test_env();
|
test_env();
|
||||||
|
|
||||||
let app_state = static_app_state().await;
|
let app_state = static_app_state();
|
||||||
|
driver::users::delete_by_username(
|
||||||
UserEntity::delete_many()
|
&app_state.database,
|
||||||
.filter(UserColumn::Username.eq("test::sign_up_multiple"))
|
&"test::sign_up_multiple".to_string(),
|
||||||
.exec(app_state.get_database())
|
);
|
||||||
.await
|
|
||||||
.expect("Failed to delete user");
|
|
||||||
|
|
||||||
let create = sign_up_client(SignUpPartial {
|
let create = sign_up_client(SignUpPartial {
|
||||||
username: "test::sign_up_multiple",
|
username: "test::sign_up_multiple".to_string(),
|
||||||
group: "ИС-214/23",
|
group: "ИС-214/23".to_string(),
|
||||||
role: UserRole::Student,
|
role: UserRole::Student,
|
||||||
})
|
})
|
||||||
.await;
|
.await;
|
||||||
@@ -332,8 +317,8 @@ mod tests {
|
|||||||
assert_eq!(create.status(), StatusCode::OK);
|
assert_eq!(create.status(), StatusCode::OK);
|
||||||
|
|
||||||
let resp = sign_up_client(SignUpPartial {
|
let resp = sign_up_client(SignUpPartial {
|
||||||
username: "test::sign_up_multiple",
|
username: "test::sign_up_multiple".to_string(),
|
||||||
group: "ИС-214/23",
|
group: "ИС-214/23".to_string(),
|
||||||
role: UserRole::Student,
|
role: UserRole::Student,
|
||||||
})
|
})
|
||||||
.await;
|
.await;
|
||||||
@@ -347,8 +332,8 @@ mod tests {
|
|||||||
|
|
||||||
// test
|
// test
|
||||||
let resp = sign_up_client(SignUpPartial {
|
let resp = sign_up_client(SignUpPartial {
|
||||||
username: "test::sign_up_invalid_role",
|
username: "test::sign_up_invalid_role".to_string(),
|
||||||
group: "ИС-214/23",
|
group: "ИС-214/23".to_string(),
|
||||||
role: UserRole::Admin,
|
role: UserRole::Admin,
|
||||||
})
|
})
|
||||||
.await;
|
.await;
|
||||||
@@ -362,8 +347,8 @@ mod tests {
|
|||||||
|
|
||||||
// test
|
// test
|
||||||
let resp = sign_up_client(SignUpPartial {
|
let resp = sign_up_client(SignUpPartial {
|
||||||
username: "test::sign_up_invalid_group",
|
username: "test::sign_up_invalid_group".to_string(),
|
||||||
group: "invalid_group",
|
group: "invalid_group".to_string(),
|
||||||
role: UserRole::Student,
|
role: UserRole::Student,
|
||||||
})
|
})
|
||||||
.await;
|
.await;
|
||||||
|
|||||||
@@ -1,5 +0,0 @@
|
|||||||
mod telegram_auth;
|
|
||||||
mod telegram_complete;
|
|
||||||
|
|
||||||
pub use telegram_auth::*;
|
|
||||||
pub use telegram_complete::*;
|
|
||||||
@@ -1,173 +0,0 @@
|
|||||||
use self::schema::*;
|
|
||||||
use crate::routes::schema::ResponseError;
|
|
||||||
use crate::utility::telegram::{WebAppInitDataMap, WebAppUser};
|
|
||||||
use crate::{utility, AppState};
|
|
||||||
use actix_web::{post, web};
|
|
||||||
use chrono::{DateTime, Duration, Utc};
|
|
||||||
use database::entity::sea_orm_active_enums::UserRole;
|
|
||||||
use database::entity::{ActiveUser, UserType};
|
|
||||||
use database::query::Query;
|
|
||||||
use database::sea_orm::{ActiveModelTrait, Set};
|
|
||||||
use objectid::ObjectId;
|
|
||||||
use std::sync::Arc;
|
|
||||||
use web::Json;
|
|
||||||
|
|
||||||
#[utoipa::path(responses(
|
|
||||||
(status = OK, body = Response),
|
|
||||||
(status = UNAUTHORIZED, body = ResponseError<ErrorCode>),
|
|
||||||
))]
|
|
||||||
#[post("/telegram-auth")]
|
|
||||||
pub async fn telegram_auth(
|
|
||||||
data_json: Json<Request>,
|
|
||||||
app_state: web::Data<AppState>,
|
|
||||||
) -> ServiceResponse {
|
|
||||||
let init_data = WebAppInitDataMap::from_str(data_json.into_inner().init_data);
|
|
||||||
|
|
||||||
{
|
|
||||||
let env = &app_state.get_env().telegram;
|
|
||||||
|
|
||||||
if let Err(error) = init_data.verify(env.bot_id, env.test_dc) {
|
|
||||||
return Err(ErrorCode::InvalidInitData(Arc::new(error))).into();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
let auth_date = DateTime::<Utc>::from_timestamp(
|
|
||||||
init_data
|
|
||||||
.data_map
|
|
||||||
.get("auth_date")
|
|
||||||
.unwrap()
|
|
||||||
.parse()
|
|
||||||
.unwrap(),
|
|
||||||
0,
|
|
||||||
)
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
if Utc::now() - auth_date > Duration::minutes(5) {
|
|
||||||
return Err(ErrorCode::ExpiredInitData).into();
|
|
||||||
}
|
|
||||||
|
|
||||||
let web_app_user =
|
|
||||||
serde_json::from_str::<WebAppUser>(init_data.data_map.get("user").unwrap()).unwrap();
|
|
||||||
|
|
||||||
let user = match Query::find_user_by_telegram_id(app_state.get_database(), web_app_user.id)
|
|
||||||
.await
|
|
||||||
.expect("Failed to find user by telegram id")
|
|
||||||
{
|
|
||||||
Some(value) => value,
|
|
||||||
None => {
|
|
||||||
let new_user = ActiveUser {
|
|
||||||
id: Set(ObjectId::new().unwrap().to_string()),
|
|
||||||
username: Set(format!("telegram_{}", web_app_user.id)), // можно оставить, а можно поменять
|
|
||||||
password: Set(None), // ибо нехуй
|
|
||||||
vk_id: Set(None),
|
|
||||||
telegram_id: Set(Some(web_app_user.id)),
|
|
||||||
group: Set(None),
|
|
||||||
role: Set(UserRole::Student), // TODO: при реге проверять данные
|
|
||||||
android_version: Set(None),
|
|
||||||
};
|
|
||||||
|
|
||||||
new_user
|
|
||||||
.insert(app_state.get_database())
|
|
||||||
.await
|
|
||||||
.expect("Failed to insert user")
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
let access_token = utility::jwt::encode(UserType::Default, &user.id);
|
|
||||||
Ok(Response::new(&access_token, user.group.is_some())).into()
|
|
||||||
}
|
|
||||||
|
|
||||||
mod schema {
|
|
||||||
use crate::routes::schema::PartialOkResponse;
|
|
||||||
use crate::state::AppState;
|
|
||||||
use crate::utility::telegram::VerifyError;
|
|
||||||
use actix_macros::ErrResponse;
|
|
||||||
use actix_web::body::EitherBody;
|
|
||||||
use actix_web::cookie::time::OffsetDateTime;
|
|
||||||
use actix_web::cookie::CookieBuilder;
|
|
||||||
use actix_web::{web, HttpRequest, HttpResponse};
|
|
||||||
use derive_more::Display;
|
|
||||||
use serde::{Deserialize, Serialize, Serializer};
|
|
||||||
use std::ops::Add;
|
|
||||||
use std::sync::Arc;
|
|
||||||
use utoipa::ToSchema;
|
|
||||||
|
|
||||||
#[derive(Debug, Deserialize, Serialize, ToSchema)]
|
|
||||||
#[serde(rename_all = "camelCase")]
|
|
||||||
#[schema(as = Flow::TelegramAuth::Request)]
|
|
||||||
pub struct Request {
|
|
||||||
/// Telegram WebApp init data.
|
|
||||||
pub init_data: String,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Serialize, ToSchema)]
|
|
||||||
#[serde(rename_all = "camelCase")]
|
|
||||||
#[schema(as = Flow::TelegramAuth::Response)]
|
|
||||||
pub struct Response {
|
|
||||||
// #[serde(skip)] // TODO: я пока не придумал как не отдавать сырой токен в ответе
|
|
||||||
// #[schema(ignore)]
|
|
||||||
access_token: String,
|
|
||||||
|
|
||||||
pub completed: bool,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Response {
|
|
||||||
pub fn new(access_token: &str, completed: bool) -> Self {
|
|
||||||
Self {
|
|
||||||
access_token: access_token.to_string(),
|
|
||||||
completed,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl PartialOkResponse for Response {
|
|
||||||
fn post_process(
|
|
||||||
&mut self,
|
|
||||||
request: &HttpRequest,
|
|
||||||
response: &mut HttpResponse<EitherBody<String>>,
|
|
||||||
) {
|
|
||||||
let access_token = &self.access_token;
|
|
||||||
|
|
||||||
let app_state = request.app_data::<web::Data<AppState>>().unwrap();
|
|
||||||
let mini_app_host = &*app_state.get_env().telegram.mini_app_host;
|
|
||||||
|
|
||||||
let cookie = CookieBuilder::new("access_token", access_token)
|
|
||||||
.domain(mini_app_host)
|
|
||||||
.path("/")
|
|
||||||
.expires(
|
|
||||||
OffsetDateTime::now_utc().add(std::time::Duration::from_secs(60 * 60 * 24 * 7)),
|
|
||||||
)
|
|
||||||
.http_only(true)
|
|
||||||
.secure(true)
|
|
||||||
.finish();
|
|
||||||
|
|
||||||
response.add_cookie(&cookie).unwrap();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub type ServiceResponse = crate::routes::schema::Response<Response, ErrorCode>;
|
|
||||||
|
|
||||||
#[derive(Clone, ToSchema, Display, ErrResponse)]
|
|
||||||
#[status_code = "actix_web::http::StatusCode::UNAUTHORIZED"]
|
|
||||||
#[schema(as = Flow::TelegramAuth::ErrorCode)]
|
|
||||||
pub enum ErrorCode {
|
|
||||||
#[display("Invalid init data provided: {_0}")]
|
|
||||||
#[schema(value_type = String)]
|
|
||||||
InvalidInitData(Arc<VerifyError>),
|
|
||||||
|
|
||||||
#[display("Expired init data provided.")]
|
|
||||||
ExpiredInitData,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Serialize for ErrorCode {
|
|
||||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
|
||||||
where
|
|
||||||
S: Serializer,
|
|
||||||
{
|
|
||||||
match self {
|
|
||||||
ErrorCode::InvalidInitData(_) => serializer.serialize_str("INVALID_INIT_DATA"),
|
|
||||||
ErrorCode::ExpiredInitData => serializer.serialize_str("EXPIRED_INIT_DATA"),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,101 +0,0 @@
|
|||||||
use self::schema::*;
|
|
||||||
use crate::extractors::base::AsyncExtractor;
|
|
||||||
use crate::routes::schema::ResponseError;
|
|
||||||
use crate::AppState;
|
|
||||||
use actix_web::{post, web};
|
|
||||||
use database::entity::User;
|
|
||||||
use database::query::Query;
|
|
||||||
use database::sea_orm::{ActiveModelTrait, IntoActiveModel, Set};
|
|
||||||
use web::Json;
|
|
||||||
|
|
||||||
#[utoipa::path(responses(
|
|
||||||
(status = OK),
|
|
||||||
(status = CONFLICT, body = ResponseError<ErrorCode>),
|
|
||||||
(status = INTERNAL_SERVER_ERROR, body = ResponseError<ErrorCode>),
|
|
||||||
(status = BAD_REQUEST, body = ResponseError<ErrorCode>)
|
|
||||||
))]
|
|
||||||
#[post("/telegram-complete")]
|
|
||||||
pub async fn telegram_complete(
|
|
||||||
data: Json<Request>,
|
|
||||||
app_state: web::Data<AppState>,
|
|
||||||
user: AsyncExtractor<User>,
|
|
||||||
) -> ServiceResponse {
|
|
||||||
let user = user.into_inner();
|
|
||||||
|
|
||||||
// проверка на перезапись уже имеющихся данных
|
|
||||||
if user.group.is_some() {
|
|
||||||
return Err(ErrorCode::AlreadyCompleted).into();
|
|
||||||
}
|
|
||||||
|
|
||||||
let data = data.into_inner();
|
|
||||||
|
|
||||||
let db = app_state.get_database();
|
|
||||||
let mut active_user = user.clone().into_active_model();
|
|
||||||
|
|
||||||
// замена существующего имени, если оно отличается
|
|
||||||
if user.username != data.username {
|
|
||||||
if Query::is_user_exists_by_username(db, &data.username)
|
|
||||||
.await
|
|
||||||
.unwrap()
|
|
||||||
{
|
|
||||||
return Err(ErrorCode::UsernameAlreadyExists).into();
|
|
||||||
}
|
|
||||||
|
|
||||||
active_user.username = Set(data.username);
|
|
||||||
}
|
|
||||||
|
|
||||||
// проверка на существование группы
|
|
||||||
if !app_state
|
|
||||||
.get_schedule_snapshot("eng_polytechnic")
|
|
||||||
.await
|
|
||||||
.unwrap()
|
|
||||||
.data
|
|
||||||
.groups
|
|
||||||
.contains_key(&data.group)
|
|
||||||
{
|
|
||||||
return Err(ErrorCode::InvalidGroupName).into();
|
|
||||||
}
|
|
||||||
|
|
||||||
active_user.group = Set(Some(data.group));
|
|
||||||
|
|
||||||
active_user.update(db).await.expect("Failed to update user");
|
|
||||||
|
|
||||||
Ok(()).into()
|
|
||||||
}
|
|
||||||
|
|
||||||
mod schema {
|
|
||||||
use actix_macros::ErrResponse;
|
|
||||||
use derive_more::Display;
|
|
||||||
use serde::{Deserialize, Serialize};
|
|
||||||
use utoipa::ToSchema;
|
|
||||||
|
|
||||||
#[derive(Debug, Deserialize, Serialize, ToSchema)]
|
|
||||||
#[schema(as = Flow::TelegramFill::Request)]
|
|
||||||
pub struct Request {
|
|
||||||
/// Username.
|
|
||||||
pub username: String,
|
|
||||||
|
|
||||||
/// Group.
|
|
||||||
pub group: String,
|
|
||||||
}
|
|
||||||
|
|
||||||
pub type ServiceResponse = crate::routes::schema::Response<(), ErrorCode>;
|
|
||||||
|
|
||||||
#[derive(Clone, Serialize, Display, ToSchema, ErrResponse)]
|
|
||||||
#[status_code = "actix_web::http::StatusCode::UNAUTHORIZED"]
|
|
||||||
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
|
|
||||||
#[schema(as = Flow::TelegramFill::ErrorCode)]
|
|
||||||
pub enum ErrorCode {
|
|
||||||
#[display("This flow is already completed.")]
|
|
||||||
#[status_code = "actix_web::http::StatusCode::CONFLICT"]
|
|
||||||
AlreadyCompleted,
|
|
||||||
|
|
||||||
#[display("User with that name already exists.")]
|
|
||||||
#[status_code = "actix_web::http::StatusCode::BAD_REQUEST"]
|
|
||||||
UsernameAlreadyExists,
|
|
||||||
|
|
||||||
#[display("The required group does not exist.")]
|
|
||||||
#[status_code = "actix_web::http::StatusCode::BAD_REQUEST"]
|
|
||||||
InvalidGroupName,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,7 +1,4 @@
|
|||||||
pub mod admin;
|
|
||||||
pub mod auth;
|
pub mod auth;
|
||||||
pub mod flow;
|
pub mod users;
|
||||||
pub mod schedule;
|
pub mod schedule;
|
||||||
mod schema;
|
mod schema;
|
||||||
pub mod users;
|
|
||||||
pub mod vk_id;
|
|
||||||
|
|||||||
@@ -1,17 +0,0 @@
|
|||||||
use crate::routes::schedule::schema::CacheStatus;
|
|
||||||
use crate::AppState;
|
|
||||||
use actix_web::{get, web};
|
|
||||||
use std::ops::Deref;
|
|
||||||
|
|
||||||
#[utoipa::path(responses(
|
|
||||||
(status = OK, body = CacheStatus),
|
|
||||||
))]
|
|
||||||
#[get("/cache-status")]
|
|
||||||
pub async fn cache_status(app_state: web::Data<AppState>) -> CacheStatus {
|
|
||||||
app_state
|
|
||||||
.get_schedule_snapshot("eng_polytechnic")
|
|
||||||
.await
|
|
||||||
.unwrap()
|
|
||||||
.deref()
|
|
||||||
.into()
|
|
||||||
}
|
|
||||||
@@ -1,9 +0,0 @@
|
|||||||
use crate::routes::schedule::schema::ScheduleView;
|
|
||||||
use crate::state::AppState;
|
|
||||||
use actix_web::{get, web};
|
|
||||||
|
|
||||||
#[utoipa::path(responses((status = OK, body = ScheduleView)))]
|
|
||||||
#[get("/")]
|
|
||||||
pub async fn schedule(app_state: web::Data<AppState>) -> ScheduleView {
|
|
||||||
ScheduleView::from(&app_state).await
|
|
||||||
}
|
|
||||||
23
src/routes/schedule/get_cache_status.rs
Normal file
23
src/routes/schedule/get_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 get_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()
|
||||||
|
}
|
||||||
99
src/routes/schedule/get_group.rs
Normal file
99
src/routes/schedule/get_group.rs
Normal file
@@ -0,0 +1,99 @@
|
|||||||
|
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 get_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 {
|
||||||
|
/// Расписание группы
|
||||||
|
pub group: ScheduleEntry,
|
||||||
|
|
||||||
|
/// Устаревшая переменная
|
||||||
|
///
|
||||||
|
/// По умолчанию возвращается пустой список
|
||||||
|
#[deprecated = "Will be removed in future versions"]
|
||||||
|
pub updated: Vec<i32>,
|
||||||
|
|
||||||
|
/// Устаревшая переменная
|
||||||
|
///
|
||||||
|
/// По умолчанию начальная дата по 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 {
|
||||||
|
/// Расписания ещё не получены
|
||||||
|
#[status_code = "actix_web::http::StatusCode::SERVICE_UNAVAILABLE"]
|
||||||
|
#[display("Schedule not parsed yet.")]
|
||||||
|
NoSchedule,
|
||||||
|
|
||||||
|
/// Группа не найдена
|
||||||
|
#[status_code = "actix_web::http::StatusCode::NOT_FOUND"]
|
||||||
|
#[display("Required group not found.")]
|
||||||
|
NotFound,
|
||||||
|
}
|
||||||
|
}
|
||||||
48
src/routes/schedule/get_group_names.rs
Normal file
48
src/routes/schedule/get_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 get_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 {
|
||||||
|
/// Список названий групп отсортированный в алфавитном порядке
|
||||||
|
#[schema(examples(json!(["ИС-214/23"])))]
|
||||||
|
pub names: Vec<String>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<Vec<String>> for Response {
|
||||||
|
fn from(names: Vec<String>) -> Self {
|
||||||
|
Self { names }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
25
src/routes/schedule/get_schedule.rs
Normal file
25
src/routes/schedule/get_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 get_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>;
|
||||||
|
}
|
||||||
97
src/routes/schedule/get_teacher.rs
Normal file
97
src/routes/schedule/get_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 get_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 {
|
||||||
|
/// Расписание преподавателя
|
||||||
|
pub teacher: ScheduleEntry,
|
||||||
|
|
||||||
|
/// Устаревшая переменная
|
||||||
|
///
|
||||||
|
/// По умолчанию возвращается пустой список
|
||||||
|
#[deprecated = "Will be removed in future versions"]
|
||||||
|
pub updated: Vec<i32>,
|
||||||
|
|
||||||
|
/// Устаревшая переменная
|
||||||
|
///
|
||||||
|
/// По умолчанию начальная дата по Unix
|
||||||
|
#[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 {
|
||||||
|
/// Расписания ещё не получены
|
||||||
|
#[status_code = "actix_web::http::StatusCode::SERVICE_UNAVAILABLE"]
|
||||||
|
#[display("Schedule not parsed yet.")]
|
||||||
|
NoSchedule,
|
||||||
|
|
||||||
|
/// Преподаватель не найден
|
||||||
|
#[status_code = "actix_web::http::StatusCode::NOT_FOUND"]
|
||||||
|
#[display("Required teacher not found.")]
|
||||||
|
NotFound,
|
||||||
|
}
|
||||||
|
}
|
||||||
48
src/routes/schedule/get_teacher_names.rs
Normal file
48
src/routes/schedule/get_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 get_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 {
|
||||||
|
/// Список имён преподавателей отсортированный в алфавитном порядке
|
||||||
|
#[schema(examples(json!(["Хомченко Н.Е."])))]
|
||||||
|
pub names: Vec<String>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<Vec<String>> for Response {
|
||||||
|
fn from(names: Vec<String>) -> Self {
|
||||||
|
Self { names }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,72 +0,0 @@
|
|||||||
use self::schema::*;
|
|
||||||
use crate::AppState;
|
|
||||||
use crate::extractors::base::AsyncExtractor;
|
|
||||||
use crate::routes::schedule::schema::ScheduleEntryResponse;
|
|
||||||
use crate::routes::schema::ResponseError;
|
|
||||||
use actix_web::{get, web};
|
|
||||||
use database::entity::User;
|
|
||||||
|
|
||||||
#[utoipa::path(responses(
|
|
||||||
(status = OK, body = ScheduleEntryResponse),
|
|
||||||
(
|
|
||||||
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: AsyncExtractor<User>, app_state: web::Data<AppState>) -> ServiceResponse {
|
|
||||||
match &user.into_inner().group {
|
|
||||||
None => Err(ErrorCode::SignUpNotCompleted),
|
|
||||||
|
|
||||||
Some(group) => match app_state
|
|
||||||
.get_schedule_snapshot("eng_polytechnic")
|
|
||||||
.await
|
|
||||||
.unwrap()
|
|
||||||
.data
|
|
||||||
.groups
|
|
||||||
.get(group)
|
|
||||||
{
|
|
||||||
None => Err(ErrorCode::NotFound),
|
|
||||||
|
|
||||||
Some(entry) => Ok(entry.clone().into()),
|
|
||||||
},
|
|
||||||
}
|
|
||||||
.into()
|
|
||||||
}
|
|
||||||
|
|
||||||
mod schema {
|
|
||||||
use crate::routes::schedule::schema::ScheduleEntryResponse;
|
|
||||||
use actix_macros::ErrResponse;
|
|
||||||
use derive_more::Display;
|
|
||||||
use serde::Serialize;
|
|
||||||
use utoipa::ToSchema;
|
|
||||||
|
|
||||||
pub type ServiceResponse = crate::routes::schema::Response<ScheduleEntryResponse, ErrorCode>;
|
|
||||||
|
|
||||||
#[derive(Clone, Serialize, Display, ToSchema, ErrResponse)]
|
|
||||||
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
|
|
||||||
#[schema(as = GroupSchedule::ErrorCode)]
|
|
||||||
pub enum ErrorCode {
|
|
||||||
/// The user tried to access the API without completing singing up.
|
|
||||||
#[status_code = "actix_web::http::StatusCode::FORBIDDEN"]
|
|
||||||
#[display("You have not completed signing up.")]
|
|
||||||
SignUpNotCompleted,
|
|
||||||
|
|
||||||
/// Group not found.
|
|
||||||
#[status_code = "actix_web::http::StatusCode::NOT_FOUND"]
|
|
||||||
#[display("Required group not found.")]
|
|
||||||
NotFound,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,65 +0,0 @@
|
|||||||
use self::schema::*;
|
|
||||||
use crate::routes::schedule::schema::ScheduleEntryResponse;
|
|
||||||
use crate::routes::schema::ResponseError;
|
|
||||||
use crate::AppState;
|
|
||||||
use actix_web::{get, web};
|
|
||||||
|
|
||||||
#[utoipa::path(responses(
|
|
||||||
(status = OK, body = ScheduleEntryResponse),
|
|
||||||
(
|
|
||||||
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/{group_name}")]
|
|
||||||
pub async fn group_by_name(
|
|
||||||
path: web::Path<String>,
|
|
||||||
app_state: web::Data<AppState>,
|
|
||||||
) -> ServiceResponse {
|
|
||||||
let group_name = path.into_inner();
|
|
||||||
|
|
||||||
match app_state
|
|
||||||
.get_schedule_snapshot("eng_polytechnic")
|
|
||||||
.await
|
|
||||||
.unwrap()
|
|
||||||
.data
|
|
||||||
.groups
|
|
||||||
.get(&group_name)
|
|
||||||
{
|
|
||||||
None => Err(ErrorCode::NotFound),
|
|
||||||
Some(entry) => Ok(entry.clone().into()),
|
|
||||||
}
|
|
||||||
.into()
|
|
||||||
}
|
|
||||||
|
|
||||||
mod schema {
|
|
||||||
use crate::routes::schedule::schema::ScheduleEntryResponse;
|
|
||||||
use actix_macros::ErrResponse;
|
|
||||||
use derive_more::Display;
|
|
||||||
use serde::Serialize;
|
|
||||||
use utoipa::ToSchema;
|
|
||||||
|
|
||||||
pub type ServiceResponse = crate::routes::schema::Response<ScheduleEntryResponse, ErrorCode>;
|
|
||||||
|
|
||||||
#[derive(Clone, Serialize, Display, ToSchema, ErrResponse)]
|
|
||||||
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
|
|
||||||
#[schema(as = GroupByNameSchedule::ErrorCode)]
|
|
||||||
pub enum ErrorCode {
|
|
||||||
/// Group not found.
|
|
||||||
#[status_code = "actix_web::http::StatusCode::NOT_FOUND"]
|
|
||||||
#[display("Required group not found.")]
|
|
||||||
NotFound,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,35 +0,0 @@
|
|||||||
use self::schema::*;
|
|
||||||
use crate::AppState;
|
|
||||||
use actix_web::{get, web};
|
|
||||||
|
|
||||||
#[utoipa::path(responses((status = OK, body = Response)))]
|
|
||||||
#[get("/group-names")]
|
|
||||||
pub async fn group_names(app_state: web::Data<AppState>) -> Response {
|
|
||||||
let mut names: Vec<String> = app_state
|
|
||||||
.get_schedule_snapshot("eng_polytechnic")
|
|
||||||
.await
|
|
||||||
.unwrap()
|
|
||||||
.data
|
|
||||||
.groups
|
|
||||||
.keys()
|
|
||||||
.cloned()
|
|
||||||
.collect();
|
|
||||||
|
|
||||||
names.sort();
|
|
||||||
|
|
||||||
Response { names }
|
|
||||||
}
|
|
||||||
|
|
||||||
mod schema {
|
|
||||||
use actix_macros::ResponderJson;
|
|
||||||
use serde::Serialize;
|
|
||||||
use utoipa::ToSchema;
|
|
||||||
|
|
||||||
#[derive(Serialize, ToSchema, ResponderJson)]
|
|
||||||
#[schema(as = GetGroupNames::Response)]
|
|
||||||
pub struct Response {
|
|
||||||
/// List of group names sorted in alphabetical order.
|
|
||||||
#[schema(examples(json!(["ИС-214/23"])))]
|
|
||||||
pub names: Vec<String>,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,16 +1,8 @@
|
|||||||
mod cache_status;
|
pub mod get_cache_status;
|
||||||
mod group;
|
pub mod get_schedule;
|
||||||
mod group_by_name;
|
pub mod get_group;
|
||||||
mod group_names;
|
pub mod get_group_names;
|
||||||
mod get;
|
pub mod get_teacher;
|
||||||
|
pub mod get_teacher_names;
|
||||||
mod schema;
|
mod schema;
|
||||||
mod teacher;
|
pub mod update_download_url;
|
||||||
mod teacher_names;
|
|
||||||
|
|
||||||
pub use cache_status::*;
|
|
||||||
pub use group::*;
|
|
||||||
pub use group_by_name::*;
|
|
||||||
pub use group_names::*;
|
|
||||||
pub use get::*;
|
|
||||||
pub use teacher::*;
|
|
||||||
pub use teacher_names::*;
|
|
||||||
|
|||||||
@@ -1,74 +1,107 @@
|
|||||||
use crate::state::AppState;
|
use crate::app_state::{AppState, Schedule};
|
||||||
use actix_macros::{OkResponse, ResponderJson};
|
use crate::parser::schema::ScheduleEntry;
|
||||||
|
use actix_macros::{IntoResponseErrorNamed, ResponderJson, StatusCode};
|
||||||
use actix_web::web;
|
use actix_web::web;
|
||||||
use providers::base::{ScheduleEntry, ScheduleSnapshot};
|
use chrono::{DateTime, Duration, Utc};
|
||||||
|
use derive_more::Display;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::ops::Deref;
|
|
||||||
use utoipa::ToSchema;
|
use utoipa::ToSchema;
|
||||||
|
|
||||||
/// Response from schedule server.
|
/// Ответ от сервера с расписаниями
|
||||||
#[derive(Serialize, ToSchema, OkResponse, ResponderJson)]
|
#[derive(Serialize, ToSchema)]
|
||||||
#[serde(rename_all = "camelCase")]
|
#[serde(rename_all = "camelCase")]
|
||||||
pub struct ScheduleView {
|
pub struct ScheduleView {
|
||||||
/// Url to xls file.
|
/// ETag расписания на сервере политехникума
|
||||||
url: String,
|
etag: String,
|
||||||
|
|
||||||
/// Groups schedule.
|
/// Дата обновления расписания на сайте политехникума
|
||||||
|
uploaded_at: DateTime<Utc>,
|
||||||
|
|
||||||
|
/// Дата последнего скачивания расписания с сервера политехникума
|
||||||
|
downloaded_at: DateTime<Utc>,
|
||||||
|
|
||||||
|
/// Расписание групп
|
||||||
groups: HashMap<String, ScheduleEntry>,
|
groups: HashMap<String, ScheduleEntry>,
|
||||||
|
|
||||||
/// Teachers schedule.
|
/// Расписание преподавателей
|
||||||
teachers: HashMap<String, ScheduleEntry>,
|
teachers: HashMap<String, ScheduleEntry>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Serialize, ToSchema, OkResponse)]
|
#[derive(Clone, Serialize, ToSchema, StatusCode, Display, IntoResponseErrorNamed)]
|
||||||
pub struct ScheduleEntryResponse(ScheduleEntry);
|
#[status_code = "actix_web::http::StatusCode::SERVICE_UNAVAILABLE"]
|
||||||
|
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
|
||||||
impl From<ScheduleEntry> for ScheduleEntryResponse {
|
#[schema(as = ScheduleShared::ErrorCode)]
|
||||||
fn from(value: ScheduleEntry) -> Self {
|
pub enum ErrorCode {
|
||||||
Self(value)
|
/// Расписания ещё не получены
|
||||||
}
|
#[display("Schedule not parsed yet.")]
|
||||||
|
NoSchedule,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ScheduleView {
|
impl TryFrom<&web::Data<AppState>> for ScheduleView {
|
||||||
pub async fn from(app_state: &web::Data<AppState>) -> Self {
|
type Error = ErrorCode;
|
||||||
let schedule = app_state
|
|
||||||
.get_schedule_snapshot("eng_polytechnic")
|
|
||||||
.await
|
|
||||||
.unwrap()
|
|
||||||
.deref()
|
|
||||||
.clone();
|
|
||||||
|
|
||||||
Self {
|
fn try_from(app_state: &web::Data<AppState>) -> Result<Self, Self::Error> {
|
||||||
url: schedule.url,
|
if let Some(schedule) = app_state.schedule.lock().unwrap().clone() {
|
||||||
groups: schedule.data.groups,
|
Ok(Self {
|
||||||
teachers: schedule.data.teachers,
|
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, OkResponse)]
|
#[derive(Serialize, Deserialize, ToSchema, ResponderJson)]
|
||||||
#[serde(rename_all = "camelCase")]
|
#[serde(rename_all = "camelCase")]
|
||||||
pub struct CacheStatus {
|
pub struct CacheStatus {
|
||||||
/// Schedule hash.
|
/// Хеш расписаний
|
||||||
pub hash: String,
|
pub cache_hash: String,
|
||||||
|
|
||||||
/// Last cache update date.
|
/// Требуется ли обновить ссылку на расписание
|
||||||
pub fetched_at: i64,
|
pub cache_update_required: bool,
|
||||||
|
|
||||||
/// Cached schedule update date.
|
/// Дата последнего обновления кеша
|
||||||
///
|
pub last_cache_update: i64,
|
||||||
/// Determined by the polytechnic's server.
|
|
||||||
pub updated_at: i64,
|
/// Дата обновления кешированного расписания
|
||||||
|
///
|
||||||
|
/// Определяется сервером политехникума
|
||||||
|
pub last_schedule_update: i64,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<&ScheduleSnapshot> for CacheStatus {
|
impl CacheStatus {
|
||||||
fn from(value: &ScheduleSnapshot) -> Self {
|
pub fn default() -> Self {
|
||||||
Self {
|
CacheStatus {
|
||||||
hash: value.hash(),
|
cache_hash: "0000000000000000000000000000000000000000".to_string(),
|
||||||
fetched_at: value.fetched_at.timestamp(),
|
cache_update_required: true,
|
||||||
updated_at: value.updated_at.timestamp(),
|
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(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,53 +0,0 @@
|
|||||||
use self::schema::*;
|
|
||||||
use crate::AppState;
|
|
||||||
use crate::routes::schema::ResponseError;
|
|
||||||
use actix_web::{get, web};
|
|
||||||
use providers::base::ScheduleEntry;
|
|
||||||
|
|
||||||
#[utoipa::path(responses(
|
|
||||||
(status = OK, body = ScheduleEntry),
|
|
||||||
(
|
|
||||||
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 {
|
|
||||||
match app_state
|
|
||||||
.get_schedule_snapshot("eng_polytechnic")
|
|
||||||
.await
|
|
||||||
.unwrap()
|
|
||||||
.data
|
|
||||||
.teachers
|
|
||||||
.get(&name.into_inner())
|
|
||||||
{
|
|
||||||
None => Err(ErrorCode::NotFound),
|
|
||||||
|
|
||||||
Some(entry) => Ok(entry.clone().into()),
|
|
||||||
}
|
|
||||||
.into()
|
|
||||||
}
|
|
||||||
|
|
||||||
mod schema {
|
|
||||||
use crate::routes::schedule::schema::ScheduleEntryResponse;
|
|
||||||
use actix_macros::ErrResponse;
|
|
||||||
use derive_more::Display;
|
|
||||||
use serde::Serialize;
|
|
||||||
use utoipa::ToSchema;
|
|
||||||
|
|
||||||
pub type ServiceResponse = crate::routes::schema::Response<ScheduleEntryResponse, ErrorCode>;
|
|
||||||
|
|
||||||
#[derive(Clone, Serialize, Display, ToSchema, ErrResponse)]
|
|
||||||
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
|
|
||||||
#[schema(as = TeacherSchedule::ErrorCode)]
|
|
||||||
pub enum ErrorCode {
|
|
||||||
/// Teacher not found.
|
|
||||||
#[status_code = "actix_web::http::StatusCode::NOT_FOUND"]
|
|
||||||
#[display("Required teacher not found.")]
|
|
||||||
NotFound,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,35 +0,0 @@
|
|||||||
use self::schema::*;
|
|
||||||
use crate::AppState;
|
|
||||||
use actix_web::{get, web};
|
|
||||||
|
|
||||||
#[utoipa::path(responses((status = OK, body = Response)))]
|
|
||||||
#[get("/teacher-names")]
|
|
||||||
pub async fn teacher_names(app_state: web::Data<AppState>) -> Response {
|
|
||||||
let mut names: Vec<String> = app_state
|
|
||||||
.get_schedule_snapshot("eng_polytechnic")
|
|
||||||
.await
|
|
||||||
.unwrap()
|
|
||||||
.data
|
|
||||||
.teachers
|
|
||||||
.keys()
|
|
||||||
.cloned()
|
|
||||||
.collect();
|
|
||||||
|
|
||||||
names.sort();
|
|
||||||
|
|
||||||
Response { names }
|
|
||||||
}
|
|
||||||
|
|
||||||
mod schema {
|
|
||||||
use actix_macros::ResponderJson;
|
|
||||||
use serde::Serialize;
|
|
||||||
use utoipa::ToSchema;
|
|
||||||
|
|
||||||
#[derive(Serialize, ToSchema, ResponderJson)]
|
|
||||||
#[schema(as = GetTeacherNames::Response)]
|
|
||||||
pub struct Response {
|
|
||||||
/// List of teacher names sorted alphabetically.
|
|
||||||
#[schema(examples(json!(["Хомченко Н.Е."])))]
|
|
||||||
pub names: Vec<String>,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
132
src/routes/schedule/update_download_url.rs
Normal file
132
src/routes/schedule/update_download_url.rs
Normal file
@@ -0,0 +1,132 @@
|
|||||||
|
use self::schema::*;
|
||||||
|
use crate::AppState;
|
||||||
|
use crate::app_state::Schedule;
|
||||||
|
use crate::parser::parse_xls;
|
||||||
|
use crate::routes::schedule::schema::CacheStatus;
|
||||||
|
use crate::routes::schema::{IntoResponseAsError, ResponseError};
|
||||||
|
use crate::xls_downloader::interface::XLSDownloader;
|
||||||
|
use actix_web::web::Json;
|
||||||
|
use actix_web::{patch, web};
|
||||||
|
use chrono::Utc;
|
||||||
|
|
||||||
|
#[utoipa::path(responses(
|
||||||
|
(status = OK, body = CacheStatus),
|
||||||
|
(status = NOT_ACCEPTABLE, body = ResponseError<ErrorCode>),
|
||||||
|
))]
|
||||||
|
#[patch("/update-download-url")]
|
||||||
|
pub async fn update_download_url(
|
||||||
|
data: Json<Request>,
|
||||||
|
app_state: web::Data<AppState>,
|
||||||
|
) -> ServiceResponse {
|
||||||
|
if !data.url.starts_with("https://politehnikum-eng.ru/") {
|
||||||
|
return ErrorCode::NonWhitelistedHost.into_response();
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut downloader = app_state.downloader.lock().unwrap();
|
||||||
|
|
||||||
|
if let Some(url) = &downloader.url {
|
||||||
|
if url.eq(&data.url) {
|
||||||
|
return Ok(CacheStatus::from(&app_state)).into();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
match downloader.set_url(data.url.clone()).await {
|
||||||
|
Ok(fetch_result) => {
|
||||||
|
let mut schedule = app_state.schedule.lock().unwrap();
|
||||||
|
|
||||||
|
if schedule.is_some()
|
||||||
|
&& fetch_result.uploaded_at < schedule.as_ref().unwrap().updated_at
|
||||||
|
{
|
||||||
|
return ErrorCode::OutdatedSchedule.into_response();
|
||||||
|
}
|
||||||
|
|
||||||
|
match downloader.fetch(false).await {
|
||||||
|
Ok(download_result) => match parse_xls(download_result.data.as_ref().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) => ErrorCode::InvalidSchedule(error).into_response(),
|
||||||
|
},
|
||||||
|
Err(error) => {
|
||||||
|
eprintln!("Unknown url provided {}", data.url);
|
||||||
|
eprintln!("{:?}", error);
|
||||||
|
|
||||||
|
ErrorCode::DownloadFailed.into_response()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(error) => {
|
||||||
|
eprintln!("Unknown url provided {}", data.url);
|
||||||
|
eprintln!("{:?}", error);
|
||||||
|
|
||||||
|
ErrorCode::FetchFailed.into_response()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
mod schema {
|
||||||
|
use crate::parser::schema::ParseError;
|
||||||
|
use crate::routes::schedule::schema::CacheStatus;
|
||||||
|
use actix_macros::{IntoResponseErrorNamed, StatusCode};
|
||||||
|
use derive_more::Display;
|
||||||
|
use serde::{Deserialize, Serialize, Serializer};
|
||||||
|
use utoipa::ToSchema;
|
||||||
|
|
||||||
|
pub type ServiceResponse = crate::routes::schema::Response<CacheStatus, ErrorCode>;
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, ToSchema)]
|
||||||
|
pub struct Request {
|
||||||
|
/// Ссылка на расписание
|
||||||
|
pub url: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, ToSchema, StatusCode, Display, IntoResponseErrorNamed)]
|
||||||
|
#[status_code = "actix_web::http::StatusCode::NOT_ACCEPTABLE"]
|
||||||
|
#[schema(as = SetDownloadUrl::ErrorCode)]
|
||||||
|
pub enum ErrorCode {
|
||||||
|
/// Передана ссылка с хостом отличающимся от politehnikum-eng.ru
|
||||||
|
#[display("URL with unknown host provided. Provide url with politehnikum-eng.ru host.")]
|
||||||
|
NonWhitelistedHost,
|
||||||
|
|
||||||
|
/// Не удалось получить мета-данные файла
|
||||||
|
#[display("Unable to retrieve metadata from the specified URL.")]
|
||||||
|
FetchFailed,
|
||||||
|
|
||||||
|
/// Не удалось скачать файл
|
||||||
|
#[display("Unable to retrieve data from the specified URL.")]
|
||||||
|
DownloadFailed,
|
||||||
|
|
||||||
|
/// Ссылка ведёт на устаревшее расписание
|
||||||
|
///
|
||||||
|
/// Под устаревшим расписанием подразумевается расписание, которое было опубликовано раньше, чем уже имеется на данный момент
|
||||||
|
#[display("The schedule is older than it already is.")]
|
||||||
|
OutdatedSchedule,
|
||||||
|
|
||||||
|
/// Не удалось преобразовать расписание
|
||||||
|
#[display("{}", "_0.display()")]
|
||||||
|
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"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -4,30 +4,33 @@ use actix_web::http::StatusCode;
|
|||||||
use actix_web::{HttpRequest, HttpResponse, Responder};
|
use actix_web::{HttpRequest, HttpResponse, Responder};
|
||||||
use serde::{Serialize, Serializer};
|
use serde::{Serialize, Serializer};
|
||||||
use std::convert::Into;
|
use std::convert::Into;
|
||||||
use std::fmt::Display;
|
|
||||||
use utoipa::PartialSchema;
|
use utoipa::PartialSchema;
|
||||||
|
|
||||||
pub struct Response<T, E>(pub Result<T, E>)
|
pub struct Response<T, E>(pub Result<T, E>)
|
||||||
where
|
where
|
||||||
T: Serialize + PartialSchema + PartialOkResponse,
|
T: Serialize + PartialSchema,
|
||||||
E: Serialize + PartialSchema + Display + PartialErrResponse;
|
E: Serialize + PartialSchema + Clone + PartialStatusCode;
|
||||||
|
|
||||||
|
pub trait PartialStatusCode {
|
||||||
|
fn status_code(&self) -> StatusCode;
|
||||||
|
}
|
||||||
|
|
||||||
/// Transform Response<T, E> into Result<T, E>
|
/// Transform Response<T, E> into Result<T, E>
|
||||||
impl<T, E> From<Response<T, E>> for Result<T, E>
|
impl<T, E> Into<Result<T, E>> for Response<T, E>
|
||||||
where
|
where
|
||||||
T: Serialize + PartialSchema + PartialOkResponse,
|
T: Serialize + PartialSchema,
|
||||||
E: Serialize + PartialSchema + Display + PartialErrResponse,
|
E: Serialize + PartialSchema + Clone + PartialStatusCode,
|
||||||
{
|
{
|
||||||
fn from(value: Response<T, E>) -> Self {
|
fn into(self) -> Result<T, E> {
|
||||||
value.0
|
self.0
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Transform T into Response<T, E>
|
/// Transform T into Response<T, E>
|
||||||
impl<T, E> From<Result<T, E>> for Response<T, E>
|
impl<T, E> From<Result<T, E>> for Response<T, E>
|
||||||
where
|
where
|
||||||
T: Serialize + PartialSchema + PartialOkResponse,
|
T: Serialize + PartialSchema,
|
||||||
E: Serialize + PartialSchema + Display + PartialErrResponse,
|
E: Serialize + PartialSchema + Clone + PartialStatusCode,
|
||||||
{
|
{
|
||||||
fn from(value: Result<T, E>) -> Self {
|
fn from(value: Result<T, E>) -> Self {
|
||||||
Response(value)
|
Response(value)
|
||||||
@@ -37,16 +40,17 @@ where
|
|||||||
/// Serialize Response<T, E>
|
/// Serialize Response<T, E>
|
||||||
impl<T, E> Serialize for Response<T, E>
|
impl<T, E> Serialize for Response<T, E>
|
||||||
where
|
where
|
||||||
T: Serialize + PartialSchema + PartialOkResponse,
|
T: Serialize + PartialSchema,
|
||||||
E: Serialize + PartialSchema + Display + PartialErrResponse + Clone + Into<ResponseError<E>>,
|
E: Serialize + PartialSchema + Clone + PartialStatusCode + Into<ResponseError<E>>,
|
||||||
{
|
{
|
||||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||||
where
|
where
|
||||||
S: Serializer,
|
S: Serializer,
|
||||||
{
|
{
|
||||||
match &self.0 {
|
match &self.0 {
|
||||||
Ok(ok) => serializer.serialize_some(&ok),
|
Ok(ok) => serializer.serialize_some::<T>(&ok),
|
||||||
Err(err) => serializer.serialize_some(&err.clone().into()),
|
Err(err) => serializer
|
||||||
|
.serialize_some::<ResponseError<E>>(&ResponseError::<E>::from(err.clone().into())),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -54,12 +58,12 @@ where
|
|||||||
/// Transform Response<T, E> to HttpResponse<String>
|
/// Transform Response<T, E> to HttpResponse<String>
|
||||||
impl<T, E> Responder for Response<T, E>
|
impl<T, E> Responder for Response<T, E>
|
||||||
where
|
where
|
||||||
T: Serialize + PartialSchema + PartialOkResponse,
|
T: Serialize + PartialSchema,
|
||||||
E: Serialize + PartialSchema + Display + PartialErrResponse + Clone + Into<ResponseError<E>>,
|
E: Serialize + PartialSchema + Clone + PartialStatusCode + Into<ResponseError<E>>,
|
||||||
{
|
{
|
||||||
type Body = EitherBody<String>;
|
type Body = EitherBody<String>;
|
||||||
|
|
||||||
fn respond_to(mut self, request: &HttpRequest) -> HttpResponse<Self::Body> {
|
fn respond_to(self, _: &HttpRequest) -> HttpResponse<Self::Body> {
|
||||||
match serde_json::to_string(&self) {
|
match serde_json::to_string(&self) {
|
||||||
Ok(body) => {
|
Ok(body) => {
|
||||||
let code = match &self.0 {
|
let code = match &self.0 {
|
||||||
@@ -67,19 +71,13 @@ where
|
|||||||
Err(e) => e.status_code(),
|
Err(e) => e.status_code(),
|
||||||
};
|
};
|
||||||
|
|
||||||
let mut response = match HttpResponse::build(code)
|
match HttpResponse::build(code)
|
||||||
.content_type(mime::APPLICATION_JSON)
|
.content_type(mime::APPLICATION_JSON)
|
||||||
.message_body(body)
|
.message_body(body)
|
||||||
{
|
{
|
||||||
Ok(res) => res.map_into_left_body(),
|
Ok(res) => res.map_into_left_body(),
|
||||||
Err(err) => HttpResponse::from_error(err).map_into_right_body(),
|
Err(err) => HttpResponse::from_error(err).map_into_right_body(),
|
||||||
};
|
|
||||||
|
|
||||||
if let Ok(ok) = &mut self.0 {
|
|
||||||
ok.post_process(request, &mut response);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
response
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
@@ -89,109 +87,72 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Трейт для всех положительных ответов от сервера
|
|
||||||
pub trait PartialOkResponse {
|
|
||||||
fn post_process(
|
|
||||||
&mut self,
|
|
||||||
_request: &HttpRequest,
|
|
||||||
_response: &mut HttpResponse<EitherBody<String>>,
|
|
||||||
) {
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl PartialOkResponse for () {}
|
|
||||||
|
|
||||||
/// Трейт для всех отрицательных ответов от сервера
|
|
||||||
pub trait PartialErrResponse {
|
|
||||||
fn status_code(&self) -> StatusCode;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// ResponseError<T>
|
/// 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)]
|
#[derive(Serialize, utoipa::ToSchema)]
|
||||||
pub struct ResponseError<T: Serialize + PartialSchema + Clone> {
|
pub struct ResponseError<T: Serialize + PartialSchema> {
|
||||||
pub code: T,
|
pub code: T,
|
||||||
pub message: String,
|
|
||||||
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
|
pub message: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T> From<T> for ResponseError<T>
|
pub trait IntoResponseAsError<T>
|
||||||
where
|
where
|
||||||
T: Serialize + PartialSchema + Display + Clone,
|
T: Serialize + PartialSchema,
|
||||||
|
Self: Serialize + PartialSchema + Clone + PartialStatusCode + Into<ResponseError<Self>>,
|
||||||
{
|
{
|
||||||
fn from(code: T) -> Self {
|
fn into_response(self) -> Response<T, Self> {
|
||||||
Self {
|
Response(Err(self))
|
||||||
message: format!("{}", code),
|
|
||||||
code,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub mod user {
|
pub mod user {
|
||||||
use actix_macros::{OkResponse, ResponderJson};
|
use crate::database::models::{User, UserRole};
|
||||||
use database::entity::sea_orm_active_enums::UserRole;
|
use actix_macros::ResponderJson;
|
||||||
use database::entity::User;
|
|
||||||
use serde::Serialize;
|
use serde::Serialize;
|
||||||
|
|
||||||
//noinspection SpellCheckingInspection
|
/// Используется для скрытия чувствительных полей, таких как хеш пароля или FCM
|
||||||
/// Используется для скрытия чувствительных полей, таких как хеш пароля
|
#[derive(Serialize, utoipa::ToSchema, ResponderJson)]
|
||||||
#[derive(Serialize, utoipa::ToSchema, ResponderJson, OkResponse)]
|
|
||||||
#[serde(rename_all = "camelCase")]
|
#[serde(rename_all = "camelCase")]
|
||||||
pub struct UserResponse {
|
pub struct UserResponse {
|
||||||
/// UUID
|
/// UUID
|
||||||
#[schema(examples("67dcc9a9507b0000772744a2"))]
|
#[schema(examples("67dcc9a9507b0000772744a2"))]
|
||||||
pub id: String,
|
id: String,
|
||||||
|
|
||||||
/// Имя пользователя
|
/// Имя пользователя
|
||||||
#[schema(examples("n08i40k"))]
|
#[schema(examples("n08i40k"))]
|
||||||
pub username: String,
|
username: String,
|
||||||
|
|
||||||
/// Группа
|
/// Группа
|
||||||
#[schema(examples("ИС-214/23"))]
|
#[schema(examples("ИС-214/23"))]
|
||||||
pub group: Option<String>,
|
group: String,
|
||||||
|
|
||||||
/// Роль
|
/// Роль
|
||||||
pub role: UserRole,
|
role: UserRole,
|
||||||
|
|
||||||
/// Идентификатор привязанного аккаунта VK
|
/// Идентификатор прявязанного аккаунта VK
|
||||||
#[schema(examples(498094647, json!(null)))]
|
#[schema(examples(498094647, json!(null)))]
|
||||||
pub vk_id: Option<i32>,
|
vk_id: Option<i32>,
|
||||||
|
|
||||||
/// Идентификатор привязанного аккаунта Telegram
|
|
||||||
#[schema(examples(996004735, json!(null)))]
|
|
||||||
pub telegram_id: Option<i64>,
|
|
||||||
|
|
||||||
/// JWT токен доступа
|
/// JWT токен доступа
|
||||||
#[schema(examples(
|
#[schema(examples(
|
||||||
"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6IjY3ZGNjOWE5NTA3YjAwMDA3NzI3NDRhMiIsImlhdCI6IjE3NDMxMDgwOTkiLCJleHAiOiIxODY5MjUyMDk5In0.rMgXRb3JbT9AvLK4eiY9HMB5LxgUudkpQyoWKOypZFY"
|
"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6IjY3ZGNjOWE5NTA3YjAwMDA3NzI3NDRhMiIsImlhdCI6IjE3NDMxMDgwOTkiLCJleHAiOiIxODY5MjUyMDk5In0.rMgXRb3JbT9AvLK4eiY9HMB5LxgUudkpQyoWKOypZFY"
|
||||||
))]
|
))]
|
||||||
#[serde(skip_serializing_if = "Option::is_none")]
|
access_token: String,
|
||||||
pub access_token: Option<String>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl UserResponse {
|
|
||||||
pub fn from_user_with_token(user: User, access_token: String) -> Self {
|
|
||||||
Self {
|
|
||||||
id: user.id.clone(),
|
|
||||||
username: user.username.clone(),
|
|
||||||
group: user.group.clone(),
|
|
||||||
role: user.role.clone(),
|
|
||||||
vk_id: user.vk_id,
|
|
||||||
telegram_id: user.telegram_id,
|
|
||||||
access_token: Some(access_token),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Create UserResponse from User ref.
|
/// Create UserResponse from User ref.
|
||||||
impl From<&User> for UserResponse {
|
impl From<&User> for UserResponse {
|
||||||
fn from(user: &User) -> Self {
|
fn from(user: &User) -> Self {
|
||||||
Self {
|
UserResponse {
|
||||||
id: user.id.clone(),
|
id: user.id.clone(),
|
||||||
username: user.username.clone(),
|
username: user.username.clone(),
|
||||||
group: user.group.clone(),
|
group: user.group.clone(),
|
||||||
role: user.role.clone(),
|
role: user.role.clone(),
|
||||||
vk_id: user.vk_id,
|
vk_id: user.vk_id.clone(),
|
||||||
telegram_id: user.telegram_id,
|
access_token: user.access_token.clone(),
|
||||||
access_token: None,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -199,14 +160,13 @@ pub mod user {
|
|||||||
/// Transform User to UserResponse.
|
/// Transform User to UserResponse.
|
||||||
impl From<User> for UserResponse {
|
impl From<User> for UserResponse {
|
||||||
fn from(user: User) -> Self {
|
fn from(user: User) -> Self {
|
||||||
Self {
|
UserResponse {
|
||||||
id: user.id,
|
id: user.id,
|
||||||
username: user.username,
|
username: user.username,
|
||||||
group: user.group,
|
group: user.group,
|
||||||
role: user.role,
|
role: user.role,
|
||||||
vk_id: user.vk_id,
|
vk_id: user.vk_id,
|
||||||
telegram_id: user.telegram_id,
|
access_token: user.access_token,
|
||||||
access_token: None,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user