Add creating IMAP folders from the app
Bump version to 0.2.0 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
@@ -639,7 +639,57 @@ internal object AndroidMailService {
|
||||
return JSONObject().put("saved", true).put("path", file.absolutePath).put("filename", filename)
|
||||
}
|
||||
|
||||
private const val CUSTOM_FOLDER_PREFIX = "Folder:"
|
||||
private val reservedFolderNames = setOf(
|
||||
"inbox", "sent", "drafts", "archive", "trash", "starred", "snoozed", "junk", "spam",
|
||||
"sent items", "sent mail", "sent messages", "archives", "all mail", "deleted items", "deleted messages"
|
||||
)
|
||||
|
||||
fun isCustomFolder(name: String) = name.startsWith(CUSTOM_FOLDER_PREFIX)
|
||||
|
||||
private fun customFolders(store: Store): JSONArray {
|
||||
val systemAttributes = setOf("\\sent", "\\archive", "\\trash", "\\drafts", "\\junk", "\\all", "\\flagged")
|
||||
val result = JSONArray()
|
||||
val folders = try { store.defaultFolder.list("*") } catch (_: Exception) { emptyArray() }
|
||||
folders.filter { folder ->
|
||||
val attributes = (folder as? IMAPFolder)?.attributes?.map { it.lowercase(Locale.ROOT) } ?: emptyList()
|
||||
folder.fullName.uppercase(Locale.ROOT) != "INBOX" &&
|
||||
(folder.type and Folder.HOLDS_MESSAGES) != 0 &&
|
||||
attributes.none { it in systemAttributes } &&
|
||||
folder.fullName.lowercase(Locale.ROOT).split(Regex("[./]")).last() !in reservedFolderNames
|
||||
}.sortedBy { it.fullName }.forEach { folder ->
|
||||
result.put(JSONObject().put("name", folder.name).put("path", folder.fullName)
|
||||
.put("id", CUSTOM_FOLDER_PREFIX + folder.fullName))
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
fun listFolders(account: JSONObject): JSONObject =
|
||||
withStore(account) { _, store -> JSONObject().put("folders", customFolders(store)) }
|
||||
|
||||
fun createFolder(account: JSONObject, rawName: String): JSONObject {
|
||||
val name = rawName.trim()
|
||||
require(name.isNotEmpty() && name.length <= 100) { "Enter a folder name of up to 100 characters." }
|
||||
require(!Regex("[\\\\/.*%#&\\u0000-\\u001f]").containsMatchIn(name)) {
|
||||
"Folder names cannot contain / \\ . * % # & or control characters."
|
||||
}
|
||||
require(name.lowercase(Locale.ROOT) !in reservedFolderNames) { "That folder name is reserved." }
|
||||
return withStore(account) { _, store ->
|
||||
val folder = store.getFolder(name)
|
||||
check(!folder.exists()) { "A folder with that name already exists." }
|
||||
check(folder.create(Folder.HOLDS_MESSAGES)) { "The mail server could not create that folder." }
|
||||
try { folder.isSubscribed = true } catch (_: Exception) {}
|
||||
JSONObject().put("ok", true)
|
||||
.put("folder", JSONObject().put("name", name).put("path", folder.fullName)
|
||||
.put("id", CUSTOM_FOLDER_PREFIX + folder.fullName))
|
||||
.put("folders", customFolders(store))
|
||||
}
|
||||
}
|
||||
|
||||
private fun resolveFolder(store: Store, requested: String): Folder? {
|
||||
if (requested.startsWith(CUSTOM_FOLDER_PREFIX)) {
|
||||
return store.getFolder(requested.removePrefix(CUSTOM_FOLDER_PREFIX)).takeIf { it.exists() }
|
||||
}
|
||||
if (requested == "Inbox" || requested == "Starred" || requested == "Snoozed") {
|
||||
return store.getFolder("INBOX")
|
||||
}
|
||||
|
||||
@@ -136,6 +136,28 @@ class EpMailPlugin : Plugin() {
|
||||
}
|
||||
}
|
||||
|
||||
@PluginMethod
|
||||
fun listFolders(call: PluginCall) {
|
||||
try {
|
||||
val account = AndroidAccountVault.account()
|
||||
?: throw IllegalStateException("Add a mail account before listing folders.")
|
||||
call.resolve(AndroidMailService.listFolders(account).toJSObject())
|
||||
} catch (error: Exception) {
|
||||
call.resolve(JSObject().put("folders", org.json.JSONArray()).put("offline", true))
|
||||
}
|
||||
}
|
||||
|
||||
@PluginMethod
|
||||
fun createFolder(call: PluginCall) {
|
||||
try {
|
||||
val account = AndroidAccountVault.account()
|
||||
?: throw IllegalStateException("Add a mail account before creating folders.")
|
||||
call.resolve(AndroidMailService.createFolder(account, call.getString("name") ?: "").toJSObject())
|
||||
} catch (error: Exception) {
|
||||
call.reject(error.message ?: "Could not create the folder.")
|
||||
}
|
||||
}
|
||||
|
||||
@PluginMethod
|
||||
fun syncFolder(call: PluginCall) {
|
||||
try {
|
||||
@@ -145,7 +167,7 @@ class EpMailPlugin : Plugin() {
|
||||
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 folder = if (selected in setOf("Inbox", "Sent", "Archive", "Trash", "Starred", "Snoozed") || AndroidMailService.isCustomFolder(selected)) selected else "Inbox"
|
||||
val offset = (call.getInt("offset") ?: 0).coerceAtLeast(0)
|
||||
AndroidMailService.flushOutbox(account)
|
||||
call.resolve(AndroidMailService.sync(account, folder, offset, JSONObject(filters.toString())).toJSObject())
|
||||
|
||||
Reference in New Issue
Block a user