Skip to content

Commit a6cce89

Browse files
author
Erika Perugachi
authored
Merge pull request #369 from erikaperugachi/event
Fix unread sync
2 parents 174eac5 + d50c5e7 commit a6cce89

File tree

6 files changed

+184
-135
lines changed

6 files changed

+184
-135
lines changed

electron_app/src/DBManager.js

Lines changed: 130 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,10 @@ const filterUniqueContacts = contacts => {
100100
return contactsUnique.contacts;
101101
};
102102

103+
const getAllContacts = () => {
104+
return db.select('name', 'email').from(Table.CONTACT);
105+
};
106+
103107
const getContactByEmails = (emails, trx) => {
104108
const knex = trx || db;
105109
return knex
@@ -116,10 +120,6 @@ const getContactByIds = (ids, trx) => {
116120
.whereIn('id', ids);
117121
};
118122

119-
const getAllContacts = () => {
120-
return db.select('name', 'email').from(Table.CONTACT);
121-
};
122-
123123
const getContactsByEmailId = async emailId => {
124124
const emailContacts = await db
125125
.select('contactId', 'type')
@@ -175,6 +175,23 @@ const createEmailLabel = async (emailLabels, trx) => {
175175
}
176176
};
177177

178+
const deleteEmailLabel = ({ emailsId, labelIds }, trx) => {
179+
const knex = trx || db;
180+
return knex
181+
.table(Table.EMAIL_LABEL)
182+
.whereIn('labelId', labelIds)
183+
.whereIn('emailId', emailsId)
184+
.del();
185+
};
186+
187+
const deleteEmailLabelsByEmailId = (emailId, trx) => {
188+
const knex = trx || db;
189+
return knex
190+
.table(Table.EMAIL_LABEL)
191+
.where({ emailId })
192+
.del();
193+
};
194+
178195
const filterEmailLabelIfNotStore = async (emailLabels, trx) => {
179196
const knex = trx || db;
180197
const emailIds = Array.from(new Set(emailLabels.map(item => item.emailId)));
@@ -198,35 +215,18 @@ const filterEmailLabelIfNotStore = async (emailLabels, trx) => {
198215
.filter(item => item !== null);
199216
};
200217

201-
const updateEmailLabel = ({ emailId, oldLabelId, newLabelId }) => {
218+
const getEmailLabelsByEmailId = emailId => {
202219
return db
220+
.select('labelId')
203221
.table(Table.EMAIL_LABEL)
204-
.where({ emailId, labelId: oldLabelId })
205-
.update({ labelId: newLabelId });
206-
};
207-
208-
const deleteEmailLabel = ({ emailsId, labelIds }, trx) => {
209-
const knex = trx || db;
210-
return knex
211-
.table(Table.EMAIL_LABEL)
212-
.whereIn('labelId', labelIds)
213-
.whereIn('emailId', emailsId)
214-
.del();
215-
};
216-
217-
const deleteEmailLabelsByEmailId = (emailId, trx) => {
218-
const knex = trx || db;
219-
return knex
220-
.table(Table.EMAIL_LABEL)
221-
.where({ emailId })
222-
.del();
222+
.where({ emailId });
223223
};
224224

225-
const getEmailLabelsByEmailId = emailId => {
225+
const updateEmailLabel = ({ emailId, oldLabelId, newLabelId }) => {
226226
return db
227-
.select('labelId')
228227
.table(Table.EMAIL_LABEL)
229-
.where({ emailId });
228+
.where({ emailId, labelId: oldLabelId })
229+
.update({ labelId: newLabelId });
230230
};
231231

232232
/* Email
@@ -308,6 +308,40 @@ const createEmail = async (params, trx) => {
308308
});
309309
};
310310

311+
const deleteEmailByKey = key => {
312+
return db
313+
.table(Table.EMAIL)
314+
.where({ key })
315+
.del();
316+
};
317+
318+
const deleteEmailsByIds = (ids, trx) => {
319+
const knex = trx || db;
320+
return knex
321+
.table(Table.EMAIL)
322+
.whereIn('id', ids)
323+
.del();
324+
};
325+
326+
const deleteEmailsByThreadId = threadIds => {
327+
return db
328+
.table(Table.EMAIL)
329+
.whereIn('threadId', threadIds)
330+
.del();
331+
};
332+
333+
const deleteEmailLabelAndContactByEmailId = (id, optionalEmailToSave) => {
334+
return db.transaction(async trx => {
335+
await deleteEmailsByIds([id], trx);
336+
await deleteEmailContactByEmailId(id, trx);
337+
await deleteEmailLabelsByEmailId(id, trx);
338+
if (optionalEmailToSave) {
339+
const [emailId] = await createEmail(optionalEmailToSave, trx);
340+
return emailId;
341+
}
342+
});
343+
};
344+
311345
const formEmailContact = ({ emailId, contactStored, contacts, type }) => {
312346
return contacts.map(contactToSearch => {
313347
const emailMatched = contactToSearch.match(/<(.*)>/);
@@ -344,6 +378,25 @@ const getEmailByKey = key => {
344378
.where({ key });
345379
};
346380

381+
const getEmailsByKeys = keys => {
382+
return db
383+
.select('*')
384+
.from(Table.EMAIL)
385+
.whereIn('key', keys);
386+
};
387+
388+
const getEmailsByLabelIds = labelIds => {
389+
return db
390+
.select(`${Table.EMAIL}.*`)
391+
.from(Table.EMAIL)
392+
.leftJoin(
393+
Table.EMAIL_LABEL,
394+
`${Table.EMAIL}.id`,
395+
`${Table.EMAIL_LABEL}.emailId`
396+
)
397+
.whereIn(`${Table.EMAIL_LABEL}.labelId`, labelIds);
398+
};
399+
347400
const getEmailsByThreadId = threadId => {
348401
return db
349402
.select(
@@ -557,14 +610,6 @@ const partThreadQueryByMatchText = (query, text) =>
557610
.orWhere('subject', 'like', `%${text}%`);
558611
});
559612

560-
const deleteEmailsByIds = (ids, trx) => {
561-
const knex = trx || db;
562-
return knex
563-
.table(Table.EMAIL)
564-
.whereIn('id', ids)
565-
.del();
566-
};
567-
568613
const getEmailsUnredByLabelId = params => {
569614
const { labelId, rejectedLabelIds } = params;
570615
return db(`${Table.EMAIL}`)
@@ -600,44 +645,6 @@ const getUnreadEmailsByThreadId = threadId => {
600645
.where({ threadId, unread: 1 });
601646
};
602647

603-
const deleteEmailByKey = key => {
604-
return db
605-
.table(Table.EMAIL)
606-
.where({ key })
607-
.del();
608-
};
609-
610-
const deleteEmailsByThreadId = threadIds => {
611-
return db
612-
.table(Table.EMAIL)
613-
.whereIn('threadId', threadIds)
614-
.del();
615-
};
616-
617-
const deleteEmailLabelAndContactByEmailId = (id, optionalEmailToSave) => {
618-
return db.transaction(async trx => {
619-
await deleteEmailsByIds([id], trx);
620-
await deleteEmailContactByEmailId(id, trx);
621-
await deleteEmailLabelsByEmailId(id, trx);
622-
if (optionalEmailToSave) {
623-
const [emailId] = await createEmail(optionalEmailToSave, trx);
624-
return emailId;
625-
}
626-
});
627-
};
628-
629-
const getEmailsByLabelIds = async labelIds => {
630-
const emails = await db
631-
.select('emailId')
632-
.from(Table.EMAIL_LABEL)
633-
.whereIn('labelId', labelIds);
634-
const emailIds = emails.map(email => email.emailId);
635-
return db
636-
.select('*')
637-
.table(Table.EMAIL)
638-
.whereIn('id', emailIds);
639-
};
640-
641648
const updateEmail = ({
642649
id,
643650
key,
@@ -668,6 +675,16 @@ const updateEmail = ({
668675
.update(params);
669676
};
670677

678+
const updateEmails = ({ keys, unread }) => {
679+
const params = noNulls({
680+
unread: typeof unread === 'boolean' ? unread : undefined
681+
});
682+
return db
683+
.table(Table.EMAIL)
684+
.whereIn('key', keys)
685+
.update(params);
686+
};
687+
671688
const updateEmailByThreadId = ({ threadId, unread }) => {
672689
const params = {};
673690
if (typeof unread === 'boolean') params.unread = unread;
@@ -683,6 +700,23 @@ const createLabel = params => {
683700
return db.table(Table.LABEL).insert(params);
684701
};
685702

703+
const deleteLabelById = id => {
704+
return db.transaction(async trx => {
705+
const emailLabels = await trx
706+
.select('*')
707+
.from(Table.EMAIL_LABEL)
708+
.where('labelId', id);
709+
const emailsId = emailLabels.map(item => item.emailId);
710+
if (emailsId.length) {
711+
await deleteEmailLabel({ emailsId, labelId: id }, trx);
712+
}
713+
return trx
714+
.table(Table.LABEL)
715+
.where({ id })
716+
.del();
717+
});
718+
};
719+
686720
const getAllLabels = () => {
687721
return db.select('*').from(Table.LABEL);
688722
};
@@ -719,23 +753,6 @@ const updateLabel = ({ id, color, text, visible }) => {
719753
.update(params);
720754
};
721755

722-
const deleteLabelById = id => {
723-
return db.transaction(async trx => {
724-
const emailLabels = await trx
725-
.select('*')
726-
.from(Table.EMAIL_LABEL)
727-
.where('labelId', id);
728-
const emailsId = emailLabels.map(item => item.emailId);
729-
if (emailsId.length) {
730-
await deleteEmailLabel({ emailsId, labelId: id }, trx);
731-
}
732-
return trx
733-
.table(Table.LABEL)
734-
.where({ id })
735-
.del();
736-
});
737-
};
738-
739756
/* File
740757
----------------------------- */
741758
const createFile = (files, trx) => {
@@ -784,28 +801,29 @@ const getFileKeyByEmailId = emailId => {
784801

785802
/* Feed Item
786803
----------------------------- */
787-
const getAllFeedItems = () => {
788-
return db.select('*').from(Table.FEEDITEM);
789-
};
790804

791805
const createFeedItem = params => {
792806
return db.table(Table.FEEDITEM).insert(params);
793807
};
794808

795-
const updateFeedItem = ({ id, seen }) => {
796-
const params = {};
797-
if (seen) params.seen = seen;
809+
const deleteFeedItemById = id => {
798810
return db
799811
.table(Table.FEEDITEM)
800812
.where({ id })
801-
.update(params);
813+
.del();
802814
};
803815

804-
const deleteFeedItemById = id => {
816+
const getAllFeedItems = () => {
817+
return db.select('*').from(Table.FEEDITEM);
818+
};
819+
820+
const updateFeedItem = ({ id, seen }) => {
821+
const params = {};
822+
if (seen) params.seen = seen;
805823
return db
806824
.table(Table.FEEDITEM)
807825
.where({ id })
808-
.del();
826+
.update(params);
809827
};
810828

811829
/* PreKeyRecord
@@ -814,20 +832,20 @@ const createPreKeyRecord = params => {
814832
return db.table(Table.PREKEYRECORD).insert(params);
815833
};
816834

817-
const getPreKeyPair = params => {
818-
return db
819-
.select('preKeyPrivKey', 'preKeyPubKey')
820-
.from(Table.PREKEYRECORD)
821-
.where(params);
822-
};
823-
824835
const deletePreKeyPair = params => {
825836
return db
826837
.table(Table.PREKEYRECORD)
827838
.where(params)
828839
.del();
829840
};
830841

842+
const getPreKeyPair = params => {
843+
return db
844+
.select('preKeyPrivKey', 'preKeyPubKey')
845+
.from(Table.PREKEYRECORD)
846+
.where(params);
847+
};
848+
831849
/* SignedPreKeyRecord
832850
----------------------------- */
833851
const createSignedPreKeyRecord = params => {
@@ -885,17 +903,17 @@ const getSessionRecordByRecipientIds = recipientIds => {
885903

886904
/* IdentityKeyRecord
887905
----------------------------- */
906+
const createIdentityKeyRecord = params => {
907+
return db.table(Table.IDENTITYKEYRECORD).insert(params);
908+
};
909+
888910
const getIdentityKeyRecord = params => {
889911
return db
890912
.select('identityKey')
891913
.from(Table.IDENTITYKEYRECORD)
892914
.where(params);
893915
};
894916

895-
const createIdentityKeyRecord = params => {
896-
return db.table(Table.IDENTITYKEYRECORD).insert(params);
897-
};
898-
899917
const updateIdentityKeyRecord = ({ recipientId, identityKey }) => {
900918
return db
901919
.table(Table.IDENTITYKEYRECORD)
@@ -943,6 +961,7 @@ module.exports = {
943961
getContactsByEmailId,
944962
getEmailById,
945963
getEmailByKey,
964+
getEmailsByKeys,
946965
getEmailsByLabelIds,
947966
getEmailsByThreadId,
948967
getEmailsCounterByLabelId,
@@ -962,6 +981,7 @@ module.exports = {
962981
deleteLabelById,
963982
updateAccount,
964983
updateEmail,
984+
updateEmails,
965985
updateEmailByThreadId,
966986
updateEmailLabel,
967987
updateFeedItem,

0 commit comments

Comments
 (0)