aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Vitaly Takmazov2018-09-05 13:10:24 +0300
committerGravatar Vitaly Takmazov2018-09-05 13:10:24 +0300
commitb2c48c5a77ca119276cab2ad6df4db4841376f04 (patch)
tree780281fb019b57aa3f182dc4acd3f6e2e70071b1
parent972d3aef3ab12c38c7f4a3b085c2c99cdc4d1d51 (diff)
ActivityStreams: followers and following collections
-rw-r--r--juick-server/src/main/java/com/juick/server/api/activity/Profile.java105
-rw-r--r--juick-server/src/main/java/com/juick/server/api/activity/model/Person.java27
-rw-r--r--juick-server/src/main/java/com/juick/service/UserServiceImpl.java11
3 files changed, 141 insertions, 2 deletions
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<User> followers = userService.getUserReaders(user.getUid());
+ Stream<User> 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<User> following = userService.getUserFriends(user.getUid());
+ Stream<User> 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;
@@ -500,6 +501,16 @@ public class UserServiceImpl extends BaseJdbcService implements UserService {
@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<com.juick.User> getUserBLUsers(final int uid) {
return getJdbcTemplate().query("SELECT users.id,users.nick FROM users INNER JOIN bl_users " +
"ON(bl_users.bl_user_id=users.id) WHERE bl_users.user_id=? ORDER BY users.nick",