Fixed race condition while handling sent messages

pull/148/head
M66B 7 years ago
parent 28fc0d9a18
commit 2f9d4c7ebb

@ -249,11 +249,12 @@ public interface DaoMessage {
" AND NOT ui_browsed") " AND NOT ui_browsed")
List<Long> getUids(long folder, Long received); List<Long> getUids(long folder, Long received);
@Query("SELECT * FROM message" + @Query("SELECT message.* FROM message" +
" WHERE folder = :folder" + " JOIN folder on folder.id = message.folder" +
" AND uid IS NULL" + " WHERE message.account = :account" +
" AND ui_browsed") " AND folder.type = '" + EntityFolder.OUTBOX + "'" +
List<EntityMessage> getSentOrphans(long folder); " AND sent IS NOT NULL")
List<EntityMessage> getSentOrphans(long account);
@Query("SELECT * FROM message WHERE NOT ui_snoozed IS NULL") @Query("SELECT * FROM message WHERE NOT ui_snoozed IS NULL")
List<EntityMessage> getSnoozed(); List<EntityMessage> getSnoozed();

@ -82,8 +82,8 @@ public class EntityIdentity {
@NonNull @NonNull
public Boolean read_receipt = false; public Boolean read_receipt = false;
@NonNull @NonNull
public Boolean store_sent = false; public Boolean store_sent = false; // obsolete
public Long sent_folder; // obsolete public Long sent_folder = null; // obsolete
public Boolean tbd; public Boolean tbd;
public String state; public String state;
public String error; public String error;
@ -122,7 +122,6 @@ public class EntityIdentity {
json.put("plain_only", plain_only); json.put("plain_only", plain_only);
json.put("delivery_receipt", delivery_receipt); json.put("delivery_receipt", delivery_receipt);
json.put("read_receipt", read_receipt); json.put("read_receipt", read_receipt);
json.put("store_sent", store_sent);
// not state // not state
// not error // not error
return json; return json;
@ -165,9 +164,6 @@ public class EntityIdentity {
if (json.has("read_receipt")) if (json.has("read_receipt"))
identity.read_receipt = json.getBoolean("read_receipt"); identity.read_receipt = json.getBoolean("read_receipt");
if (json.has("store_sent"))
identity.store_sent = json.getBoolean("store_sent");
return identity; return identity;
} }
@ -195,7 +191,6 @@ public class EntityIdentity {
(this.bcc == null ? other.bcc == null : this.bcc.equals(other.bcc)) && (this.bcc == null ? other.bcc == null : this.bcc.equals(other.bcc)) &&
this.delivery_receipt.equals(other.delivery_receipt) && this.delivery_receipt.equals(other.delivery_receipt) &&
this.read_receipt.equals(other.read_receipt) && this.read_receipt.equals(other.read_receipt) &&
this.store_sent.equals(other.store_sent) &&
(this.tbd == null ? other.tbd == null : this.tbd.equals(other.tbd)) && (this.tbd == null ? other.tbd == null : this.tbd.equals(other.tbd)) &&
(this.state == null ? other.state == null : this.state.equals(other.state)) && (this.state == null ? other.state == null : this.state.equals(other.state)) &&
(this.error == null ? other.error == null : this.error.equals(other.error)) && (this.error == null ? other.error == null : this.error.equals(other.error)) &&

@ -102,8 +102,6 @@ public class FragmentIdentity extends FragmentBase {
private CheckBox cbDeliveryReceipt; private CheckBox cbDeliveryReceipt;
private CheckBox cbReadReceipt; private CheckBox cbReadReceipt;
private CheckBox cbStoreSent;
private Button btnSave; private Button btnSave;
private ContentLoadingProgressBar pbSave; private ContentLoadingProgressBar pbSave;
private TextView tvError; private TextView tvError;
@ -169,8 +167,6 @@ public class FragmentIdentity extends FragmentBase {
cbDeliveryReceipt = view.findViewById(R.id.cbDeliveryReceipt); cbDeliveryReceipt = view.findViewById(R.id.cbDeliveryReceipt);
cbReadReceipt = view.findViewById(R.id.cbReadReceipt); cbReadReceipt = view.findViewById(R.id.cbReadReceipt);
cbStoreSent = view.findViewById(R.id.cbStoreSent);
btnSave = view.findViewById(R.id.btnSave); btnSave = view.findViewById(R.id.btnSave);
pbSave = view.findViewById(R.id.pbSave); pbSave = view.findViewById(R.id.pbSave);
tvError = view.findViewById(R.id.tvError); tvError = view.findViewById(R.id.tvError);
@ -467,7 +463,6 @@ public class FragmentIdentity extends FragmentBase {
args.putBoolean("plain_only", cbPlainOnly.isChecked()); args.putBoolean("plain_only", cbPlainOnly.isChecked());
args.putBoolean("delivery_receipt", cbDeliveryReceipt.isChecked()); args.putBoolean("delivery_receipt", cbDeliveryReceipt.isChecked());
args.putBoolean("read_receipt", cbReadReceipt.isChecked()); args.putBoolean("read_receipt", cbReadReceipt.isChecked());
args.putBoolean("store_sent", cbStoreSent.isChecked());
args.putLong("account", account == null ? -1 : account.id); args.putLong("account", account == null ? -1 : account.id);
args.putInt("auth_type", auth_type); args.putInt("auth_type", auth_type);
args.putString("host", etHost.getText().toString()); args.putString("host", etHost.getText().toString());
@ -527,7 +522,6 @@ public class FragmentIdentity extends FragmentBase {
boolean plain_only = args.getBoolean("plain_only"); boolean plain_only = args.getBoolean("plain_only");
boolean delivery_receipt = args.getBoolean("delivery_receipt"); boolean delivery_receipt = args.getBoolean("delivery_receipt");
boolean read_receipt = args.getBoolean("read_receipt"); boolean read_receipt = args.getBoolean("read_receipt");
boolean store_sent = args.getBoolean("store_sent");
if (TextUtils.isEmpty(name)) if (TextUtils.isEmpty(name))
throw new IllegalArgumentException(context.getString(R.string.title_no_name)); throw new IllegalArgumentException(context.getString(R.string.title_no_name));
@ -632,8 +626,6 @@ public class FragmentIdentity extends FragmentBase {
identity.plain_only = plain_only; identity.plain_only = plain_only;
identity.delivery_receipt = delivery_receipt; identity.delivery_receipt = delivery_receipt;
identity.read_receipt = read_receipt; identity.read_receipt = read_receipt;
identity.store_sent = store_sent;
identity.sent_folder = null;
identity.error = null; identity.error = null;
identity.last_connected = last_connected; identity.last_connected = last_connected;
@ -734,7 +726,6 @@ public class FragmentIdentity extends FragmentBase {
cbPlainOnly.setChecked(identity == null ? false : identity.plain_only); cbPlainOnly.setChecked(identity == null ? false : identity.plain_only);
cbDeliveryReceipt.setChecked(identity == null ? false : identity.delivery_receipt); cbDeliveryReceipt.setChecked(identity == null ? false : identity.delivery_receipt);
cbReadReceipt.setChecked(identity == null ? false : identity.read_receipt); cbReadReceipt.setChecked(identity == null ? false : identity.read_receipt);
cbStoreSent.setChecked(identity == null ? false : identity.store_sent);
color = (identity == null || identity.color == null ? Color.TRANSPARENT : identity.color); color = (identity == null || identity.color == null ? Color.TRANSPARENT : identity.color);

@ -338,8 +338,6 @@ public class FragmentQuickSetup extends FragmentBase {
identity.bcc = null; identity.bcc = null;
identity.delivery_receipt = false; identity.delivery_receipt = false;
identity.read_receipt = false; identity.read_receipt = false;
identity.store_sent = false;
identity.sent_folder = null;
identity.error = null; identity.error = null;
identity.id = db.identity().insertIdentity(identity); identity.id = db.identity().insertIdentity(identity);

@ -839,6 +839,7 @@ public class ServiceSynchronize extends LifecycleService {
wlAccount.acquire(); wlAccount.acquire();
String type = (e.getMessageType() == StoreEvent.ALERT ? "alert" : "notice"); String type = (e.getMessageType() == StoreEvent.ALERT ? "alert" : "notice");
if (e.getMessageType() == StoreEvent.ALERT) { if (e.getMessageType() == StoreEvent.ALERT) {
Log.w(account.name + " " + type + ": " + e.getMessage());
EntityLog.log(ServiceSynchronize.this, account.name + " " + type + ": " + e.getMessage()); EntityLog.log(ServiceSynchronize.this, account.name + " " + type + ": " + e.getMessage());
db.account().setAccountError(account.id, e.getMessage()); db.account().setAccountError(account.id, e.getMessage());
reportError(account, null, new AlertException(e.getMessage())); reportError(account, null, new AlertException(e.getMessage()));
@ -1550,6 +1551,7 @@ public class ServiceSynchronize extends LifecycleService {
db.operation().deleteOperation(op.id); db.operation().deleteOperation(op.id);
} catch (Throwable ex) { } catch (Throwable ex) {
// TODO: SMTP response codes: https://www.ietf.org/rfc/rfc821.txt // TODO: SMTP response codes: https://www.ietf.org/rfc/rfc821.txt
Log.e(folder.name, ex);
reportError(account, folder, ex); reportError(account, folder, ex);
db.operation().setOperationError(op.id, Helper.formatThrowable(ex)); db.operation().setOperationError(op.id, Helper.formatThrowable(ex));
@ -1977,116 +1979,69 @@ public class ServiceSynchronize extends LifecycleService {
db.identity().setIdentityState(ident.id, "connected"); db.identity().setIdentityState(ident.id, "connected");
// Append replied/forwarded text
String body = Helper.readText(EntityMessage.getFile(this, message.id));
File refFile = EntityMessage.getRefFile(this, message.id);
if (refFile.exists())
body += Helper.readText(refFile);
// Send message // Send message
Long sid = null; Address[] to = imessage.getAllRecipients();
itransport.sendMessage(imessage, to);
EntityLog.log(this, "Sent via " + ident.host + "/" + ident.user +
" to " + TextUtils.join(", ", to));
try { try {
// Append replied/forwarded text db.beginTransaction();
String body = Helper.readText(EntityMessage.getFile(this, message.id));
File refFile = EntityMessage.getRefFile(this, message.id);
if (refFile.exists())
body += Helper.readText(refFile);
EntityFolder sent = db.folder().getFolderByType(ident.account, EntityFolder.SENT);
if (sent != null) {
long id = message.id;
long folder = message.folder;
message.id = null;
message.folder = sent.id;
message.seen = true;
message.ui_seen = true;
message.ui_hide = true;
message.ui_browsed = true; // prevent deleting on sync
message.error = null;
message.id = db.message().insertMessage(message);
Helper.writeText(EntityMessage.getFile(this, message.id), body);
sid = message.id;
message.id = id;
message.folder = folder;
message.seen = false;
message.ui_seen = false;
message.ui_browsed = false;
message.ui_hide = false;
EntityAttachment.copy(this, db, message.id, sid);
}
Address[] to = imessage.getAllRecipients(); db.message().setMessageSent(message.id, imessage.getSentDate().getTime());
itransport.sendMessage(imessage, to); db.message().setMessageSeen(message.id, true);
EntityLog.log(this, "Sent via " + ident.host + "/" + ident.user + db.message().setMessageUiSeen(message.id, true);
" to " + TextUtils.join(", ", to)); db.message().setMessageError(message.id, null);
Helper.writeText(EntityMessage.getFile(this, message.id), body);
try { db.setTransactionSuccessful();
db.beginTransaction(); } finally {
db.endTransaction();
if (sid == null) { }
db.message().setMessageSent(message.id, imessage.getSentDate().getTime());
db.message().setMessageSeen(message.id, true);
db.message().setMessageUiSeen(message.id, true);
db.message().setMessageError(message.id, null);
Helper.writeText(EntityMessage.getFile(this, message.id), body);
} else {
db.message().setMessageSent(sid, imessage.getSentDate().getTime());
db.message().setMessageUiHide(sid, false);
db.message().deleteMessage(message.id);
if (ident.store_sent) {
message.id = sid;
message.folder = sent.id;
EntityOperation.queue(this, db, message, EntityOperation.ADD);
}
}
db.setTransactionSuccessful(); if (refFile.exists())
} finally { refFile.delete();
db.endTransaction();
}
if (refFile.exists()) if (message.inreplyto != null) {
refFile.delete(); List<EntityMessage> replieds = db.message().getMessageByMsgId(message.account, message.inreplyto);
for (EntityMessage replied : replieds)
if (replied.uid != null)
EntityOperation.queue(this, db, replied, EntityOperation.ANSWERED, true);
}
if (message.inreplyto != null) { db.identity().setIdentityConnected(ident.id, new Date().getTime());
List<EntityMessage> replieds = db.message().getMessageByMsgId(message.account, message.inreplyto); db.identity().setIdentityError(ident.id, null);
for (EntityMessage replied : replieds)
if (replied.uid != null)
EntityOperation.queue(this, db, replied, EntityOperation.ANSWERED, true);
}
db.identity().setIdentityConnected(ident.id, new Date().getTime()); NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
db.identity().setIdentityError(ident.id, null); nm.cancel("send", message.identity.intValue());
NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); if (message.to != null)
nm.cancel("send", message.identity.intValue()); for (Address recipient : message.to) {
String email = ((InternetAddress) recipient).getAddress();
if (message.to != null) String name = ((InternetAddress) recipient).getPersonal();
for (Address recipient : message.to) { List<EntityContact> contacts = db.contact().getContacts(EntityContact.TYPE_TO, email);
String email = ((InternetAddress) recipient).getAddress(); if (contacts.size() == 0) {
String name = ((InternetAddress) recipient).getPersonal(); EntityContact contact = new EntityContact();
List<EntityContact> contacts = db.contact().getContacts(EntityContact.TYPE_TO, email); contact.type = EntityContact.TYPE_TO;
if (contacts.size() == 0) { contact.email = email;
EntityContact contact = new EntityContact(); contact.name = name;
contact.type = EntityContact.TYPE_TO; db.contact().insertContact(contact);
contact.email = email; Log.i("Inserted recipient contact=" + contact);
} else {
EntityContact contact = contacts.get(0);
if (name != null && !name.equals(contact.name)) {
contact.name = name; contact.name = name;
db.contact().insertContact(contact); db.contact().updateContact(contact);
Log.i("Inserted recipient contact=" + contact); Log.i("Updated recipient contact=" + contact);
} else {
EntityContact contact = contacts.get(0);
if (name != null && !name.equals(contact.name)) {
contact.name = name;
db.contact().updateContact(contact);
Log.i("Updated recipient contact=" + contact);
}
} }
} }
}
} catch (Throwable ex) {
if (sid != null)
db.message().deleteMessage(sid);
throw ex;
}
} catch (MessagingException ex) { } catch (MessagingException ex) {
if (ex instanceof SendFailedException) { if (ex instanceof SendFailedException) {
SendFailedException sfe = (SendFailedException) ex; SendFailedException sfe = (SendFailedException) ex;
@ -2532,12 +2487,13 @@ public class ServiceSynchronize extends LifecycleService {
// Add local sent messages to remote sent folder // Add local sent messages to remote sent folder
if (EntityFolder.SENT.equals(folder.type)) { if (EntityFolder.SENT.equals(folder.type)) {
List<EntityMessage> orphans = db.message().getSentOrphans(folder.id); List<EntityMessage> orphans = db.message().getSentOrphans(folder.account);
Log.i(folder.name + " sent orphans=" + orphans.size()); Log.i(folder.name + " sent orphans=" + orphans.size() + " account=" + folder.account);
for (EntityMessage orphan : orphans) { for (EntityMessage orphan : orphans) {
Log.i(folder.name + " adding orphan id=" + orphan.id); Log.i(folder.name + " adding orphan id=" + orphan.id + " sent=" + new Date(orphan.sent));
orphan.folder = folder.id;
db.message().updateMessage(orphan);
EntityOperation.queue(this, db, orphan, EntityOperation.ADD); EntityOperation.queue(this, db, orphan, EntityOperation.ADD);
db.message().setMessageUiBrowsed(orphan.id, false); // Prevent adding again
} }
} }
@ -2631,7 +2587,8 @@ public class ServiceSynchronize extends LifecycleService {
" folder=" + dfolder.type + ":" + dup.folder + "/" + folder.type + ":" + folder.id + " folder=" + dfolder.type + ":" + dup.folder + "/" + folder.type + ":" + folder.id +
" msgid=" + dup.msgid + " thread=" + dup.thread); " msgid=" + dup.msgid + " thread=" + dup.thread);
if (dup.folder.equals(folder.id)) { if (dup.folder.equals(folder.id) ||
(EntityFolder.OUTBOX.equals(dfolder.type) && EntityFolder.SENT.equals(folder.type))) {
String thread = helper.getThreadId(uid); String thread = helper.getThreadId(uid);
Log.i(folder.name + " found as id=" + dup.id + Log.i(folder.name + " found as id=" + dup.id +
" uid=" + dup.uid + "/" + uid + " uid=" + dup.uid + "/" + uid +

@ -485,25 +485,6 @@
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/cbReadReceipt" /> app:layout_constraintTop_toBottomOf="@id/cbReadReceipt" />
<CheckBox
android:id="@+id/cbStoreSent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:text="@string/title_identity_store_sent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/tvReceipt" />
<TextView
android:id="@+id/tvStoreSent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/title_identity_store_sent_remark"
android:textAppearance="@style/TextAppearance.AppCompat.Small"
android:textStyle="italic"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/cbStoreSent" />
<Button <Button
android:id="@+id/btnSave" android:id="@+id/btnSave"
android:layout_width="wrap_content" android:layout_width="wrap_content"
@ -512,7 +493,7 @@
android:tag="disable" android:tag="disable"
android:text="@string/title_save" android:text="@string/title_save"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/tvStoreSent" /> app:layout_constraintTop_toBottomOf="@id/tvReceipt" />
<eu.faircode.email.ContentLoadingProgressBar <eu.faircode.email.ContentLoadingProgressBar
android:id="@+id/pbSave" android:id="@+id/pbSave"

@ -199,8 +199,6 @@
<string name="title_identity_read_receipt">Request read receipt</string> <string name="title_identity_read_receipt">Request read receipt</string>
<string name="title_identity_delivery_receipt">Request delivery receipt</string> <string name="title_identity_delivery_receipt">Request delivery receipt</string>
<string name="title_identity_receipt_remark">Most providers ignore receipt requests</string> <string name="title_identity_receipt_remark">Most providers ignore receipt requests</string>
<string name="title_identity_store_sent">Store sent messages</string>
<string name="title_identity_store_sent_remark">Enable this only if your provider does not automatically stores sent messages</string>
<string name="title_optional">Optional</string> <string name="title_optional">Optional</string>
<string name="title_account_linked">Linked account</string> <string name="title_account_linked">Linked account</string>
<string name="title_account_name">Account name</string> <string name="title_account_name">Account name</string>

Loading…
Cancel
Save