Skip to content

Commit 0586acc

Browse files
committed
Added a logic which prevents creation of duplicate messages after the app crash.| #328
1 parent 9592407 commit 0586acc

File tree

4 files changed

+59
-48
lines changed

4 files changed

+59
-48
lines changed

FlowCrypt/src/main/java/com/flowcrypt/email/api/email/model/OutgoingMessageInfo.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ public OutgoingMessageInfo[] newArray(int size) {
4343
private ArrayList<AttachmentInfo> forwardedAttachmentInfoList;
4444
private MessageEncryptionType messageEncryptionType;
4545
private boolean isForwarded;
46+
private long uid;
4647

4748
public OutgoingMessageInfo() {
4849
}
@@ -60,6 +61,7 @@ protected OutgoingMessageInfo(Parcel in) {
6061
this.messageEncryptionType = tmpMessageEncryptionType == -1 ? null : MessageEncryptionType.values()
6162
[tmpMessageEncryptionType];
6263
this.isForwarded = in.readByte() != 0;
64+
this.uid = in.readLong();
6365
}
6466

6567
@Override
@@ -79,6 +81,7 @@ public void writeToParcel(Parcel dest, int flags) {
7981
dest.writeTypedList(this.forwardedAttachmentInfoList);
8082
dest.writeInt(this.messageEncryptionType == null ? -1 : this.messageEncryptionType.ordinal());
8183
dest.writeByte(this.isForwarded ? (byte) 1 : (byte) 0);
84+
dest.writeLong(this.uid);
8285
}
8386

8487
public PgpContact[] getCcPgpContacts() {
@@ -152,4 +155,12 @@ public ArrayList<AttachmentInfo> getForwardedAttachmentInfoList() {
152155
public void setForwardedAttachmentInfoList(ArrayList<AttachmentInfo> forwardedAttachmentInfoList) {
153156
this.forwardedAttachmentInfoList = forwardedAttachmentInfoList;
154157
}
158+
159+
public long getUid() {
160+
return uid;
161+
}
162+
163+
public void setUid(long uid) {
164+
this.uid = uid;
165+
}
155166
}

FlowCrypt/src/main/java/com/flowcrypt/email/database/dao/source/imap/MessageDaoSource.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -680,9 +680,9 @@ public List<GeneralMessageDetails> getNewMessages(Context context, String email,
680680
* @param email The user email.
681681
* @param label The label name.
682682
* @param uid The uid of the message.
683-
* @return A list of {@link Folder} objects.
683+
* @return {@link GeneralMessageDetails} if the information about a message is exists.
684684
*/
685-
public GeneralMessageDetails getMessage(Context context, String email, String label, int uid) {
685+
public GeneralMessageDetails getMessage(Context context, String email, String label, long uid) {
686686
ContentResolver contentResolver = context.getContentResolver();
687687
Cursor cursor = contentResolver.query(getBaseContentUri(),
688688
null, COL_EMAIL + "= ? AND " + COL_FOLDER + " = ? AND " + COL_UID + " = ? ",

FlowCrypt/src/main/java/com/flowcrypt/email/service/PrepareOutgoingMessagesJobIntentService.java

Lines changed: 45 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,11 @@ public class PrepareOutgoingMessagesJobIntentService extends JobIntentService {
6969
("EXTRA_KEY_OUTGOING_MESSAGE_INFO", PrepareOutgoingMessagesJobIntentService.class);
7070
private static final String TAG = PrepareOutgoingMessagesJobIntentService.class.getSimpleName();
7171

72-
private MessageDaoSource messageDaoSource;
72+
private MessageDaoSource msgDaoSource;
7373
private Js js;
7474
private Session session;
7575
private AccountDao accountDao;
76-
private File attachmentsCacheDirectory;
76+
private File attsCacheDir;
7777

7878
/**
7979
* Enqueue a new task for {@link PrepareOutgoingMessagesJobIntentService}.
@@ -95,7 +95,7 @@ public static void enqueueWork(Context context, OutgoingMessageInfo outgoingMess
9595
public void onCreate() {
9696
super.onCreate();
9797
Log.d(TAG, "onCreate");
98-
messageDaoSource = new MessageDaoSource();
98+
msgDaoSource = new MessageDaoSource();
9999
accountDao = new AccountDaoSource().getActiveAccountInformation(getApplicationContext());
100100
session = OpenStoreHelper.getSessionForAccountDao(getApplicationContext(), accountDao);
101101
}
@@ -116,62 +116,62 @@ public boolean onStopCurrentWork() {
116116
protected void onHandleWork(@NonNull Intent intent) {
117117
Log.d(TAG, "onHandleWork");
118118
if (intent.hasExtra(EXTRA_KEY_OUTGOING_MESSAGE_INFO)) {
119-
OutgoingMessageInfo outgoingMessageInfo = intent.getParcelableExtra(EXTRA_KEY_OUTGOING_MESSAGE_INFO);
119+
OutgoingMessageInfo outgoingMsgInfo = intent.getParcelableExtra(EXTRA_KEY_OUTGOING_MESSAGE_INFO);
120+
long uid = outgoingMsgInfo.getUid();
121+
122+
if (msgDaoSource.getMessage(getApplicationContext(),
123+
accountDao.getEmail(), JavaEmailConstants.FOLDER_OUTBOX, uid) != null) {
124+
//todo-DenBond7 need to think about resolving a situation, when a message was created but the
125+
// attachments were not added.
126+
return;
127+
}
120128

121-
Log.d(TAG, "Received a new job: " + outgoingMessageInfo);
129+
Log.d(TAG, "Received a new job: " + outgoingMsgInfo);
122130

123131
setupIfNeed();
124132

125-
updateContactsLastUseDateTime(outgoingMessageInfo);
133+
updateContactsLastUseDateTime(outgoingMsgInfo);
126134

127135
Uri newMessageUri = null;
128-
long generatedUID = -1;
129136

130137
try {
131-
String[] pubKeys = outgoingMessageInfo.getMessageEncryptionType() == MessageEncryptionType.ENCRYPTED ?
138+
String[] pubKeys = outgoingMsgInfo.getMessageEncryptionType() == MessageEncryptionType.ENCRYPTED ?
132139
SecurityUtils.getRecipientsPubKeys(getApplicationContext(), js, EmailUtil.getAllRecipients
133-
(outgoingMessageInfo), accountDao, outgoingMessageInfo.getFromPgpContact().getEmail())
134-
: null;
135-
136-
String rawMessage = EmailUtil.generateRawMessageWithoutAttachments(outgoingMessageInfo, js, pubKeys);
137-
generatedUID = EmailUtil.generateOutboxUID(getApplicationContext());
140+
(outgoingMsgInfo), accountDao, outgoingMsgInfo.getFromPgpContact().getEmail()) : null;
138141

139-
MimeMessage mimeMessage = new MimeMessage(session, IOUtils.toInputStream(rawMessage,
140-
StandardCharsets.UTF_8));
142+
String rawMsg = EmailUtil.generateRawMessageWithoutAttachments(outgoingMsgInfo, js, pubKeys);
143+
MimeMessage mimeMessage = new MimeMessage(session,
144+
IOUtils.toInputStream(rawMsg, StandardCharsets.UTF_8));
141145

142-
File messageAttachmentCacheDirectory = new File(attachmentsCacheDirectory,
143-
UUID.randomUUID().toString());
146+
File msgAttsCacheDir = new File(attsCacheDir, UUID.randomUUID().toString());
144147

145-
ContentValues contentValues = prepareContentValues(outgoingMessageInfo, generatedUID, mimeMessage,
146-
rawMessage, messageAttachmentCacheDirectory);
148+
ContentValues contentValues = prepareContentValues(outgoingMsgInfo, uid, mimeMessage,
149+
rawMsg, msgAttsCacheDir);
147150

148-
newMessageUri = messageDaoSource.addRow(getApplicationContext(), contentValues);
151+
newMessageUri = msgDaoSource.addRow(getApplicationContext(), contentValues);
149152

150153
if (newMessageUri != null) {
151154
new ImapLabelsDaoSource().updateLabelMessageCount(getApplicationContext(), accountDao.getEmail(),
152-
JavaEmailConstants.FOLDER_OUTBOX, messageDaoSource.getOutboxMessages
153-
(getApplicationContext(),
154-
accountDao.getEmail()).size());
155-
156-
if (!CollectionUtils.isEmpty(outgoingMessageInfo.getAttachmentInfoArrayList())
157-
|| !CollectionUtils.isEmpty(outgoingMessageInfo.getForwardedAttachmentInfoList())) {
158-
if (!messageAttachmentCacheDirectory.exists()) {
159-
if (!messageAttachmentCacheDirectory.mkdir()) {
160-
Log.e(TAG, "Create cache directory " + attachmentsCacheDirectory.getName() + " filed!");
161-
messageDaoSource.updateMessageState(getApplicationContext(), accountDao.getEmail(),
162-
JavaEmailConstants.FOLDER_OUTBOX, generatedUID, MessageState
163-
.ERROR_CACHE_PROBLEM);
155+
JavaEmailConstants.FOLDER_OUTBOX, msgDaoSource.getOutboxMessages(getApplicationContext(),
156+
accountDao.getEmail()).size());
157+
158+
if (!CollectionUtils.isEmpty(outgoingMsgInfo.getAttachmentInfoArrayList())
159+
|| !CollectionUtils.isEmpty(outgoingMsgInfo.getForwardedAttachmentInfoList())) {
160+
if (!msgAttsCacheDir.exists()) {
161+
if (!msgAttsCacheDir.mkdir()) {
162+
Log.e(TAG, "Create cache directory " + attsCacheDir.getName() + " filed!");
163+
msgDaoSource.updateMessageState(getApplicationContext(), accountDao.getEmail(),
164+
JavaEmailConstants.FOLDER_OUTBOX, uid, MessageState.ERROR_CACHE_PROBLEM);
164165
return;
165166
}
166167
}
167168

168-
addAttachmentsToCache(outgoingMessageInfo, generatedUID, pubKeys,
169-
messageAttachmentCacheDirectory);
169+
addAttachmentsToCache(outgoingMsgInfo, uid, pubKeys, msgAttsCacheDir);
170170
}
171171

172-
if (CollectionUtils.isEmpty(outgoingMessageInfo.getForwardedAttachmentInfoList())) {
173-
messageDaoSource.updateMessageState(getApplicationContext(), accountDao.getEmail(),
174-
JavaEmailConstants.FOLDER_OUTBOX, generatedUID, MessageState.QUEUED);
172+
if (CollectionUtils.isEmpty(outgoingMsgInfo.getForwardedAttachmentInfoList())) {
173+
msgDaoSource.updateMessageState(getApplicationContext(), accountDao.getEmail(),
174+
JavaEmailConstants.FOLDER_OUTBOX, uid, MessageState.QUEUED);
175175
MessagesSenderJobService.schedule(getApplicationContext());
176176
} else {
177177
ForwardedAttachmentsDownloaderJobService.schedule(getApplicationContext());
@@ -182,14 +182,14 @@ protected void onHandleWork(@NonNull Intent intent) {
182182
ExceptionUtil.handleError(e);
183183

184184
if (newMessageUri != null) {
185-
messageDaoSource.updateMessageState(getApplicationContext(), accountDao.getEmail(),
186-
JavaEmailConstants.FOLDER_OUTBOX, generatedUID, MessageState.ERROR_DURING_CREATION);
185+
msgDaoSource.updateMessageState(getApplicationContext(), accountDao.getEmail(),
186+
JavaEmailConstants.FOLDER_OUTBOX, uid, MessageState.ERROR_DURING_CREATION);
187187
}
188188
}
189189

190190
if (newMessageUri != null) {
191191
new ImapLabelsDaoSource().updateLabelMessageCount(this, accountDao.getEmail(),
192-
JavaEmailConstants.FOLDER_OUTBOX, new MessageDaoSource().getOutboxMessages(this,
192+
JavaEmailConstants.FOLDER_OUTBOX, msgDaoSource.getOutboxMessages(this,
193193
accountDao.getEmail()).size());
194194
}
195195
}
@@ -301,12 +301,11 @@ private void setupIfNeed() {
301301
}
302302
}
303303

304-
if (attachmentsCacheDirectory == null) {
305-
attachmentsCacheDirectory = new File(getCacheDir(), Constants.ATTACHMENTS_CACHE_DIR);
306-
if (!attachmentsCacheDirectory.exists()) {
307-
if (!attachmentsCacheDirectory.mkdirs()) {
308-
throw new IllegalStateException("Create cache directory " + attachmentsCacheDirectory.getName() +
309-
" filed!");
304+
if (attsCacheDir == null) {
305+
attsCacheDir = new File(getCacheDir(), Constants.ATTACHMENTS_CACHE_DIR);
306+
if (!attsCacheDir.exists()) {
307+
if (!attsCacheDir.mkdirs()) {
308+
throw new IllegalStateException("Create cache directory " + attsCacheDir.getName() + " filed!");
310309
}
311310
}
312311
}

FlowCrypt/src/main/java/com/flowcrypt/email/ui/activity/fragment/base/CreateMessageFragment.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -951,6 +951,7 @@ private OutgoingMessageInfo getOutgoingMessageInfo() {
951951
outgoingMessageInfo.setCcPgpContacts(pgpContactsCc.toArray(new PgpContact[0]));
952952
outgoingMessageInfo.setBccPgpContacts(pgpContactsBcc.toArray(new PgpContact[0]));
953953
outgoingMessageInfo.setFromPgpContact(new PgpContact(editTextFrom.getText().toString(), null));
954+
outgoingMessageInfo.setUid(EmailUtil.generateOutboxUID(getContext()));
954955

955956
return outgoingMessageInfo;
956957
}

0 commit comments

Comments
 (0)