diff options
author | Vitaly Takmazov | 2023-01-04 07:18:16 +0300 |
---|---|---|
committer | Vitaly Takmazov | 2023-01-04 07:18:16 +0300 |
commit | 0e20ca44eee0cb2726b575aed88a495206481973 (patch) | |
tree | 65dc225857d836eebd5acca9125fbc1cdde0c158 /src/main/java/com/juick | |
parent | a48e976347abe9e61d7b4ba61831098629aabece (diff) |
ActivityPub: handle Note updates
Diffstat (limited to 'src/main/java/com/juick')
5 files changed, 41 insertions, 28 deletions
diff --git a/src/main/java/com/juick/TelegramBotManager.java b/src/main/java/com/juick/TelegramBotManager.java index 1dcc7edb..8064fc58 100644 --- a/src/main/java/com/juick/TelegramBotManager.java +++ b/src/main/java/com/juick/TelegramBotManager.java @@ -147,7 +147,7 @@ public class TelegramBotManager implements NotificationListener { User author = originalMessage.getUser(); String newMessageText = StringUtils.defaultString(message.text()); if (user_from.equals(author) && canUpdateMessage(originalMessage, newMessageText)) { - if (messagesService.updateMessage(mid, rid, newMessageText)) { + if (messagesService.updateMessage(mid, rid, newMessageText, false)) { telegramNotify(message.chat().id(), "Message updated", new com.juick.model.Message()); return; } diff --git a/src/main/java/com/juick/service/MessagesService.java b/src/main/java/com/juick/service/MessagesService.java index dda1cfd3..b9ee612a 100644 --- a/src/main/java/com/juick/service/MessagesService.java +++ b/src/main/java/com/juick/service/MessagesService.java @@ -154,7 +154,7 @@ public interface MessagesService { List<Integer> getUnread(User user); @CacheEvict(value = { "discover", "discussions", "messages", "replies" }, allEntries = true) - boolean updateMessage(Integer mid, Integer rid, String body); + boolean updateMessage(Integer mid, Integer rid, String body, boolean foreign); @CacheEvict(value = { "discover", "discussions", "messages", "replies" }, allEntries = true) boolean updateReplyUri(Message reply, URI replyUri); diff --git a/src/main/java/com/juick/service/MessagesServiceImpl.java b/src/main/java/com/juick/service/MessagesServiceImpl.java index 3a4c3767..68d47429 100644 --- a/src/main/java/com/juick/service/MessagesServiceImpl.java +++ b/src/main/java/com/juick/service/MessagesServiceImpl.java @@ -1099,14 +1099,14 @@ public class MessagesServiceImpl extends BaseJdbcService implements MessagesServ @Transactional @Override - public boolean updateMessage(Integer mid, Integer rid, String body) { + public boolean updateMessage(Integer mid, Integer rid, String body, boolean foreign) { Instant now = Instant.now(); Instant messageEditingWindow = now.minus(15, ChronoUnit.MINUTES); if (rid == 0) { Optional<Message> message = getMessage(mid); if (message.isPresent()) { Instant ts = message.get().getUpdatedAt(); - if (ts.compareTo(messageEditingWindow) >= 0) { + if (ts.compareTo(messageEditingWindow) >= 0 || foreign) { return jdbcTemplate.update( "UPDATE messages_txt SET txt=?, updated_at=? WHERE messages_txt.message_id=?", body, Timestamp.from(now), mid) > 0; @@ -1117,7 +1117,7 @@ public class MessagesServiceImpl extends BaseJdbcService implements MessagesServ Message reply = getReply(mid, rid); if (reply != null) { Instant ts = reply.getUpdatedAt(); - if (ts.compareTo(messageEditingWindow) >= 0) { + if (ts.compareTo(messageEditingWindow) >= 0 || foreign) { return jdbcTemplate.update( "UPDATE replies SET txt=?, updated_at=? WHERE message_id=? AND reply_id=?", body, Timestamp.from(now), mid, rid) > 0; diff --git a/src/main/java/com/juick/www/api/Post.java b/src/main/java/com/juick/www/api/Post.java index 2a92178d..2dba6e07 100644 --- a/src/main/java/com/juick/www/api/Post.java +++ b/src/main/java/com/juick/www/api/Post.java @@ -225,7 +225,7 @@ public class Post { @RequestParam String body) { User author = rid == 0 ? messagesService.getMessageAuthor(mid) : messagesService.getReply(mid, rid).getUser(); if (visitor.equals(author)) { - if (messagesService.updateMessage(mid, rid, body)) { + if (messagesService.updateMessage(mid, rid, body, false)) { Message result = rid == 0 ? messagesService.getMessage(mid).orElseThrow(IllegalStateException::new) : messagesService.getReply(mid, rid); diff --git a/src/main/java/com/juick/www/api/activity/Profile.java b/src/main/java/com/juick/www/api/activity/Profile.java index 7a3fdf29..2ba9b0a0 100644 --- a/src/main/java/com/juick/www/api/activity/Profile.java +++ b/src/main/java/com/juick/www/api/activity/Profile.java @@ -132,7 +132,7 @@ public class Profile { @GetMapping(value = "/u/{userName}/blog", produces = { Context.LD_JSON_MEDIA_TYPE, Context.ACTIVITYSTREAMS_PROFILE_MEDIA_TYPE }) public OrderedCollectionPage getOutboxPage(@ModelAttribute User visitor, @PathVariable String userName, - @RequestParam(required = false, defaultValue = "0") int before) { + @RequestParam(required = false, defaultValue = "0") int before) { User user = userService.getUserByName(userName); if (!user.isAnonymous() && !user.isBanned()) { UriComponentsBuilder uri = UriComponentsBuilder.fromUriString(baseUri); @@ -265,10 +265,23 @@ public class Profile { throw new HttpNotFoundException(); } + private String formatNote(Note note) { + String markdown = remarkConverter.convertFragment((String) note.getContent()); + // combine note text with attachment urls + return note.getAttachment() == null ? markdown + : note.getAttachment().stream().map(attachment -> { + String attachmentUrl = attachment.getUrl(); + String attachmentName = attachment.getName(); + return PlainTextFormatter.markdownUrl(attachmentUrl, attachmentName); + }).reduce(markdown, + (currentUrl, nextUrl) -> String.format("%s\n%s", currentUrl, nextUrl)); + } + @CacheEvict(cacheNames = "profiles", key = "{ #visitor.uri }") @PostMapping(value = "/api/inbox", consumes = { Context.LD_JSON_MEDIA_TYPE, Context.ACTIVITYSTREAMS_PROFILE_MEDIA_TYPE }) - public ResponseEntity<CommandResult> processInbox(@ModelAttribute User visitor, InputStream inboxData) throws Exception { + public ResponseEntity<CommandResult> processInbox(@ModelAttribute User visitor, InputStream inboxData) + throws Exception { String inbox = IOUtils.toString(inboxData, StandardCharsets.UTF_8); Activity activity = jsonMapper.readValue(inbox, Activity.class); if ((StringUtils.isNotEmpty(visitor.getUri().toString()) @@ -305,17 +318,9 @@ public class Profile { String postId = profileUriBuilder.postId(inReplyTo); User user = new User(); user.setUri(URI.create(activity.getActor())); - String markdown = remarkConverter.convertFragment((String) note.getContent()); - String commandBody = note.getAttachment() == null ? markdown - : note.getAttachment().stream().map(attachment -> { - String attachmentUrl = attachment.getUrl(); - String attachmentName = attachment.getName(); - return PlainTextFormatter.markdownUrl(attachmentUrl, attachmentName); - }).reduce(markdown, - (currentUrl, nextUrl) -> String.format("%s\n%s", currentUrl, nextUrl)); - CommandResult result = commandsManager.processCommand(user, - String.format("#%s %s", postId, commandBody), URI.create(StringUtils.EMPTY)); + String.format("#%s %s", postId, formatNote(note)), + URI.create(StringUtils.EMPTY)); logger.info(jsonMapper.writeValueAsString(result)); if (result.getNewMessage().isPresent()) { messagesService.updateReplyUri(result.getNewMessage().get(), noteId); @@ -329,17 +334,9 @@ public class Profile { if (reply != null) { User user = new User(); user.setUri(URI.create(activity.getActor())); - String markdown = remarkConverter.convertFragment((String) note.getContent()); - // combine note text with attachment urls - String commandBody = note.getAttachment() == null ? markdown - : note.getAttachment().stream().map(attachment -> { - String attachmentUrl = attachment.getUrl(); - String attachmentName = attachment.getName(); - return PlainTextFormatter.markdownUrl(attachmentUrl, attachmentName); - }).reduce(markdown, (currentUrl, nextUrl) -> String.format("%s\n%s", - currentUrl, nextUrl)); CommandResult result = commandsManager.processCommand(user, - String.format("#%d/%d %s", reply.getMid(), reply.getRid(), commandBody), + String.format("#%d/%d %s", reply.getMid(), reply.getRid(), + formatNote(note)), URI.create(StringUtils.EMPTY)); logger.info(jsonMapper.writeValueAsString(result)); if (result.getNewMessage().isPresent()) { @@ -386,6 +383,22 @@ public class Profile { logger.info("{} update they profile"); return new ResponseEntity<>(CommandResult.fromString("Update accepted"), HttpStatus.ACCEPTED); } + if (activity.getObject() instanceof Note) { + Note note = (Note) activity.getObject(); + if (activity.getActor().equals(note.getAttributedTo())) { + Message reply = messagesService.getReplyByUri(activity.getObject().getUrl()); + if (messagesService.updateMessage(reply.getMid(), reply.getRid(), formatNote(note), true)) { + logger.info("{} update they message {}", activity.getActor(), note.getId()); + return new ResponseEntity<>(HttpStatus.ACCEPTED); + } + ; + logger.warn("Unable to update {}", activity.getObject().getId()); + return new ResponseEntity<>(HttpStatus.SERVICE_UNAVAILABLE); + } else { + logger.warn("Invalid Update: {}", jsonMapper.writeValueAsString(activity)); + return new ResponseEntity<>(HttpStatus.BAD_REQUEST); + } + } } logger.warn("Unknown activity: {}", jsonMapper.writeValueAsString(activity)); return new ResponseEntity<>(CommandResult.fromString("Unknown activity"), HttpStatus.NOT_IMPLEMENTED); |