diff --git a/server/src/main/java/edu/sjsu/moth/server/controller/AccountController.java b/server/src/main/java/edu/sjsu/moth/server/controller/AccountController.java index 5db407f..526222d 100644 --- a/server/src/main/java/edu/sjsu/moth/server/controller/AccountController.java +++ b/server/src/main/java/edu/sjsu/moth/server/controller/AccountController.java @@ -170,10 +170,16 @@ public Mono> followUser(@PathVariable("id") String follow @GetMapping("/api/v1/accounts/{id}/following") public Mono userFollowing( @PathVariable String id, - @RequestParam(required = false) Integer page, @RequestParam(required = false) Integer limit) { + @RequestParam(required = false) Integer page, + @RequestParam(required = false, defaultValue = "40") Integer limit) { return accountService.usersFollow(id, page, limit, "following"); } + @GetMapping("/api/v2/suggestions") + public Mono> userSuggest() { + return Mono.just(new ArrayList<>()); + } + private static class RelationshipRequest { public String[] id; public Boolean with_suspended; diff --git a/server/src/main/java/edu/sjsu/moth/server/controller/StatusController.java b/server/src/main/java/edu/sjsu/moth/server/controller/StatusController.java index 544e00c..b266046 100644 --- a/server/src/main/java/edu/sjsu/moth/server/controller/StatusController.java +++ b/server/src/main/java/edu/sjsu/moth/server/controller/StatusController.java @@ -140,7 +140,8 @@ Mono> postApiV1Statuses(Principal user, @PathVariable Str @GetMapping("/api/v1/timelines/home") Mono>> getApiV1TimelinesHome(Principal user, @RequestParam(required = false) String max_id, - @RequestParam(required = false) String since_id, + @RequestParam(required = false, defaultValue = "0") + String since_id, @RequestParam(required = false) String min_id, @RequestParam(required = false, defaultValue = "20") int limit) { diff --git a/server/src/main/java/edu/sjsu/moth/server/service/AccountService.java b/server/src/main/java/edu/sjsu/moth/server/service/AccountService.java index c3502d9..261ac66 100644 --- a/server/src/main/java/edu/sjsu/moth/server/service/AccountService.java +++ b/server/src/main/java/edu/sjsu/moth/server/service/AccountService.java @@ -98,10 +98,8 @@ public Mono followerHandler(String id, JsonNode inboxNode, String reques return Mono.empty(); } - public Mono usersFollow(String id, - @RequestParam(required = false) Integer page, - @RequestParam(required = false) - Integer limit, String followType) { + public Mono usersFollow(String id, Integer page, Integer limit, + String followType) { var items = followType.equals("following") ? followRepository.findAllByFollowerId(id).map(followedUser -> followedUser.id.followed_id).take(limit) .collectList() : diff --git a/server/src/main/java/edu/sjsu/moth/server/service/StatusService.java b/server/src/main/java/edu/sjsu/moth/server/service/StatusService.java index 6a0a19f..2651867 100644 --- a/server/src/main/java/edu/sjsu/moth/server/service/StatusService.java +++ b/server/src/main/java/edu/sjsu/moth/server/service/StatusService.java @@ -1,6 +1,9 @@ package edu.sjsu.moth.server.service; +import com.querydsl.core.types.Ops; +import com.querydsl.core.types.Path; import com.querydsl.core.types.dsl.BooleanExpression; +import com.querydsl.core.types.dsl.Expressions; import edu.sjsu.moth.generated.QStatus; import edu.sjsu.moth.generated.SearchResult; import edu.sjsu.moth.generated.Status; @@ -11,6 +14,7 @@ import edu.sjsu.moth.server.db.Follow; import edu.sjsu.moth.server.db.FollowRepository; import edu.sjsu.moth.server.db.StatusRepository; +import org.bson.types.ObjectId; import org.jetbrains.annotations.NotNull; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Configuration; @@ -102,13 +106,16 @@ public Mono findStatusById(String id) { public Mono> getTimeline(Principal user, String max_id, String since_id, String min_id, int limit, boolean isFollowingTimeline) { - var qStatus = new QStatus("start"); + var qStatus = QStatus.status; var predicate = qStatus.content.isNotNull(); predicate = addRangeQueries(predicate, max_id, since_id, max_id); var external = externalStatusRepository.findAll(predicate, Sort.by(Sort.Direction.DESC, "id")) .flatMap(statuses -> filterStatusByViewable(user, statuses, isFollowingTimeline)).take(limit); var internal = statusRepository.findAll(predicate, Sort.by(Sort.Direction.DESC, "id")) - .flatMap(statuses -> filterStatusByViewable(user, statuses, isFollowingTimeline)).take(limit); + //.switchIfEmpty(Mono.fromRunnable(() -> System.out.println("No status found"))) + //.doOnNext(x -> System.out.println("before filter: " + x)) + .flatMap(statuses -> filterStatusByViewable(user, statuses, isFollowingTimeline)); + //.doOnNext(x -> System.out.println("after filter: " + x)).take(limit); //TODO: we may want to merge sort them, unsure if merge does that return Flux.merge(external, internal).collectList(); @@ -116,10 +123,15 @@ public Mono> getTimeline(Principal user, String max_id, String sinc private BooleanExpression addRangeQueries(BooleanExpression predicate, String max_id, String since_id, String min_id) { - if (max_id != null) predicate = predicate.and(new QStatus("max").id.lt(max_id)); - if (since_id != null) predicate = predicate.and(new QStatus("since").id.gt(since_id)); + Path statusIdPath = Expressions.path(ObjectId.class, QStatus.status.id.getMetadata()); + if (max_id != null) predicate = predicate.and( + Expressions.predicate(Ops.LT, statusIdPath, Expressions.constant(new ObjectId(convertToHex(max_id))))); + if (since_id != null) predicate = predicate.and(Expressions.predicate(Ops.GT, statusIdPath, + Expressions.constant(new ObjectId( + convertToHex(since_id))))); // this isn't right. i'm not sure how to express close - if (min_id != null) predicate = predicate.and(new QStatus("min").id.gt(min_id)); + if (min_id != null) predicate = predicate.and( + Expressions.predicate(Ops.GT, statusIdPath, Expressions.constant(new ObjectId(convertToHex(min_id))))); return predicate; } @@ -168,19 +180,21 @@ public Mono filterStatusSearch(String query, Principal user, Strin } private Flux filterStatusByViewable(Principal user, Status status, boolean isFollowingTimeline) { + return accountService.getAccount(user.getName()) - .switchIfEmpty(Mono.error(new UsernameNotFoundException(user.getName()))).flatMapMany( - acct -> followRepository.findAllByFollowerId(acct.id).flatMap(following -> - ((status.account.id.equals( - acct.id)) || - (!isFollowingTimeline && - status.visibility.equals( - "public")) || - following.id.followed_id.equals( - status.account.id)) ? - Flux.just( - status) : - Flux.empty())); + .switchIfEmpty(Mono.error(new UsernameNotFoundException(user.getName()))).flatMapMany(acct -> { + if (status.account.id.equals(acct.id)) { + return Flux.just(status); + } + if (!isFollowingTimeline && status.visibility.equals("public")) return Flux.just(status); + return followRepository.findAllByFollowerId(acct.id).flatMap( + following -> following.id.followed_id.equals(status.account.id) ? Flux.just(status) : + Flux.empty()); + }); + } + + private String convertToHex(String payload) { + return String.format("%1$24s", payload).replace(' ', '0'); } }