Skip to content

Commit

Permalink
Merge branch 'refs/heads/version-2' into version-2-client-experiments
Browse files Browse the repository at this point in the history
  • Loading branch information
bjsvedin committed Apr 22, 2024
2 parents 23e7598 + 96f9b0e commit b11c130
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 19 deletions.
@@ -1,17 +1,14 @@
package com.lightningkite.lightningdb

// import com.lightningkite.khrysalis.*
import com.lightningkite.kiteui.*
import com.lightningkite.kiteui.reactive.*
import com.lightningkite.uuid
import kotlinx.serialization.KSerializer
import kotlinx.serialization.json.Json

private val shared = HashMap<String, TypedWebSocket<MultiplexMessage, MultiplexMessage>>()
fun multiplexSocket(url: String, path: String, params: Map<String, List<String>>, json: Json): RetryWebsocket {
fun multiplexSocket(url: String, path: String, params: Map<String, List<String>>, json: Json, pingTime: Long = 30_000L): RetryWebsocket {
val shared = shared.getOrPut(url) {
val s = retryWebsocket(url)
// TODO: Pings
val s = retryWebsocket(url, pingTime)
s.typed(json, MultiplexMessage.serializer(), MultiplexMessage.serializer())
}
val channelOpen = Property(false)
Expand Down
Expand Up @@ -177,7 +177,7 @@ class ModelRestUpdatesWebsocketHelper private constructor(val database: () -> Da
for (changeSub in db) {
try {
changeSub._id.close()
} catch (e: Exception) {
} catch (_: Exception) {
// We don't really care. We just want to shut down as many of these as we can.
/*squish*/
}
Expand Down
Expand Up @@ -171,7 +171,7 @@ class RestApiWebsocketHelper private constructor(val database: () -> Database) {
for (changeSub in db) {
try {
changeSub._id.close()
} catch (e: Exception) {
} catch (_: Exception) {
// We don't really care. We just want to shut down as many of these as we can.
/*squish*/
}
Expand Down
Expand Up @@ -46,9 +46,9 @@ suspend fun Email.toJavaX(session: Session = Session.getDefaultInstance(Properti
val email = this@toJavaX
subject = email.subject
email.fromEmail?.let { setFrom(InternetAddress(it, email.fromLabel)) }
email.to.forEach { setRecipient(Message.RecipientType.TO, InternetAddress(it.value, it.label)) }
email.cc.forEach { setRecipient(Message.RecipientType.CC, InternetAddress(it.value, it.label)) }
email.bcc.forEach { setRecipient(Message.RecipientType.BCC, InternetAddress(it.value, it.label)) }
email.to.map { InternetAddress(it.value, it.label) }.also { setRecipients(Message.RecipientType.TO, it.toTypedArray()) }
email.cc.map { InternetAddress(it.value, it.label) }.also { setRecipients(Message.RecipientType.CC, it.toTypedArray()) }
email.bcc.map { InternetAddress(it.value, it.label) }.also { setRecipients(Message.RecipientType.BCC, it.toTypedArray()) }
HttpContentAndHeaders(
headers = email.customHeaders,
content = HttpContent.Multipart(
Expand Down
Expand Up @@ -7,6 +7,7 @@ import jakarta.mail.Authenticator
import jakarta.mail.PasswordAuthentication
import jakarta.mail.Session
import jakarta.mail.Transport
import jakarta.mail.internet.InternetAddress

/**
* An email client that will send real emails through SMTP.
Expand All @@ -15,7 +16,7 @@ class SmtpEmailClient(val smtpConfig: SmtpConfig) : EmailClient {

val session = Session.getInstance(
Properties().apply {
smtpConfig.username?.let{ username ->
smtpConfig.username?.let { username ->
put("mail.smtp.user", username)
}
put("mail.smtp.host", smtpConfig.hostName)
Expand All @@ -37,11 +38,39 @@ class SmtpEmailClient(val smtpConfig: SmtpConfig) : EmailClient {
)

override suspend fun send(email: Email) {
if(email.to.isEmpty() && email.cc.isEmpty() && email.bcc.isEmpty()) return
Transport.send(
email.copy(
fromEmail = email.fromEmail ?: smtpConfig.fromEmail,
fromLabel = email.fromLabel ?: generalSettings().projectName
).toJavaX(session)
)
}
}

override suspend fun sendBulk(template: Email, personalizations: List<EmailPersonalization>) {
if (personalizations.isEmpty()) return
session.transport
.also { it.connect() }
.use { transport ->
personalizations
.asSequence()
.map {
it(template).copy(
fromEmail = template.fromEmail ?: smtpConfig.fromEmail,
fromLabel = template.fromLabel ?: generalSettings().projectName
)
}
.forEach { email ->
transport.sendMessage(
email.toJavaX(session).also { it.saveChanges() },
email.to
.plus(email.cc)
.plus(email.bcc)
.map { InternetAddress(it.value, it.label) }
.toTypedArray()
.also { if (it.isEmpty()) return@forEach }
)
}
}
}
}
Expand Up @@ -81,7 +81,7 @@ class MultiplexWebSocketHandler(val cache: () -> Cache) : WebSockets.Handler {

override suspend fun message(event: WebSockets.MessageEvent) {
if (event.content.isBlank()) {
event.id.send("")
event.id.send(" ")
return
}
val message = Serialization.json.decodeFromString<MultiplexMessage>(event.content)
Expand Down
Expand Up @@ -14,13 +14,80 @@ class SmtpTest {
println("No credentials to test with at ${credentials.absolutePath}")
return
}
val client = SmtpEmailClient(credentials.readText().let { Serialization.json.decodeFromString(credentials.readText()) })
val client =
SmtpEmailClient(credentials.readText().let { Serialization.json.decodeFromString(credentials.readText()) })
runBlocking {
client.send(Email(
subject = "Subject 2", fromLabel = "Joseph Ivie", fromEmail = "joseph@lightningkite.com",
to = listOf(EmailLabeledValue("joseph@lightningkite.com", "Joseph Ivie")),
html = "<p>Hello world!</p>",
))
client.send(
Email(
subject = "Subject 2", fromLabel = "Joseph Ivie", fromEmail = "joseph@lightningkite.com",
to = listOf(EmailLabeledValue("joseph@lightningkite.com", "Joseph Ivie")),
html = "<p>Hello world!</p>",
)
)
}
}

@Test
fun testSendBulk(): Unit = runBlocking {

val credentials = File("local/test-smtp.json")
if (!credentials.exists()) {
println("No credentials to test with at ${credentials.absolutePath}")
return@runBlocking
}

val client =
SmtpEmailClient(credentials.readText().let { Serialization.json.decodeFromString(credentials.readText()) })

val email1 = EmailLabeledValue(
"joseph@lightningkite.com",
"Joseph Ivie One"
)
val email2 = EmailLabeledValue(
"joseph+two@lightningkite.com",
"Joseph Ivie Two"
)
val email3 = EmailLabeledValue(
"joseph+three@lightningkite.com",
"Joseph Ivie Three"
)

client.sendBulk(
Email(
subject = "Bulk Email Test", fromLabel = "Joseph Ivie", fromEmail = "joseph@lightningkite.com",
to = emptyList(),
html = "<p>Hello {{UserName}}!</p>",
),
personalizations = listOf(
EmailPersonalization(
to = listOf(email1),
cc = listOf(email2),
bcc = listOf(email3),
substitutions = mapOf("{{UserName}}" to email1.label)
),
EmailPersonalization(
to = listOf(email2),
cc = listOf(email3),
bcc = listOf(email1),
substitutions = mapOf("{{UserName}}" to email2.label)
),
EmailPersonalization(
to = listOf(email3),
cc = listOf(email1),
bcc = listOf(email2),
substitutions = mapOf("{{UserName}}" to email3.label)
),
EmailPersonalization(
to = listOf(email1, email2, email3),
substitutions = mapOf(
"{{UserName}}" to listOf(
email1.label,
email2.label,
email3.label
).joinToString { it })
),
),
)

}
}

0 comments on commit b11c130

Please sign in to comment.