Skip to content

Commit

Permalink
backend: various improvment to support an existing project
Browse files Browse the repository at this point in the history
  • Loading branch information
ElysaSrc committed Apr 25, 2024
1 parent 5ecd611 commit 107742d
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 10 deletions.
5 changes: 3 additions & 2 deletions backend/migrations/20240322224019_init_base_models.sql
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ CREATE TABLE tags (
id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
title VARCHAR(255) NOT NULL,
is_filter BOOLEAN NOT NULL DEFAULT FALSE,
filter_description TEXT
filter_description TEXT,
is_indexed BOOLEAN NOT NULL DEFAULT FALSE
);

CREATE TABLE entities (
Expand Down Expand Up @@ -87,7 +88,7 @@ CREATE INDEX comments_entity_id_idx ON comments(entity_id);

CREATE TABLE access_tokens (
id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
token VARCHAR(24) NOT NULL,
token VARCHAR(64) NOT NULL,
permissions JSONB NOT NULL,
active BOOLEAN NOT NULL DEFAULT TRUE
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ BEGIN
SELECT t.title
FROM tags t
JOIN entity_tags et ON t.id = et.tag_id
WHERE et.entity_id = refreshed_entity.id
WHERE t.is_indexed AND et.entity_id = refreshed_entity.id
LOOP
indexed_values := indexed_values || ' ' || tag_title;
END LOOP;
Expand Down
28 changes: 24 additions & 4 deletions backend/migrations/20240322224255_requests_for_entities_cache.sql
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ CREATE OR REPLACE FUNCTION fetch_entities_within_view(

families_list UUID[],
categories_list UUID[],
tags_list UUID[]
tags_list UUID[],

exclude_families_list UUID[],
exclude_categories_list UUID[],
exclude_tags_list UUID[]
) RETURNS TABLE (
id UUID,
entity_id UUID,
Expand Down Expand Up @@ -47,12 +51,18 @@ BEGIN
4326
)
)
-- Families
AND
(allow_all_families OR (family_id = ANY(families_list)))
AND NOT (family_id = ANY(exclude_families_list))
-- Categories
AND
(allow_all_categories OR (categories_ids && categories_list))
AND NOT (categories_ids && exclude_categories_list)
-- Tags
AND
(allow_all_tags OR (tags_ids && tags_list));
(allow_all_tags OR (tags_ids && tags_list))
AND NOT (tags_ids && exclude_tags_list);
END;
$$ LANGUAGE plpgsql;

Expand All @@ -65,7 +75,11 @@ CREATE OR REPLACE FUNCTION search_entities(

families_list UUID[],
categories_list UUID[],
tags_list UUID[]
tags_list UUID[],

exclude_families_list UUID[],
exclude_categories_list UUID[],
exclude_tags_list UUID[]
) RETURNS TABLE (
id UUID,
entity_id UUID,
Expand Down Expand Up @@ -93,11 +107,17 @@ BEGIN
FROM entities_caches
WHERE
(full_text_search_ts @@ to_tsquery(search_query))
-- Families
AND
(allow_all_families OR (family_id = ANY(families_list)))
AND NOT (family_id = ANY(exclude_families_list))
-- Categories
AND
(allow_all_categories OR (categories_ids && categories_list))
AND NOT (categories_ids && exclude_categories_list)
-- Tags
AND
(allow_all_tags OR (tags_ids && tags_list));
(allow_all_tags OR (tags_ids && tags_list))
AND NOT (tags_ids && exclude_tags_list);
END;
$$ LANGUAGE plpgsql;
6 changes: 6 additions & 0 deletions backend/src/api/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ pub async fn view_request(
families_list: token.perms.families_policy.allow_list.clone(),
categories_list: token.perms.categories_policy.allow_list.clone(),
tags_list: token.perms.tags_policy.allow_list.clone(),
exclude_families_list: token.perms.families_policy.force_exclude.clone(),
exclude_categories_list: token.perms.categories_policy.force_exclude.clone(),
exclude_tags_list: token.perms.tags_policy.force_exclude.clone(),
};

Ok(AppJson(
Expand Down Expand Up @@ -87,6 +90,9 @@ async fn search_request(
families_list: token.perms.families_policy.allow_list.clone(),
categories_list: token.perms.categories_policy.allow_list.clone(),
tags_list: token.perms.tags_policy.allow_list.clone(),
exclude_families_list: token.perms.families_policy.force_exclude.clone(),
exclude_categories_list: token.perms.categories_policy.force_exclude.clone(),
exclude_tags_list: token.perms.tags_policy.force_exclude.clone(),
};

Ok(AppJson(
Expand Down
1 change: 1 addition & 0 deletions backend/src/models/access_token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ pub struct Permissions {
pub struct PermissionPolicy {
pub allow_all: bool,
pub allow_list: Vec<Uuid>,
pub force_exclude: Vec<Uuid>,
}

#[derive(Deserialize, Serialize, ToSchema, Debug)]
Expand Down
22 changes: 20 additions & 2 deletions backend/src/models/entity_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,34 @@ pub struct FindEntitiesRequest {
pub upper_left_lon: f64,
pub lower_right_lat: f64,
pub lower_right_lon: f64,

pub allow_all_families: bool,
pub allow_all_categories: bool,
pub allow_all_tags: bool,

pub families_list: Vec<Uuid>,
pub categories_list: Vec<Uuid>,
pub tags_list: Vec<Uuid>,

pub exclude_families_list: Vec<Uuid>,
pub exclude_categories_list: Vec<Uuid>,
pub exclude_tags_list: Vec<Uuid>,
}

pub struct SearchEntitiesRequest {
pub search_query: String,

pub allow_all_families: bool,
pub allow_all_categories: bool,
pub allow_all_tags: bool,

pub families_list: Vec<Uuid>,
pub categories_list: Vec<Uuid>,
pub tags_list: Vec<Uuid>,

pub exclude_families_list: Vec<Uuid>,
pub exclude_categories_list: Vec<Uuid>,
pub exclude_tags_list: Vec<Uuid>,
}

impl CachedEntity {
Expand All @@ -60,7 +72,7 @@ impl CachedEntity {
latitude as "latitude!",
longitude as "longitude!",
plain_text_location as "plain_text_location!"
FROM fetch_entities_within_view($1,$2,$3,$4,$5,$6,$7,$8,$9,$10)
FROM fetch_entities_within_view($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13)
"#,
request.upper_left_lat,
request.upper_left_lon,
Expand All @@ -72,6 +84,9 @@ impl CachedEntity {
&request.families_list,
&request.categories_list,
&request.tags_list,
&request.exclude_families_list,
&request.exclude_categories_list,
&request.exclude_tags_list,
)
.fetch_all(conn)
.await
Expand All @@ -96,14 +111,17 @@ impl CachedEntity {
latitude as "latitude!",
longitude as "longitude!",
plain_text_location as "plain_text_location!"
FROM search_entities($1,$2,$3,$4,$5,$6,$7)"#,
FROM search_entities($1,$2,$3,$4,$5,$6,$7,$8,$9,$10)"#,
request.search_query,
request.allow_all_families,
request.allow_all_categories,
request.allow_all_tags,
&request.families_list,
&request.categories_list,
&request.tags_list,
&request.exclude_families_list,
&request.exclude_categories_list,
&request.exclude_tags_list,
)
.fetch_all(conn)
.await
Expand Down
1 change: 0 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
version: '3.1'
name: safehaven

services:
Expand Down

0 comments on commit 107742d

Please sign in to comment.