Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ya-procachal-tvoi-search-chyvak #73

Merged
merged 13 commits into from
Dec 28, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions blog-server-api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ tokio = { version = "1.27.0", features = ["full"] }
hyper = { version = "0.14.26", features = ["full"] }
serde = { version = "1.0.160", features = ["derive"] }
serde_json = { version = "1.0.104" }
rbs = { version = "4.3" }
rbatis = { version = "4.3" }
rbdc-pg = { version = "4.3" }
rbs = { version = "4.3.3" }
rbatis = { version = "4.3.15" }
rbdc-pg = { version = "4.3.12" }
async-trait = { version = "0.1.68" }
password-hash = "0.5.0"
argon2 = "0.5.0"
Expand All @@ -57,5 +57,4 @@ validator = { version = "0.16.1" }
reqwest = { version = "0.11.20", features = ["json"], optional = true }
sha2 = { version = "0.10.0", optional = true }
hmac = { version = "0.12.1", optional = true }
hex = { version = "0.4.3", optional = true }
ammonia = "3.3.0"
hex = { version = "0.4.3", optional = true }
5 changes: 1 addition & 4 deletions blog-server-api/src/endpoints/create_post/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ use super::response_content_failure::CreatePostContentFailure;
use super::response_content_failure::CreatePostContentFailure::*;
use super::response_content_success::CreatePostContentSuccess;

use crate::utils::html;

pub async fn http_handler(
(CreatePostRequestContent {
new_post_data,
Expand All @@ -25,10 +23,9 @@ pub async fn http_handler(
return Err(CreatingForbidden);
}

let mut base_post = new_post_data.map_err(|e| ValidationError {
let base_post = new_post_data.map_err(|e| ValidationError {
reason: e.to_string(),
})?;
base_post.content = base_post.content.map(|c| html::clean(&c));

if let Some(err) = base_post.validate().err() {
return Err(ValidationError {
Expand Down
5 changes: 1 addition & 4 deletions blog-server-api/src/endpoints/telegram_login/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,7 @@ pub async fn http_handler(
}

let telegram_base_minimal_author = BaseMinimalAuthor {
slug: author_slug_utils::extend(
&username.unwrap_or(id.to_string()),
&"t".to_string(),
),
slug: author_slug_utils::extend(&username.unwrap_or(id.to_string()), &"t".to_string()),
first_name,
last_name,
image_url: photo_url.map(|u| {
Expand Down
5 changes: 1 addition & 4 deletions blog-server-api/src/endpoints/update_post/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ use super::response_content_failure::UpdatePostContentFailure;
use super::response_content_failure::UpdatePostContentFailure::*;
use super::response_content_success::UpdatePostContentSuccess;

use crate::utils::html;

pub async fn http_handler(
(UpdatePostRequestContent {
id,
Expand Down Expand Up @@ -50,10 +48,9 @@ pub async fn http_handler(
return Err(EditingForbidden);
}

let mut base_post = updated_post_data.map_err(|e| ValidationError {
let base_post = updated_post_data.map_err(|e| ValidationError {
reason: e.to_string(),
})?;
base_post.content = base_post.content.map(|c| html::clean(&c));

if let Some(err) = base_post.validate().err() {
return Err(ValidationError {
Expand Down
1 change: 0 additions & 1 deletion blog-server-api/src/utils/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
pub mod auth;
pub mod html;
pub mod jwt;
pub mod password;
8 changes: 5 additions & 3 deletions blog-server-services/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@ package = "screw-components"

[dependencies]
blog-generic = { path = "../blog-generic" }
rbs = { version = "4.3" }
rbatis = { version = "4.3" }
rbs = { version = "4.3.3" }
rbatis = { version = "4.3.15" }
amqprs = { version = "1.5.1", features = ["urispec"] }
async-trait = { version = "0.1.68" }
serde = { version = "1.0.160", features = ["derive"] }
serde_repr = { version = "0.1.12" }
serde_json = { version = "1.0.96" }
translit = { version = "0.5.0" }
translit = { version = "0.5.0" }
ammonia = "3.3.0"
html2text = "0.9.0"
39 changes: 26 additions & 13 deletions blog-server-services/src/impls/rbatis_post_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,16 @@ impl Post {
}
#[py_sql(
"
SELECT COUNT(1) \
FROM post \
WHERE post.title ILIKE '%' || #{query} || '%' OR post.summary ILIKE '%' || #{query} || '%' OR post.content ILIKE '%' || #{query} || '%' \
AND post.published = 1 \
SELECT \
COUNT(1) \
FROM \
post, \
plainto_tsquery('russian', LOWER(#{query})) query, \
to_tsvector('russian', LOWER(post.title || ' ' || post.summary || ' ' || post.plain_text_content )) textsearch \
WHERE \
textsearch @@ query \
AND \
post.published = 1 \
"
)]
async fn count_by_query(rb: &RBatis, query: &String) -> rbatis::Result<u64> {
Expand Down Expand Up @@ -201,13 +207,19 @@ impl Post {
#[py_sql(
"
SELECT \
post.* \
FROM post \
WHERE post.title ILIKE '%' || #{query} || '%' OR post.summary ILIKE '%' || #{query} || '%' OR post.content ILIKE '%' || #{query} || '%' \
AND post.published = 1 \
ORDER BY post.id DESC \
LIMIT #{limit} \
OFFSET #{offset} \
post.*, \
ts_rank_cd(textsearch, query) AS rank \
FROM \
post, \
plainto_tsquery('russian', LOWER(#{query})) query, \
to_tsvector('russian', LOWER(post.title || ' ' || post.summary || ' ' || post.plain_text_content )) textsearch \
WHERE \
textsearch @@ query \
AND \
post.published = 1 \
ORDER BY \
rank \
DESC \
"
)]
async fn select_by_query_with_limit_and_offset(
Expand Down Expand Up @@ -312,9 +324,9 @@ impl RbatisPostService {
#[py_sql(
"
INSERT INTO post
(author_id,title,slug,summary,published,created_at,content,image_url)
(author_id,title,slug,summary,published,created_at,content,plain_text_content,image_url)
VALUES
(#{post.author_id},#{post.title},#{post.slug},#{post.summary},#{post.published},to_timestamp(#{post.created_at}),#{post.content},#{post.image_url})
(#{post.author_id},#{post.title},#{post.slug},#{post.summary},#{post.published},to_timestamp(#{post.created_at}),#{post.content},#{post.plain_text_content},#{post.image_url})
RETURNING id
"
)]
Expand All @@ -331,6 +343,7 @@ impl RbatisPostService {
summary = #{post_data.summary}, \
published = #{post_data.published}, \
content = #{post_data.content}, \
plain_text_content = #{post_data.plain_text_content}, \
image_url = #{post_data.image_url} \
WHERE id = #{post_id} \
RETURNING id
Expand Down
4 changes: 3 additions & 1 deletion blog-server-services/src/traits/post_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ pub struct BasePost {
pub published: u8,
pub created_at: u64,
pub content: Option<String>,
pub plain_text_content: Option<String>,
pub image_url: Option<String>,
}

Expand All @@ -52,7 +53,8 @@ impl From<(u64, ECommonPost)> for BasePost {
title: value.1.title,
summary: value.1.summary,
published: value.1.published,
content: value.1.content,
content: value.1.content.as_ref().map(|c| html::clean(c)),
plain_text_content: value.1.content.as_ref().map(|c| html::to_plain(c)),
image_url: value.1.image_url,
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,7 @@ pub fn clean(src: &str) -> String {
.clean(src)
.to_string()
}

pub fn to_plain(src: &str) -> String {
html2text::from_read(src.as_bytes(), usize::MAX)
}
1 change: 1 addition & 0 deletions blog-server-services/src/utils/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod html;
pub mod string_filter;
pub mod time_utils;
pub mod transliteration;
8 changes: 8 additions & 0 deletions table_pg.sql
Original file line number Diff line number Diff line change
Expand Up @@ -215,3 +215,11 @@ DO $$ BEGIN
END $$

;

DO $$ BEGIN
IF NOT EXISTS(select * from information_schema.columns where table_name = 'post' and column_name = 'plain_text_content') THEN
ALTER TABLE post ADD COLUMN plain_text_content TEXT;
END IF;
END $$

;
Loading