Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed timeline issue and rangeQuery #144

Merged
merged 2 commits into from
Jun 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -170,10 +170,16 @@ public Mono<ResponseEntity<Follow>> followUser(@PathVariable("id") String follow
@GetMapping("/api/v1/accounts/{id}/following")
public Mono<InboxController.UsersFollowResponse> 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<ArrayList<String>> userSuggest() {
return Mono.just(new ArrayList<>());
}

private static class RelationshipRequest {
public String[] id;
public Boolean with_suspended;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,8 @@ Mono<ResponseEntity<Status>> postApiV1Statuses(Principal user, @PathVariable Str
@GetMapping("/api/v1/timelines/home")
Mono<ResponseEntity<List<Status>>> 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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,8 @@ public Mono<String> followerHandler(String id, JsonNode inboxNode, String reques
return Mono.empty();
}

public Mono<InboxController.UsersFollowResponse> usersFollow(String id,
@RequestParam(required = false) Integer page,
@RequestParam(required = false)
Integer limit, String followType) {
public Mono<InboxController.UsersFollowResponse> 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() :
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -102,24 +106,32 @@ public Mono<Status> findStatusById(String id) {

public Mono<List<Status>> 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")))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed commented lines.

//.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();
}

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<ObjectId> 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;
}

Expand Down Expand Up @@ -168,19 +180,21 @@ public Mono<SearchResult> filterStatusSearch(String query, Principal user, Strin
}

private Flux<Status> 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');
}

}
Loading