From a63800dd6c5f8551b7126be6d3557e90888f0703 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Tue, 4 Sep 2018 13:52:10 +0300 Subject: outbox as OrderedCollection --- .../java/com/juick/server/api/activity/Info.java | 45 ----------- .../com/juick/server/api/activity/Profile.java | 89 ++++++++++++++++++++++ .../server/api/activity/model/ActivityObject.java | 14 +++- .../juick/server/api/activity/model/Article.java | 22 ++++++ .../juick/server/api/activity/model/Create.java | 23 ++++++ .../com/juick/server/api/activity/model/Link.java | 13 ++++ .../api/activity/model/OrderedCollection.java | 23 ++++++ .../api/activity/model/OrderedCollectionPage.java | 26 +++++++ .../juick/server/api/activity/model/Person.java | 10 --- 9 files changed, 208 insertions(+), 57 deletions(-) delete mode 100644 juick-server/src/main/java/com/juick/server/api/activity/Info.java create mode 100644 juick-server/src/main/java/com/juick/server/api/activity/Profile.java create mode 100644 juick-server/src/main/java/com/juick/server/api/activity/model/Article.java create mode 100644 juick-server/src/main/java/com/juick/server/api/activity/model/Create.java create mode 100644 juick-server/src/main/java/com/juick/server/api/activity/model/Link.java create mode 100644 juick-server/src/main/java/com/juick/server/api/activity/model/OrderedCollection.java create mode 100644 juick-server/src/main/java/com/juick/server/api/activity/model/OrderedCollectionPage.java (limited to 'juick-server/src/main') diff --git a/juick-server/src/main/java/com/juick/server/api/activity/Info.java b/juick-server/src/main/java/com/juick/server/api/activity/Info.java deleted file mode 100644 index f779049a..00000000 --- a/juick-server/src/main/java/com/juick/server/api/activity/Info.java +++ /dev/null @@ -1,45 +0,0 @@ -package com.juick.server.api.activity; - -import com.juick.User; -import com.juick.server.api.activity.model.ActivityObject; -import com.juick.server.api.activity.model.Person; -import com.juick.server.util.HttpNotFoundException; -import com.juick.service.UserService; -import org.springframework.beans.factory.annotation.Value; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.RestController; -import org.springframework.web.util.UriComponentsBuilder; - -import javax.inject.Inject; - -@RestController -public class Info { - @Inject - private UserService userService; - @Value("${web_domain:localhost}") - private String domain; - @Value("${ap_base_uri:http://localhost:8080/}") - private String baseUri; - @Value("${img_url:http://localhost:8080/i/}") - private String baseImagesUri; - - @GetMapping(value = "/u/{userName}", produces = ActivityObject.CONTENT_TYPE) - public Person getUser(@PathVariable String userName) { - User user = userService.getUserByName(userName); - if (!user.isAnonymous()) { - Person person = new Person(); - UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(baseUri); - builder.path(String.format("/u/%s", userName)); - person.setId(builder.toUriString()); - person.setName(userName); - UriComponentsBuilder outboxBuilder = UriComponentsBuilder.fromUriString(baseUri); - person.setOutbox(outboxBuilder.path(String.format("/u/%s/blog", userName)).toUriString()); - UriComponentsBuilder imgBuilder = UriComponentsBuilder.fromUriString(baseImagesUri); - imgBuilder.path(String.format("/a/%d.png", user.getUid())); - person.setIcon(imgBuilder.toUriString()); - return person; - } - throw new HttpNotFoundException(); - } -} 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 new file mode 100644 index 00000000..8311c862 --- /dev/null +++ b/juick-server/src/main/java/com/juick/server/api/activity/Profile.java @@ -0,0 +1,89 @@ +package com.juick.server.api.activity; + +import com.juick.User; +import com.juick.server.api.activity.model.*; +import com.juick.server.util.HttpNotFoundException; +import com.juick.server.util.UserUtils; +import com.juick.service.MessagesService; +import com.juick.service.UserService; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.servlet.support.ServletUriComponentsBuilder; +import org.springframework.web.util.UriComponentsBuilder; + +import javax.inject.Inject; +import java.util.List; +import java.util.stream.Collectors; + +@RestController +public class Profile { + @Inject + private UserService userService; + @Inject + private MessagesService messagesService; + @Value("${web_domain:localhost}") + private String domain; + @Value("${ap_base_uri:http://localhost:8080/}") + private String baseUri; + @Value("${img_url:http://localhost:8080/i/}") + private String baseImagesUri; + + @GetMapping(value = "/u/{userName}", produces = ActivityObject.CONTENT_TYPE) + public Person getUser(@PathVariable String userName) { + User user = userService.getUserByName(userName); + if (!user.isAnonymous()) { + Person person = new Person(); + UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(baseUri); + builder.path(String.format("/u/%s", userName)); + person.setId(builder.toUriString()); + person.setName(userName); + UriComponentsBuilder outboxBuilder = UriComponentsBuilder.fromUriString(baseUri); + person.setOutbox(outboxBuilder.path(String.format("/u/%s/blog", userName)).toUriString()); + UriComponentsBuilder imgBuilder = UriComponentsBuilder.fromUriString(baseImagesUri); + imgBuilder.path(String.format("/a/%d.png", user.getUid())); + person.setIcon(imgBuilder.toUriString()); + return person; + } + throw new HttpNotFoundException(); + } + @GetMapping(value = "/u/{userName}/blog", produces = ActivityObject.CONTENT_TYPE) + public OrderedCollection getOutbox( + @PathVariable String userName, + @RequestParam(required = false, defaultValue = "0") int before) { + User visitor = UserUtils.getCurrentUser(); + User user = userService.getUserByName(userName); + if (!user.isAnonymous()) { + ServletUriComponentsBuilder uriComponentsBuilder = ServletUriComponentsBuilder.fromCurrentRequestUri(); + OrderedCollection blog = new OrderedCollection(); + blog.setId(uriComponentsBuilder.toUriString()); + List mids = messagesService.getUserBlog(user.getUid(), 0, before); + List
articles = messagesService.getMessages(visitor, mids).stream().map(m -> { + Article article = new Article(); + article.setContent(m.getText()); + return article; + }).collect(Collectors.toList()); + OrderedCollectionPage first = new OrderedCollectionPage(); + first.setId(uriComponentsBuilder.toUriString()); + Person person = new Person(); + person.setName(user.getName()); + first.setOrderedItems(articles.stream().map(a -> { + Create create = new Create(); + create.setActor(person); + create.setObject(a); + return create; + }).collect(Collectors.toList())); + int beforeNext = mids.stream().reduce((fst, second) -> second).orElse(0); + if (beforeNext > 0) { + Link next = new Link(); + next.setHref(uriComponentsBuilder.queryParam("before", beforeNext).toUriString()); + first.setNext(next); + } + blog.setFirst(first); + return blog; + } + throw new HttpNotFoundException(); + } +} diff --git a/juick-server/src/main/java/com/juick/server/api/activity/model/ActivityObject.java b/juick-server/src/main/java/com/juick/server/api/activity/model/ActivityObject.java index bc682d2b..dcc94bf7 100644 --- a/juick-server/src/main/java/com/juick/server/api/activity/model/ActivityObject.java +++ b/juick-server/src/main/java/com/juick/server/api/activity/model/ActivityObject.java @@ -4,9 +4,19 @@ import com.fasterxml.jackson.annotation.JsonProperty; public abstract class ActivityObject { - public abstract String getId(); + private String id; - public abstract String getType(); + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public String getType() { + return getClass().getName(); + } @JsonProperty("@context") public String getContext() { diff --git a/juick-server/src/main/java/com/juick/server/api/activity/model/Article.java b/juick-server/src/main/java/com/juick/server/api/activity/model/Article.java new file mode 100644 index 00000000..e117475d --- /dev/null +++ b/juick-server/src/main/java/com/juick/server/api/activity/model/Article.java @@ -0,0 +1,22 @@ +package com.juick.server.api.activity.model; + +public class Article extends ActivityObject { + private String content; + private String attributedTo; + + public String getContent() { + return content; + } + + public void setContent(String content) { + this.content = content; + } + + public String getAttributedTo() { + return attributedTo; + } + + public void setAttributedTo(String attributedTo) { + this.attributedTo = attributedTo; + } +} diff --git a/juick-server/src/main/java/com/juick/server/api/activity/model/Create.java b/juick-server/src/main/java/com/juick/server/api/activity/model/Create.java new file mode 100644 index 00000000..7b4e7b16 --- /dev/null +++ b/juick-server/src/main/java/com/juick/server/api/activity/model/Create.java @@ -0,0 +1,23 @@ +package com.juick.server.api.activity.model; + +public class Create extends ActivityObject { + + private Person actor; + private Article object; + + public Person getActor() { + return actor; + } + + public void setActor(Person actor) { + this.actor = actor; + } + + public Article getObject() { + return object; + } + + public void setObject(Article object) { + this.object = object; + } +} diff --git a/juick-server/src/main/java/com/juick/server/api/activity/model/Link.java b/juick-server/src/main/java/com/juick/server/api/activity/model/Link.java new file mode 100644 index 00000000..b57dabbe --- /dev/null +++ b/juick-server/src/main/java/com/juick/server/api/activity/model/Link.java @@ -0,0 +1,13 @@ +package com.juick.server.api.activity.model; + +public class Link extends ActivityObject { + private String href; + + public String getHref() { + return href; + } + + public void setHref(String href) { + this.href = href; + } +} diff --git a/juick-server/src/main/java/com/juick/server/api/activity/model/OrderedCollection.java b/juick-server/src/main/java/com/juick/server/api/activity/model/OrderedCollection.java new file mode 100644 index 00000000..84654b19 --- /dev/null +++ b/juick-server/src/main/java/com/juick/server/api/activity/model/OrderedCollection.java @@ -0,0 +1,23 @@ +package com.juick.server.api.activity.model; + +public class OrderedCollection extends ActivityObject { + + private int totalItems; + + public int getTotalItems() { + return totalItems; + } + + public void setTotalItems(int totalItems) { + this.totalItems = totalItems; + } + private OrderedCollectionPage first; + + public OrderedCollectionPage getFirst() { + return first; + } + + public void setFirst(OrderedCollectionPage first) { + this.first = first; + } +} diff --git a/juick-server/src/main/java/com/juick/server/api/activity/model/OrderedCollectionPage.java b/juick-server/src/main/java/com/juick/server/api/activity/model/OrderedCollectionPage.java new file mode 100644 index 00000000..9e03cb79 --- /dev/null +++ b/juick-server/src/main/java/com/juick/server/api/activity/model/OrderedCollectionPage.java @@ -0,0 +1,26 @@ +package com.juick.server.api.activity.model; + +import java.util.List; + +public class OrderedCollectionPage extends ActivityObject { + + private Link next; + + private List orderedItems; + + public Link getNext() { + return next; + } + + public void setNext(Link next) { + this.next = next; + } + + public List getOrderedItems() { + return orderedItems; + } + + public void setOrderedItems(List orderedItems) { + this.orderedItems = orderedItems; + } +} 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 031cd73b..7ec94c28 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 @@ -2,16 +2,10 @@ package com.juick.server.api.activity.model; public class Person extends ActivityObject { - private String id; private String name; private String icon; private String outbox; - @Override - public String getId() { - return id; - } - @Override public String getType() { return "Person"; @@ -40,8 +34,4 @@ public class Person extends ActivityObject { public void setOutbox(String outbox) { this.outbox = outbox; } - - public void setId(String id) { - this.id = id; - } } -- cgit v1.2.3