Comments are displayed under Essays
This commit is contained in:
parent
b766952f4c
commit
f6f0dc086c
5 changed files with 161 additions and 40 deletions
|
@ -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<EssayistSession> sessions;
|
||||||
|
private final Provider<Routes> routes;
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
public CommentsServlet(Provider<Routes> routes, Provider<EssayistSession> 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));
|
||||||
|
}
|
||||||
|
}
|
|
@ -3,12 +3,14 @@ package com.moandjiezana.tent.essayist;
|
||||||
import com.google.common.base.Throwables;
|
import com.google.common.base.Throwables;
|
||||||
import com.moandjiezana.tent.client.TentClient;
|
import com.moandjiezana.tent.client.TentClient;
|
||||||
import com.moandjiezana.tent.client.posts.Post;
|
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.client.users.Profile;
|
||||||
import com.moandjiezana.tent.essayist.security.Csrf;
|
import com.moandjiezana.tent.essayist.security.Csrf;
|
||||||
import com.moandjiezana.tent.essayist.tent.Entities;
|
import com.moandjiezana.tent.essayist.tent.Entities;
|
||||||
import com.moandjiezana.tent.essayist.tent.EssayistPostContent;
|
import com.moandjiezana.tent.essayist.tent.EssayistPostContent;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
import javax.inject.Inject;
|
import javax.inject.Inject;
|
||||||
import javax.inject.Provider;
|
import javax.inject.Provider;
|
||||||
|
@ -40,30 +42,40 @@ public class EssayServlet extends HttpServlet {
|
||||||
String authorEntity = Entities.expandFromUrl(parts[0]);
|
String authorEntity = Entities.expandFromUrl(parts[0]);
|
||||||
String essayId = parts[1];
|
String essayId = parts[1];
|
||||||
|
|
||||||
User user = users.getByEntityOrNull(authorEntity);
|
User author = users.getByEntityOrNull(authorEntity);
|
||||||
|
|
||||||
TentClient authorTentClient;
|
TentClient tentClient;
|
||||||
if (user != null) {
|
|
||||||
authorTentClient = new TentClient(user.getProfile());
|
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 {
|
} else {
|
||||||
authorTentClient = new TentClient(authorEntity);
|
tentClient = new TentClient(authorEntity);
|
||||||
authorTentClient.discover();
|
tentClient.discover();
|
||||||
Profile profile = authorTentClient.getProfile();
|
Profile profile = tentClient.getProfile();
|
||||||
user = new User(profile, null);
|
author = new User(profile);
|
||||||
users.save(user);
|
users.save(author);
|
||||||
}
|
}
|
||||||
|
|
||||||
Post post = authorTentClient.getPost(essayId);
|
Post post = tentClient.getPost(essayId);
|
||||||
|
|
||||||
EssayistPostContent essayContent = post.getContentAs(EssayistPostContent.class);
|
EssayistPostContent essayContent = post.getContentAs(EssayistPostContent.class);
|
||||||
essayContent.setBody(csrf.stripScripts(essayContent.getBody()));
|
essayContent.setBody(csrf.stripScripts(essayContent.getBody()));
|
||||||
|
|
||||||
EssayTemplate essayPage = templates.essay();
|
EssayTemplate essayPage = templates.essay();
|
||||||
if (sessions.get().getUser().owns(post)) {
|
if (user.owns(post)) {
|
||||||
essayPage.setActive("Written");
|
essayPage.setActive("Written");
|
||||||
}
|
}
|
||||||
|
|
||||||
essayPage.render(resp.getWriter(), post, user.getProfile());
|
tentClient.getAsync().setAccessToken(author.getAccessToken());
|
||||||
|
tentClient.getAsync().setRegistrationResponse(author.getRegistration());
|
||||||
|
List<Post> comments = tentClient.getPosts(new PostQuery().mentionedPost(essayId));
|
||||||
|
|
||||||
|
essayPage.render(resp.getWriter(), post, author.getProfile(), comments);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -91,4 +103,9 @@ public class EssayServlet extends HttpServlet {
|
||||||
|
|
||||||
resp.sendRedirect(req.getContextPath() + "/" + authorEntity + "/essays");
|
resp.sendRedirect(req.getContextPath() + "/" + authorEntity + "/essays");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,8 +4,6 @@ import com.google.common.base.Throwables;
|
||||||
import com.moandjiezana.tent.client.TentClientAsync;
|
import com.moandjiezana.tent.client.TentClientAsync;
|
||||||
import com.moandjiezana.tent.client.posts.Post;
|
import com.moandjiezana.tent.client.posts.Post;
|
||||||
import com.moandjiezana.tent.client.posts.PostQuery;
|
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.ArrayList;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
|
@ -25,32 +23,26 @@ public class Essays {
|
||||||
public List<Post> getEssays(List<User> users) {
|
public List<Post> getEssays(List<User> users) {
|
||||||
CopyOnWriteArrayList<Future<List<Post>>> futurePosts = new CopyOnWriteArrayList<Future<List<Post>>>();
|
CopyOnWriteArrayList<Future<List<Post>>> futurePosts = new CopyOnWriteArrayList<Future<List<Post>>>();
|
||||||
|
|
||||||
AsyncHttpClient httpClient = new AsyncHttpClient(new JDKAsyncHttpProvider(TentClientAsync.getDefaultAsyncHttpClientConfigBuilder().build()));
|
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"))));
|
||||||
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<Post> posts = new ArrayList<Post>(futurePosts.size());
|
|
||||||
for (Future<List<Post>> futurePost : futurePosts) {
|
|
||||||
try {
|
|
||||||
posts.addAll(futurePost.get());
|
|
||||||
} catch (Exception e) {
|
|
||||||
LOGGER.error("Could not load Post", Throwables.getRootCause(e));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Collections.sort(posts, new Comparator<Post>() {
|
|
||||||
@Override
|
|
||||||
public int compare(Post post1, Post post2) {
|
|
||||||
return new Date(post2.getPublishedAt()).compareTo(new Date(post1.getPublishedAt()));
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
return posts;
|
|
||||||
} finally {
|
|
||||||
httpClient.close();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
List<Post> posts = new ArrayList<Post>(futurePosts.size());
|
||||||
|
for (Future<List<Post>> futurePost : futurePosts) {
|
||||||
|
try {
|
||||||
|
posts.addAll(futurePost.get());
|
||||||
|
} catch (Exception e) {
|
||||||
|
LOGGER.error("Could not load Post", Throwables.getRootCause(e));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Collections.sort(posts, new Comparator<Post>() {
|
||||||
|
@Override
|
||||||
|
public int compare(Post post1, Post post2) {
|
||||||
|
return new Date(post2.getPublishedAt()).compareTo(new Date(post1.getPublishedAt()));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
return posts;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,6 +11,7 @@ import com.google.inject.servlet.GuiceServletContextListener;
|
||||||
import com.google.inject.servlet.ServletModule;
|
import com.google.inject.servlet.ServletModule;
|
||||||
import com.moandjiezana.tent.client.internal.com.google.common.base.Throwables;
|
import com.moandjiezana.tent.client.internal.com.google.common.base.Throwables;
|
||||||
import com.moandjiezana.tent.essayist.AccessTokenServlet;
|
import com.moandjiezana.tent.essayist.AccessTokenServlet;
|
||||||
|
import com.moandjiezana.tent.essayist.CommentsServlet;
|
||||||
import com.moandjiezana.tent.essayist.EssayServlet;
|
import com.moandjiezana.tent.essayist.EssayServlet;
|
||||||
import com.moandjiezana.tent.essayist.EssaysServlet;
|
import com.moandjiezana.tent.essayist.EssaysServlet;
|
||||||
import com.moandjiezana.tent.essayist.GlobalFeedServlet;
|
import com.moandjiezana.tent.essayist.GlobalFeedServlet;
|
||||||
|
@ -97,6 +98,7 @@ public class EssayistServletContextListener extends GuiceServletContextListener
|
||||||
serve("/global").with(GlobalFeedServlet.class);
|
serve("/global").with(GlobalFeedServlet.class);
|
||||||
serve("/write").with(NewEssayServlet.class);
|
serve("/write").with(NewEssayServlet.class);
|
||||||
serveRegex("/(.*)/essays").with(EssaysServlet.class);
|
serveRegex("/(.*)/essays").with(EssaysServlet.class);
|
||||||
|
serveRegex("/(.*)/essay/(.*)/comment").with(CommentsServlet.class);
|
||||||
serveRegex("/(.*)/essay/(.*)").with(EssayServlet.class);
|
serveRegex("/(.*)/essay/(.*)").with(EssayServlet.class);
|
||||||
filter("/*").through(Utf8Filter.class);
|
filter("/*").through(Utf8Filter.class);
|
||||||
filter("/*").through(HttpMethodFilter.class);
|
filter("/*").through(HttpMethodFilter.class);
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
com.moandjiezana.tent.client.users.Profile;
|
com.moandjiezana.tent.client.users.Profile;
|
||||||
com.moandjiezana.tent.client.posts.*;
|
com.moandjiezana.tent.client.posts.*;
|
||||||
com.moandjiezana.tent.client.posts.content.EssayContent;
|
com.moandjiezana.tent.client.posts.content.EssayContent;
|
||||||
|
com.moandjiezana.tent.client.posts.content.StatusContent;
|
||||||
com.moandjiezana.tent.essayist.tent.*;
|
com.moandjiezana.tent.essayist.tent.*;
|
||||||
java.text.SimpleDateFormat;
|
java.text.SimpleDateFormat;
|
||||||
java.util.*;
|
java.util.*;
|
||||||
|
@ -10,10 +11,12 @@ java.util.*;
|
||||||
Post essay;
|
Post essay;
|
||||||
Profile profile;
|
Profile profile;
|
||||||
String active = "My Feed";
|
String active = "My Feed";
|
||||||
|
List<Post> comments;
|
||||||
</%args>
|
</%args>
|
||||||
<%java>
|
<%java>
|
||||||
final EssayContent content = essay.getContentAs(EssayContent.class);
|
final EssayContent content = essay.getContentAs(EssayContent.class);
|
||||||
final SimpleDateFormat dateFormat = new SimpleDateFormat("dd MMMM yyyy");
|
final SimpleDateFormat dateFormat = new SimpleDateFormat("dd MMMM yyyy");
|
||||||
|
final SimpleDateFormat dateTimeFormat = new SimpleDateFormat("dd MMMM yyyy 'at' HH:mm ZZZZ");
|
||||||
</%java>
|
</%java>
|
||||||
<&| Layout; active = active &>
|
<&| Layout; active = active &>
|
||||||
<h1><% content.getTitle() %> <small><a href="<% jamonContext.currentUrl %>" style="text-decoration: none"><% Character.valueOf('\u2693') %></a></small></h1>
|
<h1><% content.getTitle() %> <small><a href="<% jamonContext.currentUrl %>" style="text-decoration: none"><% Character.valueOf('\u2693') %></a></small></h1>
|
||||||
|
@ -21,4 +24,38 @@ final SimpleDateFormat dateFormat = new SimpleDateFormat("dd MMMM yyyy");
|
||||||
<div class="bodyText">
|
<div class="bodyText">
|
||||||
<% content.getBody() #n %>
|
<% content.getBody() #n %>
|
||||||
</div>
|
</div>
|
||||||
|
<%if jamonContext.isLoggedIn() || !comments.isEmpty() %>
|
||||||
|
<div class="row">
|
||||||
|
<div class="span12">
|
||||||
|
<h3>Comments</h3>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</%if>
|
||||||
|
<%if jamonContext.isLoggedIn() %>
|
||||||
|
<form action="<% jamonContext.routes.comment(essay) %>" method="post">
|
||||||
|
<div class="row">
|
||||||
|
<div class="span6">
|
||||||
|
<textarea name="comment" rows="3" required="required" class="span6"></textarea>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div class="row">
|
||||||
|
<div class="span6">
|
||||||
|
<input type="submit" value="Comment" class="btn pull-right" />
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</form>
|
||||||
|
</%if>
|
||||||
|
|
||||||
|
<%if !comments.isEmpty() %>
|
||||||
|
<div class="row">
|
||||||
|
<%for Post comment : comments %>
|
||||||
|
<div class="span12">
|
||||||
|
<strong><% Entities.stripScheme(comment.getEntity()) %></strong> <% dateTimeFormat.format(new Date(comment.getPublishedAt() * 1000)) %><br/>
|
||||||
|
<p>
|
||||||
|
<% comment.getContentAs(StatusContent.class).getText() %>
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
</%for>
|
||||||
|
</div>
|
||||||
|
</%if>
|
||||||
</&>
|
</&>
|
Loading…
Add table
Add a link
Reference in a new issue