aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Vitaly Takmazov2018-10-07 00:13:54 +0300
committerGravatar Vitaly Takmazov2018-10-07 00:13:54 +0300
commit2109acda68cce512b7b2dc80f8243651a213374d (patch)
treea4cecb03c00308f33fcdbff7c793fde0c0a61608
parente882914b29f384d3f530865737cc7de3790e5b0e (diff)
Do not duplicate external replies
-rw-r--r--juick-common/src/main/java/com/juick/service/MessagesService.java2
-rw-r--r--juick-server/src/main/java/com/juick/server/api/activity/Profile.java46
-rw-r--r--juick-server/src/main/java/com/juick/service/MessagesServiceImpl.java6
-rw-r--r--juick-server/src/main/resources/db/migration/V1.9__reply_uri_index.sql1
4 files changed, 35 insertions, 20 deletions
diff --git a/juick-common/src/main/java/com/juick/service/MessagesService.java b/juick-common/src/main/java/com/juick/service/MessagesService.java
index f80fe32f..2298e769 100644
--- a/juick-common/src/main/java/com/juick/service/MessagesService.java
+++ b/juick-common/src/main/java/com/juick/service/MessagesService.java
@@ -132,4 +132,6 @@ public interface MessagesService {
boolean updateMessage(Integer mid, Integer rid, String body);
boolean updateReplyUri(Message reply, URI replyUri);
+
+ boolean replyExists(URI replyUri);
}
diff --git a/juick-server/src/main/java/com/juick/server/api/activity/Profile.java b/juick-server/src/main/java/com/juick/server/api/activity/Profile.java
index 261552d7..6a35f3d9 100644
--- a/juick-server/src/main/java/com/juick/server/api/activity/Profile.java
+++ b/juick-server/src/main/java/com/juick/server/api/activity/Profile.java
@@ -280,29 +280,35 @@ public class Profile {
if (activity instanceof Create) {
if (activity.getObject() instanceof Map) {
Map<String, Object> note = (Map<String, Object>) activity.getObject();
- String inReplyTo = (String)note.get("inReplyTo");
- if (StringUtils.isNotBlank(inReplyTo)) {
- if (inReplyTo.startsWith(baseUri)) {
- UriComponents uri = UriComponentsBuilder.fromUriString(inReplyTo).build();
- String postId = uri.getPath().substring(uri.getPath().lastIndexOf('/') + 1).replace("-", "/");
- User user = new User();
- user.setUri(URI.create(activity.getActor()));
- String attachment = StringUtils.EMPTY;
- if (note.get("attachment") != null && ((List)note.get("attachment")).size() > 0) {
- Map<String, Object> attachmentObj = (Map<String, Object>) ((List<Object>) note.get("attachment")).get(0);
- attachment = (String) attachmentObj.get("url");
- }
- CommandResult result = commandsManager.processCommand(user, String.format("#%s %s", postId, note.get("content")), URI.create(attachment));
- logger.info(jsonMapper.writeValueAsString(result));
- if (result.getNewMessage().isPresent()) {
- messagesService.updateReplyUri(result.getNewMessage().get(), URI.create((String)note.get("id")));
- return new ResponseEntity<>(HttpStatus.OK);
- } else {
- return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
+ if (note.get("type").equals("Note")) {
+ URI noteId = URI.create((String) note.get("id"));
+ if (messagesService.replyExists(noteId)) {
+ return new ResponseEntity<>(HttpStatus.OK);
+ } else {
+ String inReplyTo = (String) note.get("inReplyTo");
+ if (StringUtils.isNotBlank(inReplyTo)) {
+ if (inReplyTo.startsWith(baseUri)) {
+ UriComponents uri = UriComponentsBuilder.fromUriString(inReplyTo).build();
+ String postId = uri.getPath().substring(uri.getPath().lastIndexOf('/') + 1).replace("-", "/");
+ User user = new User();
+ user.setUri(URI.create(activity.getActor()));
+ String attachment = StringUtils.EMPTY;
+ if (note.get("attachment") != null && ((List) note.get("attachment")).size() > 0) {
+ Map<String, Object> attachmentObj = (Map<String, Object>) ((List<Object>) note.get("attachment")).get(0);
+ attachment = (String) attachmentObj.get("url");
+ }
+ CommandResult result = commandsManager.processCommand(user, String.format("#%s %s", postId, note.get("content")), URI.create(attachment));
+ logger.info(jsonMapper.writeValueAsString(result));
+ if (result.getNewMessage().isPresent()) {
+ messagesService.updateReplyUri(result.getNewMessage().get(), noteId);
+ return new ResponseEntity<>(HttpStatus.OK);
+ } else {
+ return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
+ }
+ }
}
}
}
-
}
}
logger.warn("Unknown activity: {}", jsonMapper.writeValueAsString(activity));
diff --git a/juick-server/src/main/java/com/juick/service/MessagesServiceImpl.java b/juick-server/src/main/java/com/juick/service/MessagesServiceImpl.java
index 0b0a11ae..4831c9d1 100644
--- a/juick-server/src/main/java/com/juick/service/MessagesServiceImpl.java
+++ b/juick-server/src/main/java/com/juick/service/MessagesServiceImpl.java
@@ -1106,4 +1106,10 @@ public class MessagesServiceImpl extends BaseJdbcService implements MessagesServ
return jdbcTemplate.update("UPDATE replies SET reply_uri=?, html=1 WHERE message_id=? AND reply_id=?",
replyUri.toASCIIString(), reply.getMid(), reply.getRid()) > 0;
}
+
+ @Override
+ public boolean replyExists(URI replyUri) {
+ return jdbcTemplate.queryForList("SELECT reply_id FROM replies WHERE reply_uri=?",
+ Integer.class, replyUri.toASCIIString()).size() > 0;
+ }
}
diff --git a/juick-server/src/main/resources/db/migration/V1.9__reply_uri_index.sql b/juick-server/src/main/resources/db/migration/V1.9__reply_uri_index.sql
new file mode 100644
index 00000000..0ee3c77f
--- /dev/null
+++ b/juick-server/src/main/resources/db/migration/V1.9__reply_uri_index.sql
@@ -0,0 +1 @@
+create index reply_uri_index on replies(reply_uri) \ No newline at end of file