
Build Email Inbox Management and Search Features With Nylas
Learn how the Nylas Email API makes it easy to organize an email inbox and search message content.
Ben Lloyd Pearson | September 25, 2020
Want a PDF of this article?
Share it with a friend or save it for later reading.
x
We'll send the PDF straight to your inbox!
For more information about how this data is used, please view our Privacy Policy
const account = await nylas.account.get(); if (account.provider === "gmail") { // This account uses labels! } if (account.organizationUnit === "label") { // This account uses labels! }
const displayName = req.body.displayName; const label = nylas.labels.build(); label.displayName = displayName; await label.save();
res.json({ id: label.id, displayName: label.displayName });
const accountLabels = await nylas.labels.list();
const DEFAULT_LABELS = [ "inbox", "all", "trash", "archive", "drafts", "sent", "spam", "important" ];
const labels = accountLabels .filter(label => !DEFAULT_LABELS.includes(label.name)) .map(label => ({ id: label.id, displayName: label.displayName, checked: !!thread.labels.find(({ id }) => id === label.id) }));
const id = req.params.id; const thread = await nylas.threads.find(id, null, { view: "expanded" });
const threadDefaultLabels = thread.labels.filter(label => DEFAULT_LABELS.includes(label.name) ); thread.labels = [ ...threadDefaultLabels, ...req.body.labels.filter(label => label.checked) ];
await thread.save();
const PAGE_LIMIT = 6; // Limit the number of results to make the request faster const page = req.query.page >= 1 ? req.query.page : 1; const search = req.query.search; const threads = await nylas.threads.search(search, { limit: PAGE_LIMIT + 1, offset: (page - 1) * PAGE_LIMIT });
const Promise = require("bluebird"); const results = Promise.map(threads, async thread => { const messages = await nylas.messages.list({ thread_id: thread.id }); thread.messages = messages; return thread; }, { concurrency: 5 });
res.status(200).json({ hasPrevious: page > 1, hasNext: threads.length > PAGE_LIMIT, threads: threads.slice(0, PAGE_LIMIT).map(thread => ({ id: thread.id, subject: thread.subject, from: thread.from, date: thread.lastMessageTimestamp, snippet: thread.snippet, unread: thread.unread, hasAttachments: thread.hasAttachments })) });