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

Moved follow repositories & methods to AccountService, created AccountService object in InboxController #122

Merged
merged 3 commits into from
Sep 5, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
3 changes: 2 additions & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
Copy link
Contributor

Choose a reason for hiding this comment

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

reformat file to get rid of extra import and space

import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestPart;
Expand Down Expand Up @@ -122,4 +123,5 @@ public Mono<ResponseEntity<Account>> getApiV1AccountsById(Principal user, @PathV
return accountService.getAccount(id).map(ResponseEntity::ok);
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import edu.sjsu.moth.server.db.FollowersRepository;
import edu.sjsu.moth.server.db.Following;
import edu.sjsu.moth.server.db.FollowingRepository;
import edu.sjsu.moth.server.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
Expand All @@ -36,14 +37,9 @@

@RestController
public class InboxController {
@Autowired
FollowersRepository followersRepository;

@Autowired
FollowingRepository followingRepository;

@Autowired
AccountRepository accountRepository;
AccountService accountService;

//required to map payload from JSON to a Java Object for data access
ObjectMapper mappedLoad;
Expand All @@ -57,88 +53,22 @@ public Mono<String> usersInbox(@PathVariable String id, @RequestBody JsonNode in
String requestType = inboxNode.get("type").asText();
// follow or unfollow requests
if (requestType.equals("Follow") || requestType.equals("Undo"))
return followerHandler(id, inboxNode, requestType);
return Mono.empty();
}

public Mono<String> followerHandler(String id, JsonNode inboxNode, String requestType) {
String follower = inboxNode.get("actor").asText();
if (requestType.equals("Follow")) {
// check id
if (accountRepository.findItemByAcct(id)==null) {
return Mono.error(new RuntimeException("Error: Account to follow doesn't exist."));
}
// find id, grab arraylist, append
else {
return followersRepository.findItemById(id)
.switchIfEmpty(Mono.just(new Followers(id, new ArrayList<>())))
.flatMap(followedUser -> {
followedUser.getFollowers().add(follower);
return followersRepository.save(followedUser).thenReturn("done");
});
}
}
if (requestType.equals("Undo")) {
// find id, grab arraylist, remove
return followersRepository.findItemById(id).flatMap(followedUser -> {
followedUser.getFollowers().remove(follower);
return followersRepository.save(followedUser).thenReturn("done");
});
}
return accountService.followerHandler(id, inboxNode, requestType);
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");
return accountService.usersFollow(id, page, limit, "following");
}

@GetMapping("/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) {
String first = returnID + "?page=1";
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 UsersFollowResponse(newReturnID, "OrderedCollectionPage", v.size(), null, null, returnID,
paginateFollowers(v, pageNum, pageSize));
} else {
String next = returnID + "?page=" + (pageNum + 1);
if (limit != null) {
next += "&limit=" + limit;
}
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());
if (startIndex >= followers.size()) {
return Collections.emptyList();
}
return followers.subList(startIndex, endIndex);
return accountService.usersFollow(id, page, limit, "followers");
}

@JsonInclude(JsonInclude.Include.NON_NULL)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
package edu.sjsu.moth.server.service;

import com.fasterxml.jackson.databind.JsonNode;
import edu.sjsu.moth.server.controller.InboxController;
import edu.sjsu.moth.server.controller.MothController;
import edu.sjsu.moth.server.db.Account;
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.Following;
import edu.sjsu.moth.server.db.FollowingRepository;
import edu.sjsu.moth.server.db.PubKeyPair;
import edu.sjsu.moth.server.db.PubKeyPairRepository;
import edu.sjsu.moth.server.db.WebfingerAlias;
Expand All @@ -11,8 +17,15 @@
import lombok.extern.apachecommons.CommonsLog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.RequestParam;
import reactor.core.publisher.Mono;

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

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

/**
* this class manages the logic and DB access around account management.
* controllers shouldn't be doing account management directly and this class
Expand All @@ -26,6 +39,12 @@ public class AccountService {
@Autowired
AccountRepository accountRepository;

@Autowired
FollowersRepository followersRepository;

@Autowired
FollowingRepository followingRepository;

@Autowired
PubKeyPairRepository pubKeyPairRepository;

Expand Down Expand Up @@ -68,6 +87,72 @@ public Mono<String> getPublicKey(String id, boolean addIfMissing) {
return mono;
}

public Mono<String> followerHandler(String id, JsonNode inboxNode, String requestType) {
String follower = inboxNode.get("actor").asText();
if (requestType.equals("Follow")) {
// check id
if (accountRepository.findItemByAcct(id)==null) {
Copy link
Contributor

Choose a reason for hiding this comment

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

this will never be null.

the if else should be removed and line 99 should start with

return accountRepository.findItemByAcct(id).switchOnEmpty(Mono.error(new RuntimeException("Error: Account to follow does not exist")
.then(followersRepository.findItemById(id)

return Mono.error(new RuntimeException("Error: Account to follow doesn't exist."));
}
// find id, grab arraylist, append
else {
return followersRepository.findItemById(id)
.switchIfEmpty(Mono.just(new Followers(id, new ArrayList<>())))
.flatMap(followedUser -> {
followedUser.getFollowers().add(follower);
return followersRepository.save(followedUser).thenReturn("done");
});
}
}
if (requestType.equals("Undo")) {
// find id, grab arraylist, remove
return followersRepository.findItemById(id).flatMap(followedUser -> {
followedUser.getFollowers().remove(follower);
return followersRepository.save(followedUser).thenReturn("done");
});
}
return Mono.empty();
}

public Mono<InboxController.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) {
String first = returnID + "?page=1";
return items.map(
v -> new InboxController.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 InboxController.UsersFollowResponse(newReturnID, "OrderedCollectionPage", v.size(), null, null, returnID,
paginateFollowers(v, pageNum, pageSize));
} else {
String next = returnID + "?page=" + (pageNum + 1);
if (limit != null) {
next += "&limit=" + limit;
}
return new InboxController.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());
if (startIndex >= followers.size()) {
return Collections.emptyList();
}
return followers.subList(startIndex, endIndex);
}

public Mono<Account> updateAccount(Account a) {
return accountRepository.save(a);
}
Expand Down
Loading