mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-19 22:29:53 +01:00
Add basic support for listing and writing blog posts
This commit is contained in:
@@ -9,7 +9,9 @@ import java.util.Scanner;
|
|||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
import javax.annotation.ParametersAreNonnullByDefault;
|
import javax.annotation.ParametersAreNonnullByDefault;
|
||||||
|
import javax.annotation.concurrent.Immutable;
|
||||||
import javax.inject.Inject;
|
import javax.inject.Inject;
|
||||||
|
import javax.inject.Singleton;
|
||||||
|
|
||||||
import static java.lang.System.console;
|
import static java.lang.System.console;
|
||||||
import static java.lang.System.err;
|
import static java.lang.System.err;
|
||||||
@@ -18,6 +20,8 @@ import static java.lang.System.out;
|
|||||||
import static java.util.logging.Logger.getLogger;
|
import static java.util.logging.Logger.getLogger;
|
||||||
import static org.briarproject.bramble.api.identity.AuthorConstants.MAX_AUTHOR_NAME_LENGTH;
|
import static org.briarproject.bramble.api.identity.AuthorConstants.MAX_AUTHOR_NAME_LENGTH;
|
||||||
|
|
||||||
|
@Immutable
|
||||||
|
@Singleton
|
||||||
@MethodsNotNullByDefault
|
@MethodsNotNullByDefault
|
||||||
@ParametersAreNonnullByDefault
|
@ParametersAreNonnullByDefault
|
||||||
public class BriarService {
|
public class BriarService {
|
||||||
|
|||||||
@@ -1,20 +1,36 @@
|
|||||||
package org.briarproject.briar.headless;
|
package org.briarproject.briar.headless;
|
||||||
|
|
||||||
|
import org.briarproject.bramble.api.nullsafety.MethodsNotNullByDefault;
|
||||||
|
import org.briarproject.briar.headless.blogs.BlogController;
|
||||||
|
import org.briarproject.briar.headless.forums.ForumController;
|
||||||
|
|
||||||
|
import javax.annotation.ParametersAreNonnullByDefault;
|
||||||
|
import javax.annotation.concurrent.Immutable;
|
||||||
import javax.inject.Inject;
|
import javax.inject.Inject;
|
||||||
import javax.inject.Singleton;
|
import javax.inject.Singleton;
|
||||||
|
|
||||||
import io.javalin.Javalin;
|
import io.javalin.Javalin;
|
||||||
|
|
||||||
|
import static io.javalin.ApiBuilder.get;
|
||||||
|
import static io.javalin.ApiBuilder.path;
|
||||||
|
import static io.javalin.ApiBuilder.post;
|
||||||
|
|
||||||
|
@Immutable
|
||||||
@Singleton
|
@Singleton
|
||||||
|
@MethodsNotNullByDefault
|
||||||
|
@ParametersAreNonnullByDefault
|
||||||
public class Router {
|
public class Router {
|
||||||
|
|
||||||
private final BriarService briarService;
|
private final BriarService briarService;
|
||||||
private final ForumController forumController;
|
private final ForumController forumController;
|
||||||
|
private final BlogController blogController;
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
public Router(BriarService briarService, ForumController forumController) {
|
public Router(BriarService briarService, ForumController forumController,
|
||||||
|
BlogController blogController) {
|
||||||
this.briarService = briarService;
|
this.briarService = briarService;
|
||||||
this.forumController = forumController;
|
this.forumController = forumController;
|
||||||
|
this.blogController = blogController;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void start() {
|
public void start() {
|
||||||
@@ -24,9 +40,20 @@ public class Router {
|
|||||||
.port(7000)
|
.port(7000)
|
||||||
.disableStartupBanner()
|
.disableStartupBanner()
|
||||||
.enableStandardRequestLogging()
|
.enableStandardRequestLogging()
|
||||||
|
.enableRouteOverview("/")
|
||||||
|
.enableDynamicGzip()
|
||||||
.start();
|
.start();
|
||||||
app.get("/forums", forumController::list);
|
|
||||||
app.post("/forums", forumController::create);
|
app.routes(() -> {
|
||||||
|
path("/forums", () -> {
|
||||||
|
get(forumController::list);
|
||||||
|
post(forumController::create);
|
||||||
|
});
|
||||||
|
path("/blogs", () -> path("/posts", () -> {
|
||||||
|
get(blogController::listPosts);
|
||||||
|
post(blogController::createPost);
|
||||||
|
}));
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,81 @@
|
|||||||
|
package org.briarproject.briar.headless.blogs;
|
||||||
|
|
||||||
|
import org.briarproject.bramble.api.FormatException;
|
||||||
|
import org.briarproject.bramble.api.db.DbException;
|
||||||
|
import org.briarproject.bramble.api.identity.IdentityManager;
|
||||||
|
import org.briarproject.bramble.api.identity.LocalAuthor;
|
||||||
|
import org.briarproject.bramble.api.nullsafety.MethodsNotNullByDefault;
|
||||||
|
import org.briarproject.bramble.api.system.Clock;
|
||||||
|
import org.briarproject.briar.api.blog.Blog;
|
||||||
|
import org.briarproject.briar.api.blog.BlogManager;
|
||||||
|
import org.briarproject.briar.api.blog.BlogPost;
|
||||||
|
import org.briarproject.briar.api.blog.BlogPostFactory;
|
||||||
|
import org.briarproject.briar.api.blog.BlogPostHeader;
|
||||||
|
|
||||||
|
import java.security.GeneralSecurityException;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collection;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import javax.annotation.ParametersAreNonnullByDefault;
|
||||||
|
import javax.annotation.concurrent.Immutable;
|
||||||
|
import javax.inject.Inject;
|
||||||
|
import javax.inject.Singleton;
|
||||||
|
|
||||||
|
import io.javalin.Context;
|
||||||
|
|
||||||
|
@Immutable
|
||||||
|
@Singleton
|
||||||
|
@MethodsNotNullByDefault
|
||||||
|
@ParametersAreNonnullByDefault
|
||||||
|
public class BlogController {
|
||||||
|
|
||||||
|
private final BlogManager blogManager;
|
||||||
|
private final BlogPostFactory blogPostFactory;
|
||||||
|
private final IdentityManager identityManager;
|
||||||
|
private final Clock clock;
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
public BlogController(BlogManager blogManager,
|
||||||
|
BlogPostFactory blogPostFactory, IdentityManager identityManager,
|
||||||
|
Clock clock) {
|
||||||
|
this.blogManager = blogManager;
|
||||||
|
this.blogPostFactory = blogPostFactory;
|
||||||
|
this.identityManager = identityManager;
|
||||||
|
this.clock = clock;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Context listPosts(Context ctx) throws DbException {
|
||||||
|
List<OutputBlogPost> posts = new ArrayList<>();
|
||||||
|
for (Blog b : blogManager.getBlogs()) {
|
||||||
|
Collection<BlogPostHeader> headers =
|
||||||
|
blogManager.getPostHeaders(b.getId());
|
||||||
|
for (BlogPostHeader header : headers) {
|
||||||
|
String body = blogManager.getPostBody(header.getId());
|
||||||
|
OutputBlogPost post = new OutputBlogPost(header, body);
|
||||||
|
posts.add(post);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ctx.json(posts);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Context createPost(Context ctx)
|
||||||
|
throws DbException, GeneralSecurityException, FormatException {
|
||||||
|
String text = ctx.formParam("text");
|
||||||
|
if (text == null || text.length() < 1) {
|
||||||
|
return ctx.status(500).result("Expecting Blog text");
|
||||||
|
} else {
|
||||||
|
LocalAuthor author = identityManager.getLocalAuthor();
|
||||||
|
Blog blog = blogManager.getPersonalBlog(author);
|
||||||
|
long now = clock.currentTimeMillis();
|
||||||
|
BlogPost post = blogPostFactory
|
||||||
|
.createBlogPost(blog.getId(), now, null, author, text);
|
||||||
|
blogManager.addLocalPost(post);
|
||||||
|
BlogPostHeader header = blogManager
|
||||||
|
.getPostHeader(blog.getId(), post.getMessage().getId());
|
||||||
|
OutputBlogPost outputPost = new OutputBlogPost(header, text);
|
||||||
|
return ctx.json(outputPost);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,38 @@
|
|||||||
|
package org.briarproject.briar.headless.blogs;
|
||||||
|
|
||||||
|
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||||
|
import org.briarproject.bramble.identity.OutputAuthor;
|
||||||
|
import org.briarproject.briar.api.blog.BlogPostHeader;
|
||||||
|
|
||||||
|
import javax.annotation.Nullable;
|
||||||
|
import javax.annotation.concurrent.Immutable;
|
||||||
|
|
||||||
|
@Immutable
|
||||||
|
@NotNullByDefault
|
||||||
|
@SuppressWarnings("WeakerAccess")
|
||||||
|
class OutputBlogPost {
|
||||||
|
|
||||||
|
public final String body;
|
||||||
|
public final OutputAuthor author;
|
||||||
|
public final String authorStatus, type;
|
||||||
|
public final byte[] id;
|
||||||
|
@Nullable
|
||||||
|
public final byte[] parentId;
|
||||||
|
public final boolean read, rssFeed;
|
||||||
|
public final long timestamp, timestampReceived;
|
||||||
|
|
||||||
|
OutputBlogPost(BlogPostHeader header, String body) {
|
||||||
|
this.body = body;
|
||||||
|
this.author = new OutputAuthor(header.getAuthor());
|
||||||
|
this.authorStatus = header.getAuthorStatus().name().toLowerCase();
|
||||||
|
this.type = header.getType().name().toLowerCase();
|
||||||
|
this.id = header.getId().getBytes();
|
||||||
|
this.parentId = header.getParentId() == null ? null :
|
||||||
|
header.getParentId().getBytes();
|
||||||
|
this.read = header.isRead();
|
||||||
|
this.rssFeed = header.isRssFeed();
|
||||||
|
this.timestamp = header.getTimestamp();
|
||||||
|
this.timestampReceived = header.getTimeReceived();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -1,17 +1,25 @@
|
|||||||
package org.briarproject.briar.headless;
|
package org.briarproject.briar.headless.forums;
|
||||||
|
|
||||||
import org.briarproject.bramble.api.db.DbException;
|
import org.briarproject.bramble.api.db.DbException;
|
||||||
|
import org.briarproject.bramble.api.nullsafety.MethodsNotNullByDefault;
|
||||||
import org.briarproject.briar.api.forum.Forum;
|
import org.briarproject.briar.api.forum.Forum;
|
||||||
import org.briarproject.briar.api.forum.ForumManager;
|
import org.briarproject.briar.api.forum.ForumManager;
|
||||||
|
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
|
|
||||||
|
import javax.annotation.ParametersAreNonnullByDefault;
|
||||||
|
import javax.annotation.concurrent.Immutable;
|
||||||
import javax.inject.Inject;
|
import javax.inject.Inject;
|
||||||
|
import javax.inject.Singleton;
|
||||||
|
|
||||||
import io.javalin.Context;
|
import io.javalin.Context;
|
||||||
|
|
||||||
import static io.javalin.translator.json.JavalinJsonPlugin.getObjectToJsonMapper;
|
import static io.javalin.translator.json.JavalinJsonPlugin.getObjectToJsonMapper;
|
||||||
|
|
||||||
|
@Immutable
|
||||||
|
@Singleton
|
||||||
|
@MethodsNotNullByDefault
|
||||||
|
@ParametersAreNonnullByDefault
|
||||||
public class ForumController {
|
public class ForumController {
|
||||||
|
|
||||||
private final ForumManager forumManager;
|
private final ForumManager forumManager;
|
||||||
Reference in New Issue
Block a user