Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package com.github.jreddit.action;
  
  
User actions.

Author(s):
Omer Elnour
Karan Goel
Raul Rene Lepsa
Benjamin Jakobus
Evin Ugur
Andrei Sfat
Simon Kassing
Dinc Ciftci
 
 public class ProfileActions implements ActorDriven {
 
     private RestClient restClient;
     private User user;

    
Constructor. Global default user (null) is used.

Parameters:
restClient REST Client instance
 
     public ProfileActions(RestClient restClient) {
         this. = restClient;
         this. = null;
     }
    
    
Constructor.

Parameters:
restClient REST Client instance
actor User instance
 
     public ProfileActions(RestClient restClientUser actor) {
     	this. = restClient;
         this. = actor;
     }
    
    
Switch the current user for the new user who will be used when invoking retrieval requests.

Parameters:
new_actor New user
 
     public void switchActor(User new_actor) {
     	this. = new_actor;
     }
    
    
This function changes user's password Requires authentication.

Parameters:
currentPassword Current password
newPassword New password
Throws:
com.github.jreddit.exception.ActionFailedException If the action failed
 
     public boolean changePassword(String currentPasswordString newPasswordthrows ActionFailedException {
     	
     	// Make the request
         JSONObject object = (JSONObjectupdate(currentPassword""newPassword).getResponseObject();
         
         // User required
         if (object.toJSONString().contains(".error.USER_REQUIRED")) {
             ..println("Change password failed: please login first.");
             return false;
         } // Rate limit exceeded
         else if (object.toJSONString().contains(".error.RATELIMIT.field-ratelimit")) {
             ..println("Change password failed: you are doing that too much.");
             return false;
         } // Incorrect password
         else if (object.toJSONString().contains(".error.BAD_PASSWORD")) {
             ..println("Change password failed: current password is bad.");
             return false;
         } else {
         	return true;
         }
         
     }
    
    
This function updates user's profile. Requires authentication.

Parameters:
currentPassword Current password
email New e-mail address (can be empty)
newPassword New password
Throws:
com.github.jreddit.exception.ActionFailedException If the action failed
 
     public Response update(String currentPasswordString emailString newPasswordthrows ActionFailedException {
    	
    	// Format parameters
    	String params = 
    			"api_type=json"
    			+ "&curpass=" + currentPassword + 
    			"&dest=http://reddit.com/" + (!email.equals("") ? "&email=" + email : "") + 
    			(!newPassword.equals("") ? "&newpass=" + newPassword + "&verpass=" + newPassword : "")
                + "&uh=" + .getModhash();
    	
    	// Post request
    	
    }
    

    
Get info about the currently authenticated user. Corresponds to the API "/me.json" method

Returns:
UserInfoobject containing the user's info, or null if the retrieval fails
Throws:
com.github.jreddit.exception.ActionFailedException If the action failed
        if (.getCookie() == null || .getModhash() == null) {
            ..printf("Please invoke the connect method in order to login the user");
            return null;
        }
        JSONObject info = (JSONObjectjsonObject.get("data");
        return new UserInfo(info);
    }

    
Returns miscellaneous info about the user

Parameters:
username The username of the user whose account info you want to retrieve.
Returns:
UserInfo object consisting of information about the user identified by "username".
Throws:
com.github.jreddit.exception.ActionFailedException If the action failed
    public UserInfo about(String usernamethrows ActionFailedException {
        // Send GET request to get the account overview
      
        if(username.equals("[deleted]"))
        {
          throw new ActionFailedException("There is no information associated with the username [deleted]");          
        }
        JSONObject object = (JSONObject.get(String.format(.username), null).getResponseObject();
        JSONObject data = (JSONObjectobject.get("data");
        // Init account info wrapper
        return data != null ? new UserInfo(data) : null;
    }
    
    
Deletes the reddit account. Requires authentication.

Parameters:
confirm Confirmation by the user
message Optional message explaining the reason for the account's deletion
password Account's password
Throws:
com.github.jreddit.exception.ActionFailedException If the action failed
    public Response delete(Boolean confirmString messageString passwordthrows ActionFailedException {
    	
    	// Format parameters
    	String params = 
    			"api_type=json"
    			+ "&confirm=" + confirm + 
    			"&delete_message=" + message 
    				+ "&passwd=" + password 
    					+ "&uh=" + .getModhash() 
    						+ "&user=" + .getUsername();
    	
    	// Post request
    }
    
    
Creates a new account.

Parameters:
username A valid, unused, username
email A valid e-mail address (can be empty)
newPassword The account's password
copyPassword should be same as newPassword (for validation)
captcha_iden the identifier of the CAPTCHA challenge (not necessarily required)
captcha_sol the user's response to the CAPTCHA challenge (required if there was a CAPTCHA challenge)
Throws:
com.github.jreddit.exception.ActionFailedException If the action failed
    public Response register(String usernameString emailString newPasswordString copyPasswordString captcha_idenString captcha_solthrows ActionFailedException {
        
        // Format parameters
        String params = 
                "api_type=json"
                + "&captcha=" + captcha_sol 
                  +"&email=" + email 
                    + "&iden=" + captcha_iden
                      + "&passwd=" + newPassword
                        + "&passwd2=" + copyPassword 
                          + "&user=" + username;    
    
        // Post request
         
                
        Response result = .post(params."");//no user, no cookie
        JSONObject object = (JSONObjectresult.getResponseObject();
        
        if (object.toJSONString().contains("BAD_CAPTCHA")) {        
          ..println("Wrong captcha!");               
        } 
        else if (object.toJSONString().contains("RATELIMIT")) {
          ..println("User creation failed: you are doing that too much.");        
        } 
        else if (object.toJSONString().contains("USERNAME_TAKEN")) {    
          ..println("User creation failed: that username is already taken.");          
        } 
        else if (object.toJSONString().contains("BAD_PASSWORD_MATCH")) {
          ..println("User creation failed: passwords do not match."); 
          
        } else {        
          this. = new User(this.usernamenewPassword);
        }
        
        return result;
    }
    
New to GrepCode? Check out our FAQ X