/** * Create and persist one single message. * * @param status message status. * @return saved pm. */ public static PrivateMessage createAndSaveMessage(PrivateMessageStatus status, JCUser userTo, JCUser userFrom) { PrivateMessage pm = new PrivateMessage(userTo, userFrom, "Message title", "Private message body"); pm.setStatus(status); persist(pm); return pm; }
/** * Create the full private message dto from {@link PrivateMessage} * * @param pm private message for conversion * @return dto for full private message */ public static PrivateMessageDto getFullPmDtoFor(PrivateMessage pm) { PrivateMessageDto dto = new PrivateMessageDto(); dto.setBody(pm.getBody()); dto.setTitle(pm.getTitle()); if (pm.getUserTo() != null) { dto.setRecipient(pm.getUserTo().getUsername()); } dto.setId(pm.getId()); return dto; }
/** * {@inheritDoc} */ @Override @PreAuthorize("hasPermission(#userFrom.id, 'USER', 'ProfilePermission.SEND_PRIVATE_MESSAGES')") public PrivateMessage sendDraft(long id, String title, String body, JCUser recipient, JCUser userFrom) throws NotFoundException { PrivateMessage pm = new PrivateMessage(recipient, userFrom, title, body); pm.setId(id); pm.setRead(false); pm.setStatus(PrivateMessageStatus.SENT); this.getDao().saveOrUpdate(pm); userDataCache.incrementNewMessageCountFor(recipient.getUsername()); securityService.deleteFromAcl(pm); securityService.createAclBuilder().grant(GeneralPermission.READ).to(recipient).on(pm).flush(); securityService.createAclBuilder().grant(GeneralPermission.READ).to(userFrom).on(pm).flush(); if (isSendNotificationMessage(recipient)) { mailService.sendReceivedPrivateMessageNotification(recipient, pm); } logger.debug("Private message(was draft) to user {} was sent. Message id={}", recipient.getUsername(), pm.getId()); return pm; }
/** * {@inheritDoc} */ @Override @PreAuthorize("hasPermission(#userFrom.id, 'USER', 'ProfilePermission.SEND_PRIVATE_MESSAGES')") public void saveDraft(long id, JCUser userTo, String title, String body, JCUser userFrom) { PrivateMessage pm; if (this.getDao().isExist(id)) { pm = this.getDao().get(id); pm.setUserTo(userTo); pm.setTitle(title); pm.setBody(body); } else { pm = new PrivateMessage(userTo, userFrom, title, body); pm.setStatus(PrivateMessageStatus.DRAFT); } this.getDao().saveOrUpdate(pm); JCUser user = userService.getCurrentUser(); securityService.createAclBuilder().grant(GeneralPermission.READ).to(user).on(pm).flush(); securityService.createAclBuilder().grant(GeneralPermission.WRITE).to(user).on(pm).flush(); logger.debug("Updated private message draft. Message id={}", pm.getId()); }
switch (message.getStatus()) { case DRAFT: this.getDao().delete(message); break; case SENT: if (currentUser.equals(message.getUserFrom())) { message.setStatus(PrivateMessageStatus.DELETED_FROM_OUTBOX); result = "outbox"; } else { message.setStatus(PrivateMessageStatus.DELETED_FROM_INBOX); result = "inbox";
private boolean hasCurrentUserAccessToPM(PrivateMessage privateMessage) throws NotFoundException { JCUser currentUser = userService.getCurrentUser(); PrivateMessageStatus messageStatus = privateMessage.getStatus(); if (currentUser.equals(privateMessage.getUserFrom()) && (messageStatus.equals(PrivateMessageStatus.DELETED_FROM_OUTBOX))) { return false; } return !(currentUserIsAuthor(currentUser, privateMessage) && (messageStatus.equals(PrivateMessageStatus.DELETED_FROM_INBOX))); }
/** * Render the page with the form for the reply with quoting to original message. * The form has the next filled fields: recipient, title, message * * @param id {@link PrivateMessage} id * @return {@code ModelAndView} with the message having filled recipient, title, message fields * @throws NotFoundException when message not found */ @RequestMapping(value = "/quote/{pmId}", method = RequestMethod.GET) public ModelAndView quotePage(@PathVariable(PM_ID) Long id) throws NotFoundException { PrivateMessage pm = pmService.get(id); PrivateMessageDto dto = PrivateMessageDto.getReplyDtoFor(pm); dto.setBody(bbCodeService.quote(pm.getBody(), pm.getUserFrom())); return new ModelAndView(PM_FORM).addObject(DTO, dto); }
/** * Checks if the private message should be marked as read. * The follwing conditions are checked: * <p>1. Current user is the recepient * <p>2. Message is not read already * <p>3. Message is not a draft * * @param pm private messag to be tested * @return if message should be marked as read */ private boolean ifMessageShouldBeMarkedAsRead(PrivateMessage pm) { return currentUserIsAuthor(userService.getCurrentUser(), pm) && !pm.isRead() && !pm.getStatus().equals(PrivateMessageStatus.DRAFT); }
/** * Create the reply private message dto from {@link PrivateMessage} * @param pm private message for conversion in to reply * @return dto for reply */ public static PrivateMessageDto getReplyDtoFor(PrivateMessage pm) { PrivateMessageDto dto = new PrivateMessageDto(); dto.setRecipient(pm.getUserFrom().getUsername()); dto.setTitle(pm.prepareTitleForReply()); return dto; } }
/** * {@inheritDoc} */ @Override @PreAuthorize("hasPermission(#id, 'PRIVATE_MESSAGE', 'GeneralPermission.READ')") public PrivateMessage get(Long id) throws NotFoundException { PrivateMessage pm = super.get(id); if (!hasCurrentUserAccessToPM(pm)) { throw new NotFoundException(String.format("current user has no right to read pm %s with id %d", userService.getCurrentUser(), id)); } if (this.ifMessageShouldBeMarkedAsRead(pm)) { pm.setRead(true); this.getDao().saveOrUpdate(pm); userDataCache.decrementNewMessageCountFor(pm.getUserTo().getUsername()); } return pm; }
public static PrivateMessage getPrivateMessage(JCUser userTo, JCUser userFrom) { return new PrivateMessage(userTo, userFrom, "Message title", "Private message body"); }
private boolean currentUserIsAuthor(JCUser currentUser, PrivateMessage privateMessage) { boolean isAuthor = false; try { //because a recipient can be deleted. isAuthor = currentUser.equals(privateMessage.getUserTo()); } catch (org.hibernate.ObjectNotFoundException e) { logger.warn("The recipient doesn't exist", e); } return isAuthor; }
/** * Edit private message page. * * @param id {@link PrivateMessage} id * @return private message form view and populated form dto * @throws NotFoundException when message not found */ @RequestMapping(value = "/pm/drafts/{pmId}/edit", method = RequestMethod.GET) public ModelAndView editDraftPage(@PathVariable(PM_ID) Long id) throws NotFoundException { PrivateMessage pm = pmService.get(id); if (!pm.getStatus().equals(PrivateMessageStatus.DRAFT)) { // todo: 404? we need something more meaninful here throw new NotFoundException("Edit allowed only for draft messages."); } return new ModelAndView(PM_FORM).addObject(DTO, PrivateMessageDto.getFullPmDtoFor(pm)); }
/** * Sends notification to user about received private message. * * @param recipient a person to be notified about received private message by email * @param pm private message itself */ public void sendReceivedPrivateMessageNotification(JCUser recipient, PrivateMessage pm) { try { String urlSuffix = "/pm/inbox/" + pm.getId(); String url = this.getDeploymentRootUrl() + urlSuffix; Locale locale = recipient.getLanguage().getLocale(); Map<String, Object> model = new HashMap<>(); model.put("recipient", recipient); model.put(LINK, url); model.put(LINK_LABEL, getDeploymentRootUrlWithoutPort() + urlSuffix); model.put(RECIPIENT_LOCALE, locale); this.sendEmail(recipient.getEmail(), messageSource.getMessage("receivedPrivateMessageNotification.subject", new Object[]{}, locale), model, "receivedPrivateMessageNotification.vm"); } catch (MailingFailedException e) { LOGGER.error(String.format(LOG_TEMPLATE, "Private message", pm.getId(), recipient.getUsername())); } }
/** * {@inheritDoc} */ @Override @PreAuthorize("hasPermission(#userFrom.id, 'USER', 'ProfilePermission.SEND_PRIVATE_MESSAGES')") public PrivateMessage sendMessage(String title, String body, JCUser recipient, JCUser userFrom) { PrivateMessage pm = new PrivateMessage(recipient, userFrom, title, body); pm.setRead(false); pm.setStatus(PrivateMessageStatus.SENT); this.getDao().saveOrUpdate(pm); userDataCache.incrementNewMessageCountFor(recipient.getUsername()); securityService.createAclBuilder().grant(GeneralPermission.READ).to(recipient).on(pm).flush(); securityService.createAclBuilder().grant(GeneralPermission.READ).to(userFrom).on(pm).flush(); if (isSendNotificationMessage(recipient)) { mailService.sendReceivedPrivateMessageNotification(recipient, pm); } logger.debug("Private message to user {} was sent. Message id={}", recipient.getUsername(), pm.getId()); return pm; }
/** * Create the PrivateMessage with filled required fields. * * @return ready to save instance */ public static PrivateMessage getDefaultPrivateMessage() { JCUser userTo = persist(ObjectsFactory.getUser("UserTo", "mail2@mail.com")); JCUser userFrom = persist(ObjectsFactory.getUser("UserFrom", "mail1@mail.com")); return new PrivateMessage(userTo, userFrom, "Message title", "Private message body"); }
List<PrivateMessage> messages = new ArrayList<>(size); for (int i = 0; i < size; i++) { PrivateMessage pm = new PrivateMessage(userTo, userFrom, "Message title", "Private message body"); if (i % 2 == 0) { pm.setStatus(PrivateMessageStatus.SENT); } else { pm.setStatus(PrivateMessageStatus.NEW); PrivateMessage pm = new PrivateMessage(userTo, userFrom, "Message title", "Private message body"); if (i % 2 == 0) { pm.setStatus(PrivateMessageStatus.DELETED_FROM_OUTBOX); } else { pm.setStatus(PrivateMessageStatus.DELETED_FROM_INBOX); PrivateMessage pm = new PrivateMessage(userTo, userFrom, "Message title", "Private message body"); pm.setStatus(PrivateMessageStatus.DRAFT); messages.add(pm); persist(pm);