/** * {@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 int getUserPostCount(JCUser user) { int count = 0; for (Post post : getPosts()) { if (post.getUserCreated().equals(user)) { count ++; } } return count; }
/** * Checks if this topic contains only posts added by topic Owner. * * @return <code>true</code> if condition is followed, otherwise <code>false</code> */ public boolean isContainsOwnerPostsOnly() { for (Post post : getPosts()) { if (!post.getUserCreated().equals(topicStarter)) { return false; } } return true; }
/** * {@inheritDoc} */ @Override public Collection<JCUser> getAllowedSubscribers(SubscriptionAwareEntity entity){ if (entity instanceof Topic) { return this.topicDao.getAllowedSubscribers(entity); } else if (entity instanceof Post) { Post post = (Post) entity; Collection<JCUser> subscribers = topicDao.getAllowedSubscribers(post.getTopic()); /* Can't return Collections.emptyList() or Arrays.asList(...) may be problems with removing elements */ if (subscribers.contains(post.getUserCreated())) { List<JCUser> arrayList = new ArrayList<>(1); arrayList.add(post.getUserCreated()); return arrayList; } else { return new ArrayList<>(); } } else{ return this.branchDao.getAllowedSubscribers(entity); } }
/** * 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 @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()); }
/** * Performs actual topic deletion. Deletes all topic related data and * recalculates user's post count. * * @param topic topic to delete * @return branch without deleted topic */ private Branch deleteTopicSilent(Topic topic) { List<Post> topicPosts = topic.getPosts(); for (Post post : topicPosts) { JCUser user = post.getUserCreated(); user.setPostCount(user.getPostCount() - 1); } Branch branch = topic.getBranch(); Post lastPostInBranch = branch.getLastPost(); boolean branchLastPostFromDeletedTopic = topicPosts.contains(lastPostInBranch); if (branchLastPostFromDeletedTopic) { branch.clearLastPost(); } branch.deleteTopic(topic); branchDao.saveOrUpdate(branch); if (branchLastPostFromDeletedTopic) { branchLastPostService.refreshLastPostInBranch(branch); } securityService.deleteFromAcl(Topic.class, topic.getId()); return branch; }
/** * 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; }
/** * 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; }