From 32c7f35bda7ed3bdad93db4cce0dd17e7f48c3bd Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Wed, 18 Sep 2019 11:35:55 +0300 Subject: Readonly messages --- .../com/juick/service/MessagesServiceImpl.java | 28 ++++++++++++++-------- 1 file changed, 18 insertions(+), 10 deletions(-) (limited to 'src/main/java/com/juick/service/MessagesServiceImpl.java') diff --git a/src/main/java/com/juick/service/MessagesServiceImpl.java b/src/main/java/com/juick/service/MessagesServiceImpl.java index a1ba26ac..099934ab 100644 --- a/src/main/java/com/juick/service/MessagesServiceImpl.java +++ b/src/main/java/com/juick/service/MessagesServiceImpl.java @@ -184,7 +184,7 @@ public class MessagesServiceImpl extends BaseJdbcService implements MessagesServ @Transactional @Override public int createReply(final int mid, final int rid, final User user, final String txt, final String attachment) { - int ridnew = getReplyIDIncrement(mid); + int ridnew = getReplyIDIncrement(mid, user.getUid()); Timestamp ts = Timestamp.from(Instant.now()); getJdbcTemplate().update("INSERT INTO replies(message_id, reply_id, user_id, replyto, attach, txt, ts, updated_at, user_uri) " + "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)", @@ -200,27 +200,35 @@ public class MessagesServiceImpl extends BaseJdbcService implements MessagesServ return ridnew; } - @Override - public int getReplyIDIncrement(final int mid) { + public int getReplyIDIncrement(final int mid, final int uid) { return getJdbcTemplate().execute((ConnectionCallback) conn -> { conn.setAutoCommit(false); - final int replyNo; - try (PreparedStatement ps = conn.prepareStatement("SELECT maxreplyid+1 FROM messages WHERE message_id=? FOR UPDATE")) { + int replyNo; + final int readOnly; + final int userId; + try (PreparedStatement ps = conn.prepareStatement("SELECT maxreplyid+1, readonly, user_id FROM messages WHERE message_id=? FOR UPDATE")) { ps.setInt(1, mid); try (ResultSet resultSet = ps.executeQuery()) { if (resultSet.next()) { replyNo = resultSet.getInt(1); + readOnly = resultSet.getInt(2); + userId = resultSet.getInt(3); } else { throw new IncorrectResultSizeDataAccessException("while getting getReplyIDIncrement, mid=" + mid, 1, 0); } } } - try (PreparedStatement ps = conn.prepareStatement("UPDATE messages SET maxreplyid=? WHERE message_id=?")) { - ps.setInt(1, replyNo); - ps.setInt(2, mid); - if (ps.executeUpdate() != 1) { - throw new IncorrectResultSizeDataAccessException("Cannot find a message to update: " + mid, 1, 0); + // author can reply to his readonly post + if (readOnly == 0 || uid == userId) { + try (PreparedStatement ps = conn.prepareStatement("UPDATE messages SET maxreplyid=? WHERE message_id=?")) { + ps.setInt(1, replyNo); + ps.setInt(2, mid); + if (ps.executeUpdate() != 1) { + throw new IncorrectResultSizeDataAccessException("Cannot find a message to update: " + mid, 1, 0); + } } + } else { + replyNo = -1; } conn.commit(); return replyNo; -- cgit v1.2.3