diff --git a/src/main/java/com/moandjiezana/tent/essayist/CommentsServlet.java b/src/main/java/com/moandjiezana/tent/essayist/CommentsServlet.java new file mode 100644 index 0000000..716a86f --- /dev/null +++ b/src/main/java/com/moandjiezana/tent/essayist/CommentsServlet.java @@ -0,0 +1,73 @@ +package com.moandjiezana.tent.essayist; + +import com.google.common.base.Strings; +import com.moandjiezana.tent.client.TentClient; +import com.moandjiezana.tent.client.posts.Mention; +import com.moandjiezana.tent.client.posts.Post; +import com.moandjiezana.tent.client.posts.content.StatusContent; +import com.moandjiezana.tent.client.users.Permissions; +import com.moandjiezana.tent.essayist.auth.Authenticated; +import com.moandjiezana.tent.essayist.config.Routes; +import com.moandjiezana.tent.essayist.tent.Entities; + +import java.io.IOException; + +import javax.inject.Inject; +import javax.inject.Provider; +import javax.inject.Singleton; +import javax.servlet.ServletException; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +@Singleton +@Authenticated +public class CommentsServlet extends HttpServlet { + + private final Provider sessions; + private final Provider routes; + + @Inject + public CommentsServlet(Provider routes, Provider sessions) { + this.routes = routes; + this.sessions = sessions; + } + + @Override + protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + String[] parts = req.getPathInfo().split("/essay/"); + String authorEntity = Entities.expandFromUrl(parts[0]); + String essayId = parts[1].split("/")[0]; + + String commentText = req.getParameter("comment"); + + Post essay = new Post(); + essay.setEntity(authorEntity); + essay.setId(essayId); + + if (Strings.isNullOrEmpty(commentText)) { + resp.sendRedirect(routes.get().essay(essay)); + + return; + } + + User user = sessions.get().getUser(); + TentClient tentClient = new TentClient(user.getProfile()); + tentClient.getAsync().setAccessToken(user.getAccessToken()); + tentClient.getAsync().setRegistrationResponse(user.getRegistration()); + + Post comment = new Post(); + comment.setEntity(user.getProfile().getCore().getEntity()); + commentText = commentText.substring(0, Math.min(commentText.length(), 256)); + comment.setContent(new StatusContent(commentText)); + comment.setMentions(new Mention[] { new Mention(authorEntity, essayId) }); + Permissions permissions = new Permissions(); + permissions.setPublic(true); + comment.setPermissions(permissions); + comment.setLicenses(new String[] { "http://creativecommons.org/licenses/by/3.0/" }); + + tentClient.write(comment); + + resp.sendRedirect(routes.get().essay(essay)); + } +} diff --git a/src/main/java/com/moandjiezana/tent/essayist/EssayServlet.java b/src/main/java/com/moandjiezana/tent/essayist/EssayServlet.java index aa5c4eb..71f56c1 100644 --- a/src/main/java/com/moandjiezana/tent/essayist/EssayServlet.java +++ b/src/main/java/com/moandjiezana/tent/essayist/EssayServlet.java @@ -3,12 +3,14 @@ package com.moandjiezana.tent.essayist; import com.google.common.base.Throwables; import com.moandjiezana.tent.client.TentClient; import com.moandjiezana.tent.client.posts.Post; +import com.moandjiezana.tent.client.posts.PostQuery; import com.moandjiezana.tent.client.users.Profile; import com.moandjiezana.tent.essayist.security.Csrf; import com.moandjiezana.tent.essayist.tent.Entities; import com.moandjiezana.tent.essayist.tent.EssayistPostContent; import java.io.IOException; +import java.util.List; import javax.inject.Inject; import javax.inject.Provider; @@ -40,30 +42,40 @@ public class EssayServlet extends HttpServlet { String authorEntity = Entities.expandFromUrl(parts[0]); String essayId = parts[1]; - User user = users.getByEntityOrNull(authorEntity); + User author = users.getByEntityOrNull(authorEntity); - TentClient authorTentClient; - if (user != null) { - authorTentClient = new TentClient(user.getProfile()); + TentClient tentClient; + + User user = sessions.get().getUser(); + /*if (sessions.get().isLoggedIn()) { + tentClient = new TentClient(user.getProfile()); + tentClient.getAsync().setAccessToken(user.getAccessToken()); + tentClient.getAsync().setRegistrationResponse(user.getRegistration()); + } else */if (author != null) { + tentClient = new TentClient(author.getProfile()); } else { - authorTentClient = new TentClient(authorEntity); - authorTentClient.discover(); - Profile profile = authorTentClient.getProfile(); - user = new User(profile, null); - users.save(user); + tentClient = new TentClient(authorEntity); + tentClient.discover(); + Profile profile = tentClient.getProfile(); + author = new User(profile); + users.save(author); } - Post post = authorTentClient.getPost(essayId); + Post post = tentClient.getPost(essayId); EssayistPostContent essayContent = post.getContentAs(EssayistPostContent.class); essayContent.setBody(csrf.stripScripts(essayContent.getBody())); EssayTemplate essayPage = templates.essay(); - if (sessions.get().getUser().owns(post)) { + if (user.owns(post)) { essayPage.setActive("Written"); } - essayPage.render(resp.getWriter(), post, user.getProfile()); + tentClient.getAsync().setAccessToken(author.getAccessToken()); + tentClient.getAsync().setRegistrationResponse(author.getRegistration()); + List comments = tentClient.getPosts(new PostQuery().mentionedPost(essayId)); + + essayPage.render(resp.getWriter(), post, author.getProfile(), comments); } @Override @@ -91,4 +103,9 @@ public class EssayServlet extends HttpServlet { resp.sendRedirect(req.getContextPath() + "/" + authorEntity + "/essays"); } + + @Override + protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + + } } diff --git a/src/main/java/com/moandjiezana/tent/essayist/Essays.java b/src/main/java/com/moandjiezana/tent/essayist/Essays.java index 57d57f8..5093e18 100644 --- a/src/main/java/com/moandjiezana/tent/essayist/Essays.java +++ b/src/main/java/com/moandjiezana/tent/essayist/Essays.java @@ -4,8 +4,6 @@ import com.google.common.base.Throwables; import com.moandjiezana.tent.client.TentClientAsync; import com.moandjiezana.tent.client.posts.Post; import com.moandjiezana.tent.client.posts.PostQuery; -import com.ning.http.client.AsyncHttpClient; -import com.ning.http.client.providers.jdk.JDKAsyncHttpProvider; import java.util.ArrayList; import java.util.Collections; @@ -25,32 +23,26 @@ public class Essays { public List getEssays(List users) { CopyOnWriteArrayList>> futurePosts = new CopyOnWriteArrayList>>(); - AsyncHttpClient httpClient = new AsyncHttpClient(new JDKAsyncHttpProvider(TentClientAsync.getDefaultAsyncHttpClientConfigBuilder().build())); - - try { - for (User user : users) { - futurePosts.add(new TentClientAsync(user.getProfile(), httpClient).getPosts(new PostQuery().entity(user.getProfile().getCore().getEntity()).postTypes(Post.Types.essay("v0.1.0")))); - } - - List posts = new ArrayList(futurePosts.size()); - for (Future> futurePost : futurePosts) { - try { - posts.addAll(futurePost.get()); - } catch (Exception e) { - LOGGER.error("Could not load Post", Throwables.getRootCause(e)); - } - } - - Collections.sort(posts, new Comparator() { - @Override - public int compare(Post post1, Post post2) { - return new Date(post2.getPublishedAt()).compareTo(new Date(post1.getPublishedAt())); - } - }); - - return posts; - } finally { - httpClient.close(); + for (User user : users) { + futurePosts.add(new TentClientAsync(user.getProfile()).getPosts(new PostQuery().entity(user.getProfile().getCore().getEntity()).postTypes(Post.Types.essay("v0.1.0")))); } + + List posts = new ArrayList(futurePosts.size()); + for (Future> futurePost : futurePosts) { + try { + posts.addAll(futurePost.get()); + } catch (Exception e) { + LOGGER.error("Could not load Post", Throwables.getRootCause(e)); + } + } + + Collections.sort(posts, new Comparator() { + @Override + public int compare(Post post1, Post post2) { + return new Date(post2.getPublishedAt()).compareTo(new Date(post1.getPublishedAt())); + } + }); + + return posts; } } diff --git a/src/main/java/com/moandjiezana/tent/essayist/config/EssayistServletContextListener.java b/src/main/java/com/moandjiezana/tent/essayist/config/EssayistServletContextListener.java index db88d31..c22296b 100644 --- a/src/main/java/com/moandjiezana/tent/essayist/config/EssayistServletContextListener.java +++ b/src/main/java/com/moandjiezana/tent/essayist/config/EssayistServletContextListener.java @@ -11,6 +11,7 @@ import com.google.inject.servlet.GuiceServletContextListener; import com.google.inject.servlet.ServletModule; import com.moandjiezana.tent.client.internal.com.google.common.base.Throwables; import com.moandjiezana.tent.essayist.AccessTokenServlet; +import com.moandjiezana.tent.essayist.CommentsServlet; import com.moandjiezana.tent.essayist.EssayServlet; import com.moandjiezana.tent.essayist.EssaysServlet; import com.moandjiezana.tent.essayist.GlobalFeedServlet; @@ -97,6 +98,7 @@ public class EssayistServletContextListener extends GuiceServletContextListener serve("/global").with(GlobalFeedServlet.class); serve("/write").with(NewEssayServlet.class); serveRegex("/(.*)/essays").with(EssaysServlet.class); + serveRegex("/(.*)/essay/(.*)/comment").with(CommentsServlet.class); serveRegex("/(.*)/essay/(.*)").with(EssayServlet.class); filter("/*").through(Utf8Filter.class); filter("/*").through(HttpMethodFilter.class); diff --git a/src/main/templates/com/moandjiezana/tent/essayist/EssayTemplate.jamon b/src/main/templates/com/moandjiezana/tent/essayist/EssayTemplate.jamon index d1c4723..837eb8d 100644 --- a/src/main/templates/com/moandjiezana/tent/essayist/EssayTemplate.jamon +++ b/src/main/templates/com/moandjiezana/tent/essayist/EssayTemplate.jamon @@ -2,6 +2,7 @@ com.moandjiezana.tent.client.users.Profile; com.moandjiezana.tent.client.posts.*; com.moandjiezana.tent.client.posts.content.EssayContent; +com.moandjiezana.tent.client.posts.content.StatusContent; com.moandjiezana.tent.essayist.tent.*; java.text.SimpleDateFormat; java.util.*; @@ -10,10 +11,12 @@ java.util.*; Post essay; Profile profile; String active = "My Feed"; +List comments; <%java> final EssayContent content = essay.getContentAs(EssayContent.class); final SimpleDateFormat dateFormat = new SimpleDateFormat("dd MMMM yyyy"); +final SimpleDateFormat dateTimeFormat = new SimpleDateFormat("dd MMMM yyyy 'at' HH:mm ZZZZ"); <&| Layout; active = active &>

<% content.getTitle() %> <% Character.valueOf('\u2693') %>

@@ -21,4 +24,38 @@ final SimpleDateFormat dateFormat = new SimpleDateFormat("dd MMMM yyyy");
<% content.getBody() #n %>
+ <%if jamonContext.isLoggedIn() || !comments.isEmpty() %> +
+
+

Comments

+
+
+ + <%if jamonContext.isLoggedIn() %> +
+
+
+ +
+
+
+
+ +
+
+
+ + + <%if !comments.isEmpty() %> +
+ <%for Post comment : comments %> +
+ <% Entities.stripScheme(comment.getEntity()) %> <% dateTimeFormat.format(new Date(comment.getPublishedAt() * 1000)) %>
+

+ <% comment.getContentAs(StatusContent.class).getText() %> +

+
+ +
+ \ No newline at end of file