Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
240 changes: 130 additions & 110 deletions electron_app/src/DBManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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')
Expand Down Expand Up @@ -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)));
Expand All @@ -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 });
};

/* Email
Expand Down Expand Up @@ -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);
Copy link
Contributor

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.

Copy link
Contributor Author

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

if (optionalEmailToSave) {
const [emailId] = await createEmail(optionalEmailToSave, trx);
return emailId;
}
});
};

const formEmailContact = ({ emailId, contactStored, contacts, type }) => {
return contacts.map(contactToSearch => {
const emailMatched = contactToSearch.match(/<(.*)>/);
Expand Down Expand Up @@ -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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have you tested this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes! The test is ok!

};

const getEmailsByThreadId = threadId => {
return db
.select(
Expand Down Expand Up @@ -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}`)
Expand Down Expand Up @@ -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 = ({
Copy link
Contributor

Choose a reason for hiding this comment

The 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.

Copy link
Contributor Author

@erikaperugachi erikaperugachi Aug 23, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a simple join. And it was tested

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I meant updateEmail.

Copy link
Contributor Author

@erikaperugachi erikaperugachi Aug 23, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

separate: updateEmail and updateEmails ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That might be an improvement, I'll let you decide.

id,
key,
Expand Down Expand Up @@ -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;
Expand All @@ -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);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

again, triggers should handle this.

Copy link
Contributor Author

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

return trx
.table(Table.LABEL)
.where({ id })
.del();
});
};

const getAllLabels = () => {
return db.select('*').from(Table.LABEL);
};
Expand Down Expand Up @@ -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) => {
Expand Down Expand Up @@ -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
Expand All @@ -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 => {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -943,6 +961,7 @@ module.exports = {
getContactsByEmailId,
getEmailById,
getEmailByKey,
getEmailsByKeys,
getEmailsByLabelIds,
getEmailsByThreadId,
getEmailsCounterByLabelId,
Expand All @@ -962,6 +981,7 @@ module.exports = {
deleteLabelById,
updateAccount,
updateEmail,
updateEmails,
updateEmailByThreadId,
updateEmailLabel,
updateFeedItem,
Expand Down
Loading