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.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<Post> 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 {
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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<Post> getEssays(List<User> users) {
|
||||
CopyOnWriteArrayList<Future<List<Post>>> futurePosts = new CopyOnWriteArrayList<Future<List<Post>>>();
|
||||
|
||||
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<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();
|
||||
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<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.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);
|
||||
|
|
|
@ -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<Post> comments;
|
||||
</%args>
|
||||
<%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");
|
||||
</%java>
|
||||
<&| Layout; active = active &>
|
||||
<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">
|
||||
<% content.getBody() #n %>
|
||||
</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