diff options
author | Vitaly Takmazov | 2019-09-18 11:35:55 +0300 |
---|---|---|
committer | Vitaly Takmazov | 2019-09-18 11:35:55 +0300 |
commit | 32c7f35bda7ed3bdad93db4cce0dd17e7f48c3bd (patch) | |
tree | 7e41b69ad86b2f78d9e3ed37c889715408b5be7e /src/main/java/com | |
parent | 13993e1cf558baf27e8381b934286afb49b65371 (diff) |
Readonly messages
Diffstat (limited to 'src/main/java/com')
-rw-r--r-- | src/main/java/com/juick/server/CommandsManager.java | 30 | ||||
-rw-r--r-- | src/main/java/com/juick/service/MessagesService.java | 3 | ||||
-rw-r--r-- | src/main/java/com/juick/service/MessagesServiceImpl.java | 28 |
3 files changed, 35 insertions, 26 deletions
diff --git a/src/main/java/com/juick/server/CommandsManager.java b/src/main/java/com/juick/server/CommandsManager.java index 634deb35..b21ff81c 100644 --- a/src/main/java/com/juick/server/CommandsManager.java +++ b/src/main/java/com/juick/server/CommandsManager.java @@ -550,20 +550,24 @@ public class CommandsManager { boolean attachmentProcessed = !haveAttachment || StringUtils.isNotEmpty(attachmentType); String messageText = attachmentProcessed ? txt : String.format("%s %s", txt, attachment.toASCIIString()); int newrid = messagesService.createReply(mid, rid, user, messageText, attachmentType); - if (haveAttachment && attachmentProcessed) { - String fname = String.format("%d-%d.%s", mid, newrid, attachmentType); - imagesService.saveImageWithPreviews(attachmentFName, fname); + if (newrid > 0) { + if (haveAttachment && attachmentProcessed) { + String fname = String.format("%d-%d.%s", mid, newrid, attachmentType); + imagesService.saveImageWithPreviews(attachmentFName, fname); + } + applicationEventPublisher.publishEvent( + new MessageReadEvent(this, user, msg.get())); + Message original = messagesService.getMessage(mid).orElseThrow(IllegalStateException::new); + subscriptionService.subscribeMessage(original, user); + Message reply = messagesService.getReply(mid, newrid); + reply.getUser().setAvatar(webApp.getAvatarUrl(reply.getUser())); + applicationEventPublisher.publishEvent(new MessageEvent(this, reply, subscriptionService.getUsersSubscribedToComments(original, reply))); + return CommandResult.build(reply, "Reply posted.\n#" + mid + "/" + newrid + " " + + "https://juick.com/m/" + mid + "#" + newrid, + String.format("[Reply](%s) posted", PlainTextFormatter.formatUrl(reply))); + } else { + return CommandResult.fromString("Message is read-only"); } - applicationEventPublisher.publishEvent( - new MessageReadEvent(this, user, msg.get())); - Message original = messagesService.getMessage(mid).orElseThrow(IllegalStateException::new); - subscriptionService.subscribeMessage(original, user); - Message reply = messagesService.getReply(mid, newrid); - reply.getUser().setAvatar(webApp.getAvatarUrl(reply.getUser())); - applicationEventPublisher.publishEvent(new MessageEvent(this, reply, subscriptionService.getUsersSubscribedToComments(original, reply))); - return CommandResult.build(reply,"Reply posted.\n#" + mid + "/" + newrid + " " - + "https://juick.com/m/" + mid + "#" + newrid, - String.format("[Reply](%s) posted", PlainTextFormatter.formatUrl(reply))); } } diff --git a/src/main/java/com/juick/service/MessagesService.java b/src/main/java/com/juick/service/MessagesService.java index e98b2b1d..e6e039d3 100644 --- a/src/main/java/com/juick/service/MessagesService.java +++ b/src/main/java/com/juick/service/MessagesService.java @@ -33,9 +33,6 @@ public interface MessagesService { int createMessage(int uid, String txt, String attachment, List<com.juick.Tag> tags); int createReply(int mid, int rid, User user, String txt, String attachment); - - int getReplyIDIncrement(int mid); - enum RecommendStatus { Error, Added, 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<Integer>) 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; |