Exemple #1
0
 @EndpointImplementation(Endpoints.APPROVE)
 public void approve(Submission s) throws NetworkException, ApiException {
   genericPost(
       reddit
           .request()
           .endpoint(Endpoints.APPROVE)
           .post(JrawUtils.mapOf("api_type", "json", "id", s.getFullName()))
           .build());
 }
Exemple #2
0
 /**
  * Set or unset a self post as a sticky. You must be a moderator of the subreddit the submission
  * was posted in for this request to complete successfully.
  *
  * @param s The submission to set as a sticky. Must be a self post
  * @param sticky Whether or not to set the submission as a stickied post
  * @throws NetworkException If the request was not successful
  * @throws ApiException If the Reddit API returned an error
  */
 @EndpointImplementation(Endpoints.SET_SUBREDDIT_STICKY)
 public void setSticky(Submission s, boolean sticky) throws NetworkException, ApiException {
   genericPost(
       reddit
           .request()
           .endpoint(Endpoints.SET_SUBREDDIT_STICKY)
           .post(JrawUtils.mapOf("api_type", "json", "id", s.getFullName(), "state", sticky))
           .build());
 }
Exemple #3
0
 @EndpointImplementation(Endpoints.REMOVE)
 public void remove(Submission s, boolean spam) throws NetworkException, ApiException {
   genericPost(
       reddit
           .request()
           .endpoint(Endpoints.REMOVE)
           .post(JrawUtils.mapOf("api_type", "json", "id", s.getFullName(), "spam", spam))
           .build());
 }
Exemple #4
0
 /**
  * Sets whether or not this submission should be marked as not safe for work
  *
  * @param s The submission to modify
  * @param nsfw Whether or not this submission is not safe for work
  * @throws net.dean.jraw.http.NetworkException If the request was not successful
  * @throws net.dean.jraw.ApiException If the API returned an error
  */
 @EndpointImplementation({Endpoints.MARKNSFW, Endpoints.UNMARKNSFW})
 public void setNsfw(Submission s, boolean nsfw) throws NetworkException, ApiException {
   // "/api/marknsfw" if nsfw == true, "/api/unmarknsfw" if nsfw == false
   genericPost(
       reddit
           .request()
           .endpoint(nsfw ? Endpoints.MARKNSFW : Endpoints.UNMARKNSFW)
           .post(JrawUtils.mapOf("id", s.getFullName()))
           .build());
 }
Exemple #5
0
  /**
   * Sets either a user's flair or a submission's flair. If the submission and username are both
   * non-null, then the submission will be used in the request. If they are both null and there is
   * no authenticated user, then an IllegalArgumentException will be thrown.
   *
   * @param subreddit The subreddit where the flair will take effect
   * @param template The template to use
   * @param text Optional text that will be used if the FlairTemplate's text is editable. If this is
   *     null and the template is editable, the template's default text will be used.
   * @param submission The submission to set the flair for
   * @param username The name of the user to set the flair for. If this is null the authenticated
   *     user's name will be used.
   * @throws IllegalArgumentException If both the submission and the username are null
   * @throws NetworkException If the request was not successful
   */
  @EndpointImplementation(Endpoints.SELECTFLAIR)
  private void setFlair(
      String subreddit, FlairTemplate template, String text, Submission submission, String username)
      throws IllegalArgumentException, NetworkException, ApiException {
    if (subreddit == null) {
      throw new IllegalArgumentException("subreddit cannot be null");
    }
    Map<String, String> args =
        JrawUtils.mapOf("api_type", "json", "flair_template_id", template.getId());

    if (submission != null) {
      args.put("link", submission.getFullName());
    } else {
      if (username == null) {
        if (reddit.getAuthenticationMethod() == AuthenticationMethod.NOT_YET) {
          throw new IllegalArgumentException(
              "Not logged in and both submission and username were null");
        }
        if (!reddit.hasActiveUserContext())
          throw new IllegalStateException(
              "Cannot set the flair for self because there is no active user context");
        username = reddit.getAuthenticatedUser();
      }
      args.put("name", username);
    }

    if (template.isTextEditable()) {
      if (text == null) {
        // Set default text flair if none is provided
        text = template.getText();
      }
      args.put("text", text);
    }

    RestResponse response =
        reddit.execute(
            reddit
                .request()
                .post(args)
                .path("/r/" + subreddit + Endpoints.SELECTFLAIR.getEndpoint().getUri())
                .build());
    if (response.hasErrors()) {
      throw response.getError();
    }
  }
Exemple #6
0
  public static boolean doesMatch(Submission s, String baseSubreddit, boolean ignore18) {
    String title = s.getTitle();
    String body = s.getSelftext();
    String domain = s.getUrl();
    String subreddit = s.getSubredditName();
    String flair = s.getSubmissionFlair().getText() != null ? s.getSubmissionFlair().getText() : "";

    boolean titlec;
    boolean bodyc;
    boolean domainc;
    boolean subredditc;
    boolean userc;

    if (titles == null) {
      titles = SettingValues.titleFilters.replaceAll("^[,\\s]+", "").split("[,\\s]+");
    }
    if (texts == null) {
      texts = SettingValues.textFilters.replaceAll("^[,\\s]+", "").split("[,\\s]+");
    }
    if (domains == null) {
      domains = SettingValues.domainFilters.replaceAll("^[,\\s]+", "").split("[,\\s]+");
    }
    if (subreddits == null) {
      subreddits = SettingValues.subredditFilters.replaceAll("^[,\\s]+", "").split("[,\\s]+");
    }
    if (flairs == null) {
      flairs = SettingValues.flairFilters.replaceAll("^[,]+", "").split("[,]+");
    }
    if (users == null) {
      users = SettingValues.userFilters.replaceAll("^[,\\s]+", "").split("[,\\s]+");
    }

    titlec = !SettingValues.titleFilters.isEmpty() && contains(title.toLowerCase(), titles, false);

    bodyc = !SettingValues.textFilters.isEmpty() && contains(body.toLowerCase(), texts, false);

    userc =
        !SettingValues.userFilters.isEmpty() && contains(s.getAuthor().toLowerCase(), users, false);

    try {
      domainc = !SettingValues.domainFilters.isEmpty() && isDomain(domain.toLowerCase(), domains);
    } catch (MalformedURLException e) {
      domainc = false;
    }

    subredditc =
        !subreddit.equalsIgnoreCase(baseSubreddit)
            && !SettingValues.subredditFilters.isEmpty()
            && contains(subreddit.toLowerCase(), subreddits, true);

    boolean contentMatch = false;

    if (baseSubreddit == null || baseSubreddit.isEmpty()) {
      baseSubreddit = "frontpage";
    }

    baseSubreddit = baseSubreddit.toLowerCase();
    boolean gifs = isGif(baseSubreddit);
    boolean images = isImage(baseSubreddit);
    boolean nsfw = isNsfw(baseSubreddit);
    boolean albums = isAlbums(baseSubreddit);
    boolean urls = isUrls(baseSubreddit);
    boolean selftext = isSelftext(baseSubreddit);
    boolean videos = isVideo(baseSubreddit);

    if (s.isNsfw()) {
      if (nsfw) {
        contentMatch = true;
      }
      if (!Reddit.over18 && !ignore18) {
        contentMatch = true;
      }
    }
    switch (ContentType.getContentType(s)) {
      case REDDIT:
      case EMBEDDED:
      case LINK:
        if (urls) {
          contentMatch = true;
        }
        break;
      case SELF:
      case NONE:
        if (selftext) {
          contentMatch = true;
        }
        break;
      case ALBUM:
        if (albums) {
          contentMatch = true;
        }
        break;
      case IMAGE:
      case DEVIANTART:
      case IMGUR:
      case XKCD:
        if (images) {
          contentMatch = true;
        }
        break;
      case GIF:
        if (gifs) {
          contentMatch = true;
        }
        break;
      case VID_ME:
      case STREAMABLE:
      case VIDEO:
        if (videos) {
          contentMatch = true;
        }
        break;
    }

    if (!flair.isEmpty())
      for (String flairText : flairs) {
        if (flairText.toLowerCase().startsWith(baseSubreddit)) {
          String[] split = flairText.split(":");
          if (split[0].equalsIgnoreCase(baseSubreddit)) {
            if (flair.equalsIgnoreCase(split[1].trim())) {
              contentMatch = true;
              break;
            }
          }
        }
      }

    return (titlec
        || bodyc
        || userc
        || domainc
        || subredditc
        || contentMatch
        || Hidden.id.contains(s.getFullName()));
  }