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")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user