Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package com.github.jreddit.message;
  
  
 import java.util.List;


Messaging functionality

Author(s):
Karan Goel
Raul Rene Lepsa
 
 public class Messages {
 
     public static final int ALL_MESSAGES = -1;

    
Get the list of messages of a certain type for a user

Parameters:
user Reddit user for which to check the inbox
maxMessages number of messages to fetch. If it is set to ALL_MESSAGES, it will bring all messages
messageType MessageType instance, that determines the type of the message
Returns:
list of messages based on passed method
 
     @SuppressWarnings("unchecked")
     public List<MessagegetMessages(User userint maxMessagesMessageType messageType) {
 
         List<Messagemessages = null;
 
         try {
             JSONObject object = (JSONObject) Utils.get("/message/" + messageType.getValue() + ".json"user.getCookie());
             JSONObject data = (JSONObjectobject.get("data");
             messages = buildList((JSONArraydata.get("children"), maxMessages);
 
         } catch (Exception e) {
             ..println("Error retrieving messages of type " + messageType);
         }
         return messages;
     }

    
Compose and send a message. Requires an authenticated user and a Captcha challenge. A new captcha can be generated using the newCaptcha() method of the Captcha class.

Parameters:
user Reddit user for which to check the inbox
to recipient of the message (has to be an existing user)
subject Subject of the message (no longer than 100 characters)
text body of the message
iden identifier of the Captcha challenge
captchaTry The user's response to the Captcha challenge
Returns:
true if the message was sent successfully, false otherwise
 
     public boolean compose(User userString toString subjectString textString idenString captchaTry) {
 
         if (subject.length() > 100) {
             ..println("Subject cannot have more than 100 characters");
             return false;
         }
 
         try {
             JSONObject object = Utils.post("captcha=" + captchaTry + "&iden=" + iden +
                     "&subject=" + subject + "&text=" + text + "&to=" + to +
                     "&uh=" + user.getModhash(),
                     "/api/compose"user.getCookie());
 
             if (object.toJSONString().contains(".error.USER_REQUIRED")) {
                 ..println("Please login first.");
             } else if (object.toJSONString().contains(".error.RATELIMIT.field-ratelimit")) {
                 ..println("You are doing that too much.");
             } else if (object.toJSONString().contains(".error.BAD_CAPTCHA.field-captcha")) {
                 ..println("Invalid captcha submitted.");
             } else {
                 ..println(((JSONArray) ((JSONArray) ((JSONArrayobject.get("jquery")).get(14)).get(3)).get(0));   // prints a message confirming delivery
                 return true;
             }
 
         } catch (Exception e) {
             ..println("Error sending message to " + to);
         }
 
         return false;
     }

    
Mark a message as read

Parameters:
fullName Full name of the Message to mark as read. The full name is a combination of the TypePrefix and ID of the message
user Reddit user that reads the message
 
     public void readMessage(String fullNameUser user) {
         try {
             Utils.post("id=" + fullName + "&uh=" + user.getModhash(), "/api/read_message"user.getCookie());
        } catch (Exception e) {
            ..println("Error reading message: " + fullName);
        }
    }

    
Mark a message as unread

Parameters:
fullName Full name of the Message to mark as unread. The full name is a combination of the TypePrefix and ID of the message
user Reddit user that marks the message as unread
    public void unreadMessage(String fullNameUser user) {
        try {
            Utils.post("id=" + fullName + "&uh=" + user.getModhash(), "/api/read_message"user.getCookie());
        } catch (Exception e) {
            ..println("Error marking message: " + fullName + " as unread");
        }
    }

    
Builds a list of Messages from the passed array of children.
    private List<MessagebuildList(JSONArray childrenint maxMessages) {
        List<Messagemessages = new ArrayList<Message>();
        JSONObject obj;
        if (maxMessages < 0 || maxMessages > children.size()) {
            maxMessages = children.size();
        }
        for (int i = 0; i < maxMessagesi++) {
            obj = (JSONObjectchildren.get(i);
            // If the kind of the object is a MESSAGE
            if (obj.get("kind").toString().startsWith(..getValue())) {
                obj = (JSONObjectobj.get("data");
                messages.add(MessageMapper.mapMessage(obj));
                // Else it is a comment
            } else {
                obj = (JSONObjectobj.get("data");
                Comment m = new Comment();
                m.setBody(obj.get("body").toString());
                m.setLink_title(obj.get("link_title").toString());
                m.setComment(Boolean.valueOf(obj.get("was_comment").toString()));
                m.setFullName(obj.get("name").toString());
                m.setAuthor(obj.get("author").toString());
                m.setCreated(obj.get("created").toString());
                m.setRecipient(obj.get("dest").toString());
                m.setAuthor(obj.get("author").toString());
                m.setCreatedUTC(obj.get("created_utc").toString());
                m.setBodyHtml(obj.get("body_html").toString());
                m.setSubject(obj.get("subject").toString());
                m.setSubreddit(obj.get("subreddit").toString());
                m.setContext(obj.get("context").toString());
                m.setId(obj.get("id").toString());
                m.setSubject(obj.get("subject").toString());
                messages.add(m);
            }
        }
        return messages;
    }
New to GrepCode? Check out our FAQ X