Skip to content

Commit

Permalink
add sorting capabilities to mail
Browse files Browse the repository at this point in the history
Signed-off-by: hamza221 <[email protected]>
  • Loading branch information
hamza221 committed Mar 15, 2023
1 parent 9cb87af commit 1c07a48
Show file tree
Hide file tree
Showing 7 changed files with 133 additions and 21 deletions.
2 changes: 2 additions & 0 deletions lib/Contracts/IMailSearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ public function findMessage(Account $account,
* @param Mailbox $mailbox
* @param string|null $filter
* @param int|null $cursor
* @param string $sortOrder
* @param int|null $limit
*
* @return Message[]
Expand All @@ -64,6 +65,7 @@ public function findMessages(Account $account,
Mailbox $mailbox,
?string $filter,
?int $cursor,
string $sortOrder,
?int $limit): array;

/**
Expand Down
6 changes: 4 additions & 2 deletions lib/Controller/MessagesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ public function __construct(string $appName,
public function index(int $mailboxId,
int $cursor = null,
string $filter = null,
string $sortOrder = 'newest',
int $limit = null): JSONResponse {
try {
$mailbox = $this->mailManager->getMailbox($this->currentUserId, $mailboxId);
Expand All @@ -145,14 +146,15 @@ public function index(int $mailboxId,
return new JSONResponse([], Http::STATUS_FORBIDDEN);
}

$this->logger->debug("loading messages of folder <$mailboxId>");

$this->logger->debug("loading messages of mailbox <$mailboxId>");
$order = $sortOrder === 'oldest' ? 'ASC' : 'DESC';
return new JSONResponse(
$this->mailSearch->findMessages(
$account,
$mailbox,
$filter === '' ? null : $filter,
$cursor,
$order,
$limit
)
);
Expand Down
11 changes: 10 additions & 1 deletion lib/Controller/PageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,10 @@ public function index(): TemplateResponse {
'debug',
$this->config->getSystemValue('debug', false)
);

$this->initialStateService->provideInitialState(
'smime-sign-accounts',
$this->preferences->getPreference($this->currentUserId, 'smime-sign-accounts','')
);
$this->initialStateService->provideInitialState(
'ncVersion',
$this->config->getSystemValue('version', '0.0.0')
Expand Down Expand Up @@ -162,6 +165,11 @@ public function index(): TemplateResponse {
$this->tagMapper->getAllTagsForUser($this->currentUserId)
);

$this->initialStateService->provideInitialState(
'sort-order',
$this->preferences->getPreference($this->currentUserId, 'sort-order', 'newest-first')
);

try {
$password = $this->credentialStore->getLoginCredentials()->getPassword();
$passwordIsUnavailable = $password === null || $password === '';
Expand All @@ -183,6 +191,7 @@ public function index(): TemplateResponse {
'collect-data' => $this->preferences->getPreference($this->currentUserId, 'collect-data', 'true'),
'start-mailbox-id' => $this->preferences->getPreference($this->currentUserId, 'start-mailbox-id'),
'tag-classified-messages' => $this->preferences->getPreference($this->currentUserId, 'tag-classified-messages', 'true'),

]);
$this->initialStateService->provideInitialState(
'prefill_displayName',
Expand Down
20 changes: 14 additions & 6 deletions lib/Db/MessageMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -701,7 +701,7 @@ public function findByMessageId(Account $account, string $messageId): array {
*
* @return int[]
*/
public function findIdsByQuery(Mailbox $mailbox, SearchQuery $query, ?int $limit, array $uids = null): array {
public function findIdsByQuery(Mailbox $mailbox, SearchQuery $query, ?string $sortOrder, ?int $limit, array $uids = null): array {
$qb = $this->db->getQueryBuilder();

if ($this->needDistinct($query)) {
Expand Down Expand Up @@ -822,7 +822,9 @@ public function findIdsByQuery(Mailbox $mailbox, SearchQuery $query, ?int $limit

$select->andWhere($qb->expr()->isNull('m2.id'));

$select->orderBy('m.sent_at', 'desc');
if($sortOrder){
$select->orderBy('m.sent_at', $sortOrder);
}

if ($limit !== null) {
$select->setMaxResults($limit);
Expand Down Expand Up @@ -1079,7 +1081,7 @@ public function findByMailboxAndIds(Mailbox $mailbox, string $userId, array $ids
*
* @return Message[]
*/
public function findByIds(string $userId, array $ids): array {
public function findByIds(string $userId, array $ids ,?string $sortOrder ): array {
if (empty($ids)) {
return [];
}
Expand All @@ -1089,9 +1091,15 @@ public function findByIds(string $userId, array $ids): array {
->from($this->getTableName())
->where(
$qb->expr()->in('id', $qb->createParameter('ids'))
)
->orderBy('sent_at', 'desc');

);
if($sortOrder === 'desc' || $sortOrder === 'asc'){
$qb->orderBy('sent_at', $sortOrder);
}
else{
$qb->orderBy('sent_at', 'desc');
//TODO: add logging here
}

$results = [];
foreach (array_chunk($ids, 1000) as $chunk) {
$qb->setParameter('ids', $chunk, IQueryBuilder::PARAM_INT_ARRAY);
Expand Down
9 changes: 5 additions & 4 deletions lib/Service/Search/MailSearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ public function findMessages(Account $account,
Mailbox $mailbox,
?string $filter,
?int $cursor,
string $sortOrder,
?int $limit): array {
if ($mailbox->hasLocks($this->timeFactory->getTime())) {
throw MailboxLockedException::from($mailbox);
Expand All @@ -130,8 +131,9 @@ public function findMessages(Account $account,
$account,
$mailbox,
$this->messageMapper->findByIds($account->getUserId(),
$this->getIdsLocally($account, $mailbox, $query, $limit)
)
$this->getIdsLocally($account, $mailbox, $query, $limit),
$sortOrder,
)
);
}

Expand All @@ -155,8 +157,7 @@ public function findMessagesGlobally(IUser $user,
}

return $this->messageMapper->findByIds($user->getUID(),
$this->getIdsGlobally($user, $query, $limit)
);
$this->getIdsGlobally($user, $query, $limit),);
}

/**
Expand Down
79 changes: 79 additions & 0 deletions src/components/AppSettingsMenu.vue
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,38 @@
<SmimeCertificateModal v-if="displaySmimeCertificateModal"
@close="displaySmimeCertificateModal = false" />

<h2 class="mailvelope-section">
{{ t('mail', 'Sorting') }}
</h2>
<p class="app-settings">
<input
id="unread-first-toggle"
class="checkbox"
type="checkbox"
:checked="unreadFirst"
@change="onToggleUnreadFirst">
<label for="unread-first-toggle">{{ unreadFirstText }}</label>
</p>
<div class="chronological-order-section"> <p>{{t('mail', 'Sort by')}}</p>
<div>
<ButtonVue
:type="sortOrder === 'newest' ? 'primary' : 'secondary'"
@click="onSortByDate">
{{ t('mail', 'Newest') }}
</ButtonVue>
<ButtonVue
:type="sortOrder === 'oldest' ? 'primary' : 'secondary'"
@click="onSortByDate">
{{ t('mail', 'Oldest') }}
</ButtonVue>
</div>
</div>

<p class="mailvelope-section">
{{ t('mail', 'Looking for a way to encrypt your emails?') }}
</p>


<a
href="https://www.mailvelope.com/"
target="_blank"
Expand Down Expand Up @@ -143,11 +172,16 @@ export default {
displayKeyboardShortcuts: false,
// eslint-disable-next-line
autoTaggingText: t('mail', 'Automatically classify importance of new email'),
unreadFirstText:t('mail', 'Unread first'),
toggleAutoTagging: false,
displaySmimeCertificateModal: false,
sortOrder: 'newest',
}
},
computed: {
unreadFirst() {
return false //this.$store.getters.getPreference('unread-first', false) ==="true"
},
useBottomReplies() {
return this.$store.getters.getPreference('reply-mode', 'top') === 'bottom'
},
Expand Down Expand Up @@ -204,6 +238,41 @@ export default {
this.loadingOptOutSettings = false
})
},
async onSortByDate(e) {
const previousValue = this.sortOrder
this.sortBy = e.target.innerText.toLowerCase()
try {
await this.$store
.dispatch('savePreference', {
key: 'sort-order',
value: this.sortOrder,
})
} catch (error) {
Logger.error('could not save preferences', { error })
showError(t('mail', 'Could not update preference'))
} finally {
this.sortBy = previousValue
}
window.location.reload()
},
async onToggleUnreadFirst(e) {
this.toggleUnreadFirst = true
/*try {
await this.$store
.dispatch('savePreference', {
key: 'unread-first',
value: e.target.checked ? 'true' : 'false',
})
} catch (error) {
Logger.error('could not save preferences', { error })
showError(t('mail', 'Could not update preference'))
} finally {
this.toggleUnreadFirst = false
}*/
},
async onToggleAutoTagging(e) {
this.toggleAutoTagging = true
Expand Down Expand Up @@ -301,4 +370,14 @@ p.app-settings {
}
}
.chronological-order-section{
display: flex;
align-items: center;
& > div:first-of-type {
display: flex;
}
& >p {
margin-right: 5px;
}
}
</style>
27 changes: 19 additions & 8 deletions src/components/EnvelopeList.vue
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@
</transition>
<transition-group name="list">
<Envelope
v-for="(env, index) in envelopes"
v-for="(env, index) in sortedEnvelops"
:key="env.databaseId"
:data="env"
:mailbox="mailbox"
Expand Down Expand Up @@ -278,6 +278,17 @@ export default {
}
},
computed: {
sortOrder(){
return this.$store.getters.getPreference('sort-order','newest')
},
sortedEnvelops(){
if(this.sortOrder === 'oldest'){
return this.envelopes.sort((a, b) => {
return a.dateInt < b.dateInt ? -1 : 1
})
}
},
selectMode() {
// returns true when in selection mode (where the user selects several emails at once)
return this.selection.length > 0
Expand Down Expand Up @@ -307,15 +318,15 @@ export default {
return this.selectedEnvelopes.every((env) => env.flags.flagged === true)
},
selectedEnvelopes() {
return this.envelopes.filter((env) => this.selection.includes(env.databaseId))
return this.sortedEnvelops.filter((env) => this.selection.includes(env.databaseId))
},
hasMultipleAccounts() {
const mailboxIds = this.envelopes.map(envelope => envelope.mailboxId)
const mailboxIds = this.sortedEnvelops.map(envelope => envelope.mailboxId)
return Array.from(new Set(mailboxIds)).length > 1
},
},
watch: {
envelopes(newVal, oldVal) {
sortedEnvelops(newVal, oldVal) {
// Unselect vanished envelopes
const newIds = newVal.map((env) => env.databaseId)
this.selection = this.selection.filter((id) => newIds.includes(id))
Expand Down Expand Up @@ -381,7 +392,7 @@ export default {
let nextEnvelopeToNavigate
let isAllSelected
if (this.selectedEnvelopes.length === this.envelopes.length) {
if (this.selectedEnvelopes.length === this.sortedEnvelops.length) {
isAllSelected = true
} else {
const indexSelectedEnvelope = this.selectedEnvelopes.findIndex((selectedEnvelope) =>
Expand All @@ -390,7 +401,7 @@ export default {
// one of threads is selected
if (indexSelectedEnvelope !== -1) {
const lastSelectedEnvelope = this.selectedEnvelopes[this.selectedEnvelopes.length - 1]
const diff = this.envelopes.filter(envelope => envelope === lastSelectedEnvelope || !this.selectedEnvelopes.includes(envelope))
const diff = this.sortedEnvelops.filter(envelope => envelope === lastSelectedEnvelope || !this.selectedEnvelopes.includes(envelope))
const lastIndex = diff.indexOf(lastSelectedEnvelope)
nextEnvelopeToNavigate = diff[lastIndex === 0 ? 1 : lastIndex - 1]
}
Expand Down Expand Up @@ -460,12 +471,12 @@ export default {
const end = Math.max(this.lastToggledIndex, index)
const selected = this.selection.includes(envelope.databaseId)
for (let i = start; i <= end; i++) {
this.setEnvelopeSelected(this.envelopes[i], !selected)
this.setEnvelopeSelected(this.sortedEnvelops[i], !selected)
}
this.lastToggledIndex = index
},
unselectAll() {
this.envelopes.forEach((env) => {
this.sortedEnvelops.forEach((env) => {
env.flags.selected = false
})
this.selection = []
Expand Down

0 comments on commit 1c07a48

Please sign in to comment.