Skip to content

Commit

Permalink
Merge pull request #9 from MindsHub/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
Usioumeo authored Oct 19, 2023
2 parents def2042 + de684f9 commit a551ea4
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Insigno.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ intro_images = []
[default.databases.db]
url = "postgres://mindshub:test@localhost:5432/insignorocketdb"
timeout = 5
pool_size = 5
pool_size = 5
2 changes: 1 addition & 1 deletion migrations/2023-10-09-143951_verify_verdict/up.sql
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ CREATE OR REPLACE FUNCTION verify_set_verdict(
-- update the score of the marker image
UPDATE marker_images
SET verify_number = verify_number + 1,
verify_average = (verify_average * verify_number + CASE WHEN verdict_inp = TRUE THEN 1.0 ELSE 0.0 END)/verify_number
verify_average = (verify_average * verify_number + CASE WHEN verdict_inp = TRUE THEN 1.0 ELSE 0.0 END)/COALESCE(verify_number+1, 1)
WHERE marker_images.id = image_id_inp;

-- check if the remaining images in the session are down to 0
Expand Down
2 changes: 1 addition & 1 deletion migrations/2023-10-16-090225_get_near/up.sql
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ CREATE OR REPLACE FUNCTION get_near(
AND (SELECT COUNT (*) FROM marker_reports WHERE markers.id = reported_marker)<1
AND (
(SELECT COUNT (*) FROM marker_images WHERE markers.id = marker_images.refers_to AND (marker_images.verify_average>0.5 OR marker_images.approved) )>0
OR ((user_id_inp IS NOT NULL) OR markers.created_by =user_id_inp)
OR ((user_id_inp IS NOT NULL) AND markers.created_by =user_id_inp)
);
END;
$$ LANGUAGE plpgsql;
1 change: 1 addition & 0 deletions migrations/2023-10-19-143353_image_ref/down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
-- This file should undo anything in `up.sql`
45 changes: 45 additions & 0 deletions migrations/2023-10-19-143353_image_ref/up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
-- Your SQL goes here
ALTER TABLE IF EXISTS marker_images
ADD created_by BIGINT DEFAULT NULL;

UPDATE marker_images
SET created_by = markers.created_by
FROM markers
WHERE markers.id=marker_images.refers_to;

ALTER TABLE marker_images ALTER COLUMN created_by SET NOT NULL;

CREATE OR REPLACE FUNCTION public.create_verify(
user_id_inp bigint)
RETURNS TABLE(id bigint, verification_session bigint, image_id bigint, marker_id bigint, verdict boolean)
LANGUAGE 'plpgsql'
COST 100
VOLATILE PARALLEL UNSAFE
ROWS 1000

AS $BODY$
#variable_conflict use_column
DECLARE session_id BIGINT;
DECLARE to_choose BIGINT;

BEGIN
INSERT INTO verification_sessions(user_id) VALUES (user_id_inp) RETURNING id INTO session_id;

SELECT ceil(log(2, count(marker_images.id)+1))+5
FROM marker_images
WHERE verify_number<3
INTO to_choose;

INSERT INTO image_verifications(verification_session, image_id, marker_id)
SELECT session_id, id, refers_to
FROM marker_images
WHERE created_by<>user_id_inp
ORDER BY verify_number ASC,
random()
LIMIT to_choose;
RETURN QUERY
SELECT *
FROM image_verifications
WHERE verification_session = session_id;
END;
$BODY$;
8 changes: 5 additions & 3 deletions src/map/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,13 @@ fn convert_image(input: &Path, output: &Path) -> Result<(), InsignoError> {
}
}

async fn save_image(connection: Db, name: String, id: i64) -> Result<(), InsignoError> {
async fn save_image(connection: Db, name: String, id: i64, user: &User<Authenticated>) -> Result<(), InsignoError> {
let img = MarkerImage {
id: None,
path: name,
refers_to: id,
approved: false,
created_by: user.get_id(),
};

connection
Expand Down Expand Up @@ -130,7 +131,8 @@ pub(crate) async fn add_image(
markers::table
.filter(markers::id.eq(id))
.filter(markers::created_by.eq(user_id))
.load::<Marker>(conn)
.or_filter(markers::solved_by.eq(user_id))
.get_result::<Marker>(conn)
})
.await
.map_err(|e| InsignoError::new(500).debug(e))?;
Expand All @@ -147,7 +149,7 @@ pub(crate) async fn add_image(
.to_string();

// try to save it in database
save_image(connection, name.clone(), id)
save_image(connection, name.clone(), id, &user)
.map_err(|x| {
let _ = fs::remove_file(new_pos); //sync version
x
Expand Down
2 changes: 2 additions & 0 deletions src/map/marker_image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ table! {
path -> Text,
refers_to -> BigInt,
approved -> Bool,
created_by -> BigInt,
}
}
#[derive(Clone, Queryable, Insertable, Debug, QueryableByName)]
Expand All @@ -23,6 +24,7 @@ pub struct MarkerImage {
pub path: String,
pub refers_to: i64,
pub approved: bool,
pub created_by: i64,
}

impl Serialize for MarkerImage {
Expand Down

0 comments on commit a551ea4

Please sign in to comment.