mirror of
https://github.com/Foundry376/Mailspring.git
synced 2025-03-15 17:53:44 +08:00
55 lines
1.7 KiB
JavaScript
55 lines
1.7 KiB
JavaScript
import url from 'url';
|
|
import querystring from 'querystring';
|
|
import { ipcRenderer } from 'electron';
|
|
import { DatabaseStore, Thread, Matcher, Actions } from 'mailspring-exports';
|
|
|
|
const DATE_EPSILON = 60; // Seconds
|
|
|
|
const parseOpenThreadUrl = nylasUrlString => {
|
|
const parsedUrl = url.parse(nylasUrlString);
|
|
const params = querystring.parse(parsedUrl.query);
|
|
params.lastDate = parseInt(params.lastDate, 10);
|
|
return params;
|
|
};
|
|
|
|
const findCorrespondingThread = ({ subject, lastDate }, dateEpsilon = DATE_EPSILON) => {
|
|
return DatabaseStore.findBy(Thread).where([
|
|
Thread.attributes.subject.equal(subject),
|
|
new Matcher.Or([
|
|
new Matcher.And([
|
|
Thread.attributes.lastMessageSentTimestamp.lessThan(lastDate + dateEpsilon),
|
|
Thread.attributes.lastMessageSentTimestamp.greaterThan(lastDate - dateEpsilon),
|
|
]),
|
|
new Matcher.And([
|
|
Thread.attributes.lastMessageReceivedTimestamp.lessThan(lastDate + dateEpsilon),
|
|
Thread.attributes.lastMessageReceivedTimestamp.greaterThan(lastDate - dateEpsilon),
|
|
]),
|
|
]),
|
|
]);
|
|
};
|
|
|
|
const _openExternalThread = (event, nylasUrl) => {
|
|
const { subject, lastDate } = parseOpenThreadUrl(nylasUrl);
|
|
|
|
findCorrespondingThread({ subject, lastDate })
|
|
.then(thread => {
|
|
if (!thread) {
|
|
throw new Error('Thread not found');
|
|
}
|
|
Actions.popoutThread(thread);
|
|
})
|
|
.catch(error => {
|
|
AppEnv.reportError(error);
|
|
AppEnv.showErrorDialog(`The thread ${subject} does not exist in your mailbox!`);
|
|
});
|
|
};
|
|
|
|
const activate = () => {
|
|
ipcRenderer.on('openExternalThread', _openExternalThread);
|
|
};
|
|
|
|
const deactivate = () => {
|
|
ipcRenderer.removeListener('openExternalThread', _openExternalThread);
|
|
};
|
|
|
|
export default { activate, deactivate };
|