/** * Create dto * * @param post post for conversion * @return dto for post */ public static PostDto getDtoFor(Post post) { PostDto dto = new PostDto(); dto.setBodyText(post.getPostContent()); dto.setId(post.getId()); dto.setTopicId(post.getTopic().getId()); dto.setCreationDate(post.getCreationDate()); dto.setModificationDate(post.getModificationDate()); return dto; }
/** * {@inheritDoc} */ @Override public void filter(Collection<JCUser> users, SubscriptionAwareEntity entity) { if (entity instanceof Post && ((Post)entity).getTopic().getType().equals(QuestionsAndAnswersPlugin.TOPIC_TYPE)) { JCUser postCreator = ((Post)entity).getUserCreated(); boolean containsCreator = users.contains(postCreator); users.clear(); if (containsCreator) { users.add(((Post) entity).getUserCreated()); } } } }
public CodeReviewDto(Post post) { this.postId = post.getId(); for (PostComment comment : post.getComments()) { this.comments.add(new CodeReviewCommentDto(comment)); } }
@Override public int compare(Post o1, Post o2) { return o1.getRating() == o2.getRating() ? o2.getCreationDate().compareTo(o1.getCreationDate()) : o2.getRating() - o1.getRating(); } }
/** * Add new {@link Post} to the topic. * The method sets Posts.topic field to this Topic. * * @param post post to add */ public void addPost(Post post) { setModificationDate(post.getCreationDate()); post.setTopic(this); this.posts.add(post); }
/** * Creates feed item with information about the post * * @param post post to add to the feed * @param componentUrl base url of the forum component * @return item for the RSS feed */ private Item createFeedItem(Post post, String componentUrl) { Item item = new Item(); Content content = new Content(); content.setType(Content.TEXT); content.setValue(post.getPostContent()); item.setContent(content); item.setTitle(post.getTopic().getTitle()); item.setAuthor(post.getUserCreated().getUsername()); item.setLink(componentUrl + "/posts/" + post.getId()); item.setPubDate(post.getCreationDate().toDate()); return item; }
/** * Delete post by given id * * @param postId post * @return redirect to post next to deleted one. Redirects to previous post in case if it's last post in topic. * @throws NotFoundException when post was not found */ @RequestMapping(method = RequestMethod.DELETE, value = "/posts/{postId}") public ModelAndView delete(@PathVariable(POST_ID) final Long postId) throws NotFoundException { final Post post = this.postService.get(postId); Post nextPost = post.getTopic().getNeighborPost(post); retryTemplate.execute(new RetryCallback<Object, NotFoundException>() { @Override public Object doWithRetry(RetryContext context) throws NotFoundException { Post post = postService.get(postId); postService.deletePost(post); return null; } }); return new ModelAndView("redirect:/posts/" + nextPost.getId()); }
/** * {@inheritDoc} */ @Override public Set<JCUser> getSubscribers() { return getTopic().getSubscribers(); }
/** * Performs update with security checking. * * @param post an instance of post, that will be updated * @param postContent new content of the post * @throws AccessDeniedException if user tries to update the first post of code review which should be impossible, * see <a href="http://jtalks.org/display/jcommune/1.1+Larks">Requirements</a> * for details */ @PreAuthorize("(hasPermission(#post.id, 'POST', 'GeneralPermission.WRITE') and " + "hasPermission(#post.topic.branch.id, 'BRANCH', 'BranchPermission.EDIT_OWN_POSTS')) or " + "(not hasPermission(#post.id, 'POST', 'GeneralPermission.WRITE') and " + "hasPermission(#post.topic.branch.id, 'BRANCH', 'BranchPermission.EDIT_OTHERS_POSTS'))") @Override public void updatePost(Post post, String postContent) { Topic postTopic = post.getTopic(); if (postTopic.isCodeReview() && postTopic.getPosts().get(0).getId() == post.getId()) { throw new AccessDeniedException("It is impossible to edit code review!"); } post.setPostContent(postContent); post.updateModificationDate(); this.getDao().saveOrUpdate(post); userService.notifyAndMarkNewlyMentionedUsers(post); logger.debug("Post id={} updated.", post.getId()); }
/** * {@inheritDoc} */ @Override @PreAuthorize("(hasPermission(#post.topic.branch.id, 'BRANCH', 'BranchPermission.DELETE_OWN_POSTS') and " + "#post.userCreated.username == principal.username) or " + "(hasPermission(#post.topic.branch.id, 'BRANCH', 'BranchPermission.DELETE_OTHERS_POSTS') and " + "#post.userCreated.username != principal.username)") public void deletePost(Post post) { JCUser postCreator = post.getUserCreated(); postCreator.setPostCount(postCreator.getPostCount() - 1); Topic topic = post.getTopic(); topic.removePost(post); Branch branch = topic.getBranch(); boolean deletedPostIsLastPostInBranch = branch.isLastPost(post); if (deletedPostIsLastPostInBranch) { branch.clearLastPost(); } // todo: event API? topicDao.saveOrUpdate(topic); securityService.deleteFromAcl(post); /* only the creator of the post should be notified when it's removed. */ notificationService.subscribedEntityChanged(post); if (deletedPostIsLastPostInBranch) { branchLastPostService.refreshLastPostInBranch(branch); } logger.debug("Deleted post id={}", post.getId()); }
/** * Create news item * * @param topic news topic * @param url building URL * @return item for news feed */ private Item createFeedItem(Topic topic, String url) { Item item = new Item(); Description description = new Description(); description.setType("text"); String postContent = topic.getLastPost().getPostContent(); postContent = RssUtils.skipInValidXMLChars(postContent); description.setValue(postContent); Content content = new Content(); item.setContent(content); item.setTitle(topic.getTitle()); item.setAuthor(topic.getLastPost().getUserCreated().getUsername()); item.setLink(url + "/posts/" + topic.getLastPost().getId()); item.setComments(topic.getTopicStarter().getSignature()); item.setDescription(description); item.setPubDate(topic.getModificationDate().toDate()); return item; }
/** * Remove the post from the topic. * * @param postToRemove post to remove */ public void removePost(Post postToRemove) { posts.remove(postToRemove); Topic topic = postToRemove.getTopic(); if (postToRemove.getCreationDate().withMillisOfSecond(0) .equals(topic.getModificationDate().withMillisOfSecond(0))) { topic.recalculateModificationDate(); } }
/** * This method will return true if there are unread posts in that topic * for the current user. This state is NOT persisted and must be * explicitly set by calling Topic.setLastReadPostDate(). * <p/> * If setter has not been called this method will always return <code>true</code> * * @return if current topic has posts still unread by the current user */ public boolean isHasUpdates() { return (lastReadPostDate == null) || (lastReadPostDate.isBefore(getLastPost().getCreationDate())); }
/** * {@inheritDoc} */ @Override @PreAuthorize("hasPermission(#post.topic.branch.id, 'BRANCH', 'BranchPermission.CREATE_POSTS') " + "and #post.userCreated.username != principal.username") public Post vote(Post post, PostVote vote) { JCUser currentUser = userService.getCurrentUser(); if (!post.canBeVotedBy(currentUser, vote.isVotedUp())) { logger.info("User [{}] tries to vote for post with id={} in same direction more than one time", currentUser.getUsername(), post.getId()); throw new AccessDeniedException("User can't vote in same direction more than one time"); } vote.setUser(currentUser); int ratingChanges = post.calculateRatingChanges(vote); post.putVote(vote); getDao().saveOrUpdate(post); getDao().changeRating(post.getId(), ratingChanges); return post; }
/** * {@inheritDoc} */ @Override public String getUrlSuffix() { return URL_SUFFIX + getId(); }
/** * Get quote text. * If user select nothing JS will substitute whole post contents here * <p/> * Supports post method to pass large quotations. * * @param postId identifier os the post we're quoting * @param selection text selected by user for the quotation * @throws NotFoundException when topic was not found */ @RequestMapping(method = RequestMethod.POST, value = "/posts/{postId}/quote") @ResponseBody public JsonResponse getQuote(@PathVariable(POST_ID) Long postId, @RequestParam("selection") String selection) throws NotFoundException { Post source = postService.get(postId); String content = StringUtils.defaultString(selection, source.getPostContent()); return new JsonResponse(JsonResponseStatus.SUCCESS, bbCodeService.quote(content, source.getUserCreated())); }
/** * {@inheritDoc} */ @Override public PostComment addComment(Long postId, Map<String, String> attributes, String body) throws NotFoundException { Post targetPost = get(postId); JCUser currentUser = userService.getCurrentUser(); assertCommentAllowed(targetPost.getTopic()); PostComment comment = new PostComment(); comment.putAllAttributes(attributes); comment.setBody(body); comment.setCreationDate(new DateTime(System.currentTimeMillis())); comment.setAuthor(currentUser); if (currentUser.isAutosubscribe()) { targetPost.getTopic().getSubscribers().add(currentUser); } targetPost.addComment(comment); getDao().saveOrUpdate(targetPost); /** * Notify subscribers of topic if comment added */ notificationService.subscribedEntityChanged(targetPost.getTopic()); return comment; }
/** * Change BB user tag to mark user as already notified * * @param username this user was mentioned * @param postDao service for working with Post objects */ private void markUserAsAlreadyNotified(String username, PostDao postDao) { String initialUserMentioning = format(MENTIONED_NOT_NOTIFIED_USER_TEMPLATE, username); String notifiedUserMentioning = format(MENTIONED_AND_NOTIFIED_USER_TEMPLATE, username); String newPostContent = post.getPostContent().replace(initialUserMentioning, notifiedUserMentioning); post.setPostContent(newPostContent); postDao.saveOrUpdate(post); }
/** * {@inheritDoc} */ @Override @PreAuthorize("hasPermission(#branchId, 'BRANCH', 'BranchPermission.CREATE_POSTS')") public Post replyToTopic(long topicId, String answerBody, long branchId) throws NotFoundException { Topic topic = topicFetchService.getTopicSilently(topicId); this.assertPostingIsAllowed(topic); JCUser currentUser = userService.getCurrentUser(); currentUser.setPostCount(currentUser.getPostCount() + 1); Post answer = new Post(currentUser, answerBody); topic.addPost(answer); topic.removeDraftOfUser(currentUser); if (currentUser.isAutosubscribe()){ subscriptionService.subscribe(topic); } postDao.saveOrUpdate(answer); Branch branch = topic.getBranch(); branch.setLastPost(answer); branchDao.saveOrUpdate(branch); dao.saveOrUpdate(topic); securityService.createAclBuilder().grant(GeneralPermission.WRITE).to(currentUser).on(answer).flush(); notificationService.subscribedEntityChanged(topic); userService.notifyAndMarkNewlyMentionedUsers(answer); logger.debug("New post in topic. Topic id={}, Post id={}, Post author={}", new Object[]{topicId, answer.getId(), currentUser.getUsername()}); return answer; }
public int getUserPostCount(JCUser user) { int count = 0; for (Post post : getPosts()) { if (post.getUserCreated().equals(user)) { count ++; } } return count; }