diff options
author | Vitaly Takmazov | 2018-09-04 13:52:10 +0300 |
---|---|---|
committer | Vitaly Takmazov | 2018-09-04 13:52:10 +0300 |
commit | a63800dd6c5f8551b7126be6d3557e90888f0703 (patch) | |
tree | d037c0498f3e2f38f6147f20717c27f68353bea5 /juick-server | |
parent | 2f1b853bc35f14ac61038b556d21e1d0190fee8a (diff) |
outbox as OrderedCollection
Diffstat (limited to 'juick-server')
10 files changed, 218 insertions, 59 deletions
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<Integer> mids = messagesService.getUserBlog(user.getUid(), 0, before); + List<Article> 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<? extends ActivityObject> orderedItems; + + public Link getNext() { + return next; + } + + public void setNext(Link next) { + this.next = next; + } + + public List<? extends ActivityObject> getOrderedItems() { + return orderedItems; + } + + public void setOrderedItems(List<? extends ActivityObject> 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,17 +2,11 @@ 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; - } } diff --git a/juick-server/src/test/java/com/juick/server/tests/ServerTests.java b/juick-server/src/test/java/com/juick/server/tests/ServerTests.java index 63ba4b3e..a94acc82 100644 --- a/juick-server/src/test/java/com/juick/server/tests/ServerTests.java +++ b/juick-server/src/test/java/com/juick/server/tests/ServerTests.java @@ -1324,7 +1324,7 @@ public class ServerTests { } @Test - public void accountUrlsShouldBeExposedOverWebfinger() throws Exception { + public void accountUrlShouldBeExposedOverWebfinger() throws Exception { mockMvc.perform(get("/.well-known/webfinger?resource=acct:ugnich@localhost")) .andExpect(status().isOk()) .andExpect(jsonPath("$.subject", is("acct:ugnich@localhost"))) @@ -1334,10 +1334,18 @@ public class ServerTests { .andExpect(status().isNotFound()); } @Test - public void userProfilesShouldBeExposedAsActivityStream() throws Exception { + public void userProfileAndBlogShouldBeExposedAsActivityStream() throws Exception { mockMvc.perform(get("/u/ugnich").accept(ActivityObject.CONTENT_TYPE)) .andExpect(status().isOk()) .andExpect(jsonPath("$.@context", is(ActivityObject.CONTEXT_URI))) .andExpect(jsonPath("$.icon", is("http://localhost:8080/i/a/1.png"))); + jdbcTemplate.execute("DELETE FROM messages"); + IntStream.rangeClosed(1, 30).forEach(i -> { + messagesService.createMessage(ugnich.getUid(), String.format("message %d", i), null, null); + }); + mockMvc.perform(get("/u/ugnich/blog").accept(ActivityObject.CONTENT_TYPE)) + .andExpect(status().isOk()) + .andExpect(jsonPath("$.first.orderedItems", hasSize(20))) + .andExpect(jsonPath("$.first.next.href", is("http://localhost:8080/u/ugnich/blog?before=11"))); } } |