-
Notifications
You must be signed in to change notification settings - Fork 1
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 |
---|---|---|
@@ -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; | ||
|
@@ -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 | ||
|
@@ -26,6 +39,12 @@ public class AccountService { | |
@Autowired | ||
AccountRepository accountRepository; | ||
|
||
@Autowired | ||
FollowersRepository followersRepository; | ||
|
||
@Autowired | ||
FollowingRepository followingRepository; | ||
|
||
@Autowired | ||
PubKeyPairRepository pubKeyPairRepository; | ||
|
||
|
@@ -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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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") |
||
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); | ||
} | ||
|
There was a problem hiding this comment.
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