mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-11 18:29:05 +01:00
briar-headless: Add API to list all contacts
This commit is contained in:
@@ -0,0 +1,23 @@
|
||||
package org.briarproject.bramble.identity;
|
||||
|
||||
import org.briarproject.bramble.api.identity.Author;
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
|
||||
import javax.annotation.concurrent.Immutable;
|
||||
|
||||
@Immutable
|
||||
@NotNullByDefault
|
||||
@SuppressWarnings("WeakerAccess")
|
||||
public class OutputAuthor {
|
||||
|
||||
public final byte[] id;
|
||||
public final String name;
|
||||
public final byte[] publicKey;
|
||||
|
||||
public OutputAuthor(Author author) {
|
||||
this.id = author.getId().getBytes();
|
||||
this.name = author.getName();
|
||||
this.publicKey = author.getPublicKey();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -2,6 +2,7 @@ package org.briarproject.briar.headless;
|
||||
|
||||
import org.briarproject.bramble.api.nullsafety.MethodsNotNullByDefault;
|
||||
import org.briarproject.briar.headless.blogs.BlogController;
|
||||
import org.briarproject.briar.headless.contact.ContactController;
|
||||
import org.briarproject.briar.headless.forums.ForumController;
|
||||
import org.briarproject.briar.headless.messaging.MessagingController;
|
||||
|
||||
@@ -26,16 +27,19 @@ import static java.lang.Runtime.getRuntime;
|
||||
public class Router {
|
||||
|
||||
private final BriarService briarService;
|
||||
private final ContactController contactController;
|
||||
private final MessagingController messagingController;
|
||||
private final ForumController forumController;
|
||||
private final BlogController blogController;
|
||||
|
||||
@Inject
|
||||
public Router(BriarService briarService,
|
||||
ContactController contactController,
|
||||
MessagingController messagingController,
|
||||
ForumController forumController,
|
||||
BlogController blogController) {
|
||||
this.briarService = briarService;
|
||||
this.contactController = contactController;
|
||||
this.messagingController = messagingController;
|
||||
this.forumController = forumController;
|
||||
this.blogController = blogController;
|
||||
@@ -56,6 +60,7 @@ public class Router {
|
||||
.start();
|
||||
|
||||
app.routes(() -> {
|
||||
path("/contacts", () -> get(contactController::list));
|
||||
path("/messages/:contactId", () -> {
|
||||
get(messagingController::list);
|
||||
post(messagingController::write);
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
package org.briarproject.briar.headless.contact;
|
||||
|
||||
import org.briarproject.bramble.api.contact.Contact;
|
||||
import org.briarproject.bramble.api.contact.ContactManager;
|
||||
import org.briarproject.bramble.api.db.DbException;
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
import javax.annotation.concurrent.Immutable;
|
||||
import javax.inject.Inject;
|
||||
import javax.inject.Singleton;
|
||||
|
||||
import io.javalin.Context;
|
||||
|
||||
@Immutable
|
||||
@Singleton
|
||||
@NotNullByDefault
|
||||
public class ContactController {
|
||||
|
||||
private final ContactManager contactManager;
|
||||
|
||||
@Inject
|
||||
public ContactController(ContactManager contactManager) {
|
||||
this.contactManager = contactManager;
|
||||
}
|
||||
|
||||
public Context list(Context ctx) throws DbException {
|
||||
Collection<Contact> contacts = contactManager.getActiveContacts();
|
||||
List<OutputContact> outputContacts = new ArrayList<>(contacts.size());
|
||||
for (Contact c : contacts) {
|
||||
outputContacts.add(new OutputContact(c));
|
||||
}
|
||||
return ctx.json(outputContacts);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package org.briarproject.briar.headless.contact;
|
||||
|
||||
import org.briarproject.bramble.api.contact.Contact;
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
import org.briarproject.bramble.identity.OutputAuthor;
|
||||
|
||||
import javax.annotation.concurrent.Immutable;
|
||||
|
||||
@Immutable
|
||||
@NotNullByDefault
|
||||
@SuppressWarnings("WeakerAccess")
|
||||
public class OutputContact {
|
||||
|
||||
public final int id;
|
||||
public final OutputAuthor author;
|
||||
public final boolean verified;
|
||||
|
||||
public OutputContact(Contact c) {
|
||||
this.id = c.getId().getInt();
|
||||
this.author = new OutputAuthor(c.getAuthor());
|
||||
this.verified = c.isVerified();
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user