From b2c48c5a77ca119276cab2ad6df4db4841376f04 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Wed, 5 Sep 2018 13:10:24 +0300 Subject: ActivityStreams: followers and following collections --- .../com/juick/server/api/activity/Profile.java | 105 ++++++++++++++++++++- .../juick/server/api/activity/model/Person.java | 27 ++++++ .../java/com/juick/service/UserServiceImpl.java | 11 +++ 3 files changed, 141 insertions(+), 2 deletions(-) (limited to 'juick-server/src') diff --git a/juick-server/src/main/java/com/juick/server/api/activity/Profile.java b/juick-server/src/main/java/com/juick/server/api/activity/Profile.java index e19ff292..0974444f 100644 --- a/juick-server/src/main/java/com/juick/server/api/activity/Profile.java +++ b/juick-server/src/main/java/com/juick/server/api/activity/Profile.java @@ -18,6 +18,7 @@ import org.springframework.web.util.UriComponentsBuilder; import javax.inject.Inject; import java.util.List; import java.util.stream.Collectors; +import java.util.stream.Stream; @RestController public class Profile { @@ -46,6 +47,10 @@ public class Profile { person.setInbox(inboxBuilder.toUriString()); UriComponentsBuilder outboxBuilder = UriComponentsBuilder.fromUriString(baseUri); person.setOutbox(outboxBuilder.path(String.format("/u/%s/blog/toc", userName)).toUriString()); + UriComponentsBuilder followersBuilder = UriComponentsBuilder.fromUriString(baseUri); + person.setFollowers(followersBuilder.path(String.format("/u/%s/followers/toc", userName)).toUriString()); + UriComponentsBuilder followingBuilder = UriComponentsBuilder.fromUriString(baseUri); + person.setFollowing(followingBuilder.path(String.format("/u/%s/following/toc", userName)).toUriString()); UriComponentsBuilder imgBuilder = UriComponentsBuilder.fromUriString(baseImagesUri); imgBuilder.path(String.format("/a/%d.png", user.getUid())); Image avatar = new Image(); @@ -58,12 +63,11 @@ public class Profile { } @GetMapping(value = "/u/{userName}/blog/toc", produces = ActivityObject.CONTENT_TYPE) public OrderedCollection getOutbox(@PathVariable String userName) { - User visitor = UserUtils.getCurrentUser(); User user = userService.getUserByName(userName); if (!user.isAnonymous()) { UriComponentsBuilder uriComponentsBuilder = UriComponentsBuilder.fromUriString(baseUri); OrderedCollection blog = new OrderedCollection(); - blog.setId(uriComponentsBuilder.toUriString()); + blog.setId(ServletUriComponentsBuilder.fromCurrentRequestUri().toUriString()); blog.setTotalItems(userService.getStatsMessages(user.getUid())); Link first = new Link(); uriComponentsBuilder.path(String.format("/u/%s/blog", userName)); @@ -96,6 +100,7 @@ public class Profile { Person person = new Person(); person.setName(user.getName()); OrderedCollectionPage page = new OrderedCollectionPage(); + page.setId(ServletUriComponentsBuilder.fromCurrentRequestUri().toUriString()); page.setOrderedItems(notes.stream().map(a -> { Create create = new Create(); create.setActor(person); @@ -113,4 +118,100 @@ public class Profile { } throw new HttpNotFoundException(); } + @GetMapping(value = "/u/{userName}/followers/toc", produces = ActivityObject.CONTENT_TYPE) + public OrderedCollection getFollowers(@PathVariable String userName) { + User user = userService.getUserByName(userName); + if (!user.isAnonymous()) { + UriComponentsBuilder uriComponentsBuilder = UriComponentsBuilder.fromUriString(baseUri); + OrderedCollection followers = new OrderedCollection(); + followers.setId(ServletUriComponentsBuilder.fromCurrentRequestUri().toUriString()); + followers.setTotalItems(userService.getStatsMyReaders(user.getUid())); + Link first = new Link(); + uriComponentsBuilder.path(String.format("/u/%s/followers", userName)); + first.setHref(uriComponentsBuilder.toUriString()); + followers.setFirst(first); + return followers; + } + throw new HttpNotFoundException(); + } + @GetMapping(value = "/u/{userName}/followers", produces = ActivityObject.CONTENT_TYPE) + public OrderedCollectionPage getFollowersPage(@PathVariable String userName, + @RequestParam(required = false, defaultValue = "0") int page) { + User user = userService.getUserByName(userName); + if (!user.isAnonymous()) { + UriComponentsBuilder uriComponentsBuilder = UriComponentsBuilder.fromUriString(baseUri); + uriComponentsBuilder.path(String.format("/u/%s/followers", userName)); + List followers = userService.getUserReaders(user.getUid()); + Stream followersPage = followers.stream().skip(20 * page).limit(20); + + Person person = new Person(); + person.setName(user.getName()); + OrderedCollectionPage result = new OrderedCollectionPage(); + result.setId(ServletUriComponentsBuilder.fromCurrentRequestUri().toUriString()); + result.setOrderedItems(followersPage.map(a -> { + Person follower = new Person(); + follower.setName(a.getName()); + Link url = new Link(); + url.setHref(uriComponentsBuilder.replacePath(String.format("/u/%s", a.getName())).toUriString()); + follower.setUrl(url); + return follower; + }).collect(Collectors.toList())); + boolean hasNext = followers.size() <= 20 * page; + if (hasNext) { + Link next = new Link(); + next.setHref(uriComponentsBuilder.queryParam("page", page + 1).toUriString()); + result.setNext(next); + } + return result; + } + throw new HttpNotFoundException(); + } + @GetMapping(value = "/u/{userName}/following/toc", produces = ActivityObject.CONTENT_TYPE) + public OrderedCollection getFollowing(@PathVariable String userName) { + User user = userService.getUserByName(userName); + if (!user.isAnonymous()) { + UriComponentsBuilder uriComponentsBuilder = UriComponentsBuilder.fromUriString(baseUri); + OrderedCollection following = new OrderedCollection(); + following.setId(ServletUriComponentsBuilder.fromCurrentRequestUri().toUriString()); + following.setTotalItems(userService.getUserFriends(user.getUid()).size()); + Link first = new Link(); + uriComponentsBuilder.path(String.format("/u/%s/followers", userName)); + first.setHref(uriComponentsBuilder.toUriString()); + following.setFirst(first); + return following; + } + throw new HttpNotFoundException(); + } + @GetMapping(value = "/u/{userName}/following", produces = ActivityObject.CONTENT_TYPE) + public OrderedCollectionPage getFollowingPage(@PathVariable String userName, + @RequestParam(required = false, defaultValue = "0") int page) { + User user = userService.getUserByName(userName); + if (!user.isAnonymous()) { + UriComponentsBuilder uriComponentsBuilder = UriComponentsBuilder.fromUriString(baseUri); + uriComponentsBuilder.path(String.format("/u/%s/following", userName)); + List following = userService.getUserFriends(user.getUid()); + Stream followingPage = following.stream().skip(20 * page).limit(20); + + Person person = new Person(); + person.setName(user.getName()); + OrderedCollectionPage result = new OrderedCollectionPage(); + result.setId(ServletUriComponentsBuilder.fromCurrentRequestUri().toUriString()); + result.setOrderedItems(followingPage.map(a -> { + Person follower = new Person(); + follower.setName(a.getName()); + Link url = new Link(); + url.setHref(uriComponentsBuilder.replacePath(String.format("/u/%s", a.getName())).toUriString()); + follower.setUrl(url); + return follower; + }).collect(Collectors.toList())); + boolean hasNext = following.size() <= 20 * page; + if (hasNext) { + Link next = new Link(); + next.setHref(uriComponentsBuilder.queryParam("page", page + 1).toUriString()); + result.setNext(next); + } + return result; + } + throw new HttpNotFoundException(); + } } diff --git a/juick-server/src/main/java/com/juick/server/api/activity/model/Person.java b/juick-server/src/main/java/com/juick/server/api/activity/model/Person.java index d3e99bcc..f4312ffc 100644 --- a/juick-server/src/main/java/com/juick/server/api/activity/model/Person.java +++ b/juick-server/src/main/java/com/juick/server/api/activity/model/Person.java @@ -6,6 +6,9 @@ public class Person extends ActivityObject { private Image icon; private String inbox; private String outbox; + private String following; + private String followers; + private Link url; @Override public String getType() { @@ -43,4 +46,28 @@ public class Person extends ActivityObject { public void setInbox(String inbox) { this.inbox = inbox; } + + public String getFollowing() { + return following; + } + + public void setFollowing(String following) { + this.following = following; + } + + public String getFollowers() { + return followers; + } + + public void setFollowers(String followers) { + this.followers = followers; + } + + public Link getUrl() { + return url; + } + + public void setUrl(Link url) { + this.url = url; + } } diff --git a/juick-server/src/main/java/com/juick/service/UserServiceImpl.java b/juick-server/src/main/java/com/juick/service/UserServiceImpl.java index e841b539..bd72bed9 100644 --- a/juick-server/src/main/java/com/juick/service/UserServiceImpl.java +++ b/juick-server/src/main/java/com/juick/service/UserServiceImpl.java @@ -26,6 +26,7 @@ import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.lang3.RandomStringUtils; import org.apache.commons.lang3.StringUtils; import org.springframework.dao.DuplicateKeyException; +import org.springframework.dao.EmptyResultDataAccessException; import org.springframework.jdbc.core.RowMapper; import org.springframework.jdbc.core.namedparam.MapSqlParameterSource; import org.springframework.jdbc.support.GeneratedKeyHolder; @@ -498,6 +499,16 @@ public class UserServiceImpl extends BaseJdbcService implements UserService { uid); } + @Transactional(readOnly = true) + @Override + public Integer getUserRecommendations(User user) { + try { + return jdbcTemplate.queryForObject("SELECT COUNT(*) FROM favorites WHERE user_id=?", Integer.class, user.getUid()); + } catch (EmptyResultDataAccessException e) { + return 0; + } + } + @Transactional(readOnly = true) @Override public List getUserBLUsers(final int uid) { -- cgit v1.2.3