Add advanced search and offline mail support
Bump version to 0.1.20 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
@@ -140,9 +140,13 @@ class EpMailPlugin : Plugin() {
|
||||
val account = AndroidAccountVault.account()
|
||||
?: throw IllegalStateException("Add a mail account before syncing.")
|
||||
val requested = call.getString("folder") ?: "Inbox"
|
||||
val folder = if (requested in setOf("Inbox", "Sent", "Archive", "Trash", "Starred", "Snoozed")) requested else "Inbox"
|
||||
val filters = call.getObject("filters") ?: JSObject()
|
||||
val filterFolder = filters.getString("folder")
|
||||
val selected = filterFolder?.takeIf { it in setOf("Inbox", "Sent", "Archive", "Trash", "Starred") } ?: requested
|
||||
val folder = if (selected in setOf("Inbox", "Sent", "Archive", "Trash", "Starred", "Snoozed")) selected else "Inbox"
|
||||
val offset = (call.getInt("offset") ?: 0).coerceAtLeast(0)
|
||||
call.resolve(AndroidMailService.sync(account, folder, offset).toJSObject())
|
||||
AndroidMailService.flushOutbox(account)
|
||||
call.resolve(AndroidMailService.sync(account, folder, offset, JSONObject(filters.toString())).toJSObject())
|
||||
} catch (error: Exception) {
|
||||
call.reject(error.message ?: "Could not sync the mailbox.")
|
||||
}
|
||||
@@ -155,7 +159,14 @@ class EpMailPlugin : Plugin() {
|
||||
?: throw IllegalStateException("Add a mail account before changing messages.")
|
||||
call.resolve(AndroidMailService.updateMessage(account, call.data).toJSObject())
|
||||
} catch (error: Exception) {
|
||||
call.reject(error.message ?: "Could not update the message.")
|
||||
val account = AndroidAccountVault.account()
|
||||
if (account != null && isConnectivityFailure(error)) {
|
||||
AndroidAccountVault.enqueueMailOperation(account.optString("id"), JSONObject()
|
||||
.put("id", UUID.randomUUID().toString())
|
||||
.put("type", "action")
|
||||
.put("data", JSONObject(call.data.toString())))
|
||||
call.resolve(JSObject().put("ok", true).put("queued", true))
|
||||
} else call.reject(error.message ?: "Could not update the message.")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -215,15 +226,83 @@ class EpMailPlugin : Plugin() {
|
||||
|
||||
@PluginMethod
|
||||
fun send(call: PluginCall) {
|
||||
val input = JSONObject(call.data.toString()).apply {
|
||||
if (optString("messageId").isBlank()) put("messageId", "<${UUID.randomUUID()}@ep-mail.local>")
|
||||
}
|
||||
try {
|
||||
val account = AndroidAccountVault.account()
|
||||
?: throw IllegalStateException("Add a mail account before sending.")
|
||||
call.resolve(AndroidMailService.send(account, call.data).toJSObject())
|
||||
call.resolve(AndroidMailService.send(account, input).toJSObject())
|
||||
} catch (error: Exception) {
|
||||
call.reject(error.message ?: "Could not send the message.")
|
||||
val account = AndroidAccountVault.account()
|
||||
if (account != null && isConnectivityFailure(error)) {
|
||||
AndroidAccountVault.enqueueMailOperation(account.optString("id"), JSONObject()
|
||||
.put("id", UUID.randomUUID().toString())
|
||||
.put("type", "send")
|
||||
.put("data", input))
|
||||
call.resolve(JSObject().put("ok", true).put("queued", true))
|
||||
} else call.reject(error.message ?: "Could not send the message.")
|
||||
}
|
||||
}
|
||||
|
||||
@PluginMethod
|
||||
fun listDrafts(call: PluginCall) {
|
||||
try {
|
||||
val account = AndroidAccountVault.account()
|
||||
?: throw IllegalStateException("Add a mail account before opening drafts.")
|
||||
call.resolve(JSObject().put("drafts", account.optJSONArray("drafts") ?: JSONArray()).put("offline", true))
|
||||
} catch (error: Exception) {
|
||||
call.reject(error.message ?: "Could not open drafts.")
|
||||
}
|
||||
}
|
||||
|
||||
@PluginMethod
|
||||
fun saveDraft(call: PluginCall) {
|
||||
try {
|
||||
val account = AndroidAccountVault.account()
|
||||
?: throw IllegalStateException("Add a mail account before saving drafts.")
|
||||
val input = JSONObject(call.data.toString())
|
||||
val draft = JSONObject()
|
||||
.put("id", input.optString("id").ifBlank { UUID.randomUUID().toString() })
|
||||
.put("to", input.optString("to").take(4000))
|
||||
.put("cc", input.optString("cc").take(4000))
|
||||
.put("subject", input.optString("subject").take(998))
|
||||
.put("text", input.optString("text").take(500000))
|
||||
.put("updatedAt", System.currentTimeMillis())
|
||||
AndroidAccountVault.saveDraft(account.optString("id"), draft)
|
||||
call.resolve(draft.toJSObject())
|
||||
} catch (error: Exception) {
|
||||
call.reject(error.message ?: "Could not save the draft.")
|
||||
}
|
||||
}
|
||||
|
||||
@PluginMethod
|
||||
fun deleteDraft(call: PluginCall) {
|
||||
try {
|
||||
val account = AndroidAccountVault.account()
|
||||
?: throw IllegalStateException("Add a mail account before deleting drafts.")
|
||||
val id = call.getString("id") ?: throw IllegalArgumentException("Choose a draft.")
|
||||
AndroidAccountVault.deleteDraft(account.optString("id"), id)
|
||||
call.resolve(JSObject().put("ok", true))
|
||||
} catch (error: Exception) {
|
||||
call.reject(error.message ?: "Could not delete the draft.")
|
||||
}
|
||||
}
|
||||
|
||||
private fun isConnectivityFailure(error: Throwable): Boolean {
|
||||
var current: Throwable? = error
|
||||
while (current != null) {
|
||||
if (current is java.net.UnknownHostException ||
|
||||
current is java.net.ConnectException ||
|
||||
current is java.net.SocketTimeoutException ||
|
||||
current is javax.net.ssl.SSLException) return true
|
||||
val message = current.message.orEmpty().lowercase(Locale.ROOT)
|
||||
if (listOf("network is unreachable", "connection timed out", "connection refused", "unable to resolve host", "socket closed").any(message::contains)) return true
|
||||
current = current.cause
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
@PluginMethod
|
||||
fun davList(call: PluginCall) {
|
||||
try {
|
||||
|
||||
Reference in New Issue
Block a user