@ModelAttribute(ApplicationConstants.SESSION_SIGNIN_USER) User signInUser,
Model model, HttpServletRequest request, HttpSession session){
if(!ajaxUtil.isAjaxRequest(request)){
throw new ResourceNotFoundException();
}
User target = userRepository.findOne(targetId);
if(target==null){
throw new RuntimeException("Invalid user id : " + targetId);
}
FollowShip fs = followShipRepository.getByTargetAndFollowed(targetId, signInUser.getId());
boolean followed = false;
if(fs!=null && fs.getStatus() == ApplicationConstants.FOLLOWSHIP_DISABLED){
followed = true;
fs.setStatus(ApplicationConstants.FOLLOWSHIP_NORMAL);
fs.setUpdatedAt(new Date());
userRepository.inc(signInUser.getId(), "followCount", 1);
userRepository.inc(target.getId(), "fansCount", 1);
}else if(fs==null){
followed = true;
fs = new FollowShip();
fs.setCreatedAt(new Date());
fs.setUpdatedAt(fs.getCreatedAt());
fs.setTarget(target);
fs.setFollowed(signInUser);
fs.setStatus(ApplicationConstants.FOLLOWSHIP_NORMAL);
userRepository.inc(signInUser.getId(), "followCount", 1);
userRepository.inc(target.getId(), "fansCount", 1);
}
if(followed){
followShipRepository.save(fs);
// save activity
Activity activity = new Activity();
activity.setOwner(signInUser.getId());
activity.setCreatedAt(new Date());
activity.setTargetUser(target.getId());
activity.setType(ActivityType.FOLLOW);
activity.setBy(sessionUtil.getBy(session));
activityRepository.save(activity);
}
return new AjaxResult(AjaxResultCode.SUCCESS);