Skip to content

Commit

Permalink
added following/merged followers to new method (#89)
Browse files Browse the repository at this point in the history
- added following
- created new method for both following/followers
- updated the record with one made by angela and renamed it
  • Loading branch information
seannian authored Jul 20, 2023
1 parent 1e6f74f commit 0fd3367
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,28 @@
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import com.fasterxml.jackson.databind.JsonNode;

import com.fasterxml.jackson.databind.ObjectMapper;
import edu.sjsu.moth.server.db.AccountRepository;

import edu.sjsu.moth.server.db.Followers;
import edu.sjsu.moth.server.db.FollowersRepository;
import edu.sjsu.moth.server.db.AccountRepository;
import edu.sjsu.moth.server.db.Following;
import edu.sjsu.moth.server.db.FollowingRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Mono;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import static org.springframework.beans.support.PagedListHolder.DEFAULT_PAGE_SIZE;

import java.util.ArrayList;

Expand All @@ -23,9 +38,12 @@
public class InboxController {
@Autowired
FollowersRepository followersRepository;

@Autowired
AccountRepository accountRepository;
FollowingRepository followingRepository;

@Autowired
AccountRepository accountRepository;

//required to map payload from JSON to a Java Object for data access
ObjectMapper mappedLoad;
Expand Down Expand Up @@ -70,32 +88,50 @@ public Mono<String> followerHandler(String id, JsonNode inboxNode, String reques
return Mono.empty();
}

@GetMapping("/users/{id}/following")
public Mono<UsersFollowResponse> usersFollowing(@PathVariable String id,
@RequestParam(required = false) Integer page,
@RequestParam(required = false) Integer limit) {
return usersFollow(id, page, limit, "following");
}

@GetMapping("/users/{id}/followers")
public Mono<UsersFollowersResponse> usersFollowers(@PathVariable String id, @RequestParam(required = false) Integer page, @RequestParam(required = false) Integer limit) {
var items = followersRepository.findItemById(id).map(Followers::getFollowers);
String returnID = MothController.BASE_URL + "/users/" + id + "/followers";
public Mono<UsersFollowResponse> usersFollowers(@PathVariable String id,
@RequestParam(required = false) Integer page,
@RequestParam(required = false) Integer limit) {
return usersFollow(id, page, limit, "followers");
}

public Mono<UsersFollowResponse> usersFollow(String id, @RequestParam(required = false) Integer page,
@RequestParam(required = false) Integer limit, String followType) {
var items = followType.equals("following") ? followingRepository.findItemById(id)
.map(Following::getFollowing) : followersRepository.findItemById(id).map(Followers::getFollowers);
String returnID = MothController.BASE_URL + "/users/" + id + followType;
int pageSize = limit != null ? limit : DEFAULT_PAGE_SIZE;
if(page == null) {
if (page == null) {
String first = returnID + "?page=1";
return items.map(v -> new UsersFollowersResponse(returnID, "OrderedCollection", v.size(), first, null, null, null));
return items.map(
v -> new UsersFollowResponse(returnID, "OrderedCollection", v.size(), first, null, null, null));
} else { // page number is given
int pageNum = page < 1 ? 1 : page;
return items.map(v -> {
String newReturnID = limit != null ? returnID + "?page=" + page + "&limit=" + limit : returnID + "?page=" + page;
if(pageNum*pageSize >= v.size()) { // no next page
return new UsersFollowersResponse(newReturnID, "OrderedCollectionPage", v.size(), null, null, returnID, paginateFollowers(v, pageNum, pageSize));
String newReturnID = limit != null ? returnID + "?page=" + page + "&limit=" + limit : returnID +
"?page=" + page;
if (pageNum * pageSize >= v.size()) { // no next page
return new UsersFollowResponse(newReturnID, "OrderedCollectionPage", v.size(), null, null, returnID,
paginateFollowers(v, pageNum, pageSize));
} else {
String next = returnID + "?page=" + (pageNum+1);
if(limit != null) {
String next = returnID + "?page=" + (pageNum + 1);
if (limit != null) {
next += "&limit=" + limit;
}
return new UsersFollowersResponse(newReturnID, "OrderedCollectionPage", v.size(), null, next, returnID, paginateFollowers(v, pageNum, pageSize));
return new UsersFollowResponse(newReturnID, "OrderedCollectionPage", v.size(), null, next, returnID,
paginateFollowers(v, pageNum, pageSize));
}
});
}
}


public List<String> paginateFollowers(ArrayList<String> followers, int pageNo, int pageSize) {
int startIndex = (pageNo - 1) * pageSize;
int endIndex = Math.min(startIndex + pageSize, followers.size());
Expand All @@ -106,14 +142,12 @@ public List<String> paginateFollowers(ArrayList<String> followers, int pageNo, i
}

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({"@context", "id", "type", "totalItems", "first", "next", "partOf", "orderedItems"})
public record UsersFollowersResponse(String id, String type, int totalItems, String first, String next, String partOf, List<String> orderedItems) {
@JsonPropertyOrder({ "@context", "id", "type", "totalItems", "first", "next", "partOf", "orderedItems" })
public record UsersFollowResponse(String id, String type, int totalItems, String first, String next, String partOf,
List<String> orderedItems) {
@JsonProperty("@context")
public String getContext() {
return "https://www.w3.org/ns/activitystreams";
}
}



}
27 changes: 27 additions & 0 deletions server/src/main/java/edu/sjsu/moth/server/db/Following.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package edu.sjsu.moth.server.db;

import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;

import java.util.ArrayList;

@Document("Following")
public class Following {
@Id
public String id;
public ArrayList<String> following;

public Following(String id, ArrayList<String> following) {
this.id = id;
this.following = following;
}

// id to query for, to locate a users' followers document
// afterwards, have a String[] array containing the users' followers and append to it, or delete
// 'Following' can follow a similar format, but in a different collection

public ArrayList<String> getFollowing() {
return following;
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package edu.sjsu.moth.server.db;

import org.springframework.data.mongodb.repository.Query;
import org.springframework.data.mongodb.repository.ReactiveMongoRepository;
import reactor.core.publisher.Mono;

public interface FollowingRepository extends ReactiveMongoRepository<Following, String>{
@Query("{id:'?0'}")
Mono<Following> findItemById(String id);
}

0 comments on commit 0fd3367

Please sign in to comment.