Synchronize pushed add/change/delete

pull/162/head
M66B 5 years ago
parent e210b19c4a
commit 2f62dc21f1

@ -311,6 +311,7 @@ The low priority status bar notification shows the number of pending operations,
* *add*: add message to remote folder
* *move*: move message to another remote folder
* *copy*: copy message to another remote folder
* *fetch*: fetched pushed message
* *delete*: delete message from remote folder
* *send*: send message
* *seen*: mark message as read/unread in remote folder
@ -323,6 +324,7 @@ The low priority status bar notification shows the number of pending operations,
* *attachment*: download attachment
* *sync*: synchronize local and remote messages
* *subscribe*: subscribe to remote folder
* *send*: send message
Operations are processed only when there is a connection to the email server or when manually synchronizing.
See also [this FAQ](#user-content-faq16).

@ -79,8 +79,6 @@ import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Properties;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;
import java.util.regex.Pattern;
@ -115,7 +113,6 @@ import static androidx.core.app.NotificationCompat.DEFAULT_SOUND;
class Core {
private static int lastUnseen = -1;
private static ConcurrentMap<Long, Long> lockFolders = new ConcurrentHashMap<>();
private static final int MAX_NOTIFICATION_COUNT = 100; // per group
private static final int SYNC_CHUNCK_SIZE = 200;
@ -167,7 +164,8 @@ class Core {
db.operation().setOperationState(op.id, "executing");
if (message == null) {
if (!EntityOperation.SYNC.equals(op.name) &&
if (!EntityOperation.FETCH.equals(op.name) &&
!EntityOperation.SYNC.equals(op.name) &&
!EntityOperation.SUBSCRIBE.equals(op.name))
throw new MessageRemovedException();
} else {
@ -177,6 +175,14 @@ class Core {
// Operations should use database transaction when needed
// Squash operations
if (i + 1 < ops.size() && op.canSquash(ops.get(i + 1))) {
Log.i(folder.name +
" squashing op=" + op.id + "/" + op.name +
" msg=" + op.message + " args=" + op.args);
continue;
}
switch (op.name) {
case EntityOperation.SEEN:
onSeen(context, jargs, folder, message, (IMAPFolder) ifolder);
@ -195,23 +201,6 @@ class Core {
break;
case EntityOperation.ADD:
boolean squash = false;
for (int j = i + 1; j < ops.size(); j++) {
EntityOperation next = ops.get(j);
if (next.message != null &&
next.message.equals(op.message) &&
(EntityOperation.ADD.equals(next.name) ||
EntityOperation.DELETE.equals(next.name))) {
squash = true;
break;
}
}
if (squash)
Log.i(folder.name +
" squashing op=" + op.id + "/" + op.name +
" msg=" + op.message +
" args=" + op.args);
else
onAdd(context, jargs, folder, message, (IMAPStore) istore, (IMAPFolder) ifolder);
break;
@ -223,6 +212,10 @@ class Core {
onMove(context, jargs, true, folder, message, (IMAPStore) istore, (IMAPFolder) ifolder);
break;
case EntityOperation.FETCH:
onFetch(context, jargs, folder, (IMAPFolder) ifolder, state);
break;
case EntityOperation.DELETE:
onDelete(context, jargs, folder, message, (IMAPFolder) ifolder);
break;
@ -663,10 +656,45 @@ class Core {
}
}
private static void onFetch(Context context, JSONArray jargs, EntityFolder folder, IMAPFolder ifolder, State state) throws JSONException, MessagingException, IOException {
long uid = jargs.getLong(0);
DB db = DB.getInstance(context);
try {
EntityAccount account = db.account().getAccount(folder.account);
boolean download = db.folder().getFolderDownload(folder.id);
List<EntityRule> rules = db.rule().getEnabledRules(folder.id);
IMAPMessage imessage = (IMAPMessage) ifolder.getMessageByUID(uid);
if (imessage == null)
throw new MessageRemovedException();
FetchProfile fp = new FetchProfile();
fp.add(FetchProfile.Item.ENVELOPE);
fp.add(FetchProfile.Item.FLAGS);
fp.add(FetchProfile.Item.CONTENT_INFO); // body structure
fp.add(UIDFolder.FetchProfileItem.UID);
fp.add(IMAPFolder.FetchProfileItem.HEADERS);
//fp.add(IMAPFolder.FetchProfileItem.MESSAGE);
fp.add(FetchProfile.Item.SIZE);
fp.add(IMAPFolder.FetchProfileItem.INTERNALDATE);
ifolder.fetch(new Message[]{imessage}, fp);
EntityMessage message = synchronizeMessage(context, account, folder, ifolder, imessage, false, download, rules, state);
if (download)
downloadMessage(context, folder, ifolder, imessage, message.id, state);
imessage.invalidateHeaders();
} finally {
int count = ifolder.getMessageCount();
db.folder().setFolderTotal(folder.id, count < 0 ? null : count);
}
}
private static void onDelete(Context context, JSONArray jargs, EntityFolder folder, EntityMessage message, IMAPFolder ifolder) throws MessagingException {
// Delete message
DB db = DB.getInstance(context);
try {
boolean deleted = false;
if (message.uid != null) {
@ -702,6 +730,10 @@ class Core {
ifolder.expunge();
db.message().deleteMessage(message.id);
} finally {
int count = ifolder.getMessageCount();
db.folder().setFolderTotal(folder.id, count < 0 ? null : count);
}
}
private static void onHeaders(Context context, EntityFolder folder, EntityMessage message, IMAPFolder ifolder) throws MessagingException {
@ -1387,9 +1419,6 @@ class Core {
IMAPFolder ifolder, IMAPMessage imessage,
boolean browsed, boolean download,
List<EntityRule> rules, State state) throws MessagingException, IOException {
// Instead of locking the database while performing message I/O
lockFolders.putIfAbsent(folder.id, folder.id);
synchronized (lockFolders.get(folder.id)) {
long uid = ifolder.getUID(imessage);
if (imessage.isExpunged()) {
@ -1704,7 +1733,6 @@ class Core {
return message;
}
}
private static EntityIdentity matchIdentity(Context context, EntityFolder folder, EntityMessage message) {
DB db = DB.getInstance(context);

@ -382,9 +382,6 @@ public interface DaoMessage {
@Query("UPDATE message SET ui_hide = :ui_hide WHERE id = :id")
int setMessageUiHide(long id, long ui_hide);
@Query("UPDATE message SET ui_hide = :ui_hide WHERE folder = :folder AND uid = :uid")
int setMessageUiHide(long folder, long uid, long ui_hide);
@Query("UPDATE message SET ui_ignored = :ui_ignored WHERE id = :id")
int setMessageUiIgnored(long id, boolean ui_ignored);

@ -78,7 +78,7 @@ public class EntityOperation {
static final String MOVE = "move";
static final String COPY = "copy";
static final String DELETE = "delete";
static final String SEND = "send";
static final String FETCH = "fetch";
static final String SEEN = "seen";
static final String ANSWERED = "answered";
static final String FLAG = "flag";
@ -89,6 +89,7 @@ public class EntityOperation {
static final String ATTACHMENT = "attachment";
static final String SYNC = "sync";
static final String SUBSCRIBE = "subscribe";
static final String SEND = "send";
static void queue(Context context, EntityMessage message, String name, Object... values) {
DB db = DB.getInstance(context);
@ -259,6 +260,36 @@ public class EntityOperation {
ServiceSynchronize.process(context, false);
}
static void queue(Context context, EntityFolder folder, String name, Object... values) {
DB db = DB.getInstance(context);
JSONArray jargs = new JSONArray();
for (Object value : values)
jargs.put(value);
EntityOperation op = new EntityOperation();
op.account = folder.account;
op.folder = folder.id;
op.message = null;
op.name = name;
op.args = jargs.toString();
op.created = new Date().getTime();
op.id = db.operation().insertOperation(op);
Log.i("Queued op=" + op.id + "/" + op.name +
" folder=" + op.folder + " msg=" + op.message +
" args=" + op.args);
Map<String, String> crumb = new HashMap<>();
crumb.put("name", op.name);
crumb.put("args", op.args);
crumb.put("folder", op.account + ":" + op.folder);
if (op.message != null)
crumb.put("message", Long.toString(op.message));
crumb.put("free", Integer.toString(Log.getFreeMemMb()));
Log.breadcrumb("queued", crumb);
}
static void sync(Context context, long fid, boolean foreground) {
DB db = DB.getInstance(context);
@ -308,6 +339,21 @@ public class EntityOperation {
Log.i("Queued subscribe=" + subscribe + " folder=" + folder);
}
boolean canSquash(EntityOperation next) throws JSONException {
if (Objects.equals(this.message, next.message) &&
ADD.equals(this.name) &&
(ADD.equals(next.name) || DELETE.equals(next.name)))
return true;
if (FETCH.equals(this.name) && FETCH.equals(next.name)) {
JSONArray jargs1 = new JSONArray(this.args);
JSONArray jargs2 = new JSONArray(next.args);
return (jargs1.optLong(0, -1) == jargs2.optLong(0, -2));
}
return false;
}
@Override
public boolean equals(Object obj) {
if (obj instanceof EntityOperation) {

@ -44,9 +44,7 @@ import androidx.lifecycle.Observer;
import androidx.preference.PreferenceManager;
import com.sun.mail.imap.IMAPFolder;
import com.sun.mail.imap.IMAPMessage;
import java.io.IOException;
import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Calendar;
@ -63,17 +61,14 @@ import java.util.concurrent.Executors;
import java.util.concurrent.RejectedExecutionException;
import javax.mail.AuthenticationFailedException;
import javax.mail.FetchProfile;
import javax.mail.Folder;
import javax.mail.FolderClosedException;
import javax.mail.FolderNotFoundException;
import javax.mail.Message;
import javax.mail.MessageRemovedException;
import javax.mail.MessagingException;
import javax.mail.NoSuchProviderException;
import javax.mail.ReadOnlyFolderException;
import javax.mail.StoreClosedException;
import javax.mail.UIDFolder;
import javax.mail.event.ConnectionAdapter;
import javax.mail.event.ConnectionEvent;
import javax.mail.event.FolderAdapter;
@ -890,52 +885,10 @@ public class ServiceSynchronize extends ServiceBase {
wlMessage.acquire();
Log.i(folder.name + " messages added");
FetchProfile fp = new FetchProfile();
fp.add(FetchProfile.Item.ENVELOPE);
fp.add(FetchProfile.Item.FLAGS);
fp.add(FetchProfile.Item.CONTENT_INFO); // body structure
fp.add(UIDFolder.FetchProfileItem.UID);
fp.add(IMAPFolder.FetchProfileItem.HEADERS);
//fp.add(IMAPFolder.FetchProfileItem.MESSAGE);
fp.add(FetchProfile.Item.SIZE);
fp.add(IMAPFolder.FetchProfileItem.INTERNALDATE);
ifolder.fetch(e.getMessages(), fp);
boolean download = db.folder().getFolderDownload(folder.id);
for (Message imessage : e.getMessages())
try {
EntityMessage message = Core.synchronizeMessage(
ServiceSynchronize.this,
account, folder,
ifolder, (IMAPMessage) imessage,
false, download,
db.rule().getEnabledRules(folder.id),
state);
if (download)
Core.downloadMessage(ServiceSynchronize.this,
folder, ifolder,
(IMAPMessage) imessage, message.id, state);
} catch (MessageRemovedException ex) {
Log.w(folder.name, ex);
} catch (FolderClosedException ex) {
throw ex;
} catch (IOException ex) {
if (ex.getCause() instanceof MessagingException) {
Log.w(folder.name, ex);
db.folder().setFolderError(folder.id, Helper.formatThrowable(ex));
} else
throw ex;
} catch (Throwable ex) {
Log.e(folder.name, ex);
db.folder().setFolderError(folder.id, Helper.formatThrowable(ex));
} finally {
((IMAPMessage) imessage).invalidateHeaders();
for (Message imessage : e.getMessages()) {
long uid = ifolder.getUID(imessage);
EntityOperation.queue(ServiceSynchronize.this, folder, EntityOperation.FETCH, uid);
}
int count = ifolder.getMessageCount();
db.folder().setFolderTotal(folder.id, count < 0 ? null : count);
} catch (Throwable ex) {
Log.e(folder.name, ex);
EntityLog.log(
@ -952,29 +905,26 @@ public class ServiceSynchronize extends ServiceBase {
try {
wlMessage.acquire();
Log.i(folder.name + " messages removed");
for (Message imessage : e.getMessages())
try {
for (Message imessage : e.getMessages()) {
long uid = ifolder.getUID(imessage);
try {
db.beginTransaction();
DB db = DB.getInstance(ServiceSynchronize.this);
int count = db.message().setMessageUiHide(folder.id, uid, new Date().getTime());
// Will be deleted on next sync
EntityMessage message = db.message().getMessageByUid(folder.id, uid);
if (message != null)
EntityOperation.queue(ServiceSynchronize.this, message, EntityOperation.DELETE);
Log.i(folder.name + " deleted uid=" + uid + " count=" + count);
} catch (MessageRemovedException ex) {
Log.w(folder.name, ex);
db.setTransactionSuccessful();
} finally {
((IMAPMessage) imessage).invalidateHeaders();
db.endTransaction();
}
}
int count = ifolder.getMessageCount();
db.folder().setFolderTotal(folder.id, count < 0 ? null : count);
} catch (Throwable ex) {
Log.e(folder.name, ex);
EntityLog.log(
ServiceSynchronize.this,
folder.name + " " + Helper.formatThrowable(ex, false));
db.folder().setFolderError(folder.id, Helper.formatThrowable(ex));
state.error(ex);
} finally {
wlMessage.release();
@ -990,44 +940,10 @@ public class ServiceSynchronize extends ServiceBase {
public void messageChanged(MessageChangedEvent e) {
try {
wlMessage.acquire();
try {
Log.i(folder.name + " message changed");
FetchProfile fp = new FetchProfile();
fp.add(UIDFolder.FetchProfileItem.UID);
fp.add(IMAPFolder.FetchProfileItem.FLAGS);
ifolder.fetch(new Message[]{e.getMessage()}, fp);
boolean download = db.folder().getFolderDownload(folder.id);
EntityMessage message = Core.synchronizeMessage(
ServiceSynchronize.this,
account, folder,
ifolder, (IMAPMessage) e.getMessage(),
false, download,
db.rule().getEnabledRules(folder.id),
state);
if (download)
Core.downloadMessage(ServiceSynchronize.this,
folder, ifolder,
(IMAPMessage) e.getMessage(), message.id, state);
} catch (MessageRemovedException ex) {
Log.w(folder.name, ex);
} catch (FolderClosedException ex) {
throw ex;
} catch (IOException ex) {
if (ex.getCause() instanceof MessagingException) {
Log.w(folder.name, ex);
db.folder().setFolderError(folder.id, Helper.formatThrowable(ex));
} else
throw ex;
} catch (Throwable ex) {
Log.e(folder.name, ex);
db.folder().setFolderError(folder.id, Helper.formatThrowable(ex));
} finally {
((IMAPMessage) e.getMessage()).invalidateHeaders();
}
long uid = ifolder.getUID(e.getMessage());
EntityOperation.queue(ServiceSynchronize.this, folder, EntityOperation.FETCH, uid);
} catch (Throwable ex) {
Log.e(folder.name, ex);
EntityLog.log(

Loading…
Cancel
Save