From c293f2055997de3f50e6809310ed61afd9563514 Mon Sep 17 00:00:00 2001 From: timoschneider Date: Tue, 11 Jun 2024 20:35:23 +0200 Subject: [PATCH] improved anonymization --- .../de/anxietyprime/swajodel/JodelPost.java | 13 +++------ .../java/de/anxietyprime/swajodel/Routes.java | 29 ++++++++++++++----- .../swajodel/SwaJodelApplicationTests.java | 4 +-- 3 files changed, 28 insertions(+), 18 deletions(-) diff --git a/src/main/java/de/anxietyprime/swajodel/JodelPost.java b/src/main/java/de/anxietyprime/swajodel/JodelPost.java index 97c7986..78a0c55 100644 --- a/src/main/java/de/anxietyprime/swajodel/JodelPost.java +++ b/src/main/java/de/anxietyprime/swajodel/JodelPost.java @@ -35,21 +35,16 @@ public class JodelPost { public Optional parent = Optional.empty(); // anonymize function to recursively anonymize the posts - public void anonymize(Optional> idCache) { - // check if this is the first post in this process - if (idCache.isEmpty()) { - // create a new Vector as cache - idCache = Optional.of(new Vector<>()); - } + public void anonymize(Vector idCache) { // get the anonymized id as index in cached authorIDs - int i = idCache.get().indexOf(this.authorID); + int i = idCache.indexOf(this.authorID); // if the index is -1 the authorID has not been cached jet if (i == -1) { // set the current anonymousID as length of the cache (== next index) - this.anonymousID = (long) idCache.get().size(); + this.anonymousID = (long) idCache.size(); // push the current authorID to to cache - idCache.get().add(this.authorID); + idCache.add(this.authorID); } // the authorID has been anonymized once before, so we can get it from cache else this.anonymousID = (long) i; diff --git a/src/main/java/de/anxietyprime/swajodel/Routes.java b/src/main/java/de/anxietyprime/swajodel/Routes.java index d339182..0e21782 100644 --- a/src/main/java/de/anxietyprime/swajodel/Routes.java +++ b/src/main/java/de/anxietyprime/swajodel/Routes.java @@ -18,6 +18,9 @@ public class Routes { // list of all posts (not comments) in range Vector posts = new Vector<>(); + // get UserID + Long userID = User.getUserID(); + // DB connection and statement Connection c; PreparedStatement stmt; @@ -73,11 +76,11 @@ public class Routes { (SELECT * FROM comments inner join posts ON comments.child = posts.id) com inner join targets ON targets.id = com.parent ) - SELECT targets.*, reactions.positive AS reaction FROM targets, reactions WHERE targets.id = reactions.post AND reactions.userid = (?);"""); + SELECT * FROM targets LEFT JOIN (SELECT post, positive as reaction FROM reactions WHERE userid = (?)) ON post = id;"""); stmt.setObject(1, longitude); stmt.setObject(2, latitude); - stmt.setObject(3, User.getUserID()); + stmt.setObject(3, userID); // query recursively for posts inside a 10km radius ResultSet rs = stmt.executeQuery(); @@ -116,7 +119,12 @@ public class Routes { } // calculate anonymous IDs for the posts - posts.forEach(post -> post.anonymize(Optional.empty())); + posts.forEach(post -> { + Vector anonymousIDs = new Vector(); + anonymousIDs.add(userID); + post.anonymize(anonymousIDs); + }); + // return the posts return posts; } @@ -126,6 +134,9 @@ public class Routes { // list of all posts (not comments) in range Optional root_post = Optional.empty(); + // get UserID + Long userID = User.getUserID(); + // DB connection and statement Connection c; PreparedStatement stmt; @@ -178,10 +189,10 @@ public class Routes { (SELECT * FROM comments inner join posts ON comments.child = posts.id) com inner join targets ON targets.id = com.parent ) - SELECT targets.*, reactions.positive AS reaction FROM targets, reactions WHERE targets.id = reactions.post AND reactions.userid = (?);"""); + SELECT * FROM targets LEFT JOIN (SELECT post, positive as reaction FROM reactions WHERE userid = (?)) ON post = id;"""); stmt.setObject(1, id); - stmt.setObject(2, User.getUserID()); + stmt.setObject(2, userID); // query recursively for posts inside a 10km radius ResultSet rs = stmt.executeQuery(); @@ -220,7 +231,9 @@ public class Routes { } // calculate anonymous IDs for the post - root_post.ifPresent(root -> root.anonymize(Optional.empty())); + Vector anonymousIDs = new Vector(); + anonymousIDs.add(userID); + root_post.ifPresent(root -> root.anonymize(anonymousIDs)); // return the posts if (root_post.isEmpty()) throw new ResponseStatusException(HttpStatus.NOT_FOUND, "No post found"); @@ -296,7 +309,9 @@ public class Routes { post.reaction = Optional.empty(); // anonymize code - post.anonymize(Optional.empty()); + Vector anonymousIDs = new Vector(); + anonymousIDs.add(0L); + post.anonymize(anonymousIDs); return post; } diff --git a/src/test/java/de/anxietyprime/swajodel/SwaJodelApplicationTests.java b/src/test/java/de/anxietyprime/swajodel/SwaJodelApplicationTests.java index f0c90f3..67b157d 100644 --- a/src/test/java/de/anxietyprime/swajodel/SwaJodelApplicationTests.java +++ b/src/test/java/de/anxietyprime/swajodel/SwaJodelApplicationTests.java @@ -3,7 +3,7 @@ package de.anxietyprime.swajodel; import org.junit.jupiter.api.Test; import org.springframework.boot.test.context.SpringBootTest; -import java.util.Optional; +import java.util.Vector; @SpringBootTest class SwaJodelApplicationTests { @@ -25,7 +25,7 @@ class SwaJodelApplicationTests { post.comments.add(new JodelPost(1001)); post.comments.add(new JodelPost(1001)); - post.anonymize(Optional.empty()); + post.anonymize(new Vector()); assert (post.anonymousID == 0); assert (post.comments.get(0).anonymousID == 1);