Example #1
0
  @POST
  @Path("/delete")
  @ApiOperation(value = "Delete a category", notes = "Delete an existing feed category")
  public Response deleteCategory(@ApiParam(required = true) IDRequest req) {

    Preconditions.checkNotNull(req);
    Preconditions.checkNotNull(req.getId());

    FeedCategory cat = feedCategoryDAO.findById(getUser(), req.getId());
    if (cat != null) {
      List<FeedSubscription> subs = feedSubscriptionDAO.findByCategory(getUser(), cat);
      for (FeedSubscription sub : subs) {
        sub.setCategory(null);
      }
      feedSubscriptionDAO.saveOrUpdate(subs);
      List<FeedCategory> categories = feedCategoryDAO.findAllChildrenCategories(getUser(), cat);
      for (FeedCategory child : categories) {
        if (!child.getId().equals(cat.getId()) && child.getParent().getId().equals(cat.getId())) {
          child.setParent(null);
        }
      }
      feedCategoryDAO.saveOrUpdate(categories);

      feedCategoryDAO.delete(cat);
      cache.invalidateUserRootCategory(getUser());
      return Response.ok().build();
    } else {
      return Response.status(Status.NOT_FOUND).build();
    }
  }
Example #2
0
  @Path("/mark")
  @POST
  @ApiOperation(
      value = "Mark category entries",
      notes = "Mark feed entries of this category as read")
  public Response markCategoryEntries(
      @ApiParam(value = "category id, or 'all'", required = true) MarkRequest req) {
    Preconditions.checkNotNull(req);
    Preconditions.checkNotNull(req.getId());

    Date olderThan = req.getOlderThan() == null ? null : new Date(req.getOlderThan());

    if (ALL.equals(req.getId())) {
      List<FeedSubscription> subscriptions = feedSubscriptionDAO.findAll(getUser());
      feedEntryService.markSubscriptionEntries(getUser(), subscriptions, olderThan);
    } else if (STARRED.equals(req.getId())) {
      feedEntryService.markStarredEntries(getUser(), olderThan);
    } else {
      FeedCategory parent = feedCategoryDAO.findById(getUser(), Long.valueOf(req.getId()));
      List<FeedCategory> categories = feedCategoryDAO.findAllChildrenCategories(getUser(), parent);
      List<FeedSubscription> subs = feedSubscriptionDAO.findByCategories(getUser(), categories);
      feedEntryService.markSubscriptionEntries(getUser(), subs, olderThan);
    }
    return Response.ok(Status.OK).build();
  }
Example #3
0
 public void unregister(User user) {
   feedEntryStatusDAO.delete(feedEntryStatusDAO.findAll(user, false, ReadingOrder.desc, false));
   feedSubscriptionDAO.delete(feedSubscriptionDAO.findAll(user));
   feedCategoryDAO.delete(feedCategoryDAO.findAll(user));
   userSettingsDAO.delete(userSettingsDAO.findByUser(user));
   userRoleDAO.delete(userRoleDAO.findAll(user));
   userDAO.delete(user);
 }
Example #4
0
  public Opml export(User user) {
    Opml opml = new Opml();
    opml.setFeedType("opml_1.1");
    opml.setTitle(String.format("%s subscriptions in CommaFeed", user.getName()));
    opml.setCreated(new Date());

    List<FeedCategory> categories = feedCategoryDAO.findAll(user);
    Collections.sort(
        categories,
        (e1, e2) ->
            MoreObjects.firstNonNull(e1.getPosition(), 0)
                - MoreObjects.firstNonNull(e2.getPosition(), 0));

    List<FeedSubscription> subscriptions = feedSubscriptionDAO.findAll(user);
    Collections.sort(
        subscriptions,
        (e1, e2) ->
            MoreObjects.firstNonNull(e1.getPosition(), 0)
                - MoreObjects.firstNonNull(e2.getPosition(), 0));

    // export root categories
    for (FeedCategory cat :
        categories.stream().filter(c -> c.getParent() == null).collect(Collectors.toList())) {
      opml.getOutlines().add(buildCategoryOutline(cat, categories, subscriptions));
    }

    // export root subscriptions
    for (FeedSubscription sub :
        subscriptions.stream().filter(s -> s.getCategory() == null).collect(Collectors.toList())) {
      opml.getOutlines().add(buildSubscriptionOutline(sub));
    }

    return opml;
  }
Example #5
0
  @POST
  @Path("/collapse")
  @ApiOperation(
      value = "Collapse a category",
      notes = "Save collapsed or expanded status for a category")
  public Response collapse(@ApiParam(required = true) CollapseRequest req) {
    Preconditions.checkNotNull(req);
    Preconditions.checkNotNull(req.getId());

    FeedCategory category = feedCategoryDAO.findById(getUser(), Long.valueOf(req.getId()));
    if (category == null) {
      return Response.status(Status.NOT_FOUND).build();
    }
    category.setCollapsed(req.isCollapse());
    feedCategoryDAO.saveOrUpdate(category);
    cache.invalidateUserRootCategory(getUser());
    return Response.ok(Status.OK).build();
  }
Example #6
0
  @POST
  @Path("/modify")
  @ApiOperation(value = "Rename a category", notes = "Rename an existing feed category")
  public Response modifyCategory(@ApiParam(required = true) CategoryModificationRequest req) {
    Preconditions.checkNotNull(req);
    Preconditions.checkNotNull(req.getId());

    FeedCategory category = feedCategoryDAO.findById(getUser(), req.getId());

    if (StringUtils.isNotBlank(req.getName())) {
      category.setName(req.getName());
    }

    FeedCategory parent = null;
    if (req.getParentId() != null
        && !CategoryREST.ALL.equals(req.getParentId())
        && !StringUtils.equals(req.getParentId(), String.valueOf(req.getId()))) {
      parent = feedCategoryDAO.findById(getUser(), Long.valueOf(req.getParentId()));
    }
    category.setParent(parent);

    if (req.getPosition() != null) {
      List<FeedCategory> categories = feedCategoryDAO.findByParent(getUser(), parent);
      Collections.sort(
          categories,
          new Comparator<FeedCategory>() {
            @Override
            public int compare(FeedCategory o1, FeedCategory o2) {
              return ObjectUtils.compare(o1.getPosition(), o2.getPosition());
            }
          });

      int existingIndex = -1;
      for (int i = 0; i < categories.size(); i++) {
        if (ObjectUtils.equals(categories.get(i).getId(), category.getId())) {
          existingIndex = i;
        }
      }
      if (existingIndex != -1) {
        categories.remove(existingIndex);
      }

      categories.add(Math.min(req.getPosition(), categories.size()), category);
      for (int i = 0; i < categories.size(); i++) {
        categories.get(i).setPosition(i);
      }
      feedCategoryDAO.saveOrUpdate(categories);
    } else {
      feedCategoryDAO.saveOrUpdate(category);
    }

    feedCategoryDAO.saveOrUpdate(category);
    cache.invalidateUserRootCategory(getUser());
    return Response.ok(Status.OK).build();
  }
Example #7
0
  @SuppressWarnings("unchecked")
  public Opml export(User user) {
    Opml opml = new Opml();
    opml.setFeedType("opml_1.1");
    opml.setTitle(String.format("%s subscriptions in CommaFeed", user.getName()));
    opml.setCreated(Calendar.getInstance().getTime());

    List<FeedCategory> categories = feedCategoryDAO.findAll(user);
    List<FeedSubscription> subscriptions = feedSubscriptionDAO.findAll(user);

    for (FeedCategory cat : categories) {
      opml.getOutlines().add(buildCategoryOutline(cat, subscriptions));
    }
    for (FeedSubscription sub : subscriptions) {
      if (sub.getCategory() == null) {
        opml.getOutlines().add(buildSubscriptionOutline(sub));
      }
    }

    return opml;
  }
Example #8
0
  @GET
  @Path("/get")
  @ApiOperation(
      value = "Get feed categories",
      notes = "Get all categories and subscriptions of the user",
      responseClass = "com.commafeed.frontend.model.Category")
  public Response getSubscriptions() {
    User user = getUser();

    Category root = cache.getUserRootCategory(user);
    if (root == null) {
      log.debug("tree cache miss for {}", user.getId());
      List<FeedCategory> categories = feedCategoryDAO.findAll(user);
      List<FeedSubscription> subscriptions = feedSubscriptionDAO.findAll(user);
      Map<Long, UnreadCount> unreadCount = feedSubscriptionService.getUnreadCount(user);

      root = buildCategory(null, categories, subscriptions, unreadCount);
      root.setId("all");
      root.setName("All");
      cache.setUserRootCategory(user, root);
    }

    return Response.ok(root).build();
  }
Example #9
0
  @Path("/add")
  @POST
  @ApiOperation(
      value = "Add a category",
      notes = "Add a new feed category",
      responseClass = "java.lang.Long")
  public Response addCategory(@ApiParam(required = true) AddCategoryRequest req) {
    Preconditions.checkNotNull(req);
    Preconditions.checkNotNull(req.getName());

    FeedCategory cat = new FeedCategory();
    cat.setName(req.getName());
    cat.setUser(getUser());
    cat.setPosition(0);
    String parentId = req.getParentId();
    if (parentId != null && !ALL.equals(parentId)) {
      FeedCategory parent = new FeedCategory();
      parent.setId(Long.valueOf(parentId));
      cat.setParent(parent);
    }
    feedCategoryDAO.saveOrUpdate(cat);
    cache.invalidateUserRootCategory(getUser());
    return Response.ok(cat.getId()).build();
  }
Example #10
0
  @Path("/entries")
  @GET
  @ApiOperation(
      value = "Get category entries",
      notes = "Get a list of category entries",
      responseClass = "com.commafeed.frontend.model.Entries")
  public Response getCategoryEntries(
      @ApiParam(value = "id of the category, 'all' or 'starred'", required = true) @QueryParam("id")
          String id,
      @ApiParam(
              value = "all entries or only unread ones",
              allowableValues = "all,unread",
              required = true)
          @DefaultValue("unread")
          @QueryParam("readType")
          ReadingMode readType,
      @ApiParam(value = "only entries newer than this") @QueryParam("newerThan") Long newerThan,
      @ApiParam(value = "offset for paging") @DefaultValue("0") @QueryParam("offset") int offset,
      @ApiParam(value = "limit for paging, default 20, maximum 1000")
          @DefaultValue("20")
          @QueryParam("limit")
          int limit,
      @ApiParam(value = "date ordering", allowableValues = "asc,desc")
          @QueryParam("order")
          @DefaultValue("desc")
          ReadingOrder order,
      @ApiParam(
              value =
                  "search for keywords in either the title or the content of the entries, separated by spaces, 3 characters minimum")
          @QueryParam("keywords")
          String keywords,
      @ApiParam(value = "return only entry ids") @DefaultValue("false") @QueryParam("onlyIds")
          boolean onlyIds) {

    Preconditions.checkNotNull(readType);

    keywords = StringUtils.trimToNull(keywords);
    Preconditions.checkArgument(keywords == null || StringUtils.length(keywords) >= 3);

    limit = Math.min(limit, 1000);
    limit = Math.max(0, limit);

    Entries entries = new Entries();
    entries.setOffset(offset);
    entries.setLimit(limit);
    boolean unreadOnly = readType == ReadingMode.unread;
    if (StringUtils.isBlank(id)) {
      id = ALL;
    }

    Date newerThanDate = newerThan == null ? null : new Date(Long.valueOf(newerThan));

    if (ALL.equals(id)) {
      entries.setName("All");
      List<FeedSubscription> subscriptions = feedSubscriptionDAO.findAll(getUser());
      List<FeedEntryStatus> list =
          feedEntryStatusDAO.findBySubscriptions(
              subscriptions,
              unreadOnly,
              keywords,
              newerThanDate,
              offset,
              limit + 1,
              order,
              true,
              onlyIds);

      for (FeedEntryStatus status : list) {
        entries
            .getEntries()
            .add(
                Entry.build(
                    status,
                    applicationSettingsService.get().getPublicUrl(),
                    applicationSettingsService.get().isImageProxyEnabled()));
      }

    } else if (STARRED.equals(id)) {
      entries.setName("Starred");
      List<FeedEntryStatus> starred =
          feedEntryStatusDAO.findStarred(
              getUser(), newerThanDate, offset, limit + 1, order, !onlyIds);
      for (FeedEntryStatus status : starred) {
        entries
            .getEntries()
            .add(
                Entry.build(
                    status,
                    applicationSettingsService.get().getPublicUrl(),
                    applicationSettingsService.get().isImageProxyEnabled()));
      }
    } else {
      FeedCategory parent = feedCategoryDAO.findById(getUser(), Long.valueOf(id));
      if (parent != null) {
        List<FeedCategory> categories =
            feedCategoryDAO.findAllChildrenCategories(getUser(), parent);
        List<FeedSubscription> subs = feedSubscriptionDAO.findByCategories(getUser(), categories);

        List<FeedEntryStatus> list =
            feedEntryStatusDAO.findBySubscriptions(
                subs, unreadOnly, keywords, newerThanDate, offset, limit + 1, order, true, onlyIds);

        for (FeedEntryStatus status : list) {
          entries
              .getEntries()
              .add(
                  Entry.build(
                      status,
                      applicationSettingsService.get().getPublicUrl(),
                      applicationSettingsService.get().isImageProxyEnabled()));
        }
        entries.setName(parent.getName());
      }
    }

    boolean hasMore = entries.getEntries().size() > limit;
    if (hasMore) {
      entries.setHasMore(true);
      entries.getEntries().remove(entries.getEntries().size() - 1);
    }

    entries.setTimestamp(System.currentTimeMillis());
    FeedUtils.removeUnwantedFromSearch(entries.getEntries(), keywords);
    return Response.ok(entries).build();
  }