Add Snooze messages until a chosen time and show a follow-up reminder when they return. Bump version V0.1.17
This commit is contained in:
@@ -57,8 +57,9 @@ internal object AndroidMailService {
|
||||
JSONObject().put("mailbox", "INBOX").put("messages", count)
|
||||
}
|
||||
|
||||
fun sync(account: JSONObject, requestedFolder: String, offset: Int): JSONObject =
|
||||
withStore(account) { _, store ->
|
||||
fun sync(account: JSONObject, requestedFolder: String, offset: Int): JSONObject {
|
||||
if (requestedFolder == "Snoozed") return syncSnoozed(account, offset)
|
||||
return withStore(account) { _, store ->
|
||||
val folder = resolveFolder(store, requestedFolder)
|
||||
if (folder == null) return@withStore JSONObject()
|
||||
.put("messages", JSONArray()).put("total", 0).put("folder", requestedFolder)
|
||||
@@ -73,7 +74,15 @@ internal object AndroidMailService {
|
||||
val first = (end - 19).coerceAtLeast(1)
|
||||
val result = JSONArray()
|
||||
val batch = folder.getMessages(first, end).reversed()
|
||||
val snoozed = account.optJSONArray("snoozedMessages") ?: JSONArray()
|
||||
for (message in batch) {
|
||||
val uid = (folder as? UIDFolder)?.getUID(message) ?: 0L
|
||||
val isSnoozed = (0 until snoozed.length()).any { index ->
|
||||
val item = snoozed.getJSONObject(index)
|
||||
item.optString("folder") == requestedFolder && item.optLong("uid") == uid &&
|
||||
item.optLong("dueAt") > System.currentTimeMillis()
|
||||
}
|
||||
if (isSnoozed) continue
|
||||
result.put(messageJson(folder, message, requestedFolder))
|
||||
}
|
||||
JSONObject().put("messages", result).put("total", total)
|
||||
@@ -82,13 +91,44 @@ internal object AndroidMailService {
|
||||
folder.close(false)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun syncSnoozed(account: JSONObject, offset: Int): JSONObject =
|
||||
withStore(account) { _, store ->
|
||||
val snoozes = account.optJSONArray("snoozedMessages") ?: JSONArray()
|
||||
val pending = (0 until snoozes.length())
|
||||
.map { snoozes.getJSONObject(it) }
|
||||
.filter { it.optLong("dueAt") > System.currentTimeMillis() }
|
||||
.sortedBy { it.optLong("dueAt") }
|
||||
val page = pending.drop(offset.coerceAtLeast(0)).take(20)
|
||||
val messages = JSONArray()
|
||||
for (snooze in page) {
|
||||
val sourceFolder = snooze.optString("folder", "Inbox")
|
||||
val folder = resolveFolder(store, sourceFolder) ?: continue
|
||||
folder.open(Folder.READ_ONLY)
|
||||
try {
|
||||
val message = (folder as? UIDFolder)?.getMessageByUID(snooze.optLong("uid")) ?: continue
|
||||
messages.put(messageJson(folder, message, "Snoozed")
|
||||
.put("sourceFolder", sourceFolder)
|
||||
.put("snoozedUntil", snooze.optLong("dueAt")))
|
||||
} finally {
|
||||
folder.close(false)
|
||||
}
|
||||
}
|
||||
JSONObject()
|
||||
.put("messages", messages)
|
||||
.put("total", pending.size)
|
||||
.put("start", if (page.isEmpty()) 0 else offset + 1)
|
||||
.put("end", offset + messages.length())
|
||||
.put("folder", "Snoozed")
|
||||
}
|
||||
|
||||
fun updateMessage(account: JSONObject, input: JSONObject): JSONObject =
|
||||
withStore(account) { _, store ->
|
||||
val uid = input.optLong("uid")
|
||||
require(uid > 0) { "That message could not be identified." }
|
||||
val action = input.optString("action")
|
||||
require(action in setOf("star", "unread", "archive", "delete")) { "That mail action is not supported." }
|
||||
require(action in setOf("star", "unread", "archive", "delete", "inbox")) { "That mail action is not supported." }
|
||||
val source = resolveFolder(store, input.optString("folder", "Inbox"))
|
||||
?: throw IllegalStateException("That mail folder could not be found.")
|
||||
var expungeDeleted = false
|
||||
@@ -99,18 +139,38 @@ internal object AndroidMailService {
|
||||
when (action) {
|
||||
"star" -> message.setFlag(Flags.Flag.FLAGGED, input.optBoolean("enabled"))
|
||||
"unread" -> message.setFlag(Flags.Flag.SEEN, !input.optBoolean("enabled"))
|
||||
"archive", "delete" -> {
|
||||
expungeDeleted = true
|
||||
val targetName = if (action == "archive") "Archive" else "Trash"
|
||||
"archive", "delete", "inbox" -> {
|
||||
val targetName = when (action) {
|
||||
"archive" -> "Archive"
|
||||
"delete" -> "Trash"
|
||||
else -> "Inbox"
|
||||
}
|
||||
val target = resolveFolder(store, targetName) ?: store.getFolder(targetName)
|
||||
if (!target.exists()) target.create(Folder.HOLDS_MESSAGES)
|
||||
source.copyMessages(arrayOf(message), target)
|
||||
message.setFlag(Flags.Flag.DELETED, true)
|
||||
if (source.fullName == target.fullName) {
|
||||
if (action == "delete") {
|
||||
expungeDeleted = true
|
||||
message.setFlag(Flags.Flag.DELETED, true)
|
||||
}
|
||||
} else {
|
||||
expungeDeleted = true
|
||||
source.copyMessages(arrayOf(message), target)
|
||||
message.setFlag(Flags.Flag.DELETED, true)
|
||||
}
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
source.close(expungeDeleted)
|
||||
}
|
||||
if (action == "archive" || action == "delete" || action == "inbox") {
|
||||
AndroidAccountVault.setSnooze(
|
||||
account.optString("id"),
|
||||
uid,
|
||||
input.optString("folder", "Inbox"),
|
||||
null,
|
||||
""
|
||||
)
|
||||
}
|
||||
JSONObject().put("ok", true)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user