57 Commits

Author SHA1 Message Date
e64011ba16 feat!: add telegram auth and async refactor
- Removed "/schedule/update-download-url" endpoint, this mechanism was replaced by Yandex Cloud FaaS. Ура :)
- Improved schedule caching mechanism.
- Added Telegram WebApp authentication support.
- Reworked endpoints responses and errors mechanism.
- Refactored application state management.
- Make synchronous database operations, middlewares and extractors to asynchronous.
- Made user password field optional to support multiple auth methods.
- Renamed users table column "version" to "android_version" and made it nullable.
2025-06-08 01:43:45 +04:00
6a106a366c feat(parser): add ability to parse mistyped date 2025-06-08 01:03:50 +04:00
4fca22662c feat(parser)!: rework of subgroups parsing 2025-06-08 01:03:00 +04:00
d23092a32a feat(parser): add lesson types "course project" and "course project defense" 2025-05-27 02:06:13 +04:00
01bfa38969 feat(parser): speed improvement, lesson type guessing and parsing of merged lesson cabinets 2025-05-27 02:03:54 +04:00
851ec9225f refactor(parser): improve readability 2025-05-26 21:12:23 +04:00
8de1891724 chore(release): bump version to 1.0.5 2025-05-26 05:30:44 +04:00
4cf6df379e fix(parser): fix lessons merging 2025-05-26 05:24:13 +04:00
ba8b164b6a refactor(parser): rewrite some parts of code 2025-05-26 05:24:08 +04:00
ff9d7d6c3a fix(cache): fix setting cache_update_required flag in cache status 2025-05-25 17:39:23 +04:00
9090716f87 fix(test): fix test sign_up_invalid_group 2025-05-25 15:57:18 +04:00
ee992f1b55 chore(xls): update schedule xls 2025-05-25 15:49:52 +04:00
7f71fb1616 refactor(env): remove unsave env::set_var call 2025-05-25 15:48:43 +04:00
234055eaeb feat(test): add ability to use test env without schedule 2025-05-25 15:48:10 +04:00
fceffb900d release/v1.0.3 2025-04-18 00:29:04 +04:00
49ce0005dc Исправление работы подключения к сайтам из-за отсутствия сертификатов. 2025-04-18 00:28:55 +04:00
4c738085f2 release/v1.0.2 2025-04-18 00:11:55 +04:00
20602eb863 Улучшенное отображение ошибок при обновлении ссылки расписания. 2025-04-18 00:11:05 +04:00
e04d462223 1.0.1 2025-04-17 23:08:58 +04:00
22af02464d Исправление работы авторизации с помощью VK ID. 2025-04-17 23:07:19 +04:00
9a517519db User-Agent для reqwest теперь устанавливается с помощью переменной окружения. 2025-04-17 22:41:42 +04:00
65376e75f7 Workflow для публикации релизов.
- Запускает тесты.
- Собирает приложение.
- Отправляет отладочную информацию в Sentry.
- Собирает и отправляет в реестр Docker image с приложением.
- Создаёт релиз со списком изменений и артефактами сборки.
2025-04-17 21:34:46 +04:00
bef6163c1b Отключение тестов при pull request. 2025-04-17 16:39:39 +04:00
283858fea3 Возможный фикс тестов. 2025-04-17 01:10:19 +04:00
66ad4ef938 Подключение sentry. 2025-04-17 01:07:03 +04:00
28f59389ed Исправление тестов.
FCMClient теперь не инициализируется, если отсутствует требуемая переменная окружения.
2025-04-16 16:38:37 +04:00
e71ab0526d Middleware для явного указания кодировки в заголовке Content-Type. 2025-04-16 16:21:53 +04:00
ff05614404 Исправление обработки времени у пар. 2025-04-16 16:21:18 +04:00
9cc03c4ffe Фильтр эндпоинтов для middleware. 2025-04-16 16:20:32 +04:00
5068fe3069 Обновление документации. 2025-04-15 22:09:10 +04:00
2fd6d787a0 Эндпоинт users/change-group. 2025-04-15 19:39:46 +04:00
7a1b32d843 Эндпоинт users/change-username. 2025-04-15 18:55:45 +04:00
542258df01 Эндпоинт fcm/set-token. 2025-04-15 18:44:43 +04:00
ccaabfe909 Асинхронный вариант MutexScope. 2025-04-15 18:44:03 +04:00
4c5e0761eb Подключение FCM. 2025-04-15 14:35:05 +04:00
057dac5b09 Использование функции для осуществления операций в базе данных вместо ручного блокирования мьютекса. 2025-04-15 14:33:58 +04:00
5b6f5c830f Реформат путей к эндпоинтам.
Добавлен экстрактор пользователя с дополнительными полями.

Добавлена связь таблиц User и FCM.

Завершена реализация авторизации с помощью VK ID.

Добавлен эндпоинт fcm/update-callback/{version}.
2025-04-14 22:08:28 +04:00
680419ea78 0.8.0
Реализованы все требуемые эндпоинты schedule.

Улучшена документация.
2025-03-28 23:24:37 +04:00
30c985a3d7 Добавлена возможность создания ResponseError с описанием ошибки.
Добавлен макрос для трансформации ErrorCode в Response, а также для имплементации треита PartialStatusCode.
2025-03-28 15:42:45 +04:00
70a7480ea3 0.7.0
Добавлена OpenAPI документация эндпоинтов и структур с интерфейсом RapiDoc.

Добавлены derive макросы для преобразования структуры в HttpResponse с помощью ResponderJson и IResponse<T> с помощью IntoIResponse.

Ревью кода эндпоинтов связанных с авторизацией.

Эндпоинт users/me теперь объект пользователя в требуемом виде.
2025-03-28 01:21:49 +04:00
1add903f36 0.6.0
Добавлена проверка токена пользователя для перед обработкой запроса.
2025-03-27 20:03:35 +04:00
f703cc8326 0.5.0
Возвращёна реализация сериализации в json для IResponse

Добавлены типы для экстракции данных из запросов средствами actix-web

Добавлен экстрактор для получения пользователя по токену доступа передаваемому в запросе

Добавлен макрос для автоматической реализации ResponseError для ошибок экстракторов

Добавлен эндпоинт users/me

Из главного проекта исключена зависимость actix-http посредством переноса части тестового функционала в отдельный crate
2025-03-26 08:05:22 +04:00
ab1cbd795e 0.4.0
Авторизация через токен вк

Слияние schedule_parser с проектом

Перенос схемы запросов/ответов в файлы эндпоинтов

Переход с библиотеки jwt на jsonwebtokens
2025-03-25 02:05:27 +04:00
0316f58592 Обновление workflow тестов 2025-03-23 06:19:51 +04:00
a95494d3be Регистрация и тесты эндпоинтов 2025-03-23 06:11:13 +04:00
844c89a365 Тесты JWT
Имплементация PartialEq для utils::jwt::VerifyError

Замена устаревшего changeset_options на diesel

Удалена проверка на ошибку создания токена, так как вероятность её появления близка к нулю
2025-03-22 23:14:14 +04:00
ba86dfc3fe Полностью рабочая авторизация 2025-03-22 22:44:52 +04:00
9f7460973e Подключение к Postgres и тестовый эндпоинт авторизации 2025-03-22 03:20:55 +04:00
Nikita
3cf42eea8a Create LICENSE 2025-03-22 00:31:03 +04:00
Nikita
d19b6c1069 Create CODE_OF_CONDUCT.md 2025-03-22 00:30:18 +04:00
126ba23001 Скачивание XLS документа по ссылке 2025-03-21 23:55:16 +04:00
d75d3fbc97 Установка разрешений для Workflow 2025-03-21 21:03:28 +04:00
Nikita
627cf1a74e Create dependabot.yml 2025-03-21 20:59:40 +04:00
b508db693e Добавлена конвертация расписания групп в расписание преподавателей 2025-03-21 20:54:52 +04:00
436d08a56a Добавление README 2025-03-21 07:39:56 +04:00
aa2618c5f5 Action для тестирования 2025-03-21 07:36:39 +04:00
f0a951ad38 Удаление неиспользуемых зависимостей 2025-03-21 07:28:37 +04:00
105 changed files with 9881 additions and 856 deletions

6
.github/dependabot.yml vendored Normal file
View File

@@ -0,0 +1,6 @@
version: 2
updates:
- package-ecosystem: "cargo"
directory: "/"
schedule:
interval: "weekly"

174
.github/workflows/release.yml vendored Normal file
View File

@@ -0,0 +1,174 @@
name: release
on:
push:
tags: [ "release/v*" ]
permissions:
contents: write
env:
CARGO_TERM_COLOR: always
BINARY_NAME: schedule-parser-rusted
TEST_DB: ${{ secrets.TEST_DATABASE_URL }}
SENTRY_AUTH_TOKEN: ${{ secrets.SENTRY_AUTH_TOKEN }}
SENTRY_ORG: ${{ secrets.SENTRY_ORG }}
SENTRY_PROJECT: ${{ secrets.SENTRY_PROJECT }}
DOCKER_IMAGE_NAME: ${{ github.repository }}
DOCKER_REGISTRY_HOST: registry.n08i40k.ru
DOCKER_REGISTRY_USERNAME: ${{ github.repository_owner }}
DOCKER_REGISTRY_PASSWORD: ${{ secrets.DOCKER_REGISTRY_PASSWORD }}
jobs:
test:
name: Test
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Setup Rust
uses: actions-rust-lang/setup-rust-toolchain@v1.11.0
with:
toolchain: stable
- name: Test
run: |
touch .env.test
cargo test --verbose
env:
DATABASE_URL: ${{ env.TEST_DB }}
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

37
.github/workflows/test.yml vendored Normal file
View File

@@ -0,0 +1,37 @@
name: cargo test
on:
push:
branches: [ "master" ]
tags-ignore: [ "release/v*" ]
permissions:
contents: read
env:
CARGO_TERM_COLOR: always
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Build
run: cargo build
- name: Create .env.test
run: touch .env.test
- name: Run tests
run: cargo test
env:
DATABASE_URL: ${{ secrets.TEST_DATABASE_URL }}
SCHEDULE_DISABLE_AUTO_UPDATE: 1
JWT_SECRET: "test-secret-at-least-256-bits-used"
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: ""

6
.gitignore vendored
View File

@@ -1,3 +1,7 @@
/target /target
.~*.xls .~*.xls
schedule.json schedule.json
teachers.json
.env*
/*-firebase-adminsdk-*.json

12
.idea/dataSources.xml generated Normal file
View File

@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="DataSourceManagerImpl" format="xml" multifile-model="true">
<data-source source="LOCAL" name="sp@localhost" uuid="28502a90-08bf-4cc0-8494-10dc74e37189">
<driver-ref>postgresql</driver-ref>
<synchronize>true</synchronize>
<jdbc-driver>org.postgresql.Driver</jdbc-driver>
<jdbc-url>jdbc:postgresql://localhost:5432/sp</jdbc-url>
<working-dir>$ProjectFileDir$</working-dir>
</data-source>
</component>
</project>

14
.idea/discord.xml generated Normal file
View File

@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="DiscordProjectSettings">
<option name="show" value="PROJECT_FILES" />
<option name="description" value="" />
<option name="applicationTheme" value="default" />
<option name="iconsTheme" value="default" />
<option name="button1Title" value="" />
<option name="button1Url" value="" />
<option name="button2Title" value="" />
<option name="button2Url" value="" />
<option name="customApplicationId" value="" />
</component>
</project>

View File

@@ -2,10 +2,16 @@
<module type="EMPTY_MODULE" version="4"> <module type="EMPTY_MODULE" version="4">
<component name="NewModuleRootManager"> <component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$"> <content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/lib/schedule_parser/benches" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/lib/schedule_parser/src" isTestSource="false" /> <sourceFolder url="file://$MODULE_DIR$/lib/schedule_parser/src" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" /> <sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/actix-macros/src" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/actix-test/src" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/schedule-parser/benches" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/schedule-parser/src" isTestSource="false" />
<excludeFolder url="file://$MODULE_DIR$/actix-macros/target" />
<excludeFolder url="file://$MODULE_DIR$/actix-test/target" />
<excludeFolder url="file://$MODULE_DIR$/target" /> <excludeFolder url="file://$MODULE_DIR$/target" />
<excludeFolder url="file://$MODULE_DIR$/.idea/dataSources" />
</content> </content>
<orderEntry type="inheritedJdk" /> <orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" /> <orderEntry type="sourceFolder" forTests="false" />

128
CODE_OF_CONDUCT.md Normal file
View File

@@ -0,0 +1,128 @@
# Contributor Covenant Code of Conduct
## Our Pledge
We as members, contributors, and leaders pledge to make participation in our
community a harassment-free experience for everyone, regardless of age, body
size, visible or invisible disability, ethnicity, sex characteristics, gender
identity and expression, level of experience, education, socio-economic status,
nationality, personal appearance, race, religion, or sexual identity
and orientation.
We pledge to act and interact in ways that contribute to an open, welcoming,
diverse, inclusive, and healthy community.
## Our Standards
Examples of behavior that contributes to a positive environment for our
community include:
* Demonstrating empathy and kindness toward other people
* Being respectful of differing opinions, viewpoints, and experiences
* Giving and gracefully accepting constructive feedback
* Accepting responsibility and apologizing to those affected by our mistakes,
and learning from the experience
* Focusing on what is best not just for us as individuals, but for the
overall community
Examples of unacceptable behavior include:
* The use of sexualized language or imagery, and sexual attention or
advances of any kind
* Trolling, insulting or derogatory comments, and personal or political attacks
* Public or private harassment
* Publishing others' private information, such as a physical or email
address, without their explicit permission
* Other conduct which could reasonably be considered inappropriate in a
professional setting
## Enforcement Responsibilities
Community leaders are responsible for clarifying and enforcing our standards of
acceptable behavior and will take appropriate and fair corrective action in
response to any behavior that they deem inappropriate, threatening, offensive,
or harmful.
Community leaders have the right and responsibility to remove, edit, or reject
comments, commits, code, wiki edits, issues, and other contributions that are
not aligned to this Code of Conduct, and will communicate reasons for moderation
decisions when appropriate.
## Scope
This Code of Conduct applies within all community spaces, and also applies when
an individual is officially representing the community in public spaces.
Examples of representing our community include using an official e-mail address,
posting via an official social media account, or acting as an appointed
representative at an online or offline event.
## Enforcement
Instances of abusive, harassing, or otherwise unacceptable behavior may be
reported to the community leaders responsible for enforcement at
email.
All complaints will be reviewed and investigated promptly and fairly.
All community leaders are obligated to respect the privacy and security of the
reporter of any incident.
## Enforcement Guidelines
Community leaders will follow these Community Impact Guidelines in determining
the consequences for any action they deem in violation of this Code of Conduct:
### 1. Correction
**Community Impact**: Use of inappropriate language or other behavior deemed
unprofessional or unwelcome in the community.
**Consequence**: A private, written warning from community leaders, providing
clarity around the nature of the violation and an explanation of why the
behavior was inappropriate. A public apology may be requested.
### 2. Warning
**Community Impact**: A violation through a single incident or series
of actions.
**Consequence**: A warning with consequences for continued behavior. No
interaction with the people involved, including unsolicited interaction with
those enforcing the Code of Conduct, for a specified period of time. This
includes avoiding interactions in community spaces as well as external channels
like social media. Violating these terms may lead to a temporary or
permanent ban.
### 3. Temporary Ban
**Community Impact**: A serious violation of community standards, including
sustained inappropriate behavior.
**Consequence**: A temporary ban from any sort of interaction or public
communication with the community for a specified period of time. No public or
private interaction with the people involved, including unsolicited interaction
with those enforcing the Code of Conduct, is allowed during this period.
Violating these terms may lead to a permanent ban.
### 4. Permanent Ban
**Community Impact**: Demonstrating a pattern of violation of community
standards, including sustained inappropriate behavior, harassment of an
individual, or aggression toward or disparagement of classes of individuals.
**Consequence**: A permanent ban from any sort of public interaction within
the community.
## Attribution
This Code of Conduct is adapted from the [Contributor Covenant][homepage],
version 2.0, available at
https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
Community Impact Guidelines were inspired by [Mozilla's code of conduct
enforcement ladder](https://github.com/mozilla/diversity).
[homepage]: https://www.contributor-covenant.org
For answers to common questions about this code of conduct, see the FAQ at
https://www.contributor-covenant.org/faq. Translations are available at
https://www.contributor-covenant.org/translations.

2652
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@@ -1,15 +1,50 @@
[workspace] [workspace]
members = ["lib/schedule_parser"] members = ["actix-macros", "actix-test", "schedule-parser"]
[package] [package]
name = "schedule-parser-rusted" name = "schedule-parser-rusted"
version = "0.1.0" version = "1.0.5"
edition = "2024" edition = "2024"
publish = false publish = false
[profile.release]
debug = true
[dependencies] [dependencies]
actix-web = "4.10.2" actix-web = "4.10.2"
actix-macros = { path = "actix-macros" }
schedule-parser = { path = "schedule-parser", features = ["test-utils"] }
bcrypt = "0.17.0"
chrono = { version = "0.4.40", features = ["serde"] }
derive_more = { version = "2", features = ["full"] }
diesel = { version = "2.2.8", features = ["postgres"] }
diesel-derive-enum = { git = "https://github.com/Havunen/diesel-derive-enum.git", features = ["postgres"] }
dotenvy = "0.15.7"
env_logger = "0.11.7"
firebase-messaging-rs = { git = "https://github.com/i10416/firebase-messaging-rs.git" }
futures-util = "0.3.31"
jsonwebtoken = { version = "9.3.1", features = ["use_pem"] }
hex = "0.4.3"
mime = "0.3.17"
objectid = "0.2.0"
reqwest = { version = "0.12.15", features = ["json"] }
sentry = "0.38"
sentry-actix = "0.38"
serde = { version = "1.0.219", features = ["derive"] } serde = { version = "1.0.219", features = ["derive"] }
serde_repr = "0.1.20"
serde_json = "1.0.140" serde_json = "1.0.140"
schedule_parser = { path = "./lib/schedule_parser" } serde_with = "3.12.0"
sha1 = "0.11.0-pre.5"
tokio = { version = "1.44.1", features = ["macros", "rt-multi-thread"] }
utoipa = { version = "5", features = ["actix_extras", "chrono"] }
utoipa-rapidoc = { version = "6.0.0", features = ["actix-web"] }
utoipa-actix-web = "0.1"
uuid = { version = "1.16.0", features = ["v4"] }
ed25519-dalek = "2.1.1"
hex-literal = "1.0.0"
log = "0.4.26"
base64 = "0.22.1"
percent-encoding = "2.3.1"
ua_generator = "0.5.16"
[dev-dependencies]
actix-test = { path = "actix-test" }

14
Dockerfile Normal file
View File

@@ -0,0 +1,14 @@
FROM debian:stable-slim
LABEL authors="n08i40k"
ARG BINARY_NAME
WORKDIR /app/
RUN apt update && \
apt install -y libpq5 ca-certificates openssl
COPY ./${BINARY_NAME} /bin/main
RUN chmod +x /bin/main
ENTRYPOINT ["main"]

21
LICENSE Normal file
View File

@@ -0,0 +1,21 @@
MIT License
Copyright (c) 2025 Nikita
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

3
README.md Normal file
View File

@@ -0,0 +1,3 @@
# API для получения расписания политехникума
[![Rust](https://github.com/n08i40k/schedule-parser-rusted/actions/workflows/test.yml/badge.svg)](https://github.com/n08i40k/schedule-parser-rusted/actions/workflows/test.yml)

1
actix-macros/.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
/target

7
actix-macros/Cargo.lock generated Normal file
View File

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

12
actix-macros/Cargo.toml Normal file
View File

@@ -0,0 +1,12 @@
[package]
name = "actix-macros"
version = "0.1.0"
edition = "2024"
[dependencies]
syn = "2.0.100"
quote = "1.0.40"
proc-macro2 = "1.0.94"
[lib]
proc-macro = true

172
actix-macros/src/lib.rs Normal file
View File

@@ -0,0 +1,172 @@
extern crate proc_macro;
use proc_macro::TokenStream;
mod shared {
use quote::{ToTokens, quote};
use syn::{Attribute, DeriveInput};
pub fn find_status_code(attrs: &Vec<Attribute>) -> Option<proc_macro2::TokenStream> {
attrs
.iter()
.find_map(|attr| -> Option<proc_macro2::TokenStream> {
if !attr.path().is_ident("status_code") {
return None;
}
let meta = attr.meta.require_name_value().ok()?;
let code = meta.value.to_token_stream().to_string();
let trimmed_code = code.trim_matches('"');
if let Ok(numeric_code) = trimmed_code.parse::<u16>() {
Some(quote! { actix_web::http::StatusCode::from_u16(#numeric_code).unwrap() })
} else {
let string_code: proc_macro2::TokenStream =
trimmed_code.to_string().parse().unwrap();
Some(quote! { #string_code })
}
})
}
pub fn get_arms(ast: &DeriveInput) -> Vec<proc_macro2::TokenStream> {
let name = &ast.ident;
let variants = if let syn::Data::Enum(data) = &ast.data {
&data.variants
} else {
panic!("Only enums are supported");
};
let mut status_code_arms: Vec<proc_macro2::TokenStream> = variants
.iter()
.map(|v| -> Option<proc_macro2::TokenStream> {
let status_code = find_status_code(&v.attrs)?;
let variant_name = &v.ident;
Some(quote! { #name::#variant_name => #status_code, })
})
.filter(|v| v.is_some())
.map(|v| v.unwrap())
.collect();
if status_code_arms.len() < variants.len() {
let status_code = find_status_code(&ast.attrs)
.unwrap_or_else(|| quote! { ::actix_web::http::StatusCode::INTERNAL_SERVER_ERROR });
status_code_arms.push(quote! { _ => #status_code });
}
status_code_arms
}
}
mod middleware_error {
use proc_macro::TokenStream;
use quote::quote;
pub fn fmt(ast: &syn::DeriveInput) -> TokenStream {
let name = &ast.ident;
let status_code_arms = super::shared::get_arms(ast);
TokenStream::from(quote! {
impl ::actix_web::ResponseError for #name {
fn status_code(&self) -> ::actix_web::http::StatusCode {
match self {
#(#status_code_arms)*
}
}
fn error_response(&self) -> ::actix_web::HttpResponse<BoxBody> {
::actix_web::HttpResponse::build(self.status_code())
.json(crate::utility::error::MiddlewareError::new(self.clone()))
}
}
})
}
}
mod responder_json {
use proc_macro::TokenStream;
use quote::quote;
pub fn fmt(ast: &syn::DeriveInput) -> TokenStream {
let name = &ast.ident;
TokenStream::from(quote! {
impl ::actix_web::Responder for #name {
type Body = ::actix_web::body::EitherBody<::actix_web::body::BoxBody>;
fn respond_to(self, _: &::actix_web::HttpRequest) -> ::actix_web::HttpResponse<Self::Body> {
::actix_web::HttpResponse::Ok()
.json(self)
.map_into_left_body()
}
}
})
}
}
mod ok_response {
use proc_macro::TokenStream;
use quote::quote;
pub fn fmt(ast: &syn::DeriveInput) -> TokenStream {
let name = &ast.ident;
TokenStream::from(quote! {
impl crate::routes::schema::PartialOkResponse for #name {}
})
}
}
mod err_response {
use proc_macro::TokenStream;
use quote::quote;
pub fn fmt(ast: &syn::DeriveInput) -> TokenStream {
let name = &ast.ident;
let status_code_arms = super::shared::get_arms(ast);
TokenStream::from(quote! {
impl crate::routes::schema::PartialErrResponse for #name {
fn status_code(&self) -> ::actix_web::http::StatusCode {
match self {
#(#status_code_arms)*
}
}
}
})
}
}
#[proc_macro_derive(MiddlewareError, attributes(status_code))]
pub fn moddleware_error_derive(input: TokenStream) -> TokenStream {
let ast = syn::parse(input).unwrap();
middleware_error::fmt(&ast)
}
#[proc_macro_derive(ResponderJson)]
pub fn responser_json_derive(input: TokenStream) -> TokenStream {
let ast = syn::parse(input).unwrap();
responder_json::fmt(&ast)
}
#[proc_macro_derive(OkResponse)]
pub fn ok_response_derive(input: TokenStream) -> TokenStream {
let ast = syn::parse(input).unwrap();
ok_response::fmt(&ast)
}
#[proc_macro_derive(ErrResponse, attributes(status_code))]
pub fn err_response_derive(input: TokenStream) -> TokenStream {
let ast = syn::parse(input).unwrap();
err_response::fmt(&ast)
}

1
actix-test/.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
/target

1520
actix-test/Cargo.lock generated Normal file

File diff suppressed because it is too large Load Diff

8
actix-test/Cargo.toml Normal file
View File

@@ -0,0 +1,8 @@
[package]
name = "actix-test"
version = "0.1.0"
edition = "2024"
[dependencies]
actix-http = "3.10.0"
actix-web = "4.10.2"

12
actix-test/src/lib.rs Normal file
View File

@@ -0,0 +1,12 @@
use actix_web::dev::{HttpServiceFactory, Service, ServiceResponse};
use actix_web::{App, test, web};
pub async fn test_app<F, A: 'static>(
app_state: web::Data<A>,
factory: F,
) -> impl Service<actix_http::Request, Response = ServiceResponse, Error = actix_web::Error>
where
F: HttpServiceFactory + 'static,
{
test::init_service(App::new().app_data(app_state).service(factory)).await
}

9
diesel.toml Normal file
View 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"

View File

@@ -1,23 +0,0 @@
[package]
name = "schedule_parser"
version = "0.1.0"
edition = "2024"
[lib]
name = "schedule_parser"
path = "src/lib/lib.rs"
[dependencies]
serde = { version = "1.0.219", features = ["derive"] }
serde_repr = "0.1.20"
chrono = { version = "0.4.40", features = ["serde"] }
calamine = "0.26.1"
regex = "1.11.1"
fuzzy-matcher = "0.3.7"
[dev-dependencies]
criterion = "0.5.1"
[[bench]]
name = "parse"
harness = false

View File

@@ -1,627 +0,0 @@
use crate::LessonParseResult::{Lessons, Street};
use crate::schema::LessonType::Break;
use crate::schema::{Day, Group, Lesson, LessonSubGroup, LessonTime, LessonType};
use calamine::{Reader, Xls, open_workbook};
use chrono::{Duration, NaiveDateTime};
use fuzzy_matcher::FuzzyMatcher;
use fuzzy_matcher::skim::SkimMatcherV2;
use regex::Regex;
use std::collections::HashMap;
use std::path::Path;
use std::sync::LazyLock;
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) -> (Vec<InternalId>, Vec<InternalId>) {
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().expect("Could not find start");
let end = range.end().expect("Could not find end");
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;
}
}
(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,
) -> LessonParseResult {
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 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 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): (Option<[u8; 2]>, LessonTime) = {
// 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().expect("Unable to find lesson time!");
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,
};
(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 Lessons(Vec::from([lesson]));
} else {
&day.lessons[day.lessons.len() - 1]
};
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) -> (String, Vec<LessonSubGroup>) {
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 NAME_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();
(
NAME_CLEAN_RE.replace(&capture_str, "").to_string(),
name[0..name.find(&*capture_name).unwrap()].to_string(),
)
} else {
return (NAME_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>()
.expect("Unable to read subgroup index!")
} 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()
}
(lesson_name, subgroups)
}
pub fn parse_xls(path: &Path) -> HashMap<String, Group> {
let mut workbook: Xls<_> = open_workbook(path).expect("Can't open workbook");
let worksheet: WorkSheet = workbook
.worksheets()
.first()
.expect("No worksheet found")
.1
.to_owned();
let (days_markup, groups_markup) = parse_skeleton(&worksheet);
let mut groups: HashMap<String, Group> = 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 = Group {
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)
.expect("Unable to obtain lesson start and end!");
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);
}
groups
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn it_works() {
let result = parse_xls(Path::new("../../schedule.xls"));
assert_ne!(result.len(), 0);
}
}

View File

@@ -1,97 +0,0 @@
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use serde_repr::{Deserialize_repr, Serialize_repr};
#[derive(Serialize, Deserialize, Debug)]
pub struct LessonTime {
pub start: DateTime<Utc>,
pub end: DateTime<Utc>,
}
#[derive(Serialize_repr, Deserialize_repr, Debug, PartialEq, Clone)]
#[repr(u8)]
pub enum LessonType {
Default = 0, // Обычная
Additional, // Допы
Break, // Перемена
Consultation, // Консультация
IndependentWork, // Самостоятельная работа
Exam, // Зачёт
ExamWithGrade, // Зачет с оценкой
ExamDefault, // Экзамен
}
#[derive(Serialize, Deserialize, Debug)]
pub struct LessonSubGroup {
pub number: u8,
pub cabinet: Option<String>,
pub teacher: String,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct Lesson {
/**
* Тип занятия
*/
#[serde(rename = "type")]
pub lesson_type: LessonType,
/**
* Индексы пар, если присутствуют
*/
#[serde(rename = "defaultRange")]
pub default_range: Option<[u8; 2]>,
/**
* Название занятия
*/
pub name: Option<String>,
/**
* Начало и конец занятия
*/
pub time: LessonTime,
/**
* Подгруппы
*/
#[serde(rename = "subGroups")]
pub subgroups: Option<Vec<LessonSubGroup>>,
/**
* Группа (только для расписания преподавателей)
*/
pub group: Option<String>,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct Day {
pub name: String,
pub street: Option<String>,
pub date: DateTime<Utc>,
pub lessons: Vec<Lesson>,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct Group {
pub name: String,
pub days: Vec<Day>,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct Schedule {
#[serde(rename = "updatedAt")]
pub updated_at: DateTime<Utc>,
pub groups: HashMap<String, Group>,
#[serde(rename = "updatedGroups")]
pub updated_groups: Vec<Vec<usize>>,
}

0
migrations/.keep Normal file
View File

View File

@@ -0,0 +1,6 @@
-- This file was automatically created by Diesel to set up 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();

View File

@@ -0,0 +1,36 @@
-- This file was automatically created by Diesel to set up 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;

View File

@@ -0,0 +1 @@
DROP TYPE user_role;

View File

@@ -0,0 +1,4 @@
CREATE TYPE user_role AS ENUM (
'STUDENT',
'TEACHER',
'ADMIN');

View File

@@ -0,0 +1 @@
DROP TABLE users;

View 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
);

View File

@@ -0,0 +1 @@
DROP TABLE fcm;

View File

@@ -0,0 +1,6 @@
CREATE TABLE fcm
(
user_id text PRIMARY KEY NOT NULL REFERENCES users (id),
token text NOT NULL,
topics text[] NOT NULL CHECK ( array_position(topics, null) is null )
);

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@@ -0,0 +1,26 @@
[package]
name = "schedule-parser"
version = "0.1.0"
edition = "2024"
[features]
test-utils = []
[dependencies]
calamine = "0.26"
chrono = { version = "0.4", features = ["serde"] }
derive_more = { version = "2", features = ["full"] }
sentry = "0.38"
serde = { version = "1.0.219", features = ["derive"] }
serde_repr = "0.1.20"
regex = "1.11.1"
utoipa = { version = "5", features = ["chrono"] }
strsim = "0.11.1"
log = "0.4.26"
[dev-dependencies]
criterion = "0.6"
[[bench]]
name = "parse"
harness = false

View File

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

800
schedule-parser/src/lib.rs Normal file
View File

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

View File

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

View File

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

View File

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

Binary file not shown.

164
src/database/driver.rs Normal file
View File

@@ -0,0 +1,164 @@
pub mod users {
use crate::database::models::User;
use crate::database::schema::users::dsl::users;
use crate::database::schema::users::dsl::*;
use crate::state::AppState;
use actix_web::web;
use diesel::{ExpressionMethods, QueryResult, insert_into};
use diesel::{QueryDsl, RunQueryDsl};
use diesel::{SaveChangesDsl, SelectableHelper};
use std::ops::DerefMut;
pub async fn get(state: &web::Data<AppState>, _id: &String) -> QueryResult<User> {
users
.filter(id.eq(_id))
.select(User::as_select())
.first(state.get_database().await.deref_mut())
}
pub async fn get_by_username(
state: &web::Data<AppState>,
_username: &String,
) -> QueryResult<User> {
users
.filter(username.eq(_username))
.select(User::as_select())
.first(state.get_database().await.deref_mut())
}
//noinspection RsTraitObligations
pub async fn get_by_vk_id(state: &web::Data<AppState>, _vk_id: i32) -> QueryResult<User> {
users
.filter(vk_id.eq(_vk_id))
.select(User::as_select())
.first(state.get_database().await.deref_mut())
}
//noinspection RsTraitObligations
pub async fn get_by_telegram_id(
state: &web::Data<AppState>,
_telegram_id: i64,
) -> QueryResult<User> {
users
.filter(telegram_id.eq(_telegram_id))
.select(User::as_select())
.first(state.get_database().await.deref_mut())
}
//noinspection DuplicatedCode
pub async fn contains_by_username(state: &web::Data<AppState>, _username: &String) -> bool {
// и как это нахуй сократить блять примеров нихуя нет, нихуя не работает
// как меня этот раст заебал уже
match users
.filter(username.eq(_username))
.count()
.get_result::<i64>(state.get_database().await.deref_mut())
{
Ok(count) => count > 0,
Err(_) => false,
}
}
//noinspection DuplicatedCode
//noinspection RsTraitObligations
pub async fn contains_by_vk_id(state: &web::Data<AppState>, _vk_id: i32) -> bool {
match users
.filter(vk_id.eq(_vk_id))
.count()
.get_result::<i64>(state.get_database().await.deref_mut())
{
Ok(count) => count > 0,
Err(_) => false,
}
}
pub async fn insert(state: &web::Data<AppState>, user: &User) -> QueryResult<usize> {
insert_into(users)
.values(user)
.execute(state.get_database().await.deref_mut())
}
/// Function declaration [User::save][UserSave::save].
pub trait UserSave {
/// Saves the user's changes to the database.
///
/// # Arguments
///
/// * `state`: The state of the actix-web application that stores the mutex of the [connection][diesel::PgConnection].
///
/// returns: `QueryResult<User>`
///
/// # Examples
///
/// ```
/// use crate::database::driver::users;
///
/// #[derive(Deserialize)]
/// struct Params {
/// pub username: String,
/// }
///
/// #[patch("/")]
/// async fn patch_user(
/// app_state: web::Data<AppState>,
/// user: SyncExtractor<User>,
/// web::Query(params): web::Query<Params>,
/// ) -> web::Json<User> {
/// let mut user = user.into_inner();
///
/// user.username = params.username;
///
/// match user.save(&app_state) {
/// Ok(user) => web::Json(user),
/// Err(e) => {
/// eprintln!("Failed to save user: {e}");
/// panic!();
/// }
/// }
/// }
/// ```
async fn save(&self, state: &web::Data<AppState>) -> QueryResult<User>;
}
/// Implementation of [UserSave][UserSave] trait.
impl UserSave for User {
async fn save(&self, state: &web::Data<AppState>) -> QueryResult<User> {
self.save_changes::<Self>(state.get_database().await.deref_mut())
}
}
#[cfg(test)]
pub async fn delete_by_username(state: &web::Data<AppState>, _username: &String) -> bool {
match diesel::delete(users.filter(username.eq(_username)))
.execute(state.get_database().await.deref_mut())
{
Ok(count) => count > 0,
Err(_) => false,
}
}
#[cfg(test)]
pub async fn insert_or_ignore(state: &web::Data<AppState>, user: &User) -> QueryResult<usize> {
insert_into(users)
.values(user)
.on_conflict_do_nothing()
.execute(state.get_database().await.deref_mut())
}
}
pub mod fcm {
use crate::database::models::{FCM, User};
use crate::state::AppState;
use actix_web::web;
use diesel::QueryDsl;
use diesel::RunQueryDsl;
use diesel::{BelongingToDsl, QueryResult, SelectableHelper};
use std::ops::DerefMut;
pub async fn from_user(state: &web::Data<AppState>, user: &User) -> QueryResult<FCM> {
FCM::belonging_to(&user)
.select(FCM::as_select())
.get_result(state.get_database().await.deref_mut())
}
}

3
src/database/mod.rs Normal file
View File

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

87
src/database/models.rs Normal file
View File

@@ -0,0 +1,87 @@
use actix_macros::ResponderJson;
use diesel::QueryId;
use diesel::prelude::*;
use serde::{Deserialize, Serialize};
use utoipa::ToSchema;
#[derive(
Copy, Clone, PartialEq, Debug, Serialize, Deserialize, diesel_derive_enum::DbEnum, ToSchema,
)]
#[ExistingTypePath = "crate::database::schema::sql_types::UserRole"]
#[DbValueStyle = "UPPERCASE"]
#[serde(rename_all = "UPPERCASE")]
pub enum UserRole {
Student,
Teacher,
Admin,
}
#[derive(
Identifiable,
AsChangeset,
Queryable,
QueryId,
Selectable,
Serialize,
Insertable,
Debug,
ToSchema,
ResponderJson,
)]
#[diesel(table_name = crate::database::schema::users)]
#[diesel(treat_none_as_null = true)]
pub struct User {
/// Account UUID.
pub id: String,
/// User name.
pub username: String,
/// BCrypt password hash.
pub password: Option<String>,
/// ID of the linked VK account.
pub vk_id: Option<i32>,
/// JWT access token.
pub access_token: Option<String>,
/// Group.
pub group: Option<String>,
/// Role.
pub role: UserRole,
/// Version of the installed Polytechnic+ application.
pub android_version: Option<String>,
/// ID of the linked Telegram account.
pub telegram_id: Option<i64>,
}
#[derive(
Debug,
Clone,
Serialize,
Identifiable,
Queryable,
Selectable,
Insertable,
AsChangeset,
Associations,
ToSchema,
ResponderJson,
)]
#[diesel(belongs_to(User))]
#[diesel(table_name = crate::database::schema::fcm)]
#[diesel(primary_key(user_id))]
pub struct FCM {
/// Account UUID.
pub user_id: String,
/// FCM token.
pub token: String,
/// List of topics subscribed to by the user.
pub topics: Vec<Option<String>>,
}

39
src/database/schema.rs Normal file
View File

@@ -0,0 +1,39 @@
// @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 -> Array<Nullable<Text>>,
}
}
diesel::table! {
use diesel::sql_types::*;
use super::sql_types::UserRole;
users (id) {
id -> Text,
username -> Text,
password -> Nullable<Text>,
vk_id -> Nullable<Int4>,
access_token -> Nullable<Text>,
group -> Nullable<Text>,
role -> UserRole,
android_version -> Nullable<Text>,
telegram_id -> Nullable<Int8>,
}
}
diesel::joinable!(fcm -> users (user_id));
diesel::allow_tables_to_appear_in_same_query!(
fcm,
users,
);

View File

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

156
src/extractors/base.rs Normal file
View File

@@ -0,0 +1,156 @@
use actix_web::dev::Payload;
use actix_web::{FromRequest, HttpRequest};
use futures_util::future::LocalBoxFuture;
use std::future::{Ready, ready};
use std::ops;
/// # Async extractor.
/// Asynchronous object extractor from a query.
pub struct AsyncExtractor<T>(T);
impl<T> AsyncExtractor<T> {
#[allow(dead_code)]
/// Retrieve the object extracted with the extractor.
pub fn into_inner(self) -> T {
self.0
}
}
impl<T> ops::Deref for AsyncExtractor<T> {
type Target = T;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl<T> ops::DerefMut for AsyncExtractor<T> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}
pub trait FromRequestAsync: Sized {
type Error: Into<actix_web::Error>;
/// Asynchronous function for extracting data from a query.
///
/// returns: Result<Self, Self::Error>
///
/// # Examples
///
/// ```
/// struct User {
/// pub id: String,
/// pub username: String,
/// }
///
/// // TODO: Я вообще этот экстрактор не использую, нахуя мне тогда писать пример, если я не ебу как его использовать. Я забыл.
///
/// #[get("/")]
/// fn get_user_async(
/// user: web::AsyncExtractor<User>,
/// ) -> web::Json<User> {
/// let user = user.into_inner();
///
/// web::Json(user)
/// }
/// ```
async fn from_request_async(
req: &HttpRequest,
payload: &mut Payload,
) -> Result<Self, Self::Error>;
}
impl<T: FromRequestAsync> FromRequest for AsyncExtractor<T> {
type Error = T::Error;
type Future = LocalBoxFuture<'static, Result<Self, Self::Error>>;
fn from_request(req: &HttpRequest, _payload: &mut Payload) -> Self::Future {
let req = req.clone();
let mut payload = Payload::None;
Box::pin(async move {
T::from_request_async(&req, &mut payload)
.await
.map(|res| Self(res))
})
}
}
/// # Sync extractor.
/// Synchronous object extractor from a query.
pub struct SyncExtractor<T>(T);
impl<T> SyncExtractor<T> {
/// Retrieving an object extracted with the extractor.
#[allow(unused)]
pub fn into_inner(self) -> T {
self.0
}
}
impl<T> ops::Deref for SyncExtractor<T> {
type Target = T;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl<T> ops::DerefMut for SyncExtractor<T> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}
pub trait FromRequestSync: Sized {
type Error: Into<actix_web::Error>;
/// Synchronous function for extracting data from a query.
///
/// returns: Result<Self, Self::Error>
///
/// # Examples
///
/// ```
/// struct User {
/// pub id: String,
/// pub username: String,
/// }
///
/// impl FromRequestSync for User {
/// type Error = actix_web::Error;
///
/// fn from_request_sync(req: &HttpRequest, _: &mut Payload) -> Result<Self, Self::Error> {
/// // do magic here.
///
/// Ok(User {
/// id: "qwerty".to_string(),
/// username: "n08i40k".to_string()
/// })
/// }
/// }
///
/// #[get("/")]
/// fn get_user_sync(
/// user: web::SyncExtractor<User>,
/// ) -> web::Json<User> {
/// let user = user.into_inner();
///
/// web::Json(user)
/// }
/// ```
fn from_request_sync(req: &HttpRequest, payload: &mut Payload) -> Result<Self, Self::Error>;
}
impl<T: FromRequestSync> FromRequest for SyncExtractor<T> {
type Error = T::Error;
type Future = Ready<Result<Self, Self::Error>>;
fn from_request(req: &HttpRequest, payload: &mut Payload) -> Self::Future {
ready(T::from_request_sync(req, payload).map(|res| Self(res)))
}
}

2
src/extractors/mod.rs Normal file
View File

@@ -0,0 +1,2 @@
pub mod authorized_user;
pub mod base;

View File

@@ -1,15 +1,129 @@
use std::fs; use crate::middlewares::authorization::JWTAuthorization;
use std::path::Path; use crate::middlewares::content_type::ContentTypeBootstrap;
use schedule_parser::parse_xls; use crate::state::{AppState, new_app_state};
use actix_web::dev::{ServiceFactory, ServiceRequest};
use actix_web::{App, Error, HttpServer};
use dotenvy::dotenv;
use log::info;
use std::io;
use utoipa_actix_web::AppExt;
use utoipa_actix_web::scope::Scope;
use utoipa_rapidoc::RapiDoc;
fn main() { mod state;
let groups = parse_xls(Path::new("./schedule.xls"));
fs::write( mod database;
"./schedule.json",
serde_json::to_string_pretty(&groups) mod xls_downloader;
.expect("Failed to serialize schedule!")
.as_bytes(), mod extractors;
) mod middlewares;
.expect("Failed to write schedule"); mod routes;
mod utility;
mod test_env;
pub fn get_api_scope<
I: Into<Scope<T>>,
T: ServiceFactory<ServiceRequest, Config = (), Error = Error, InitError = ()>,
>(
scope: I,
) -> Scope<T> {
let auth_scope = utoipa_actix_web::scope("/auth")
.service(routes::auth::sign_in)
.service(routes::auth::sign_in_vk)
.service(routes::auth::sign_up)
.service(routes::auth::sign_up_vk);
let users_scope = utoipa_actix_web::scope("/users")
.wrap(JWTAuthorization::default())
.service(routes::users::change_group)
.service(routes::users::change_username)
.service(routes::users::me);
let schedule_scope = utoipa_actix_web::scope("/schedule")
.wrap(JWTAuthorization {
ignore: &["/group-names", "/teacher-names"],
})
.service(routes::schedule::schedule)
.service(routes::schedule::cache_status)
.service(routes::schedule::group)
.service(routes::schedule::group_names)
.service(routes::schedule::teacher)
.service(routes::schedule::teacher_names);
let fcm_scope = utoipa_actix_web::scope("/fcm")
.wrap(JWTAuthorization::default())
.service(routes::fcm::update_callback)
.service(routes::fcm::set_token);
let flow_scope = utoipa_actix_web::scope("/flow")
.wrap(JWTAuthorization {
ignore: &["/telegram-auth"],
})
.service(routes::flow::telegram_auth)
.service(routes::flow::telegram_complete);
let vk_id_scope = utoipa_actix_web::scope("/vkid") //
.service(routes::vk_id::oauth);
utoipa_actix_web::scope(scope)
.service(auth_scope)
.service(users_scope)
.service(schedule_scope)
.service(fcm_scope)
.service(flow_scope)
.service(vk_id_scope)
}
async fn async_main() -> io::Result<()> {
info!("Запуск сервера...");
let app_state = new_app_state().await.unwrap();
HttpServer::new(move || {
let (app, api) = App::new()
.into_utoipa_app()
.app_data(app_state.clone())
.service(
get_api_scope("/api/v1")
.wrap(sentry_actix::Sentry::new())
.wrap(ContentTypeBootstrap),
)
.split_for_parts();
let rapidoc_service = RapiDoc::with_openapi("/api-docs-json", api).path("/api-docs");
// Because CORS error on non-localhost
let patched_rapidoc_html = rapidoc_service.to_html().replace(
"https://unpkg.com/rapidoc/dist/rapidoc-min.js",
"https://cdn.jsdelivr.net/npm/rapidoc/dist/rapidoc-min.min.js",
);
app.service(rapidoc_service.custom_html(patched_rapidoc_html))
})
.workers(4)
.bind(("0.0.0.0", 5050))?
.run()
.await
}
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()
},
));
dotenv().unwrap();
env_logger::init();
actix_web::rt::System::new().block_on(async { async_main().await })?;
Ok(())
} }

View File

@@ -0,0 +1,115 @@
use crate::database::models::User;
use crate::extractors::authorized_user;
use crate::extractors::base::FromRequestAsync;
use actix_web::body::{BoxBody, EitherBody};
use actix_web::dev::{Payload, Service, ServiceRequest, ServiceResponse, Transform, forward_ready};
use actix_web::{Error, HttpRequest, ResponseError};
use futures_util::future::LocalBoxFuture;
use std::future::{Ready, ready};
use std::rc::Rc;
/// Middleware guard working with JWT tokens.
pub struct JWTAuthorization {
/// List of ignored endpoints.
pub ignore: &'static [&'static str],
}
impl Default for JWTAuthorization {
fn default() -> Self {
Self { ignore: &[] }
}
}
impl<S, B> Transform<S, ServiceRequest> for JWTAuthorization
where
S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = Error> + 'static,
S::Future: 'static,
B: 'static,
{
type Response = ServiceResponse<EitherBody<B, BoxBody>>;
type Error = Error;
type Transform = JWTAuthorizationMiddleware<S>;
type InitError = ();
type Future = Ready<Result<Self::Transform, Self::InitError>>;
fn new_transform(&self, service: S) -> Self::Future {
ready(Ok(JWTAuthorizationMiddleware {
service: Rc::new(service),
ignore: self.ignore,
}))
}
}
pub struct JWTAuthorizationMiddleware<S> {
service: Rc<S>,
/// List of ignored endpoints.
ignore: &'static [&'static str],
}
impl<S, B> JWTAuthorizationMiddleware<S>
where
S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = Error>,
S::Future: 'static,
B: 'static,
{
/// Checking the validity of the token.
async fn check_authorization(req: &HttpRequest) -> Result<(), authorized_user::Error> {
let mut payload = Payload::None;
User::from_request_async(req, &mut payload)
.await
.map(|_| ())
.map_err(|e| e.as_error::<authorized_user::Error>().unwrap().clone())
}
fn should_skip(&self, req: &ServiceRequest) -> bool {
let path = req.match_info().unprocessed();
self.ignore.iter().any(|ignore| {
if !path.starts_with(ignore) {
return false;
}
if let Some(other) = path.as_bytes().iter().nth(ignore.len()) {
return ['?' as u8, '/' as u8].contains(other);
}
true
})
}
}
impl<S, B> Service<ServiceRequest> for JWTAuthorizationMiddleware<S>
where
S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = Error> + 'static,
S::Future: 'static,
B: 'static,
{
type Response = ServiceResponse<EitherBody<B, BoxBody>>;
type Error = Error;
type Future = LocalBoxFuture<'static, Result<Self::Response, Self::Error>>;
forward_ready!(service);
fn call(&self, req: ServiceRequest) -> Self::Future {
if self.should_skip(&req) {
let fut = self.service.call(req);
return Box::pin(async move { Ok(fut.await?.map_into_left_body()) });
}
let service = Rc::clone(&self.service);
Box::pin(async move {
match Self::check_authorization(req.request()).await {
Ok(_) => {
let fut = service.call(req).await?;
Ok(fut.map_into_left_body())
}
Err(err) => Ok(ServiceResponse::new(
req.into_parts().0,
err.error_response().map_into_right_body(),
)),
}
})
}
}

View File

@@ -0,0 +1,64 @@
use actix_web::Error;
use actix_web::body::{BoxBody, EitherBody};
use actix_web::dev::{Service, ServiceRequest, ServiceResponse, Transform, forward_ready};
use actix_web::http::header;
use actix_web::http::header::HeaderValue;
use futures_util::future::LocalBoxFuture;
use std::future::{Ready, ready};
/// Middleware to specify the encoding in the Content-Type header.
pub struct ContentTypeBootstrap;
impl<S, B> Transform<S, ServiceRequest> for ContentTypeBootstrap
where
S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = Error>,
S::Future: 'static,
B: 'static,
{
type Response = ServiceResponse<EitherBody<B, BoxBody>>;
type Error = Error;
type Transform = ContentTypeMiddleware<S>;
type InitError = ();
type Future = Ready<Result<Self::Transform, Self::InitError>>;
fn new_transform(&self, service: S) -> Self::Future {
ready(Ok(ContentTypeMiddleware { service }))
}
}
pub struct ContentTypeMiddleware<S> {
service: S,
}
impl<'a, S, B> Service<ServiceRequest> for ContentTypeMiddleware<S>
where
S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = Error>,
S::Future: 'static,
B: 'static,
{
type Response = ServiceResponse<EitherBody<B, BoxBody>>;
type Error = Error;
type Future = LocalBoxFuture<'static, Result<Self::Response, Self::Error>>;
forward_ready!(service);
fn call(&self, req: ServiceRequest) -> Self::Future {
let fut = self.service.call(req);
Box::pin(async move {
let mut response = fut.await?;
let headers = response.response_mut().headers_mut();
if let Some(content_type) = headers.get("Content-Type") {
if content_type == "application/json" {
headers.insert(
header::CONTENT_TYPE,
HeaderValue::from_static("application/json; charset=utf8"),
);
}
}
Ok(response.map_into_left_body())
})
}
}

2
src/middlewares/mod.rs Normal file
View File

@@ -0,0 +1,2 @@
pub mod authorization;
pub mod content_type;

8
src/routes/auth/mod.rs Normal file
View File

@@ -0,0 +1,8 @@
mod shared;
mod sign_in;
mod sign_up;
pub use sign_in::*;
pub use sign_up::*;
// TODO: change-password

83
src/routes/auth/shared.rs Normal file
View File

@@ -0,0 +1,83 @@
use jsonwebtoken::errors::ErrorKind;
use jsonwebtoken::{Algorithm, DecodingKey, Validation, decode};
use serde::{Deserialize, Serialize};
#[derive(Deserialize, Serialize)]
struct TokenData {
iis: String,
sub: i32,
app: i32,
exp: i32,
iat: i32,
jti: i32,
}
#[derive(Debug, Serialize, Deserialize)]
struct Claims {
sub: i32,
iis: String,
jti: i32,
app: i32,
}
#[derive(Debug, PartialEq)]
pub enum Error {
JwtError(ErrorKind),
InvalidSignature,
InvalidToken,
Expired,
UnknownIssuer(String),
UnknownType(i32),
UnknownClientId(i32),
}
//noinspection SpellCheckingInspection
const VK_PUBLIC_KEY: &str = concat!(
"-----BEGIN PUBLIC KEY-----\n",
"MIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKCAgEAvsvJlhFX9Ju/pvCz1frB\n",
"DgJs592VjdwQuRAmnlJAItyHkoiDIOEocPzgcUBTbDf1plDcTyO2RCkUt0pz0WK6\n",
"6HNhpJyIfARjaWHeUlv4TpuHXAJJsBKklkU2gf1cjID+40sWWYjtq5dAkXnSJUVA\n",
"UR+sq0lJ7GmTdJtAr8hzESqGEcSP15PTs7VUdHZ1nkC2XgkuR8KmKAUb388ji1Q4\n",
"n02rJNOPQgd9r0ac4N2v/yTAFPXumO78N25bpcuWf5vcL9e8THk/U2zt7wf+aAWL\n",
"748e0pREqNluTBJNZfmhC79Xx6GHtwqHyyduiqfPmejmiujNM/rqnA4e30Tg86Yn\n",
"cNZ6vLJyF72Eva1wXchukH/aLispbY+EqNPxxn4zzCWaLKHG87gaCxpVv9Tm0jSD\n",
"2es22NjrUbtb+2pAGnXbyDp2eGUqw0RrTQFZqt/VcmmSCE45FlcZMT28otrwG1ZB\n",
"kZAb5Js3wLEch3ZfYL8sjhyNRPBmJBrAvzrd8qa3rdUjkC9sKyjGAaHu2MNmFl1Y\n",
"JFQ3J54tGpkGgJjD7Kz3w0K6OiPDlVCNQN5sqXm24fCw85Pbi8SJiaLTp/CImrs1\n",
"Z3nHW5q8hljA7OGmqfOP0nZS/5zW9GHPyepsI1rW6CympYLJ15WeNzePxYS5KEX9\n",
"EncmkSD9b45ge95hJeJZteUCAwEAAQ==\n",
"-----END PUBLIC KEY-----"
);
pub fn parse_vk_id(token_str: &String, client_id: i32) -> Result<i32, Error> {
let dkey = DecodingKey::from_rsa_pem(VK_PUBLIC_KEY.as_bytes()).unwrap();
match decode::<Claims>(&token_str, &dkey, &Validation::new(Algorithm::RS256)) {
Ok(token_data) => {
let claims = token_data.claims;
if claims.iis != "VK" {
Err(Error::UnknownIssuer(claims.iis))
} else if claims.jti != 21 {
Err(Error::UnknownType(claims.jti))
} else if claims.app != client_id {
Err(Error::UnknownClientId(claims.app))
} else {
Ok(claims.sub)
}
}
Err(err) => Err(match err.into_kind() {
ErrorKind::InvalidToken => Error::InvalidToken,
ErrorKind::InvalidSignature => Error::InvalidSignature,
ErrorKind::InvalidAlgorithmName => Error::InvalidToken,
ErrorKind::MissingRequiredClaim(_) => Error::InvalidToken,
ErrorKind::ExpiredSignature => Error::Expired,
ErrorKind::InvalidAlgorithm => Error::InvalidToken,
ErrorKind::MissingAlgorithm => Error::InvalidToken,
ErrorKind::Base64(_) => Error::InvalidToken,
ErrorKind::Json(_) => Error::InvalidToken,
ErrorKind::Utf8(_) => Error::InvalidToken,
kind => Error::JwtError(kind),
}),
}
}

236
src/routes/auth/sign_in.rs Normal file
View File

@@ -0,0 +1,236 @@
use self::schema::*;
use crate::database::driver;
use crate::database::driver::users::UserSave;
use crate::routes::auth::shared::parse_vk_id;
use crate::routes::auth::sign_in::schema::SignInData::{Default, VkOAuth};
use crate::routes::schema::ResponseError;
use crate::routes::schema::user::UserResponse;
use crate::{AppState, utility};
use actix_web::{post, web};
use web::Json;
async fn sign_in_combined(
data: SignInData,
app_state: &web::Data<AppState>,
) -> Result<UserResponse, ErrorCode> {
let user = match &data {
Default(data) => driver::users::get_by_username(&app_state, &data.username).await,
VkOAuth(id) => driver::users::get_by_vk_id(&app_state, *id).await,
};
match user {
Ok(mut user) => {
if let Default(data) = data {
if user.password.is_none() {
return Err(ErrorCode::IncorrectCredentials);
}
match bcrypt::verify(&data.password, &user.password.as_ref().unwrap()) {
Ok(result) => {
if !result {
return Err(ErrorCode::IncorrectCredentials);
}
}
Err(_) => {
return Err(ErrorCode::IncorrectCredentials);
}
}
}
user.access_token = Some(utility::jwt::encode(&user.id));
user.save(&app_state).await.expect("Failed to update user");
Ok(user.into())
}
Err(_) => Err(ErrorCode::IncorrectCredentials),
}
}
#[utoipa::path(responses(
(status = OK, body = UserResponse),
(status = NOT_ACCEPTABLE, body = ResponseError<ErrorCode>)
))]
#[post("/sign-in")]
pub async fn sign_in(data: Json<Request>, app_state: web::Data<AppState>) -> ServiceResponse {
sign_in_combined(Default(data.into_inner()), &app_state)
.await
.into()
}
#[utoipa::path(responses(
(status = OK, body = UserResponse),
(status = NOT_ACCEPTABLE, body = ResponseError<ErrorCode>)
))]
#[post("/sign-in-vk")]
pub async fn sign_in_vk(
data_json: Json<vk::Request>,
app_state: web::Data<AppState>,
) -> ServiceResponse {
let data = data_json.into_inner();
match parse_vk_id(&data.access_token, app_state.get_env().vk_id.client_id) {
Ok(id) => sign_in_combined(VkOAuth(id), &app_state).await,
Err(_) => Err(ErrorCode::InvalidVkAccessToken),
}
.into()
}
mod schema {
use crate::routes::schema::user::UserResponse;
use actix_macros::ErrResponse;
use derive_more::Display;
use serde::{Deserialize, Serialize};
use utoipa::ToSchema;
#[derive(Deserialize, Serialize, ToSchema)]
#[schema(as = SignIn::Request)]
pub struct Request {
/// User name.
#[schema(examples("n08i40k"))]
pub username: String,
/// Password.
pub password: String,
}
pub mod vk {
use serde::{Deserialize, Serialize};
use utoipa::ToSchema;
#[derive(Serialize, Deserialize, ToSchema)]
#[serde(rename_all = "camelCase")]
#[schema(as = SignInVk::Request)]
pub struct Request {
/// VK ID token.
pub access_token: String,
}
}
pub type ServiceResponse = crate::routes::schema::Response<UserResponse, ErrorCode>;
#[derive(Clone, Serialize, Display, ToSchema, ErrResponse)]
#[schema(as = SignIn::ErrorCode)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
#[status_code = "actix_web::http::StatusCode::NOT_ACCEPTABLE"]
pub enum ErrorCode {
/// Incorrect username or password.
#[display("Incorrect username or password.")]
IncorrectCredentials,
/// Invalid VK ID token.
#[display("Invalid VK ID token.")]
InvalidVkAccessToken,
}
/// Internal
/// Type of authorization.
pub enum SignInData {
/// User and password name and password.
Default(Request),
/// Identifier of the attached account VK.
VkOAuth(i32),
}
}
#[cfg(test)]
mod tests {
use super::schema::*;
use crate::database::driver;
use crate::database::models::{User, UserRole};
use crate::routes::auth::sign_in::sign_in;
use crate::test_env::tests::{static_app_state, test_app_state, test_env};
use crate::utility;
use actix_test::test_app;
use actix_web::dev::ServiceResponse;
use actix_web::http::Method;
use actix_web::http::StatusCode;
use actix_web::test;
use sha1::{Digest, Sha1};
use std::fmt::Write;
async fn sign_in_client(data: Request) -> ServiceResponse {
let app = test_app(test_app_state().await, sign_in).await;
let req = test::TestRequest::with_uri("/sign-in")
.method(Method::POST)
.set_json(data)
.to_request();
test::call_service(&app, req).await
}
async fn prepare(username: String) {
let id = {
let mut sha = Sha1::new();
sha.update(&username);
let result = sha.finalize();
let bytes = &result[..12];
let mut hex = String::new();
for byte in bytes {
write!(&mut hex, "{:02x}", byte).unwrap();
}
hex
};
test_env();
let app_state = static_app_state().await;
driver::users::insert_or_ignore(
&app_state,
&User {
id: id.clone(),
username,
password: Some(bcrypt::hash("example".to_string(), bcrypt::DEFAULT_COST).unwrap()),
vk_id: None,
telegram_id: None,
access_token: Some(utility::jwt::encode(&id)),
group: Some("ИС-214/23".to_string()),
role: UserRole::Student,
android_version: None,
},
)
.await
.unwrap();
}
#[actix_web::test]
async fn sign_in_ok() {
prepare("test::sign_in_ok".to_string()).await;
let resp = sign_in_client(Request {
username: "test::sign_in_ok".to_string(),
password: "example".to_string(),
})
.await;
assert_eq!(resp.status(), StatusCode::OK);
}
#[actix_web::test]
async fn sign_in_err() {
prepare("test::sign_in_err".to_string()).await;
let invalid_username = sign_in_client(Request {
username: "test::sign_in_err::username".to_string(),
password: "example".to_string(),
})
.await;
assert_eq!(invalid_username.status(), StatusCode::NOT_ACCEPTABLE);
let invalid_password = sign_in_client(Request {
username: "test::sign_in_err".to_string(),
password: "bad_password".to_string(),
})
.await;
assert_eq!(invalid_password.status(), StatusCode::NOT_ACCEPTABLE);
}
}

355
src/routes/auth/sign_up.rs Normal file
View File

@@ -0,0 +1,355 @@
use self::schema::*;
use crate::AppState;
use crate::database::driver;
use crate::database::models::UserRole;
use crate::routes::auth::shared::parse_vk_id;
use crate::routes::schema::ResponseError;
use crate::routes::schema::user::UserResponse;
use actix_web::{post, web};
use web::Json;
async fn sign_up_combined(
data: SignUpData,
app_state: &web::Data<AppState>,
) -> Result<UserResponse, ErrorCode> {
// If user selected forbidden role.
if data.role == UserRole::Admin {
return Err(ErrorCode::DisallowedRole);
}
if !app_state
.get_schedule_snapshot()
.await
.data
.groups
.contains_key(&data.group)
{
return Err(ErrorCode::InvalidGroupName);
}
// If user with specified username already exists.
if driver::users::contains_by_username(&app_state, &data.username).await {
return Err(ErrorCode::UsernameAlreadyExists);
}
// If user with specified VKID already exists.
if let Some(id) = data.vk_id {
if driver::users::contains_by_vk_id(&app_state, id).await {
return Err(ErrorCode::VkAlreadyExists);
}
}
let user = data.into();
driver::users::insert(&app_state, &user).await.unwrap();
Ok(UserResponse::from(&user)).into()
}
#[utoipa::path(responses(
(status = OK, body = UserResponse),
(status = NOT_ACCEPTABLE, body = ResponseError<ErrorCode>)
))]
#[post("/sign-up")]
pub async fn sign_up(data_json: Json<Request>, app_state: web::Data<AppState>) -> ServiceResponse {
let data = data_json.into_inner();
sign_up_combined(
SignUpData {
username: data.username,
password: Some(data.password),
vk_id: None,
group: data.group,
role: data.role,
version: data.version,
},
&app_state,
)
.await
.into()
}
#[utoipa::path(responses(
(status = OK, body = UserResponse),
(status = NOT_ACCEPTABLE, body = ResponseError<ErrorCode>)
))]
#[post("/sign-up-vk")]
pub async fn sign_up_vk(
data_json: Json<vk::Request>,
app_state: web::Data<AppState>,
) -> ServiceResponse {
let data = data_json.into_inner();
match parse_vk_id(&data.access_token, app_state.get_env().vk_id.client_id) {
Ok(id) => {
sign_up_combined(
SignUpData {
username: data.username,
password: None,
vk_id: Some(id),
group: data.group,
role: data.role,
version: data.version,
},
&app_state,
)
.await
}
Err(_) => Err(ErrorCode::InvalidVkAccessToken),
}
.into()
}
mod schema {
use crate::database::models::{User, UserRole};
use crate::routes::schema::user::UserResponse;
use crate::utility;
use actix_macros::ErrResponse;
use derive_more::Display;
use objectid::ObjectId;
use serde::{Deserialize, Serialize};
/// WEB
#[derive(Serialize, Deserialize, utoipa::ToSchema)]
#[schema(as = SignUp::Request)]
pub struct Request {
/// User name.
#[schema(examples("n08i40k"))]
pub username: String,
/// Password.
pub password: String,
/// Group.
#[schema(examples("ИС-214/23"))]
pub group: String,
/// Role.
pub role: UserRole,
/// Version of the installed Polytechnic+ application.
#[schema(examples("3.0.0"))]
pub version: String,
}
pub mod vk {
use crate::database::models::UserRole;
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize, utoipa::ToSchema)]
#[serde(rename_all = "camelCase")]
#[schema(as = SignUpVk::Request)]
pub struct Request {
/// VK ID token.
pub access_token: String,
/// User name.
#[schema(examples("n08i40k"))]
pub username: String,
/// Group.
#[schema(examples("ИС-214/23"))]
pub group: String,
/// Role.
pub role: UserRole,
/// Version of the installed Polytechnic+ application.
#[schema(examples("3.0.0"))]
pub version: String,
}
}
pub type ServiceResponse = crate::routes::schema::Response<UserResponse, ErrorCode>;
#[derive(Clone, Serialize, Display, utoipa::ToSchema, ErrResponse)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
#[schema(as = SignUp::ErrorCode)]
#[status_code = "actix_web::http::StatusCode::NOT_ACCEPTABLE"]
pub enum ErrorCode {
/// Conveyed the role of Admin.
#[display("Conveyed the role of Admin.")]
DisallowedRole,
/// Unknown name of the group.
#[display("Unknown name of the group.")]
InvalidGroupName,
/// User with this name is already registered.
#[display("User with this name is already registered.")]
UsernameAlreadyExists,
/// Invalid VK ID token.
#[display("Invalid VK ID token.")]
InvalidVkAccessToken,
/// User with such an account VK is already registered.
#[display("User with such an account VK is already registered.")]
VkAlreadyExists,
}
/// Internal
/// Data for registration.
pub struct SignUpData {
// TODO: сделать ограничение на минимальную и максимальную длину при регистрации и смене.
/// User name.
pub username: String,
/// Password.
///
/// Should be present even if registration occurs using the VK ID token.
pub password: Option<String>,
/// Account identifier VK.
pub vk_id: Option<i32>,
/// Group.
pub group: String,
/// Role.
pub role: UserRole,
/// Version of the installed Polytechnic+ application.
pub version: String,
}
impl Into<User> for SignUpData {
fn into(self) -> User {
assert_ne!(self.password.is_some(), self.vk_id.is_some());
let id = ObjectId::new().unwrap().to_string();
let access_token = Some(utility::jwt::encode(&id));
User {
id,
username: self.username,
password: self
.password
.map(|x| bcrypt::hash(x, bcrypt::DEFAULT_COST).unwrap()),
vk_id: self.vk_id,
telegram_id: None,
access_token,
group: Some(self.group),
role: self.role,
android_version: Some(self.version),
}
}
}
}
#[cfg(test)]
mod tests {
use crate::database::driver;
use crate::database::models::UserRole;
use crate::routes::auth::sign_up::schema::Request;
use crate::routes::auth::sign_up::sign_up;
use crate::test_env::tests::{static_app_state, test_app_state, test_env};
use actix_test::test_app;
use actix_web::dev::ServiceResponse;
use actix_web::http::Method;
use actix_web::http::StatusCode;
use actix_web::test;
struct SignUpPartial<'a> {
username: &'a str,
group: &'a str,
role: UserRole,
}
async fn sign_up_client(data: SignUpPartial<'_>) -> ServiceResponse {
let app = test_app(test_app_state().await, sign_up).await;
let req = test::TestRequest::with_uri("/sign-up")
.method(Method::POST)
.set_json(Request {
username: data.username.to_string(),
password: "example".to_string(),
group: data.group.to_string(),
role: data.role.clone(),
version: "1.0.0".to_string(),
})
.to_request();
test::call_service(&app, req).await
}
#[actix_web::test]
async fn sign_up_valid() {
// prepare
test_env();
let app_state = static_app_state().await;
driver::users::delete_by_username(&app_state, &"test::sign_up_valid".to_string()).await;
// test
let resp = sign_up_client(SignUpPartial {
username: "test::sign_up_valid",
group: "ИС-214/23",
role: UserRole::Student,
})
.await;
assert_eq!(resp.status(), StatusCode::OK);
}
#[actix_web::test]
async fn sign_up_multiple() {
// prepare
test_env();
let app_state = static_app_state().await;
driver::users::delete_by_username(&app_state, &"test::sign_up_multiple".to_string()).await;
let create = sign_up_client(SignUpPartial {
username: "test::sign_up_multiple",
group: "ИС-214/23",
role: UserRole::Student,
})
.await;
assert_eq!(create.status(), StatusCode::OK);
let resp = sign_up_client(SignUpPartial {
username: "test::sign_up_multiple",
group: "ИС-214/23",
role: UserRole::Student,
})
.await;
assert_eq!(resp.status(), StatusCode::NOT_ACCEPTABLE);
}
#[actix_web::test]
async fn sign_up_invalid_role() {
test_env();
// test
let resp = sign_up_client(SignUpPartial {
username: "test::sign_up_invalid_role",
group: "ИС-214/23",
role: UserRole::Admin,
})
.await;
assert_eq!(resp.status(), StatusCode::NOT_ACCEPTABLE);
}
#[actix_web::test]
async fn sign_up_invalid_group() {
test_env();
// test
let resp = sign_up_client(SignUpPartial {
username: "test::sign_up_invalid_group",
group: "invalid_group",
role: UserRole::Student,
})
.await;
assert_eq!(resp.status(), StatusCode::NOT_ACCEPTABLE);
}
}

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

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

View File

@@ -0,0 +1,94 @@
use crate::database;
use crate::database::models::FCM;
use crate::extractors::authorized_user::UserExtractor;
use crate::extractors::base::AsyncExtractor;
use crate::state::AppState;
use actix_web::{HttpResponse, Responder, patch, web};
use diesel::{RunQueryDsl, SaveChangesDsl};
use firebase_messaging_rs::topic::TopicManagementSupport;
use serde::Deserialize;
use std::ops::DerefMut;
#[derive(Debug, Deserialize)]
struct Params {
pub token: String,
}
async fn get_fcm(
app_state: &web::Data<AppState>,
user_data: &UserExtractor<true>,
token: String,
) -> Result<FCM, diesel::result::Error> {
match user_data.fcm() {
Some(fcm) => {
let mut fcm = fcm.clone();
fcm.token = token;
Ok(fcm)
}
None => {
let fcm = FCM {
user_id: user_data.user().id.clone(),
token,
topics: vec![],
};
match diesel::insert_into(database::schema::fcm::table)
.values(&fcm)
.execute(app_state.get_database().await.deref_mut())
{
Ok(_) => Ok(fcm),
Err(e) => Err(e),
}
}
}
}
#[utoipa::path(responses((status = OK)))]
#[patch("/set-token")]
pub async fn set_token(
app_state: web::Data<AppState>,
web::Query(params): web::Query<Params>,
user_data: AsyncExtractor<UserExtractor<true>>,
) -> impl Responder {
let user_data = user_data.into_inner();
// If token not changes - exit.
if let Some(fcm) = user_data.fcm() {
if fcm.token == params.token {
return HttpResponse::Ok();
}
}
let fcm = get_fcm(&app_state, &user_data, params.token.clone()).await;
if let Err(e) = fcm {
eprintln!("Failed to get FCM: {e}");
return HttpResponse::Ok();
}
let mut fcm = fcm.ok().unwrap();
// Add default topics.
if !fcm.topics.contains(&Some("common".to_string())) {
fcm.topics.push(Some("common".to_string()));
}
fcm.save_changes::<FCM>(app_state.get_database().await.deref_mut())
.unwrap();
let fcm_client = app_state.get_fcm_client().await.unwrap();
for topic in fcm.topics.clone() {
if let Some(topic) = topic {
if let Err(error) = fcm_client
.register_token_to_topic(&*topic, &*fcm.token)
.await
{
eprintln!("Failed to subscribe token to topic: {:?}", error);
return HttpResponse::Ok();
}
}
}
HttpResponse::Ok()
}

View File

@@ -0,0 +1,24 @@
use crate::database::driver::users::UserSave;
use crate::database::models::User;
use crate::extractors::base::AsyncExtractor;
use crate::state::AppState;
use actix_web::{HttpResponse, Responder, post, web};
#[utoipa::path(responses(
(status = OK),
(status = INTERNAL_SERVER_ERROR)
))]
#[post("/update-callback/{version}")]
async fn update_callback(
app_state: web::Data<AppState>,
version: web::Path<String>,
user: AsyncExtractor<User>,
) -> impl Responder {
let mut user = user.into_inner();
user.android_version = Some(version.into_inner());
user.save(&app_state).await.unwrap();
HttpResponse::Ok()
}

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

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

View File

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

View File

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

7
src/routes/mod.rs Normal file
View File

@@ -0,0 +1,7 @@
pub mod auth;
pub mod fcm;
pub mod flow;
pub mod schedule;
mod schema;
pub mod users;
pub mod vk_id;

View File

@@ -0,0 +1,11 @@
use crate::AppState;
use crate::routes::schedule::schema::CacheStatus;
use actix_web::{get, web};
#[utoipa::path(responses(
(status = OK, body = CacheStatus),
))]
#[get("/cache-status")]
pub async fn cache_status(app_state: web::Data<AppState>) -> CacheStatus {
CacheStatus::from(&app_state).await.into()
}

View File

@@ -0,0 +1,71 @@
use self::schema::*;
use crate::AppState;
use crate::database::models::User;
use crate::extractors::base::AsyncExtractor;
use crate::routes::schedule::schema::ScheduleEntryResponse;
use crate::routes::schema::ResponseError;
use actix_web::{get, web};
#[utoipa::path(responses(
(status = OK, body = 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()
.await
.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,
}
}

View File

@@ -0,0 +1,34 @@
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()
.await
.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>,
}
}

View File

@@ -0,0 +1,14 @@
mod cache_status;
mod group;
mod group_names;
mod schedule;
mod schema;
mod teacher;
mod teacher_names;
pub use cache_status::*;
pub use group::*;
pub use group_names::*;
pub use schedule::*;
pub use teacher::*;
pub use teacher_names::*;

View File

@@ -0,0 +1,9 @@
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
}

View File

@@ -0,0 +1,75 @@
use crate::state::{AppState, ScheduleSnapshot};
use actix_macros::{OkResponse, ResponderJson};
use actix_web::web;
use schedule_parser::schema::ScheduleEntry;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::ops::Deref;
use utoipa::ToSchema;
/// Response from schedule server.
#[derive(Serialize, ToSchema, OkResponse, ResponderJson)]
#[serde(rename_all = "camelCase")]
pub struct ScheduleView {
/// Url to xls file.
url: String,
/// Groups schedule.
groups: HashMap<String, ScheduleEntry>,
/// Teachers schedule.
teachers: HashMap<String, ScheduleEntry>,
}
#[derive(Serialize, ToSchema, OkResponse)]
pub struct ScheduleEntryResponse(ScheduleEntry);
impl From<ScheduleEntry> for ScheduleEntryResponse {
fn from(value: ScheduleEntry) -> Self {
Self(value)
}
}
impl ScheduleView {
pub async fn from(app_state: &web::Data<AppState>) -> Self {
let schedule = app_state.get_schedule_snapshot().await.clone();
Self {
url: schedule.url,
groups: schedule.data.groups,
teachers: schedule.data.teachers,
}
}
}
/// Cached schedule status.
#[derive(Serialize, Deserialize, ToSchema, ResponderJson, OkResponse)]
#[serde(rename_all = "camelCase")]
pub struct CacheStatus {
/// Schedule hash.
pub hash: String,
/// Last cache update date.
pub fetched_at: i64,
/// Cached schedule update date.
///
/// Determined by the polytechnic's server.
pub updated_at: i64,
}
impl CacheStatus {
pub async fn from(value: &web::Data<AppState>) -> Self {
From::<&ScheduleSnapshot>::from(value.get_schedule_snapshot().await.deref())
}
}
impl From<&ScheduleSnapshot> for CacheStatus {
fn from(value: &ScheduleSnapshot) -> Self {
Self {
hash: value.hash(),
fetched_at: value.fetched_at.timestamp(),
updated_at: value.updated_at.timestamp(),
}
}
}

View File

@@ -0,0 +1,52 @@
use self::schema::*;
use crate::AppState;
use crate::routes::schema::ResponseError;
use actix_web::{get, web};
use schedule_parser::schema::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()
.await
.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,
}
}

View File

@@ -0,0 +1,34 @@
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()
.await
.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>,
}
}

197
src/routes/schema.rs Normal file
View File

@@ -0,0 +1,197 @@
use actix_web::body::EitherBody;
use actix_web::error::JsonPayloadError;
use actix_web::http::StatusCode;
use actix_web::{HttpRequest, HttpResponse, Responder};
use serde::{Serialize, Serializer};
use std::convert::Into;
use std::fmt::Display;
use utoipa::PartialSchema;
pub struct Response<T, E>(pub Result<T, E>)
where
T: Serialize + PartialSchema + PartialOkResponse,
E: Serialize + PartialSchema + Display + PartialErrResponse;
/// Transform Response<T, E> into Result<T, E>
impl<T, E> Into<Result<T, E>> for Response<T, E>
where
T: Serialize + PartialSchema + PartialOkResponse,
E: Serialize + PartialSchema + Display + PartialErrResponse,
{
fn into(self) -> Result<T, E> {
self.0
}
}
/// Transform T into Response<T, E>
impl<T, E> From<Result<T, E>> for Response<T, E>
where
T: Serialize + PartialSchema + PartialOkResponse,
E: Serialize + PartialSchema + Display + PartialErrResponse,
{
fn from(value: Result<T, E>) -> Self {
Response(value)
}
}
/// Serialize Response<T, E>
impl<T, E> Serialize for Response<T, E>
where
T: Serialize + PartialSchema + PartialOkResponse,
E: Serialize + PartialSchema + Display + PartialErrResponse + Clone + Into<ResponseError<E>>,
{
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
match &self.0 {
Ok(ok) => serializer.serialize_some(&ok),
Err(err) => serializer.serialize_some(&ResponseError::<E>::from(err.clone().into())),
}
}
}
/// Transform Response<T, E> to HttpResponse<String>
impl<T, E> Responder for Response<T, E>
where
T: Serialize + PartialSchema + PartialOkResponse,
E: Serialize + PartialSchema + Display + PartialErrResponse + Clone + Into<ResponseError<E>>,
{
type Body = EitherBody<String>;
fn respond_to(mut self, request: &HttpRequest) -> HttpResponse<Self::Body> {
match serde_json::to_string(&self) {
Ok(body) => {
let code = match &self.0 {
Ok(_) => StatusCode::OK,
Err(e) => e.status_code(),
};
let mut response = match HttpResponse::build(code)
.content_type(mime::APPLICATION_JSON)
.message_body(body)
{
Ok(res) => res.map_into_left_body(),
Err(err) => HttpResponse::from_error(err).map_into_right_body(),
};
if let Ok(ok) = &mut self.0 {
ok.post_process(request, &mut response);
}
response
}
Err(err) => {
HttpResponse::from_error(JsonPayloadError::Serialize(err)).map_into_right_body()
}
}
}
}
/// Трейт для всех положительных ответов от сервера
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>
#[derive(Serialize, utoipa::ToSchema)]
pub struct ResponseError<T: Serialize + PartialSchema + Clone> {
pub code: T,
pub message: String,
}
impl<T> From<T> for ResponseError<T>
where
T: Serialize + PartialSchema + Display + Clone,
{
fn from(code: T) -> Self {
Self {
message: format!("{}", code),
code,
}
}
}
pub mod user {
use crate::database::models::{User, UserRole};
use actix_macros::{OkResponse, ResponderJson};
use serde::Serialize;
//noinspection SpellCheckingInspection
/// Используется для скрытия чувствительных полей, таких как хеш пароля или FCM
#[derive(Serialize, utoipa::ToSchema, ResponderJson, OkResponse)]
#[serde(rename_all = "camelCase")]
pub struct UserResponse {
/// UUID
#[schema(examples("67dcc9a9507b0000772744a2"))]
pub id: String,
/// Имя пользователя
#[schema(examples("n08i40k"))]
pub username: String,
/// Группа
#[schema(examples("ИС-214/23"))]
pub group: Option<String>,
/// Роль
pub role: UserRole,
/// Идентификатор привязанного аккаунта VK
#[schema(examples(498094647, json!(null)))]
pub vk_id: Option<i32>,
/// Идентификатор привязанного аккаунта Telegram
#[schema(examples(996004735, json!(null)))]
pub telegram_id: Option<i64>,
/// JWT токен доступа
#[schema(examples(
"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6IjY3ZGNjOWE5NTA3YjAwMDA3NzI3NDRhMiIsImlhdCI6IjE3NDMxMDgwOTkiLCJleHAiOiIxODY5MjUyMDk5In0.rMgXRb3JbT9AvLK4eiY9HMB5LxgUudkpQyoWKOypZFY"
))]
pub access_token: Option<String>,
}
/// Create UserResponse from User ref.
impl From<&User> for UserResponse {
fn from(user: &User) -> Self {
UserResponse {
id: user.id.clone(),
username: user.username.clone(),
group: user.group.clone(),
role: user.role.clone(),
vk_id: user.vk_id.clone(),
telegram_id: user.telegram_id.clone(),
access_token: user.access_token.clone(),
}
}
}
/// Transform User to UserResponse.
impl From<User> for UserResponse {
fn from(user: User) -> Self {
UserResponse {
id: user.id,
username: user.username,
group: user.group,
role: user.role,
vk_id: user.vk_id,
telegram_id: user.telegram_id,
access_token: user.access_token,
}
}
}
}

View File

@@ -0,0 +1,62 @@
use self::schema::*;
use crate::database::driver::users::UserSave;
use crate::database::models::User;
use crate::extractors::base::AsyncExtractor;
use crate::state::AppState;
use actix_web::{post, web};
#[utoipa::path(responses((status = OK)))]
#[post("/change-group")]
pub async fn change_group(
app_state: web::Data<AppState>,
user: AsyncExtractor<User>,
data: web::Json<Request>,
) -> ServiceResponse {
let mut user = user.into_inner();
if user.group.is_some_and(|group| group == data.group) {
return Ok(()).into();
}
if !app_state
.get_schedule_snapshot()
.await
.data
.groups
.contains_key(&data.group)
{
return Err(ErrorCode::NotFound).into();
}
user.group = Some(data.into_inner().group);
user.save(&app_state).await.unwrap();
Ok(()).into()
}
mod schema {
use actix_macros::ErrResponse;
use derive_more::Display;
use serde::{Deserialize, Serialize};
use utoipa::ToSchema;
pub type ServiceResponse = crate::routes::schema::Response<(), ErrorCode>;
#[derive(Deserialize, ToSchema)]
#[schema(as = ChangeGroup::Request)]
pub struct Request {
// Group.
pub group: String,
}
#[derive(Clone, Serialize, Display, ToSchema, ErrResponse)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
#[schema(as = ChangeGroup::ErrorCode)]
#[status_code = "actix_web::http::StatusCode::CONFLICT"]
pub enum ErrorCode {
/// The required group does not exist.
#[display("The required group does not exist.")]
#[status_code = "actix_web::http::StatusCode::NOT_FOUND"]
NotFound,
}
}

View File

@@ -0,0 +1,59 @@
use self::schema::*;
use crate::database::driver;
use crate::database::driver::users::UserSave;
use crate::database::models::User;
use crate::extractors::base::AsyncExtractor;
use crate::state::AppState;
use actix_web::{post, web};
#[utoipa::path(responses((status = OK)))]
#[post("/change-username")]
pub async fn change_username(
app_state: web::Data<AppState>,
user: AsyncExtractor<User>,
data: web::Json<Request>,
) -> ServiceResponse {
let mut user = user.into_inner();
if user.username == data.username {
return Ok(()).into();
}
if driver::users::get_by_username(&app_state, &data.username)
.await
.is_ok()
{
return Err(ErrorCode::AlreadyExists).into();
}
user.username = data.into_inner().username;
user.save(&app_state).await.unwrap();
Ok(()).into()
}
mod schema {
use actix_macros::ErrResponse;
use derive_more::Display;
use serde::{Deserialize, Serialize};
use utoipa::ToSchema;
pub type ServiceResponse = crate::routes::schema::Response<(), ErrorCode>;
#[derive(Serialize, Deserialize, ToSchema)]
#[schema(as = ChangeUsername::Request)]
pub struct Request {
/// User name.
pub username: String,
}
#[derive(Clone, Serialize, Display, ToSchema, ErrResponse)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
#[schema(as = ChangeUsername::ErrorCode)]
#[status_code = "actix_web::http::StatusCode::CONFLICT"]
pub enum ErrorCode {
/// A user with this name already exists.
#[display("A user with this name already exists.")]
AlreadyExists,
}
}

10
src/routes/users/me.rs Normal file
View File

@@ -0,0 +1,10 @@
use crate::database::models::User;
use crate::extractors::base::AsyncExtractor;
use crate::routes::schema::user::UserResponse;
use actix_web::get;
#[utoipa::path(responses((status = OK, body = UserResponse)))]
#[get("/me")]
pub async fn me(user: AsyncExtractor<User>) -> UserResponse {
user.into_inner().into()
}

7
src/routes/users/mod.rs Normal file
View File

@@ -0,0 +1,7 @@
mod change_group;
mod change_username;
mod me;
pub use change_group::*;
pub use change_username::*;
pub use me::*;

3
src/routes/vk_id/mod.rs Normal file
View File

@@ -0,0 +1,3 @@
mod oauth;
pub use oauth::*;

117
src/routes/vk_id/oauth.rs Normal file
View File

@@ -0,0 +1,117 @@
use self::schema::*;
use crate::routes::schema::ResponseError;
use crate::state::AppState;
use actix_web::{post, web};
use serde::Deserialize;
use std::collections::HashMap;
use uuid::Uuid;
#[allow(dead_code)]
#[derive(Deserialize)]
struct VkIdAuthResponse {
refresh_token: String,
access_token: String,
id_token: String,
token_type: String,
expires_in: i32,
user_id: i32,
state: String,
scope: String,
}
#[utoipa::path(responses(
(status = OK, body = Response),
(
status = NOT_ACCEPTABLE,
body = ResponseError<ErrorCode>,
example = json!({
"code": "VK_ID_ERROR",
"message": "VK server returned an error"
})
),
))]
#[post("/oauth")]
async fn oauth(data: web::Json<Request>, app_state: web::Data<AppState>) -> ServiceResponse {
let data = data.into_inner();
let state = Uuid::new_v4().simple().to_string();
let vk_id = &app_state.get_env().vk_id;
let client_id = vk_id.client_id.clone().to_string();
let mut params = HashMap::new();
params.insert("grant_type", "authorization_code");
params.insert("client_id", client_id.as_str());
params.insert("state", state.as_str());
params.insert("code_verifier", data.code_verifier.as_str());
params.insert("code", data.code.as_str());
params.insert("device_id", data.device_id.as_str());
params.insert("redirect_uri", vk_id.redirect_url.as_str());
let client = reqwest::Client::new();
match client
.post("https://id.vk.com/oauth2/auth")
.form(&params)
.send()
.await
{
Ok(res) => {
if !res.status().is_success() {
return Err(ErrorCode::VkIdError).into();
}
match res.json::<VkIdAuthResponse>().await {
Ok(auth_data) => Ok(Response {
access_token: auth_data.id_token,
}),
Err(error) => {
sentry::capture_error(&error);
Err(ErrorCode::VkIdError)
}
}
}
Err(_) => Err(ErrorCode::VkIdError),
}
.into()
}
mod schema {
use actix_macros::{ErrResponse, OkResponse};
use derive_more::Display;
use serde::{Deserialize, Serialize};
use utoipa::ToSchema;
pub type ServiceResponse = crate::routes::schema::Response<Response, ErrorCode>;
#[derive(Deserialize, ToSchema)]
#[serde(rename_all = "camelCase")]
#[schema(as = VkIdOAuth::Request)]
pub struct Request {
/// Код подтверждения authorization_code.
pub code: String,
/// Parameter to protect transmitted data.
pub code_verifier: String,
/// Device ID.
pub device_id: String,
}
#[derive(Serialize, ToSchema, OkResponse)]
#[serde(rename_all = "camelCase")]
#[schema(as = VkIdOAuth::Response)]
pub struct Response {
/// ID token.
pub access_token: String,
}
#[derive(Clone, Serialize, Display, ToSchema, ErrResponse)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
#[schema(as = VkIdOAuth::ErrorCode)]
#[status_code = "actix_web::http::StatusCode::NOT_ACCEPTABLE"]
pub enum ErrorCode {
/// VK server returned an error.
#[display("VK server returned an error")]
VkIdError,
}
}

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

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

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

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

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

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

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

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

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

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

15
src/state/fcm_client.rs Normal file
View File

@@ -0,0 +1,15 @@
use firebase_messaging_rs::FCMClient;
use std::env;
use tokio::sync::Mutex;
#[derive(Clone)]
pub struct FCMClientData;
impl FCMClientData {
pub async fn new() -> Option<Mutex<FCMClient>> {
match env::var("GOOGLE_APPLICATION_CREDENTIALS") {
Ok(_) => Some(Mutex::new(FCMClient::new().await.unwrap())),
Err(_) => None,
}
}
}

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

@@ -0,0 +1,88 @@
mod env;
mod fcm_client;
mod schedule;
use crate::state::fcm_client::FCMClientData;
use crate::xls_downloader::basic_impl::BasicXlsDownloader;
use actix_web::web;
use diesel::{Connection, PgConnection};
use firebase_messaging_rs::FCMClient;
use std::ops::DerefMut;
use tokio::sync::{MappedMutexGuard, Mutex, MutexGuard};
pub use self::schedule::{Schedule, ScheduleSnapshot};
pub use crate::state::env::AppEnv;
/// Common data provided to endpoints.
pub struct AppState {
database: Mutex<PgConnection>,
downloader: Mutex<BasicXlsDownloader>,
schedule: Mutex<Schedule>,
env: AppEnv,
fcm_client: Option<Mutex<FCMClient>>,
}
impl AppState {
pub async fn new() -> Result<Self, self::schedule::Error> {
let database_url = std::env::var("DATABASE_URL").expect("DATABASE_URL must be set");
let mut _self = Self {
downloader: Mutex::new(BasicXlsDownloader::new()),
schedule: Mutex::new(Schedule::default()),
database: Mutex::new(
PgConnection::establish(&database_url)
.unwrap_or_else(|_| panic!("Error connecting to {}", database_url)),
),
env: AppEnv::default(),
fcm_client: FCMClientData::new().await,
};
if _self.env.schedule.auto_update {
_self
.get_schedule()
.await
.init(_self.get_downloader().await.deref_mut(), &_self.env)
.await?;
}
Ok(_self)
}
pub async fn get_downloader(&'_ self) -> MutexGuard<'_, BasicXlsDownloader> {
self.downloader.lock().await
}
pub async fn get_schedule(&'_ self) -> MutexGuard<'_, Schedule> {
self.schedule.lock().await
}
pub async fn get_schedule_snapshot(&'_ self) -> MappedMutexGuard<'_, ScheduleSnapshot> {
let snapshot =
MutexGuard::<'_, Schedule>::map(self.schedule.lock().await, |schedule| unsafe {
schedule.snapshot.assume_init_mut()
});
snapshot
}
pub async fn get_database(&'_ self) -> MutexGuard<'_, PgConnection> {
self.database.lock().await
}
pub fn get_env(&self) -> &AppEnv {
&self.env
}
pub async fn get_fcm_client(&'_ self) -> Option<MutexGuard<'_, FCMClient>> {
match &self.fcm_client {
Some(client) => Some(client.lock().await),
None => None,
}
}
}
/// Create a new object web::Data<AppState>.
pub async fn new_app_state() -> Result<web::Data<AppState>, self::schedule::Error> {
Ok(web::Data::new(AppState::new().await?))
}

290
src/state/schedule.rs Normal file
View File

@@ -0,0 +1,290 @@
use crate::state::env::AppEnv;
use crate::utility::hasher::DigestHasher;
use chrono::{DateTime, Utc};
use derive_more::{Display, Error};
use schedule_parser::parse_xls;
use schedule_parser::schema::{ParseError, ParseResult};
use sha1::{Digest, Sha1};
use std::hash::Hash;
use std::mem::MaybeUninit;
use crate::xls_downloader::basic_impl::BasicXlsDownloader;
use crate::xls_downloader::interface::{FetchError, XLSDownloader};
/// Represents errors that can occur during schedule-related operations.
#[derive(Debug, Display, Error)]
pub enum Error {
/// An error occurred while querying the Yandex Cloud API for a URL.
///
/// This may result from network failures, invalid API credentials, or issues with the Yandex Cloud Function invocation.
/// See [`QueryUrlError`] for more details about specific causes.
QueryUrlFailed(QueryUrlError),
/// The schedule snapshot creation process failed.
///
/// This can happen due to URL conflicts (same URL already in use), failed network requests,
/// download errors, or invalid XLS file content. See [`SnapshotCreationError`] for details.
SnapshotCreationFailed(SnapshotCreationError),
}
/// Errors that may occur when querying the Yandex Cloud API to retrieve a URL.
#[derive(Debug, Display, Error)]
pub enum QueryUrlError {
/// Occurs when the request to the Yandex Cloud API fails.
///
/// This may be due to network issues, invalid API key, incorrect function ID, or other
/// problems with the Yandex Cloud Function invocation.
#[display("An error occurred during the request to the Yandex Cloud API: {_0}")]
RequestFailed(reqwest::Error),
}
/// Errors that may occur during the creation of a schedule snapshot.
#[derive(Debug, Display, Error)]
pub enum SnapshotCreationError {
/// The URL is the same as the one already being used (no update needed).
#[display("The URL is the same as the one already being used.")]
SameUrl,
/// The URL query for the XLS file failed to execute, either due to network issues or invalid API parameters.
#[display("Failed to fetch URL: {_0}")]
FetchFailed(FetchError),
/// Downloading the XLS file content failed after successfully obtaining the URL.
#[display("Download failed: {_0}")]
DownloadFailed(FetchError),
/// The XLS file could not be parsed into a valid schedule format.
#[display("Schedule data is invalid: {_0}")]
InvalidSchedule(ParseError),
}
/// 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: ParseResult,
}
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();
}
/// Constructs a new `ScheduleSnapshot` by downloading and parsing schedule data from the specified URL.
///
/// This method first checks if the provided URL is the same as the one already configured in the downloader.
/// If different, it updates the downloader's URL, fetches the XLS content, parses it, and creates a snapshot.
/// Errors are returned for URL conflicts, network issues, download failures, or invalid data.
///
/// # Arguments
///
/// * `downloader`: A mutable reference to an `XLSDownloader` implementation used to fetch and parse the schedule data.
/// * `url`: The source URL pointing to the XLS file containing schedule data.
///
/// returns: Result<ScheduleSnapshot, SnapshotCreationError>
pub async fn new(
downloader: &mut BasicXlsDownloader,
url: String,
) -> Result<Self, SnapshotCreationError> {
if downloader.url.as_ref().is_some_and(|_url| _url.eq(&url)) {
return Err(SnapshotCreationError::SameUrl);
}
let head_result = downloader.set_url(&*url).await.map_err(|error| {
if let FetchError::Unknown(error) = &error {
sentry::capture_error(&error);
}
SnapshotCreationError::FetchFailed(error)
})?;
let xls_data = downloader
.fetch(false)
.await
.map_err(|error| {
if let FetchError::Unknown(error) = &error {
sentry::capture_error(&error);
}
SnapshotCreationError::DownloadFailed(error)
})?
.data
.unwrap();
let parse_result = parse_xls(&xls_data).map_err(|error| {
sentry::capture_error(&error);
SnapshotCreationError::InvalidSchedule(error)
})?;
Ok(ScheduleSnapshot {
fetched_at: head_result.requested_at,
updated_at: head_result.uploaded_at,
url,
data: parse_result,
})
}
}
pub struct Schedule {
pub snapshot: MaybeUninit<ScheduleSnapshot>,
}
impl Default for Schedule {
fn default() -> Self {
Self {
snapshot: MaybeUninit::uninit(),
}
}
}
impl Schedule {
/// Queries the Yandex Cloud Function (FaaS) to obtain a URL for the schedule file.
///
/// This sends a POST request to the specified Yandex Cloud Function endpoint,
/// using the provided API key for authentication. The returned URI is combined
/// with the "https://politehnikum-eng.ru" base domain to form the complete URL.
///
/// # Arguments
///
/// * `api_key` - Authentication token for Yandex Cloud API
/// * `func_id` - ID of the target Yandex Cloud Function to invoke
///
/// # Returns
///
/// Result containing:
/// - `Ok(String)` - Complete URL constructed from the Function's response
/// - `Err(QueryUrlError)` - If the request or response processing fails
async fn query_url(api_key: &str, func_id: &str) -> Result<String, QueryUrlError> {
let client = reqwest::Client::new();
let uri = client
.post(format!(
"https://functions.yandexcloud.net/{}?integration=raw",
func_id
))
.header("Authorization", format!("Api-Key {}", api_key))
.send()
.await
.map_err(|error| QueryUrlError::RequestFailed(error))?
.text()
.await
.map_err(|error| QueryUrlError::RequestFailed(error))?;
Ok(format!("https://politehnikum-eng.ru{}", uri.trim()))
}
/// Initializes the schedule by fetching the URL from the environment or Yandex Cloud Function (FaaS)
/// and creating a [`ScheduleSnapshot`] with the downloaded data.
///
/// # Arguments
///
/// * `downloader`: Mutable reference to an `XLSDownloader` implementation used to fetch and parse the schedule
/// * `app_env`: Reference to the application environment containing either a predefined URL or Yandex Cloud credentials
///
/// # Returns
///
/// Returns `Ok(())` if the snapshot was successfully initialized, or an `Error` if:
/// - URL query to Yandex Cloud failed ([`QueryUrlError`])
/// - Schedule snapshot creation failed ([`SnapshotCreationError`])
pub async fn init(
&mut self,
downloader: &mut BasicXlsDownloader,
app_env: &AppEnv,
) -> Result<(), Error> {
let url = if let Some(url) = &app_env.schedule.url {
log::info!("The default link {} will be used", url);
url.clone()
} else {
log::info!("Obtaining a link using FaaS...");
Self::query_url(
&*app_env.yandex_cloud.api_key,
&*app_env.yandex_cloud.func_id,
)
.await
.map_err(|error| Error::QueryUrlFailed(error))?
};
log::info!("For the initial setup, a link {} will be used", url);
let snapshot = ScheduleSnapshot::new(downloader, url)
.await
.map_err(|error| Error::SnapshotCreationFailed(error))?;
log::info!("Schedule snapshot successfully created!");
self.snapshot.write(snapshot);
Ok(())
}
/// 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
///
/// Uses `unsafe` to access the initialized snapshot, guaranteed valid by prior `init()` call
#[allow(unused)] // TODO: сделать авто апдейт
pub async fn update(
&mut self,
downloader: &mut BasicXlsDownloader,
app_env: &AppEnv,
) -> Result<(), Error> {
assert!(app_env.schedule.auto_update);
let url = Self::query_url(
&*app_env.yandex_cloud.api_key,
&*app_env.yandex_cloud.func_id,
)
.await
.map_err(|error| Error::QueryUrlFailed(error))?;
let snapshot = match ScheduleSnapshot::new(downloader, url).await {
Ok(snapshot) => snapshot,
Err(SnapshotCreationError::SameUrl) => {
unsafe { self.snapshot.assume_init_mut() }.update();
return Ok(());
}
Err(error) => return Err(Error::SnapshotCreationFailed(error)),
};
self.snapshot.write(snapshot);
Ok(())
}
}

33
src/test_env.rs Normal file
View File

@@ -0,0 +1,33 @@
#[cfg(test)]
pub(crate) mod tests {
use crate::state::{AppState, ScheduleSnapshot, new_app_state};
use actix_web::web;
use log::info;
use schedule_parser::test_utils::test_result;
use std::default::Default;
use tokio::sync::OnceCell;
pub fn test_env() {
info!("Loading test environment file...");
dotenvy::from_path(".env.test").expect("Failed to load test environment file");
}
pub async fn test_app_state() -> web::Data<AppState> {
let state = new_app_state().await.unwrap();
state.get_schedule().await.snapshot.write(ScheduleSnapshot {
fetched_at: Default::default(),
updated_at: Default::default(),
url: "".to_string(),
data: test_result().unwrap(),
});
state.clone()
}
pub async fn static_app_state() -> web::Data<AppState> {
static STATE: OnceCell<web::Data<AppState>> = OnceCell::const_new();
STATE.get_or_init(|| test_app_state()).await.clone()
}
}

19
src/utility/error.rs Normal file
View File

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

38
src/utility/hasher.rs Normal file
View File

@@ -0,0 +1,38 @@
use sha1::Digest;
use std::hash::Hasher;
/// Hesher returning hash from the algorithm implementing Digest
pub struct DigestHasher<D: Digest> {
digest: D,
}
impl<D> DigestHasher<D>
where
D: Digest,
{
/// Obtain hash.
pub fn finalize(self) -> String {
hex::encode(self.digest.finalize().0)
}
}
impl<D> From<D> for DigestHasher<D>
where
D: Digest,
{
/// Creating a hash from an algorithm implementing Digest.
fn from(digest: D) -> Self {
DigestHasher { digest }
}
}
impl<D: Digest> Hasher for DigestHasher<D> {
/// Stopper to prevent calling the standard Hasher result.
fn finish(&self) -> u64 {
unimplemented!("Do not call finish()");
}
fn write(&mut self, bytes: &[u8]) {
self.digest.update(bytes);
}
}

169
src/utility/jwt.rs Normal file
View File

@@ -0,0 +1,169 @@
use chrono::Duration;
use chrono::Utc;
use jsonwebtoken::errors::ErrorKind;
use jsonwebtoken::{Algorithm, DecodingKey, EncodingKey, Header, Validation, decode};
use serde::{Deserialize, Serialize};
use serde_with::DisplayFromStr;
use serde_with::serde_as;
use std::env;
use std::mem::discriminant;
use std::sync::LazyLock;
/// Key for token verification.
static DECODING_KEY: LazyLock<DecodingKey> = LazyLock::new(|| {
let secret = env::var("JWT_SECRET").expect("JWT_SECRET must be set");
DecodingKey::from_secret(secret.as_bytes())
});
/// Key for creating a signed token.
static ENCODING_KEY: LazyLock<EncodingKey> = LazyLock::new(|| {
let secret = env::var("JWT_SECRET").expect("JWT_SECRET must be set");
EncodingKey::from_secret(secret.as_bytes())
});
/// Token verification errors.
#[allow(dead_code)]
#[derive(Debug)]
pub enum Error {
/// The token has a different signature.
InvalidSignature,
/// Token reading error.
InvalidToken(ErrorKind),
/// Token expired.
Expired,
}
impl PartialEq for Error {
fn eq(&self, other: &Self) -> bool {
discriminant(self) == discriminant(other)
}
}
/// The data the token holds.
#[serde_as]
#[derive(Debug, Serialize, Deserialize)]
struct Claims {
/// User account UUID.
id: String,
/// Token creation date.
#[serde_as(as = "DisplayFromStr")]
iat: u64,
/// Token expiry date.
#[serde_as(as = "DisplayFromStr")]
exp: u64,
}
/// Token signing algorithm.
pub(crate) const DEFAULT_ALGORITHM: Algorithm = Algorithm::HS256;
/// Checking the token and extracting the UUID of the user account from it.
pub fn verify_and_decode(token: &String) -> Result<String, Error> {
let mut validation = Validation::new(DEFAULT_ALGORITHM);
validation.required_spec_claims.remove("exp");
validation.validate_exp = false;
let result = decode::<Claims>(&token, &*DECODING_KEY, &validation);
match result {
Ok(token_data) => {
if token_data.claims.exp < Utc::now().timestamp().unsigned_abs() {
Err(Error::Expired)
} else {
Ok(token_data.claims.id)
}
}
Err(err) => Err(match err.into_kind() {
ErrorKind::InvalidSignature => Error::InvalidSignature,
ErrorKind::ExpiredSignature => Error::Expired,
kind => Error::InvalidToken(kind),
}),
}
}
/// Creating a user token.
pub fn encode(id: &String) -> String {
let header = Header {
typ: Some(String::from("JWT")),
..Default::default()
};
let iat = Utc::now();
let exp = iat + Duration::days(365 * 4);
let claims = Claims {
id: id.clone(),
iat: iat.timestamp().unsigned_abs(),
exp: exp.timestamp().unsigned_abs(),
};
jsonwebtoken::encode(&header, &claims, &*ENCODING_KEY).unwrap()
}
#[cfg(test)]
mod tests {
use super::*;
use crate::test_env::tests::test_env;
#[test]
fn test_encode() {
test_env();
assert_eq!(encode(&"test".to_string()).is_empty(), false);
}
#[test]
fn test_decode_invalid_token() {
test_env();
let token = "".to_string();
let result = verify_and_decode(&token);
assert!(result.is_err());
assert_eq!(
result.err().unwrap(),
Error::InvalidToken(ErrorKind::InvalidToken)
);
}
//noinspection SpellCheckingInspection
#[test]
fn test_decode_invalid_signature() {
test_env();
let token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOiIxNjE2NTI2Mzc2IiwiaWF0IjoiMTQ5MDM4MjM3NiIsImlkIjoiNjdkY2M5YTk1MDdiMDAwMDc3Mjc0NGEyIn0.Qc2LbMJTvl2hWzDM2XyQv4m9lIqR84COAESQAieUxz8".to_string();
let result = verify_and_decode(&token);
assert!(result.is_err());
assert_eq!(result.err().unwrap(), Error::InvalidSignature);
}
//noinspection SpellCheckingInspection
#[test]
fn test_decode_expired() {
test_env();
let token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjY3ZGNjOWE5NTA3YjAwMDA3NzI3NDRhMiIsImlhdCI6IjAiLCJleHAiOiIwIn0.GBsVYvnZIfHXt00t-qmAdUMyHSyWOBtC0Mrxwg1HQOM".to_string();
let result = verify_and_decode(&token);
assert!(result.is_err());
assert_eq!(result.err().unwrap(), Error::Expired);
}
//noinspection SpellCheckingInspection
#[test]
fn test_decode_ok() {
test_env();
let token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjY3ZGNjOWE5NTA3YjAwMDA3NzI3NDRhMiIsImlhdCI6Ijk5OTk5OTk5OTkiLCJleHAiOiI5OTk5OTk5OTk5In0.o1vN-ze5iaJrnlHqe7WARXMBhhzjxTjTKkjlmTGEnOI".to_string();
let result = verify_and_decode(&token);
assert!(result.is_ok());
}
}

Some files were not shown because too many files have changed in this diff Show More