Skip to content

Commit

Permalink
fix(email): properly sync email, add option to open in gmail
Browse files Browse the repository at this point in the history
  • Loading branch information
thesophiaxu committed Mar 31, 2022
1 parent 42d359a commit d0a4e6d
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,19 @@ const getQuery = (msgid) => `(func: uid(parIds)) @cascade {
}
}`;

const dest = parsed.map((el) => {
const dest = parsed.map((el, index) => {
return {
name: {
type: { 'unigraph.id': '$/schema/note' },
_value: el.subject || '',
},
...(msgs[index].externalUrl
? {
$context: {
_externalUrl: msgs[index].externalUrl,
},
}
: {}),
message_id: el.messageId,
message: {
date_received: el.date?.toISOString?.() || new Date(0).toISOString(),
Expand Down Expand Up @@ -97,28 +104,29 @@ for (let i = 0; i < toAddChunks.length; i += 1) {
}
}

const inboxEntries = await unigraph.getObject('$/entity/inbox').then((x) => x._value.children._value ?? []);
const inboxEntryUids = inboxEntries.map((x) => x._value._value.uid);
// const inboxEntries = await unigraph.getObject('$/entity/inbox').then((x) => x._value.children._value ?? []);
// const inboxEntryUids = inboxEntries.map((x) => x._value._value.uid);

const mirrorEmailInbox = unigraph.getState('settings/email/mirrorEmailInbox').value;
// const mirrorEmailInbox = unigraph.getState('settings/email/mirrorEmailInbox').value;
uids.forEach((el, index) => {
// if (!readMask[index] && el) inboxEls.push(el);
const elShouldBeInbox = (mirrorEmailInbox ? inboxMask[index] : !readMask[index]) && el;
const elInInbox = inboxEntryUids.includes(el);
if (elShouldBeInbox && !elInInbox) inboxEls.push(el);
if (inboxMask[index] && el) inboxEls.push(el);
// const elShouldBeInbox = (mirrorEmailInbox ? inboxMask[index] : !readMask[index]) && el;
// const elInInbox = inboxEntryUids.includes(el);
// if (elShouldBeInbox && !elInInbox) inboxEls.push(el);
});

await unigraph.runExecutable('$/executable/add-item-to-list', {
where: '$/entity/inbox',
item: inboxEls.reverse(),
});
setTimeout(
() =>
unigraph.addNotification({
name: 'Inboxes synced',
from: 'unigraph.email',
content: `Added ${count} emails (${inboxEls.length} unread).`,
actions: [],
}),
1000,
);
if (count > 0)
setTimeout(
() =>
unigraph.addNotification({
name: 'Inboxes synced',
from: 'unigraph.email',
content: `Added ${count} emails (${inboxEls.length} in inbox).`,
actions: [],
}),
1000,
);
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,21 @@ const updateAccountInbox = async (account) => {
messages.map((id) => gmail.users.messages.get({ userId: 'me', id, format: 'raw' })),
);
const inbox = await unigraph.getObject('$/entity/inbox');
const inboxEntries = inbox._value.children['_value['] ?? [];
const inboxEntries = (inbox._value.children['_value['] ?? []).filter(
(x) => x._value._value.type?.['unigraph.id'] === '$/schema/email_message',
);
const inboxEntryUids = inboxEntries.map((x) => x._value._value.uid);
const inboxEntryMsgIds = inboxEntries
.map((x) => [x._value._value?._value?.message_id?.['_value.%'], x._value._value.uid])
.filter(Boolean);
const gmailIdsForCurrentInbox = await Promise.all(
inboxEntryMsgIds.map((el) =>
gmail.users.messages.list({ userId: 'me', q: `rfc822msgid:${el[0]}`, labelIds: ['INBOX'] }),
),
);
const uidsToRemoveInbox = gmailIdsForCurrentInbox
.map((el, idx) => (el.data?.messages?.length > 0 ? undefined : inboxEntryMsgIds[idx][1]))
.filter(Boolean);

const getOldUid = _.curry((condition, el, index) => {
const isCondition = condition(el);
Expand All @@ -111,14 +124,12 @@ const updateAccountInbox = async (account) => {
(el) => el.data.labelIds?.includes('TRASH') || el.data.labelIds?.includes('SPAM'),
);
const isOldAndInOriginInbox = getOldUid((el) => el.data.labelIds?.includes('INBOX'));
const isOldAndNotInOriginInbox = getOldUid((el) => !el.data.labelIds?.includes('INBOX'));

const uidsToDelete = msgResps.map(isOldAndInOriginTrash).filter((el) => el !== undefined);
const uidsToInbox = msgResps
.map(isOldAndInOriginInbox)
.filter((el) => el !== undefined)
.filter((x) => !inboxEntryUids.includes(x));
const uidsToRemoveInbox = msgResps.map(isOldAndNotInOriginInbox).filter((el) => el !== undefined);

// TODO: remove or hide deleted msgs

Expand All @@ -132,6 +143,7 @@ const updateAccountInbox = async (account) => {
dont_check_unique: true,
messages: newMsgResps.map((el) => ({
message: Buffer.from(el.data.raw, 'base64').toString(),
externalUrl: `https://mail.google.com/mail/u/${account._value.username['_value.%']}/#all/${el.data.id}`,
read: !el.data.labelIds?.includes('UNREAD'),
inbox: el.data.labelIds?.includes('INBOX'),
})),
Expand Down
8 changes: 7 additions & 1 deletion packages/unigraph-dev-explorer/src/examples/email/Email.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,13 @@ const EmailMessage: DynamicViewRenderer = ({ data, callbacks }) => {
secondary={[
Sugar.Date.relative(new Date(unpadded?.message?.date_received)),
<div style={{ display: 'flex', alignItems: 'center' }}>
<Link />
<Link
style={{ display: data?._externalUrl ? '' : 'none', marginRight: '4px' }}
onClick={(ev) => {
ev.stopPropagation();
window.open(data._externalUrl, '_blank');
}}
/>
{`${unpadded.content?.abstract}...`}
</div>,
]}
Expand Down

0 comments on commit d0a4e6d

Please sign in to comment.