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 6 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.5.2" }
rbatis = { version = "4.5.7" }
rbdc-pg = { version = "4.5.2" }
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
19 changes: 10 additions & 9 deletions blog-server-api/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

mod endpoints;
mod extensions;
mod migrations;
mod router;
mod utils;

Expand Down Expand Up @@ -33,19 +34,19 @@ async fn main() -> screw_components::dyn_result::DResult<()> {
}

pub async fn init_db() -> rbatis::RBatis {
let rb = rbatis::RBatis::new_with_opt(rbatis::RBatisOption::default());
rb.init(rbdc_pg::driver::PgDriver {}, PG_URL).unwrap();

let sql = std::fs::read_to_string("./table_pg.sql").unwrap();
rb.exec(&sql, vec![]).await.expect("DB migration failed");
let rb = rbatis::RBatis::new();
rb.init(rbdc_pg::driver::PgDriver {}, PG_URL)
.expect("DB init failed");
migrations::exec(&rb).await.expect("DB migration failed");
return rb;
}

pub async fn init_rabbit(
) -> Box<dyn blog_server_services::traits::event_bus_service::EventBusService> {
if RABBIT_URL.is_empty() {
blog_server_services::impls::create_rabbit_event_bus_service(None).await
blog_server_services::impls::create_rabbit_event_bus_service(if RABBIT_URL.is_empty() {
None
} else {
blog_server_services::impls::create_rabbit_event_bus_service(Some(RABBIT_URL)).await
}
Some(RABBIT_URL)
})
.await
}
6 changes: 6 additions & 0 deletions blog-server-api/src/migrations/base.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
pub async fn exec(rb: &rbatis::RBatis) -> Result<(), Box<dyn std::error::Error>> {
let sql = std::fs::read_to_string("./table_pg.sql")?;
rb.exec(&sql, vec![]).await?;

Ok(())
}
40 changes: 40 additions & 0 deletions blog-server-api/src/migrations/content.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
pub async fn exec(rb: &rbatis::RBatis) -> Result<(), Box<dyn std::error::Error>> {
let is_content_migrated: bool = rb
.query_decode::<u64>(
"select count(1) as count from migration where key='content_migration'",
tikitko marked this conversation as resolved.
Show resolved Hide resolved
vec![],
)
.await?
> 0;

if !is_content_migrated {
let posts: Vec<blog_server_services::traits::post_service::Post> =
rb.query_decode("select * from post", vec![]).await?;
for post in posts {
let content = post
.base
.content
.as_ref()
.map(|c| blog_server_services::utils::html::clean(c));
let plain_text_content = content
.as_ref()
.map(|c| blog_server_services::utils::html::to_plain(c));
rb.query(
"update post set content=?, plain_text_content=? where id=?",
vec![
rbs::to_value!(content),
rbs::to_value!(plain_text_content),
rbs::to_value!(post.id),
],
)
.await?;
}
rb.query(
"insert into migration (key) values ('content_migration')",
vec![],
)
.await?;
}

Ok(())
}
8 changes: 8 additions & 0 deletions blog-server-api/src/migrations/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
mod base;
mod content;

pub async fn exec(rb: &rbatis::RBatis) -> Result<(), Box<dyn std::error::Error>> {
base::exec(rb).await?;
content::exec(rb).await?;
Ok(())
}
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.5.2" }
rbatis = { version = "4.5.7" }
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"
43 changes: 30 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,23 @@ 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 \
LIMIT \
#{limit} \
OFFSET \
#{offset} \
"
)]
async fn select_by_query_with_limit_and_offset(
Expand Down Expand Up @@ -312,9 +328,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 +347,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
35 changes: 20 additions & 15 deletions blog-server-services/src/traits/post_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,27 +33,32 @@ 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>,
}

impl From<(u64, ECommonPost)> for BasePost {
fn from(value: (u64, ECommonPost)) -> Self {
fn from((author_id, post): (u64, ECommonPost)) -> Self {
let slug = {
let transliterated = transliteration::ru_to_latin_single(
post.title.clone(),
transliteration::TranslitOption::ToLowerCase,
)
.transliterated;
string_filter::remove_non_latin_or_number_chars(&transliterated)
};
let content = post.content.as_ref().map(|c| html::clean(c));
let plain_text_content = content.as_ref().map(|c| html::to_plain(c));
BasePost {
author_id: value.0,
author_id,
created_at: time_utils::now_as_secs(),
slug: {
let transliterated = transliteration::ru_to_latin_single(
value.1.title.clone(),
transliteration::TranslitOption::ToLowerCase,
)
.transliterated;
string_filter::remove_non_latin_or_number_chars(&transliterated)
},
title: value.1.title,
summary: value.1.summary,
published: value.1.published,
content: value.1.content,
image_url: value.1.image_url,
slug,
title: post.title,
summary: post.summary,
published: post.published,
content,
plain_text_content,
image_url: post.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;
19 changes: 19 additions & 0 deletions table_pg.sql
Original file line number Diff line number Diff line change
Expand Up @@ -215,3 +215,22 @@ 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 $$

;

DO $$ BEGIN
IF NOT EXISTS(select * from pg_tables where schemaname = 'public' and tablename = 'migration') THEN
CREATE TABLE migration (
key VARCHAR(100) NOT NULL,
PRIMARY KEY (key)
tikitko marked this conversation as resolved.
Show resolved Hide resolved
);
END IF;
END $$

;