From 12e2206f9d24b29c8276b2743603635620643444 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Wed, 18 Jan 2023 20:39:41 +0300 Subject: RestTemplate -> OkHttpClient --- .../java/com/juick/www/api/activity/Profile.java | 222 +++++++++++---------- 1 file changed, 112 insertions(+), 110 deletions(-) (limited to 'src/main/java/com/juick/www/api/activity/Profile.java') 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 19a28a39..7b440a6a 100644 --- a/src/main/java/com/juick/www/api/activity/Profile.java +++ b/src/main/java/com/juick/www/api/activity/Profile.java @@ -279,142 +279,144 @@ public class Profile { @CacheEvict(cacheNames = "profiles", key = "{ #visitor.uri }") @PostMapping(value = "/api/inbox", consumes = { Context.LD_JSON_MEDIA_TYPE, Context.ACTIVITY_MEDIA_TYPE, Context.ACTIVITYSTREAMS_PROFILE_MEDIA_TYPE, MediaType.APPLICATION_JSON_VALUE }) - public ResponseEntity processInbox(@Parameter(hidden = true) User visitor, InputStream inboxData) + public ResponseEntity processInbox(@Parameter(hidden = true) User visitor, @RequestBody Context context) throws Exception { - String inbox = IOUtils.toString(inboxData, StandardCharsets.UTF_8); - Activity activity = jsonMapper.readValue(inbox, Activity.class); - if ((StringUtils.isNotEmpty(visitor.getUri().toString()) - && visitor.getUri().equals(URI.create(activity.getActor()))) || !visitor.isAnonymous()) { - if (activity instanceof Follow) { - Follow followRequest = (Follow) activity; - applicationEventPublisher.publishEvent(new FollowEvent(this, followRequest)); - return new ResponseEntity<>(CommandResult.fromString("Follow request accepted"), HttpStatus.ACCEPTED); - } - if (activity instanceof Undo) { - Context object = activity.getObject(); - if (object instanceof Follow) { - applicationEventPublisher.publishEvent( - new UndoFollowEvent(this, activity.getActor(), ((Activity) object).getObject().getId())); - return new ResponseEntity<>(CommandResult.fromString("Undo follow request accepted"), - HttpStatus.OK); - } else if (object instanceof Like || object instanceof Announce) { - applicationEventPublisher.publishEvent( - new UndoAnnounceEvent(this, activity.getActor(), ((Activity) object).getObject().getId())); - return new ResponseEntity<>(CommandResult.fromString("Undo like/announce request accepted"), - HttpStatus.OK); + if (context instanceof Activity activity) { + if ((StringUtils.isNotEmpty(visitor.getUri().toString()) + && visitor.getUri().equals(URI.create(activity.getActor()))) || !visitor.isAnonymous()) { + if (activity instanceof Follow) { + Follow followRequest = (Follow) activity; + applicationEventPublisher.publishEvent(new FollowEvent(this, followRequest)); + return new ResponseEntity<>(CommandResult.fromString("Follow request accepted"), HttpStatus.ACCEPTED); } - } - if (activity instanceof Create) { - if (activity.getObject() instanceof Note) { - Note note = (Note) activity.getObject(); - URI noteId = URI.create((String) note.getId()); - if (messagesService.replyExists(noteId)) { - return new ResponseEntity<>(CommandResult.fromString("Reply already exists"), HttpStatus.OK); - } else { - String inReplyTo = (String) note.getInReplyTo(); - if (StringUtils.isNotBlank(inReplyTo)) { - if (inReplyTo.startsWith(baseUri)) { - String postId = profileUriBuilder.postId(inReplyTo); - User user = new User(); - user.setUri(URI.create(activity.getActor())); - CommandResult result = commandsManager.processCommand(user, - 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); - return new ResponseEntity<>(result, HttpStatus.OK); - } else { - logger.warn("Invalid request: {}", inbox); - return new ResponseEntity<>(result, HttpStatus.BAD_REQUEST); - } - } else { - Message reply = messagesService.getReplyByUri(inReplyTo); - if (reply != null) { + if (activity instanceof Undo) { + Context object = activity.getObject(); + if (object instanceof Follow) { + applicationEventPublisher.publishEvent( + new UndoFollowEvent(this, activity.getActor(), ((Activity) object).getObject().getId())); + return new ResponseEntity<>(CommandResult.fromString("Undo follow request accepted"), + HttpStatus.OK); + } else if (object instanceof Like || object instanceof Announce) { + applicationEventPublisher.publishEvent( + new UndoAnnounceEvent(this, activity.getActor(), ((Activity) object).getObject().getId())); + return new ResponseEntity<>(CommandResult.fromString("Undo like/announce request accepted"), + HttpStatus.OK); + } + } + if (activity instanceof Create) { + if (activity.getObject() instanceof Note) { + Note note = (Note) activity.getObject(); + URI noteId = URI.create(note.getId()); + if (messagesService.replyExists(noteId)) { + return new ResponseEntity<>(CommandResult.fromString("Reply already exists"), HttpStatus.OK); + } else { + String inReplyTo = note.getInReplyTo(); + if (StringUtils.isNotBlank(inReplyTo)) { + if (inReplyTo.startsWith(baseUri)) { + String postId = profileUriBuilder.postId(inReplyTo); User user = new User(); user.setUri(URI.create(activity.getActor())); CommandResult result = commandsManager.processCommand(user, - String.format("#%d/%d %s", reply.getMid(), reply.getRid(), - formatNote(note)), + 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); return new ResponseEntity<>(result, HttpStatus.OK); + } else { + logger.warn("Invalid request: {}", context.getId()); + return new ResponseEntity<>(result, HttpStatus.BAD_REQUEST); + } + } else { + Message reply = messagesService.getReplyByUri(inReplyTo); + if (reply != null) { + User user = new User(); + user.setUri(URI.create(activity.getActor())); + CommandResult result = commandsManager.processCommand(user, + String.format("#%d/%d %s", reply.getMid(), reply.getRid(), + formatNote(note)), + URI.create(StringUtils.EMPTY)); + logger.info(jsonMapper.writeValueAsString(result)); + if (result.getNewMessage().isPresent()) { + messagesService.updateReplyUri(result.getNewMessage().get(), noteId); + return new ResponseEntity<>(result, HttpStatus.OK); + } } } + } else { + if (note.getTo().stream().anyMatch(recipient -> recipient.startsWith(baseUri))) { + logger.warn("Possible direct message from {}", note.getAttributedTo()); + applicationEventPublisher.publishEvent(new DirectMessageEvent(this, note)); + return new ResponseEntity<>(CommandResult.fromString("Message accepted"), + HttpStatus.ACCEPTED); + } } - } else { - if (note.getTo().stream().anyMatch(recipient -> recipient.startsWith(baseUri))) { - logger.warn("Possible direct message from {}", note.getAttributedTo()); - applicationEventPublisher.publishEvent(new DirectMessageEvent(this, note)); - return new ResponseEntity<>(CommandResult.fromString("Message accepted"), - HttpStatus.ACCEPTED); - } + logger.warn("Request with invalid recipient from {}", activity.getActor()); + return new ResponseEntity<>(CommandResult.fromString("Message accepted"), HttpStatus.ACCEPTED); } - logger.warn("Request with invalid recipient from {}", activity.getActor()); - return new ResponseEntity<>(CommandResult.fromString("Message accepted"), HttpStatus.ACCEPTED); } } - } - if (activity instanceof Delete) { - URI objectId = URI.create(activity.getObject().getId()); - if (messagesService.replyExists(objectId)) { - URI actor = URI.create(activity.getActor()); - messagesService.deleteReply(actor, objectId); + if (activity instanceof Delete) { + URI objectId = URI.create(activity.getObject().getId()); + if (messagesService.replyExists(objectId)) { + URI actor = URI.create(activity.getActor()); + messagesService.deleteReply(actor, objectId); + } + // accept all authenticated Delete activities + return new ResponseEntity<>(CommandResult.fromString("Delete request accepted"), HttpStatus.ACCEPTED); } - // accept all authenticated Delete activities - return new ResponseEntity<>(CommandResult.fromString("Delete request accepted"), HttpStatus.ACCEPTED); - } - if (activity instanceof Like || activity instanceof Announce) { - String messageUri = activity.getObject().getId(); - applicationEventPublisher.publishEvent(new AnnounceEvent(this, activity.getActor(), messageUri)); - return new ResponseEntity<>(CommandResult.fromString("Like/announce request accepted"), HttpStatus.OK); - } - if (activity instanceof Flag) { - URI actor = URI.create(activity.getActor()); - logger.info("{} flag some objects: {}", actor, activity.getObject()); - return new ResponseEntity<>(CommandResult.fromString("Report accepted"), HttpStatus.ACCEPTED); - } - if (activity instanceof Update) { - if (activity.getObject() instanceof Person - && activity.getActor().equals(activity.getObject().getId())) { - logger.info("{} update they profile"); - return new ResponseEntity<>(CommandResult.fromString("Update accepted"), HttpStatus.ACCEPTED); + if (activity instanceof Like || activity instanceof Announce) { + String messageUri = activity.getObject().getId(); + applicationEventPublisher.publishEvent(new AnnounceEvent(this, activity.getActor(), messageUri)); + return new ResponseEntity<>(CommandResult.fromString("Like/announce request accepted"), HttpStatus.OK); + } + if (activity instanceof Flag) { + URI actor = URI.create(activity.getActor()); + logger.info("{} flag some objects: {}", actor, activity.getObject()); + return new ResponseEntity<>(CommandResult.fromString("Report accepted"), HttpStatus.ACCEPTED); } - if (activity.getObject() instanceof Note) { - Note note = (Note) activity.getObject(); - logger.info("Got update to {}", note.getId()); - if (activity.getActor().equals(note.getAttributedTo())) { - Message reply = messagesService.getReplyByUri(note.getId()); - if (reply != null) { - if (messagesService.updateMessage(reply.getMid(), reply.getRid(), formatNote(note), true)) { - logger.info("{} update they message {}", activity.getActor(), note.getId()); + if (activity instanceof Update) { + if (activity.getObject() instanceof Person + && activity.getActor().equals(activity.getObject().getId())) { + logger.info("{} update they profile"); + return new ResponseEntity<>(CommandResult.fromString("Update accepted"), HttpStatus.ACCEPTED); + } + if (activity.getObject() instanceof Note) { + Note note = (Note) activity.getObject(); + logger.info("Got update to {}", note.getId()); + if (activity.getActor().equals(note.getAttributedTo())) { + Message reply = messagesService.getReplyByUri(note.getId()); + if (reply != null) { + 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 {}", note.getId()); + return new ResponseEntity<>(HttpStatus.SERVICE_UNAVAILABLE); + } else { + logger.warn("Update not found: {}", note.getId()); return new ResponseEntity<>(HttpStatus.ACCEPTED); } - logger.warn("Unable to update {}", note.getId()); - return new ResponseEntity<>(HttpStatus.SERVICE_UNAVAILABLE); } else { - logger.warn("Update not found: {}", note.getId()); - return new ResponseEntity<>(HttpStatus.ACCEPTED); + logger.warn("Invalid Update: {}", jsonMapper.writeValueAsString(activity)); + return new ResponseEntity<>(HttpStatus.BAD_REQUEST); } - } else { - logger.warn("Invalid Update: {}", jsonMapper.writeValueAsString(activity)); - return new ResponseEntity<>(HttpStatus.BAD_REQUEST); } } + if (activity instanceof Block) { + logger.info("{} blocks {} (room_full_of_people_who_care.jpg)", activity.getActor(), activity.getObject().getId()); + } + logger.warn("Unknown activity: {}", jsonMapper.writeValueAsString(activity)); + return new ResponseEntity<>(CommandResult.fromString("Unknown activity"), HttpStatus.NOT_IMPLEMENTED); } - if (activity instanceof Block) { - logger.info("{} blocks {} (room_full_of_people_who_care.jpg)", activity.getActor(), activity.getObject().getId()); - } - logger.warn("Unknown activity: {}", jsonMapper.writeValueAsString(activity)); - return new ResponseEntity<>(CommandResult.fromString("Unknown activity"), HttpStatus.NOT_IMPLEMENTED); - } - if (activity instanceof Delete) { - // Delete gone user - if (activity.getActor().equals(activity.getObject().getId())) { - return new ResponseEntity<>(CommandResult.fromString("Delete request accepted"), HttpStatus.ACCEPTED); + if (activity instanceof Delete) { + // Delete gone user + if (activity.getActor().equals(activity.getObject().getId())) { + return new ResponseEntity<>(CommandResult.fromString("Delete request accepted"), HttpStatus.ACCEPTED); + } } + } else { + return new ResponseEntity<>(CommandResult.fromString("Can't parse"), HttpStatus.BAD_REQUEST); } return new ResponseEntity<>(CommandResult.fromString("Can not authenticate"), HttpStatus.UNAUTHORIZED); } -- cgit v1.2.3