Last code cleanup before submitting merge request

parent e4c7f138
package org.briarproject.bramble.account;
import org.briarproject.bramble.api.account.AccountManager;
import org.briarproject.bramble.api.crypto.CryptoComponent;
import org.briarproject.bramble.api.db.DatabaseConfig;
import org.briarproject.bramble.api.identity.IdentityManager;
import javax.inject.Inject;
class HeadlessAccountManager extends AccountManagerImpl
implements AccountManager {
@Inject
HeadlessAccountManager(DatabaseConfig databaseConfig,
CryptoComponent crypto, IdentityManager identityManager) {
super(databaseConfig, crypto, identityManager);
}
}
package org.briarproject.bramble.account;
import org.briarproject.bramble.api.account.AccountManager;
import javax.inject.Singleton;
import dagger.Module;
import dagger.Provides;
@Module
public class HeadlessAccountModule {
@Provides
@Singleton
AccountManager provideAccountManager(HeadlessAccountManager accountManager) {
return accountManager;
}
}
...@@ -15,3 +15,7 @@ data class OutputAuthor( ...@@ -15,3 +15,7 @@ data class OutputAuthor(
publicKey = author.publicKey publicKey = author.publicKey
) )
} }
fun Author.output() = OutputAuthor(this)
fun Author.Status.output() = name.toLowerCase()
package org.briarproject.briar.headless;
import org.briarproject.bramble.BrambleCoreEagerSingletons;
import org.briarproject.bramble.BrambleCoreModule;
import org.briarproject.bramble.account.HeadlessAccountModule;
import org.briarproject.bramble.system.DesktopSecureRandomModule;
import org.briarproject.briar.BriarCoreEagerSingletons;
import org.briarproject.briar.BriarCoreModule;
import javax.inject.Singleton;
import dagger.Component;
@Component(modules = {
BrambleCoreModule.class,
BriarCoreModule.class,
DesktopSecureRandomModule.class,
HeadlessAccountModule.class,
HeadlessModule.class
})
@Singleton
public interface BriarHeadlessApp
extends BrambleCoreEagerSingletons, BriarCoreEagerSingletons {
Router router();
}
\ No newline at end of file
package org.briarproject.briar.headless
import dagger.Component
import org.briarproject.bramble.BrambleCoreEagerSingletons
import org.briarproject.bramble.BrambleCoreModule
import org.briarproject.bramble.account.AccountModule
import org.briarproject.bramble.system.DesktopSecureRandomModule
import org.briarproject.briar.BriarCoreEagerSingletons
import org.briarproject.briar.BriarCoreModule
import javax.inject.Singleton
@Component(
modules = [
BrambleCoreModule::class,
BriarCoreModule::class,
DesktopSecureRandomModule::class,
AccountModule::class,
HeadlessModule::class
]
)
@Singleton
internal interface BriarHeadlessApp : BrambleCoreEagerSingletons, BriarCoreEagerSingletons {
fun router(): Router
}
...@@ -15,7 +15,7 @@ import javax.inject.Singleton ...@@ -15,7 +15,7 @@ import javax.inject.Singleton
@Immutable @Immutable
@Singleton @Singleton
class BriarService @Inject internal class BriarService @Inject
constructor( constructor(
private val accountManager: AccountManager, private val accountManager: AccountManager,
private val lifecycleManager: LifecycleManager, private val lifecycleManager: LifecycleManager,
......
package org.briarproject.briar.headless
import org.briarproject.bramble.api.identity.Author
import org.briarproject.bramble.identity.OutputAuthor
import org.briarproject.briar.api.blog.MessageType
fun Author.output() = OutputAuthor(this)
fun Author.Status.output() = name.toLowerCase()
fun MessageType.output() = name.toLowerCase()
package org.briarproject.briar.headless;
import org.briarproject.bramble.api.db.DatabaseConfig;
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
import java.io.File;
@NotNullByDefault
class HeadlessDatabaseConfig implements DatabaseConfig {
private final File dbDir, keyDir;
HeadlessDatabaseConfig(File dbDir, File keyDir) {
this.dbDir = dbDir;
this.keyDir = keyDir;
}
@Override
public File getDatabaseDirectory() {
return dbDir;
}
@Override
public File getDatabaseKeyDirectory() {
return keyDir;
}
@Override
public long getMaxSize() {
return Long.MAX_VALUE;
}
}
package org.briarproject.briar.headless
import org.briarproject.bramble.api.db.DatabaseConfig
import java.io.File
import java.lang.Long.MAX_VALUE
internal class HeadlessDatabaseConfig(private val dbDir: File, private val keyDir: File) :
DatabaseConfig {
override fun getDatabaseDirectory(): File {
return dbDir
}
override fun getDatabaseKeyDirectory(): File {
return keyDir
}
override fun getMaxSize(): Long {
return MAX_VALUE
}
}
...@@ -40,7 +40,7 @@ import javax.net.SocketFactory ...@@ -40,7 +40,7 @@ import javax.net.SocketFactory
MessagingModule::class MessagingModule::class
] ]
) )
class HeadlessModule(private val appDir: File) { internal class HeadlessModule(private val appDir: File) {
@Provides @Provides
@Singleton @Singleton
......
...@@ -24,7 +24,7 @@ import java.util.logging.LogManager ...@@ -24,7 +24,7 @@ import java.util.logging.LogManager
private const val DEFAULT_PORT = 7000 private const val DEFAULT_PORT = 7000
private val DEFAULT_DATA_DIR = getProperty("user.home") + separator + ".briar" private val DEFAULT_DATA_DIR = getProperty("user.home") + separator + ".briar"
class Main : CliktCommand( private class Main : CliktCommand(
name = "briar-headless", name = "briar-headless",
help = "A Briar client without GUI that exposes a REST and Websocket API" help = "A Briar client without GUI that exposes a REST and Websocket API"
) { ) {
...@@ -61,7 +61,7 @@ class Main : CliktCommand( ...@@ -61,7 +61,7 @@ class Main : CliktCommand(
1 -> INFO 1 -> INFO
else -> ALL else -> ALL
} }
setProperty(DEFAULT_LOG_LEVEL_KEY, levelSlf4j); setProperty(DEFAULT_LOG_LEVEL_KEY, levelSlf4j)
LogManager.getLogManager().getLogger("").level = level LogManager.getLogManager().getLogger("").level = level
val dataDir = getDataDir() val dataDir = getDataDir()
...@@ -84,11 +84,11 @@ class Main : CliktCommand( ...@@ -84,11 +84,11 @@ class Main : CliktCommand(
} else if (!file.isDirectory) { } else if (!file.isDirectory) {
throw IOException("Data dir is not a directory: ${file.absolutePath}") throw IOException("Data dir is not a directory: ${file.absolutePath}")
} }
val perms = HashSet<PosixFilePermission>(); val perms = HashSet<PosixFilePermission>()
perms.add(OWNER_READ); perms.add(OWNER_READ)
perms.add(OWNER_WRITE); perms.add(OWNER_WRITE)
perms.add(OWNER_EXECUTE); perms.add(OWNER_EXECUTE)
setPosixFilePermissions(file.toPath(), perms); setPosixFilePermissions(file.toPath(), perms)
return file return file
} }
......
...@@ -19,7 +19,7 @@ import kotlin.system.exitProcess ...@@ -19,7 +19,7 @@ import kotlin.system.exitProcess
@Immutable @Immutable
@Singleton @Singleton
class Router @Inject internal class Router @Inject
constructor( constructor(
private val briarService: BriarService, private val briarService: BriarService,
private val webSocketController: WebSocketController, private val webSocketController: WebSocketController,
......
...@@ -24,7 +24,6 @@ internal class WebSocketControllerImpl @Inject constructor() : WebSocketControll ...@@ -24,7 +24,6 @@ internal class WebSocketControllerImpl @Inject constructor() : WebSocketControll
} }
@Immutable @Immutable
@Suppress("unused")
internal class OutputEvent(val name: String, val data: Any) { internal class OutputEvent(val name: String, val data: Any) {
val type = "event" val type = "event"
} }
package org.briarproject.briar.headless.blogs
import org.briarproject.briar.api.blog.BlogPostHeader
internal fun BlogPostHeader.output(body: String) = OutputBlogPost(this, body)
package org.briarproject.briar.headless.blogs package org.briarproject.briar.headless.blogs
import org.briarproject.bramble.identity.OutputAuthor import org.briarproject.bramble.identity.OutputAuthor
import org.briarproject.bramble.identity.output
import org.briarproject.briar.api.blog.BlogPostHeader import org.briarproject.briar.api.blog.BlogPostHeader
import org.briarproject.briar.headless.output import org.briarproject.briar.api.blog.MessageType
import javax.annotation.concurrent.Immutable import javax.annotation.concurrent.Immutable
@Immutable @Immutable
...@@ -31,3 +32,7 @@ internal data class OutputBlogPost( ...@@ -31,3 +32,7 @@ internal data class OutputBlogPost(
timestampReceived = header.timeReceived timestampReceived = header.timeReceived
) )
} }
internal fun BlogPostHeader.output(body: String) = OutputBlogPost(this, body)
internal fun MessageType.output() = name.toLowerCase()
package org.briarproject.briar.headless.contact
import org.briarproject.bramble.api.contact.Contact
internal fun Contact.output() = OutputContact(this)
...@@ -2,7 +2,7 @@ package org.briarproject.briar.headless.contact ...@@ -2,7 +2,7 @@ package org.briarproject.briar.headless.contact
import org.briarproject.bramble.api.contact.Contact import org.briarproject.bramble.api.contact.Contact
import org.briarproject.bramble.identity.OutputAuthor import org.briarproject.bramble.identity.OutputAuthor
import org.briarproject.briar.headless.output import org.briarproject.bramble.identity.output
import javax.annotation.concurrent.Immutable import javax.annotation.concurrent.Immutable
@Immutable @Immutable
...@@ -17,3 +17,5 @@ internal data class OutputContact( ...@@ -17,3 +17,5 @@ internal data class OutputContact(
verified = c.isVerified verified = c.isVerified
) )
} }
internal fun Contact.output() = OutputContact(this)
package org.briarproject.briar.headless.forums
import org.briarproject.briar.api.forum.Forum
internal fun Forum.output() = OutputForum(this)
internal fun Collection<Forum>.output() = map { it.output() }
...@@ -13,3 +13,7 @@ internal data class OutputForum( ...@@ -13,3 +13,7 @@ internal data class OutputForum(
id = forum.id.bytes id = forum.id.bytes
) )
} }
internal fun Forum.output() = OutputForum(this)
internal fun Collection<Forum>.output() = map { it.output() }
package org.briarproject.briar.headless.messaging
import org.briarproject.bramble.api.contact.ContactId
import org.briarproject.briar.api.introduction.IntroductionRequest
import org.briarproject.briar.api.introduction.IntroductionResponse
import org.briarproject.briar.api.messaging.PrivateMessage
import org.briarproject.briar.api.messaging.PrivateMessageHeader
import org.briarproject.briar.api.messaging.PrivateRequest
import org.briarproject.briar.api.messaging.PrivateResponse
import org.briarproject.briar.api.messaging.event.PrivateMessageReceivedEvent
import org.briarproject.briar.api.sharing.InvitationRequest
import org.briarproject.briar.api.sharing.InvitationResponse
internal fun PrivateMessageHeader.output(
contactId: ContactId,
body: String?
) = OutputPrivateMessageHeader(this, contactId, body)
internal fun PrivateMessage.output(contactId: ContactId, body: String) =
OutputPrivateMessageHeader(this, contactId, body)
internal fun PrivateMessageReceivedEvent<*>.output(body: String) =
messageHeader.output(contactId, body)
internal fun IntroductionRequest.output(contactId: ContactId) =
OutputIntroductionRequest(this, contactId)
internal fun PrivateRequest<*>.output(contactId: ContactId): OutputPrivateMessage {
return when (this) {
is IntroductionRequest -> OutputIntroductionRequest(this, contactId)
is InvitationRequest -> OutputInvitationRequest(this, contactId)
else -> throw AssertionError("Unknown PrivateRequest")
}
}
internal fun PrivateResponse.output(contactId: ContactId): OutputPrivateMessage {
return when (this) {
is IntroductionResponse -> OutputIntroductionResponse(this, contactId)
is InvitationResponse -> OutputInvitationResponse(this, contactId)
else -> throw AssertionError("Unknown PrivateResponse")
}
}
...@@ -3,6 +3,7 @@ package org.briarproject.briar.headless.messaging ...@@ -3,6 +3,7 @@ package org.briarproject.briar.headless.messaging
import org.briarproject.bramble.api.contact.ContactId import org.briarproject.bramble.api.contact.ContactId
import org.briarproject.briar.api.messaging.PrivateMessage import org.briarproject.briar.api.messaging.PrivateMessage
import org.briarproject.briar.api.messaging.PrivateMessageHeader import org.briarproject.briar.api.messaging.PrivateMessageHeader
import org.briarproject.briar.api.messaging.event.PrivateMessageReceivedEvent
import javax.annotation.concurrent.Immutable import javax.annotation.concurrent.Immutable
@Immutable @Immutable
...@@ -48,3 +49,14 @@ internal data class OutputPrivateMessageHeader( ...@@ -48,3 +49,14 @@ internal data class OutputPrivateMessageHeader(
), contactId, body ), contactId, body
) )
} }
internal fun PrivateMessageHeader.output(
contactId: ContactId,
body: String?
) = OutputPrivateMessageHeader(this, contactId, body)
internal fun PrivateMessage.output(contactId: ContactId, body: String) =
OutputPrivateMessageHeader(this, contactId, body)
internal fun PrivateMessageReceivedEvent<*>.output(body: String) =
messageHeader.output(contactId, body)
...@@ -46,3 +46,14 @@ internal data class OutputInvitationRequest( ...@@ -46,3 +46,14 @@ internal data class OutputInvitationRequest(
val canBeOpened get() = iHeader.canBeOpened() val canBeOpened get() = iHeader.canBeOpened()
} }
internal fun PrivateRequest<*>.output(contactId: ContactId): OutputPrivateMessage {
return when (this) {
is IntroductionRequest -> OutputIntroductionRequest(this, contactId)
is InvitationRequest -> OutputInvitationRequest(this, contactId)
else -> throw AssertionError("Unknown PrivateRequest")
}
}
internal fun IntroductionRequest.output(contactId: ContactId) =
OutputIntroductionRequest(this, contactId)
...@@ -3,13 +3,13 @@ ...@@ -3,13 +3,13 @@
package org.briarproject.briar.headless.messaging package org.briarproject.briar.headless.messaging
import org.briarproject.bramble.api.contact.ContactId import org.briarproject.bramble.api.contact.ContactId
import org.briarproject.bramble.identity.output
import org.briarproject.briar.api.blog.BlogInvitationResponse import org.briarproject.briar.api.blog.BlogInvitationResponse
import org.briarproject.briar.api.forum.ForumInvitationResponse import org.briarproject.briar.api.forum.ForumInvitationResponse
import org.briarproject.briar.api.introduction.IntroductionResponse import org.briarproject.briar.api.introduction.IntroductionResponse
import org.briarproject.briar.api.messaging.PrivateResponse import org.briarproject.briar.api.messaging.PrivateResponse
import org.briarproject.briar.api.privategroup.invitation.GroupInvitationResponse import org.briarproject.briar.api.privategroup.invitation.GroupInvitationResponse
import org.briarproject.briar.api.sharing.InvitationResponse import org.briarproject.briar.api.sharing.InvitationResponse
import org.briarproject.briar.headless.output
import javax.annotation.concurrent.Immutable import javax.annotation.concurrent.Immutable
@Immutable @Immutable
...@@ -45,3 +45,11 @@ internal data class OutputInvitationResponse( ...@@ -45,3 +45,11 @@ internal data class OutputInvitationResponse(
} }
val shareableId: ByteArray get() = iHeader.shareableId.bytes val shareableId: ByteArray get() = iHeader.shareableId.bytes
} }
internal fun PrivateResponse.output(contactId: ContactId): OutputPrivateMessage {
return when (this) {
is IntroductionResponse -> OutputIntroductionResponse(this, contactId)
is InvitationResponse -> OutputInvitationResponse(this, contactId)
else -> throw AssertionError("Unknown PrivateResponse")
}
}
...@@ -8,12 +8,12 @@ import io.mockk.just ...@@ -8,12 +8,12 @@ import io.mockk.just
import io.mockk.mockk import io.mockk.mockk
import org.briarproject.bramble.api.identity.Author.Status.OURSELVES import org.briarproject.bramble.api.identity.Author.Status.OURSELVES
import org.briarproject.bramble.api.sync.MessageId import org.briarproject.bramble.api.sync.MessageId
import org.briarproject.bramble.identity.output
import org.briarproject.bramble.util.StringUtils.getRandomString import org.briarproject.bramble.util.StringUtils.getRandomString
import org.briarproject.briar.api.blog.* import org.briarproject.briar.api.blog.*
import org.briarproject.briar.api.blog.BlogConstants.MAX_BLOG_POST_BODY_LENGTH import org.briarproject.briar.api.blog.BlogConstants.MAX_BLOG_POST_BODY_LENGTH
import org.briarproject.briar.api.blog.MessageType.POST import org.briarproject.briar.api.blog.MessageType.POST
import org.briarproject.briar.headless.ControllerTest import org.briarproject.briar.headless.ControllerTest
import org.briarproject.briar.headless.output
import org.junit.jupiter.api.Assertions.assertThrows import org.junit.jupiter.api.Assertions.assertThrows
import org.junit.jupiter.api.Test import org.junit.jupiter.api.Test
......
...@@ -3,8 +3,8 @@ package org.briarproject.briar.headless.contact ...@@ -3,8 +3,8 @@ package org.briarproject.briar.headless.contact
import io.javalin.json.JavalinJson.toJson import io.javalin.json.JavalinJson.toJson
import io.mockk.every import io.mockk.every
import org.briarproject.bramble.api.contact.Contact import org.briarproject.bramble.api.contact.Contact
import org.briarproject.bramble.identity.output
import org.briarproject.briar.headless.ControllerTest import org.briarproject.briar.headless.ControllerTest
import org.briarproject.briar.headless.output
import org.junit.jupiter.api.Test import org.junit.jupiter.api.Test
internal class ContactControllerTest : ControllerTest() { internal class ContactControllerTest : ControllerTest() {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment