-
Notifications
You must be signed in to change notification settings - Fork 23
Fix unread sync #369
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix unread sync #369
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -100,6 +100,10 @@ const filterUniqueContacts = contacts => { | |
| return contactsUnique.contacts; | ||
| }; | ||
|
|
||
| const getAllContacts = () => { | ||
| return db.select('name', 'email').from(Table.CONTACT); | ||
| }; | ||
|
|
||
| const getContactByEmails = (emails, trx) => { | ||
| const knex = trx || db; | ||
| return knex | ||
|
|
@@ -116,10 +120,6 @@ const getContactByIds = (ids, trx) => { | |
| .whereIn('id', ids); | ||
| }; | ||
|
|
||
| const getAllContacts = () => { | ||
| return db.select('name', 'email').from(Table.CONTACT); | ||
| }; | ||
|
|
||
| const getContactsByEmailId = async emailId => { | ||
| const emailContacts = await db | ||
| .select('contactId', 'type') | ||
|
|
@@ -175,6 +175,23 @@ const createEmailLabel = async (emailLabels, trx) => { | |
| } | ||
| }; | ||
|
|
||
| const deleteEmailLabel = ({ emailsId, labelIds }, trx) => { | ||
| const knex = trx || db; | ||
| return knex | ||
| .table(Table.EMAIL_LABEL) | ||
| .whereIn('labelId', labelIds) | ||
| .whereIn('emailId', emailsId) | ||
| .del(); | ||
| }; | ||
|
|
||
| const deleteEmailLabelsByEmailId = (emailId, trx) => { | ||
| const knex = trx || db; | ||
| return knex | ||
| .table(Table.EMAIL_LABEL) | ||
| .where({ emailId }) | ||
| .del(); | ||
| }; | ||
|
|
||
| const filterEmailLabelIfNotStore = async (emailLabels, trx) => { | ||
| const knex = trx || db; | ||
| const emailIds = Array.from(new Set(emailLabels.map(item => item.emailId))); | ||
|
|
@@ -198,35 +215,18 @@ const filterEmailLabelIfNotStore = async (emailLabels, trx) => { | |
| .filter(item => item !== null); | ||
| }; | ||
|
|
||
| const updateEmailLabel = ({ emailId, oldLabelId, newLabelId }) => { | ||
| const getEmailLabelsByEmailId = emailId => { | ||
| return db | ||
| .select('labelId') | ||
| .table(Table.EMAIL_LABEL) | ||
| .where({ emailId, labelId: oldLabelId }) | ||
| .update({ labelId: newLabelId }); | ||
| }; | ||
|
|
||
| const deleteEmailLabel = ({ emailsId, labelIds }, trx) => { | ||
| const knex = trx || db; | ||
| return knex | ||
| .table(Table.EMAIL_LABEL) | ||
| .whereIn('labelId', labelIds) | ||
| .whereIn('emailId', emailsId) | ||
| .del(); | ||
| }; | ||
|
|
||
| const deleteEmailLabelsByEmailId = (emailId, trx) => { | ||
| const knex = trx || db; | ||
| return knex | ||
| .table(Table.EMAIL_LABEL) | ||
| .where({ emailId }) | ||
| .del(); | ||
| .where({ emailId }); | ||
| }; | ||
|
|
||
| const getEmailLabelsByEmailId = emailId => { | ||
| const updateEmailLabel = ({ emailId, oldLabelId, newLabelId }) => { | ||
| return db | ||
| .select('labelId') | ||
| .table(Table.EMAIL_LABEL) | ||
| .where({ emailId }); | ||
| .where({ emailId, labelId: oldLabelId }) | ||
| .update({ labelId: newLabelId }); | ||
| }; | ||
|
|
||
|
|
@@ -308,6 +308,40 @@ const createEmail = async (params, trx) => { | |
| }); | ||
| }; | ||
|
|
||
| const deleteEmailByKey = key => { | ||
| return db | ||
| .table(Table.EMAIL) | ||
| .where({ key }) | ||
| .del(); | ||
| }; | ||
|
|
||
| const deleteEmailsByIds = (ids, trx) => { | ||
| const knex = trx || db; | ||
| return knex | ||
| .table(Table.EMAIL) | ||
| .whereIn('id', ids) | ||
| .del(); | ||
| }; | ||
|
|
||
| const deleteEmailsByThreadId = threadIds => { | ||
| return db | ||
| .table(Table.EMAIL) | ||
| .whereIn('threadId', threadIds) | ||
| .del(); | ||
| }; | ||
|
|
||
| const deleteEmailLabelAndContactByEmailId = (id, optionalEmailToSave) => { | ||
| return db.transaction(async trx => { | ||
| await deleteEmailsByIds([id], trx); | ||
| await deleteEmailContactByEmailId(id, trx); | ||
| await deleteEmailLabelsByEmailId(id, trx); | ||
| if (optionalEmailToSave) { | ||
| const [emailId] = await createEmail(optionalEmailToSave, trx); | ||
| return emailId; | ||
| } | ||
| }); | ||
| }; | ||
|
|
||
| const formEmailContact = ({ emailId, contactStored, contacts, type }) => { | ||
| return contacts.map(contactToSearch => { | ||
| const emailMatched = contactToSearch.match(/<(.*)>/); | ||
|
|
@@ -344,6 +378,25 @@ const getEmailByKey = key => { | |
| .where({ key }); | ||
| }; | ||
|
|
||
| const getEmailsByKeys = keys => { | ||
| return db | ||
| .select('*') | ||
| .from(Table.EMAIL) | ||
| .whereIn('key', keys); | ||
| }; | ||
|
|
||
| const getEmailsByLabelIds = labelIds => { | ||
| return db | ||
| .select(`${Table.EMAIL}.*`) | ||
| .from(Table.EMAIL) | ||
| .leftJoin( | ||
| Table.EMAIL_LABEL, | ||
| `${Table.EMAIL}.id`, | ||
| `${Table.EMAIL_LABEL}.emailId` | ||
| ) | ||
| .whereIn(`${Table.EMAIL_LABEL}.labelId`, labelIds); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Have you tested this?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes! The test is ok! |
||
| }; | ||
|
|
||
| const getEmailsByThreadId = threadId => { | ||
| return db | ||
| .select( | ||
|
|
@@ -557,14 +610,6 @@ const partThreadQueryByMatchText = (query, text) => | |
| .orWhere('subject', 'like', `%${text}%`); | ||
| }); | ||
|
|
||
| const deleteEmailsByIds = (ids, trx) => { | ||
| const knex = trx || db; | ||
| return knex | ||
| .table(Table.EMAIL) | ||
| .whereIn('id', ids) | ||
| .del(); | ||
| }; | ||
|
|
||
| const getEmailsUnredByLabelId = params => { | ||
| const { labelId, rejectedLabelIds } = params; | ||
| return db(`${Table.EMAIL}`) | ||
|
|
@@ -600,44 +645,6 @@ const getUnreadEmailsByThreadId = threadId => { | |
| .where({ threadId, unread: 1 }); | ||
| }; | ||
|
|
||
| const deleteEmailByKey = key => { | ||
| return db | ||
| .table(Table.EMAIL) | ||
| .where({ key }) | ||
| .del(); | ||
| }; | ||
|
|
||
| const deleteEmailsByThreadId = threadIds => { | ||
| return db | ||
| .table(Table.EMAIL) | ||
| .whereIn('threadId', threadIds) | ||
| .del(); | ||
| }; | ||
|
|
||
| const deleteEmailLabelAndContactByEmailId = (id, optionalEmailToSave) => { | ||
| return db.transaction(async trx => { | ||
| await deleteEmailsByIds([id], trx); | ||
| await deleteEmailContactByEmailId(id, trx); | ||
| await deleteEmailLabelsByEmailId(id, trx); | ||
| if (optionalEmailToSave) { | ||
| const [emailId] = await createEmail(optionalEmailToSave, trx); | ||
| return emailId; | ||
| } | ||
| }); | ||
| }; | ||
|
|
||
| const getEmailsByLabelIds = async labelIds => { | ||
| const emails = await db | ||
| .select('emailId') | ||
| .from(Table.EMAIL_LABEL) | ||
| .whereIn('labelId', labelIds); | ||
| const emailIds = emails.map(email => email.emailId); | ||
| return db | ||
| .select('*') | ||
| .table(Table.EMAIL) | ||
| .whereIn('id', emailIds); | ||
| }; | ||
|
|
||
| const updateEmail = ({ | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this method is so complex, it does many different things depending on the input parameters. I suggest you split it into smaller, simpler functions that do one thing.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a simple join. And it was tested
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I meant
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. separate: updateEmail and updateEmails ?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That might be an improvement, I'll let you decide. |
||
| id, | ||
| key, | ||
|
|
@@ -668,6 +675,16 @@ const updateEmail = ({ | |
| .update(params); | ||
| }; | ||
|
|
||
| const updateEmails = ({ keys, unread }) => { | ||
| const params = noNulls({ | ||
| unread: typeof unread === 'boolean' ? unread : undefined | ||
| }); | ||
| return db | ||
| .table(Table.EMAIL) | ||
| .whereIn('key', keys) | ||
| .update(params); | ||
| }; | ||
|
|
||
| const updateEmailByThreadId = ({ threadId, unread }) => { | ||
| const params = {}; | ||
| if (typeof unread === 'boolean') params.unread = unread; | ||
|
|
@@ -683,6 +700,23 @@ const createLabel = params => { | |
| return db.table(Table.LABEL).insert(params); | ||
| }; | ||
|
|
||
| const deleteLabelById = id => { | ||
| return db.transaction(async trx => { | ||
| const emailLabels = await trx | ||
| .select('*') | ||
| .from(Table.EMAIL_LABEL) | ||
| .where('labelId', id); | ||
| const emailsId = emailLabels.map(item => item.emailId); | ||
| if (emailsId.length) { | ||
| await deleteEmailLabel({ emailsId, labelId: id }, trx); | ||
| } | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. again, triggers should handle this.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is pending in other pull request, to handle this. I just change the position |
||
| return trx | ||
| .table(Table.LABEL) | ||
| .where({ id }) | ||
| .del(); | ||
| }); | ||
| }; | ||
|
|
||
| const getAllLabels = () => { | ||
| return db.select('*').from(Table.LABEL); | ||
| }; | ||
|
|
@@ -719,23 +753,6 @@ const updateLabel = ({ id, color, text, visible }) => { | |
| .update(params); | ||
| }; | ||
|
|
||
| const deleteLabelById = id => { | ||
| return db.transaction(async trx => { | ||
| const emailLabels = await trx | ||
| .select('*') | ||
| .from(Table.EMAIL_LABEL) | ||
| .where('labelId', id); | ||
| const emailsId = emailLabels.map(item => item.emailId); | ||
| if (emailsId.length) { | ||
| await deleteEmailLabel({ emailsId, labelId: id }, trx); | ||
| } | ||
| return trx | ||
| .table(Table.LABEL) | ||
| .where({ id }) | ||
| .del(); | ||
| }); | ||
| }; | ||
|
|
||
| /* File | ||
| ----------------------------- */ | ||
| const createFile = (files, trx) => { | ||
|
|
@@ -784,28 +801,29 @@ const getFileKeyByEmailId = emailId => { | |
|
|
||
| /* Feed Item | ||
| ----------------------------- */ | ||
| const getAllFeedItems = () => { | ||
| return db.select('*').from(Table.FEEDITEM); | ||
| }; | ||
|
|
||
| const createFeedItem = params => { | ||
| return db.table(Table.FEEDITEM).insert(params); | ||
| }; | ||
|
|
||
| const updateFeedItem = ({ id, seen }) => { | ||
| const params = {}; | ||
| if (seen) params.seen = seen; | ||
| const deleteFeedItemById = id => { | ||
| return db | ||
| .table(Table.FEEDITEM) | ||
| .where({ id }) | ||
| .update(params); | ||
| .del(); | ||
| }; | ||
|
|
||
| const deleteFeedItemById = id => { | ||
| const getAllFeedItems = () => { | ||
| return db.select('*').from(Table.FEEDITEM); | ||
| }; | ||
|
|
||
| const updateFeedItem = ({ id, seen }) => { | ||
| const params = {}; | ||
| if (seen) params.seen = seen; | ||
| return db | ||
| .table(Table.FEEDITEM) | ||
| .where({ id }) | ||
| .del(); | ||
| .update(params); | ||
| }; | ||
|
|
||
| /* PreKeyRecord | ||
|
|
@@ -814,20 +832,20 @@ const createPreKeyRecord = params => { | |
| return db.table(Table.PREKEYRECORD).insert(params); | ||
| }; | ||
|
|
||
| const getPreKeyPair = params => { | ||
| return db | ||
| .select('preKeyPrivKey', 'preKeyPubKey') | ||
| .from(Table.PREKEYRECORD) | ||
| .where(params); | ||
| }; | ||
|
|
||
| const deletePreKeyPair = params => { | ||
| return db | ||
| .table(Table.PREKEYRECORD) | ||
| .where(params) | ||
| .del(); | ||
| }; | ||
|
|
||
| const getPreKeyPair = params => { | ||
| return db | ||
| .select('preKeyPrivKey', 'preKeyPubKey') | ||
| .from(Table.PREKEYRECORD) | ||
| .where(params); | ||
| }; | ||
|
|
||
| /* SignedPreKeyRecord | ||
| ----------------------------- */ | ||
| const createSignedPreKeyRecord = params => { | ||
|
|
@@ -885,17 +903,17 @@ const getSessionRecordByRecipientIds = recipientIds => { | |
|
|
||
| /* IdentityKeyRecord | ||
| ----------------------------- */ | ||
| const createIdentityKeyRecord = params => { | ||
| return db.table(Table.IDENTITYKEYRECORD).insert(params); | ||
| }; | ||
|
|
||
| const getIdentityKeyRecord = params => { | ||
| return db | ||
| .select('identityKey') | ||
| .from(Table.IDENTITYKEYRECORD) | ||
| .where(params); | ||
| }; | ||
|
|
||
| const createIdentityKeyRecord = params => { | ||
| return db.table(Table.IDENTITYKEYRECORD).insert(params); | ||
| }; | ||
|
|
||
| const updateIdentityKeyRecord = ({ recipientId, identityKey }) => { | ||
| return db | ||
| .table(Table.IDENTITYKEYRECORD) | ||
|
|
@@ -943,6 +961,7 @@ module.exports = { | |
| getContactsByEmailId, | ||
| getEmailById, | ||
| getEmailByKey, | ||
| getEmailsByKeys, | ||
| getEmailsByLabelIds, | ||
| getEmailsByThreadId, | ||
| getEmailsCounterByLabelId, | ||
|
|
@@ -962,6 +981,7 @@ module.exports = { | |
| deleteLabelById, | ||
| updateAccount, | ||
| updateEmail, | ||
| updateEmails, | ||
| updateEmailByThreadId, | ||
| updateEmailLabel, | ||
| updateFeedItem, | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if you use cascade triggers there is no need for you to delete relations of emails you already deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is pending in other pull request, to handle this. I just change the position