Skip to content

Commit

Permalink
Probably fix #36
Browse files Browse the repository at this point in the history
  • Loading branch information
grishka committed Oct 29, 2021
1 parent 4bc7b0a commit 1ef0d84
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 21 deletions.
43 changes: 26 additions & 17 deletions src/main/java/smithereen/storage/GroupStorage.java
Original file line number Diff line number Diff line change
Expand Up @@ -355,17 +355,22 @@ public static void joinGroup(Group group, int userID, boolean tentative, boolean
conn.createStatement().execute("START TRANSACTION");
boolean success=false;
try{
PreparedStatement stmt=conn.prepareStatement("INSERT INTO group_memberships (user_id, group_id, tentative, accepted) VALUES (?, ?, ?, ?)");
stmt.setInt(1, userID);
stmt.setInt(2, group.id);
stmt.setBoolean(3, tentative);
stmt.setBoolean(4, accepted);
stmt.execute();
new SQLQueryBuilder(conn)
.insertInto("group_memberships")
.value("user_id", userID)
.value("group_id", group.id)
.value("tentative", tentative)
.value("accepted", accepted)
.createStatement()
.execute();

String memberCountField=tentative ? "tentative_member_count" : "member_count";
stmt=conn.prepareStatement("UPDATE groups SET "+memberCountField+"="+memberCountField+"+1 WHERE id=?");
stmt.setInt(1, group.id);
stmt.execute();
new SQLQueryBuilder(conn)
.update("groups")
.valueExpr(memberCountField, memberCountField+"+1")
.where("id=?", group.id)
.createStatement()
.execute();

removeFromCache(group);

Expand All @@ -380,15 +385,19 @@ public static void leaveGroup(Group group, int userID, boolean tentative) throws
conn.createStatement().execute("START TRANSACTION");
boolean success=false;
try{
PreparedStatement stmt=conn.prepareStatement("DELETE FROM group_memberships WHERE user_id=? AND group_id=?");
stmt.setInt(1, userID);
stmt.setInt(2, group.id);
stmt.execute();
new SQLQueryBuilder(conn)
.deleteFrom("group_memberships")
.where("user_id=? AND group_id=?", userID, group.id)
.createStatement()
.execute();

String memberCountField=tentative ? "tentative_member_count" : "member_count";
stmt=conn.prepareStatement("UPDATE groups SET "+memberCountField+"="+memberCountField+"-1 WHERE id=?");
stmt.setInt(1, group.id);
stmt.execute();
new SQLQueryBuilder(conn)
.update("groups")
.valueExpr(memberCountField, memberCountField+"-1")
.where("id=?", group.id)
.createStatement()
.execute();

removeFromCache(group);

Expand Down Expand Up @@ -471,7 +480,7 @@ public static List<URI> getGroupMemberURIs(int groupID, boolean tentative, int o

public static List<URI> getGroupMemberInboxes(int groupID) throws SQLException{
Connection conn=DatabaseConnectionManager.getConnection();
PreparedStatement stmt=conn.prepareStatement("SELECT DISTINCT IFNULL(ap_shared_inbox, ap_inbox) FROM users WHERE id IN (SELECT user_id FROM group_memberships WHERE group_id=? AND accepted=1) AND ap_inbox IS NOT NULL");
PreparedStatement stmt=conn.prepareStatement("SELECT DISTINCT IFNULL(ap_shared_inbox, ap_inbox) FROM `users` WHERE id IN (SELECT user_id FROM group_memberships WHERE group_id=? AND accepted=1) AND ap_inbox IS NOT NULL");
stmt.setInt(1, groupID);
ArrayList<URI> inboxes=new ArrayList<>();
try(ResultSet res=stmt.executeQuery()){
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/smithereen/storage/UserStorage.java
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ public static List<Integer> getFriendIDsForUser(int userID) throws SQLException{

public static List<URI> getActivityPubFriendList(int userID, int offset, int count) throws SQLException{
Connection conn=DatabaseConnectionManager.getConnection();
PreparedStatement stmt=conn.prepareStatement("SELECT followee_id, ap_id FROM followings JOIN users ON followee_id=users.id WHERE follower_id=? AND mutual=1 ORDER BY followee_id ASC LIMIT ? OFFSET ?");
PreparedStatement stmt=conn.prepareStatement("SELECT followee_id, ap_id FROM followings JOIN `users` ON followee_id=users.id WHERE follower_id=? AND mutual=1 ORDER BY followee_id ASC LIMIT ? OFFSET ?");
stmt.setInt(1, userID);
stmt.setInt(2, count);
stmt.setInt(3, offset);
Expand Down Expand Up @@ -975,9 +975,9 @@ public static BirthdayReminder getBirthdayReminderForUser(int userID, LocalDate
}
LocalDate nextDay=date.plusDays(1);
Connection conn=DatabaseConnectionManager.getConnection();
PreparedStatement stmt=SQLQueryBuilder.prepareStatement(conn, "SELECT users.id, users.bdate FROM users RIGHT JOIN followings ON followings.followee_id=users.id" +
" WHERE followings.follower_id=? AND followings.mutual=1 AND users.bdate IS NOT NULL" +
" AND ((DAY(users.bdate)=? AND MONTH(users.bdate)=?) OR (DAY(users.bdate)=? AND MONTH(users.bdate)=?))",
PreparedStatement stmt=SQLQueryBuilder.prepareStatement(conn, "SELECT `users`.id, `users`.bdate FROM `users` RIGHT JOIN followings ON followings.followee_id=`users`.id" +
" WHERE followings.follower_id=? AND followings.mutual=1 AND `users`.bdate IS NOT NULL" +
" AND ((DAY(`users`.bdate)=? AND MONTH(`users`.bdate)=?) OR (DAY(`users`.bdate)=? AND MONTH(`users`.bdate)=?))",
userID, date.getDayOfMonth(), date.getMonthValue(), nextDay.getDayOfMonth(), nextDay.getMonthValue());

List<Integer> today=new ArrayList<>(), tomorrow=new ArrayList<>();
Expand Down

0 comments on commit 1ef0d84

Please sign in to comment.