@@ -71,8 +71,8 @@ public final String submit(final Model model, @RequestParam final Map<String, St
7171
7272 @ RequestMapping ("/post" )
7373 public final String showSubmissionForm (final Model model ) {
74- final String needsCaptchaResult = needsCaptcha ();
75- if (needsCaptchaResult . equalsIgnoreCase ( "true" ) ) {
74+ final boolean isCaptchaNeeded = getCurrentUser (). isCaptchaNeeded ();
75+ if (isCaptchaNeeded ) {
7676 final String iden = getNewCaptcha ();
7777 model .addAttribute ("iden" , iden );
7878 }
@@ -81,8 +81,8 @@ public final String showSubmissionForm(final Model model) {
8181
8282 @ RequestMapping ("/postSchedule" )
8383 public final String showSchedulePostForm (final Model model ) {
84- final String needsCaptchaResult = needsCaptcha ();
85- if (needsCaptchaResult . equalsIgnoreCase ( "true" ) ) {
84+ final boolean isCaptchaNeeded = getCurrentUser (). isCaptchaNeeded ();
85+ if (isCaptchaNeeded ) {
8686 model .addAttribute ("msg" , "Sorry, You do not have enought karma" );
8787 return "submissionResponse" ;
8888 }
@@ -92,7 +92,7 @@ public final String showSchedulePostForm(final Model model) {
9292 @ RequestMapping (value = "/schedule" , method = RequestMethod .POST )
9393 public final String schedule (final Model model , @ RequestParam final Map <String , String > formParams ) throws ParseException {
9494 logger .info ("User scheduling Post with these parameters: " + formParams .entrySet ());
95- final User user = userReopsitory . findByAccessToken ( redditRestTemplate . getAccessToken (). getValue () );
95+ final User user = getCurrentUser ( );
9696 final Post post = new Post ();
9797 post .setUser (user );
9898 post .setSent (false );
@@ -116,7 +116,7 @@ public final String schedule(final Model model, @RequestParam final Map<String,
116116
117117 @ RequestMapping ("/posts" )
118118 public final String getScheduledPosts (final Model model ) {
119- final User user = userReopsitory . findByAccessToken ( redditRestTemplate . getAccessToken (). getValue () );
119+ final User user = getCurrentUser ( );
120120 final List <Post > posts = postReopsitory .findByUser (user );
121121 model .addAttribute ("posts" , posts );
122122 return "postListView" ;
@@ -160,6 +160,10 @@ public String updatePost(Model model, @PathVariable("id") final Long id, @Reques
160160
161161 // === private
162162
163+ private User getCurrentUser () {
164+ return userReopsitory .findByAccessToken (redditRestTemplate .getAccessToken ().getValue ());
165+ }
166+
163167 private final MultiValueMap <String , String > constructParams (final Map <String , String > formParams ) {
164168 final MultiValueMap <String , String > param = new LinkedMultiValueMap <String , String >();
165169 param .add (RedditApiConstants .API_TYPE , "json" );
@@ -211,9 +215,15 @@ private final void addUser(final String name, final OAuth2AccessToken token) {
211215 user .setAccessToken (token .getValue ());
212216 user .setRefreshToken (token .getRefreshToken ().getValue ());
213217 user .setTokenExpiration (token .getExpiration ());
214- userReopsitory .save (user );
215218 }
216219
220+ final String needsCaptchaResult = needsCaptcha ();
221+ if (needsCaptchaResult .equalsIgnoreCase ("true" )) {
222+ user .setNeedCaptcha (true );
223+ } else {
224+ user .setNeedCaptcha (false );
225+ }
226+ userReopsitory .save (user );
217227 }
218228
219229}
0 commit comments