improved anonymization

This commit is contained in:
2024-06-11 20:35:23 +02:00
parent 2ca997fca9
commit c293f20559
3 changed files with 28 additions and 18 deletions

View File

@@ -35,21 +35,16 @@ public class JodelPost {
public Optional<Long> parent = Optional.empty();
// anonymize function to recursively anonymize the posts
public void anonymize(Optional<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<>());
}
public void anonymize(Vector<Long> 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;

View File

@@ -18,6 +18,9 @@ public class Routes {
// list of all posts (not comments) in range
Vector<JodelPost> 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<Long> anonymousIDs = new Vector<Long>();
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<JodelPost> 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<Long> anonymousIDs = new Vector<Long>();
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<Long> anonymousIDs = new Vector<Long>();
anonymousIDs.add(0L);
post.anonymize(anonymousIDs);
return post;
}

View File

@@ -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<Long>());
assert (post.anonymousID == 0);
assert (post.comments.get(0).anonymousID == 1);