Mailspring/app/internal_packages/thread-search/lib/search-mailbox-perspective.es6

87 lines
2.5 KiB
Text
Raw Normal View History

import {
Folder,
ChangeLabelsTask,
ChangeFolderTask,
AccountStore,
CategoryStore,
TaskFactory,
MailboxPerspective,
2017-09-27 02:33:08 +08:00
} from 'nylas-exports';
import SearchQuerySubscription from './search-query-subscription';
class SearchMailboxPerspective extends MailboxPerspective {
constructor(sourcePerspective, searchQuery) {
2017-09-27 02:33:08 +08:00
super(sourcePerspective.accountIds);
if (typeof searchQuery !== 'string') {
2017-09-27 02:33:08 +08:00
throw new Error('SearchMailboxPerspective: Expected a `string` search query');
}
this.searchQuery = searchQuery;
if (sourcePerspective instanceof SearchMailboxPerspective) {
this.sourcePerspective = sourcePerspective.sourcePerspective;
} else {
this.sourcePerspective = sourcePerspective;
}
2017-09-27 02:33:08 +08:00
this.name = `Searching ${this.sourcePerspective.name}`;
}
_folderScope() {
// When the inbox is focused we don't specify a folder scope. If the user
// wants to search just the inbox then they have to specify it explicitly.
if (this.sourcePerspective.isInbox()) {
return '';
}
2017-09-27 02:33:08 +08:00
const folderQuery = this.sourcePerspective
.categories()
.map(c => c.displayName)
.join('" OR in:"');
return `AND (in:"${folderQuery}")`;
}
emptyMessage() {
2017-09-27 02:33:08 +08:00
return 'No search results available';
}
isEqual(other) {
2017-09-27 02:33:08 +08:00
return super.isEqual(other) && other.searchQuery === this.searchQuery;
}
threads() {
2017-09-27 02:33:08 +08:00
return new SearchQuerySubscription(
`(${this.searchQuery}) ${this._folderScope()}`,
this.accountIds
);
}
canReceiveThreadsFromAccountIds() {
2017-09-27 02:33:08 +08:00
return false;
}
tasksForRemovingItems(threads) {
return TaskFactory.tasksForThreadsByAccountId(threads, (accountThreads, accountId) => {
const account = AccountStore.accountForId(accountId);
const dest = account.preferredRemovalDestination();
if (dest instanceof Folder) {
return new ChangeFolderTask({
threads: accountThreads,
2017-09-27 02:33:08 +08:00
source: 'Dragged out of list',
folder: dest,
2017-09-27 02:33:08 +08:00
});
}
if (dest.role === 'all') {
// if you're searching and archive something, it really just removes the inbox label
return new ChangeLabelsTask({
threads: accountThreads,
2017-09-27 02:33:08 +08:00
source: 'Dragged out of list',
labelsToRemove: [CategoryStore.getInboxCategory(accountId)],
2017-09-27 02:33:08 +08:00
});
}
2017-09-27 02:33:08 +08:00
throw new Error('Unexpected destination returned from preferredRemovalDestination()');
});
}
}
export default SearchMailboxPerspective;