Compare commits

..

2 Commits

Author SHA1 Message Date
5a74e1cb5b Merge remote-tracking branch 'origin/main' 2024-06-11 21:10:38 +02:00
c293f20559 improved anonymization 2024-06-11 21:10:27 +02:00
3 changed files with 26 additions and 16 deletions

View File

@@ -35,21 +35,16 @@ public class JodelPost {
public Optional<Long> parent = Optional.empty(); public Optional<Long> parent = Optional.empty();
// anonymize function to recursively anonymize the posts // anonymize function to recursively anonymize the posts
public void anonymize(Optional<Vector<Long>> idCache) { public void anonymize(Vector<Long> 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<>());
}
// get the anonymized id as index in cached authorIDs // 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 the index is -1 the authorID has not been cached jet
if (i == -1) { if (i == -1) {
// set the current anonymousID as length of the cache (== next index) // 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 // 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 // the authorID has been anonymized once before, so we can get it from cache
else this.anonymousID = (long) i; else this.anonymousID = (long) i;

View File

@@ -18,6 +18,9 @@ public class Routes {
// list of all posts (not comments) in range // list of all posts (not comments) in range
Vector<JodelPost> posts = new Vector<>(); Vector<JodelPost> posts = new Vector<>();
// get UserID
Long userID = User.getUserID();
// DB connection and statement // DB connection and statement
Connection c; Connection c;
PreparedStatement stmt; PreparedStatement stmt;
@@ -77,7 +80,7 @@ public class Routes {
stmt.setObject(1, longitude); stmt.setObject(1, longitude);
stmt.setObject(2, latitude); stmt.setObject(2, latitude);
stmt.setObject(3, User.getUserID()); stmt.setObject(3, userID);
// query recursively for posts inside a 10km radius // query recursively for posts inside a 10km radius
ResultSet rs = stmt.executeQuery(); ResultSet rs = stmt.executeQuery();
@@ -116,7 +119,12 @@ public class Routes {
} }
// calculate anonymous IDs for the posts // calculate anonymous IDs for the posts
posts.forEach(post -> post.anonymize(Optional.empty())); posts.forEach(post -> {
Vector<Long> anonymousIDs = new Vector<Long>();
anonymousIDs.add(userID);
post.anonymize(anonymousIDs);
});
// return the posts // return the posts
return posts; return posts;
} }
@@ -126,6 +134,9 @@ public class Routes {
// list of all posts (not comments) in range // list of all posts (not comments) in range
Optional<JodelPost> root_post = Optional.empty(); Optional<JodelPost> root_post = Optional.empty();
// get UserID
Long userID = User.getUserID();
// DB connection and statement // DB connection and statement
Connection c; Connection c;
PreparedStatement stmt; PreparedStatement stmt;
@@ -181,7 +192,7 @@ public class Routes {
SELECT * FROM targets LEFT JOIN (SELECT post, positive as reaction FROM reactions WHERE userid = (?)) ON post = id;"""); SELECT * FROM targets LEFT JOIN (SELECT post, positive as reaction FROM reactions WHERE userid = (?)) ON post = id;""");
stmt.setObject(1, id); stmt.setObject(1, id);
stmt.setObject(2, User.getUserID()); stmt.setObject(2, userID);
// query recursively for posts inside a 10km radius // query recursively for posts inside a 10km radius
ResultSet rs = stmt.executeQuery(); ResultSet rs = stmt.executeQuery();
@@ -220,7 +231,9 @@ public class Routes {
} }
// calculate anonymous IDs for the post // calculate anonymous IDs for the post
root_post.ifPresent(root -> root.anonymize(Optional.empty())); Vector<Long> anonymousIDs = new Vector<Long>();
anonymousIDs.add(userID);
root_post.ifPresent(root -> root.anonymize(anonymousIDs));
// return the posts // return the posts
if (root_post.isEmpty()) throw new ResponseStatusException(HttpStatus.NOT_FOUND, "No post found"); if (root_post.isEmpty()) throw new ResponseStatusException(HttpStatus.NOT_FOUND, "No post found");
@@ -296,7 +309,9 @@ public class Routes {
post.reaction = Optional.empty(); post.reaction = Optional.empty();
// anonymize code // anonymize code
post.anonymize(Optional.empty()); Vector<Long> anonymousIDs = new Vector<Long>();
anonymousIDs.add(0L);
post.anonymize(anonymousIDs);
return post; return post;
} }

View File

@@ -3,7 +3,7 @@ package de.anxietyprime.swajodel;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import java.util.Optional; import java.util.Vector;
@SpringBootTest @SpringBootTest
class SwaJodelApplicationTests { class SwaJodelApplicationTests {
@@ -25,7 +25,7 @@ class SwaJodelApplicationTests {
post.comments.add(new JodelPost(1001)); post.comments.add(new JodelPost(1001));
post.comments.add(new JodelPost(1001)); post.comments.add(new JodelPost(1001));
post.anonymize(Optional.empty()); post.anonymize(new Vector<Long>());
assert (post.anonymousID == 0); assert (post.anonymousID == 0);
assert (post.comments.get(0).anonymousID == 1); assert (post.comments.get(0).anonymousID == 1);