-
Notifications
You must be signed in to change notification settings - Fork 260
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make it possible to mark messages important
Signed-off-by: Christoph Wurst <christoph@winzerhof-wurst.at>
- Loading branch information
1 parent
2bf7d91
commit e8772da
Showing
13 changed files
with
651 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace OCA\Mail\Migration; | ||
|
||
use Closure; | ||
use OCP\DB\ISchemaWrapper; | ||
use OCP\IDBConnection; | ||
use OCP\Migration\IOutput; | ||
use OCP\Migration\SimpleMigrationStep; | ||
|
||
class Version1040Date20200422130220 extends SimpleMigrationStep { | ||
|
||
/** @var IDBConnection */ | ||
protected $connection; | ||
|
||
public function __construct(IDBConnection $connection) { | ||
$this->connection = $connection; | ||
} | ||
|
||
/** | ||
* @param IOutput $output | ||
* @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper` | ||
* @param array $options | ||
* | ||
* @return ISchemaWrapper | ||
*/ | ||
public function changeSchema(IOutput $output, Closure $schemaClosure, array $options): ISchemaWrapper { | ||
/** @var ISchemaWrapper $schema */ | ||
$schema = $schemaClosure(); | ||
|
||
$schema->dropTable('mail_messages'); | ||
|
||
return $schema; | ||
} | ||
|
||
public function postSchemaChange(IOutput $output, \Closure $schemaClosure, array $options) { | ||
// Reset locks and sync tokens | ||
$qb1 = $this->connection->getQueryBuilder(); | ||
$updateMailboxes = $qb1->update('mail_mailboxes') | ||
->set('sync_new_lock', $qb1->createNamedParameter(null)) | ||
->set('sync_new_token', $qb1->createNamedParameter(null)) | ||
->set('sync_changed_lock', $qb1->createNamedParameter(null)) | ||
->set('sync_changed_token', $qb1->createNamedParameter(null)) | ||
->set('sync_vanished_lock', $qb1->createNamedParameter(null)) | ||
->set('sync_vanished_token', $qb1->createNamedParameter(null)); | ||
$updateMailboxes->execute(); | ||
|
||
// Clean up some orphaned data | ||
$qb2 = $this->connection->getQueryBuilder(); | ||
$deleteRecipients = $qb2->delete('mail_recipients'); | ||
$deleteRecipients->execute(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace OCA\Mail\Migration; | ||
|
||
use Closure; | ||
use OCP\DB\ISchemaWrapper; | ||
use OCP\Migration\IOutput; | ||
use OCP\Migration\SimpleMigrationStep; | ||
|
||
class Version1040Date20200422142920 extends SimpleMigrationStep { | ||
|
||
/** | ||
* @param IOutput $output | ||
* @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper` | ||
* @param array $options | ||
* | ||
* @return null|ISchemaWrapper | ||
*/ | ||
public function changeSchema(IOutput $output, Closure $schemaClosure, array $options) { | ||
/** @var ISchemaWrapper $schema */ | ||
$schema = $schemaClosure(); | ||
|
||
$messagesTable = $schema->createTable('mail_messages'); | ||
$messagesTable->addColumn('id', 'integer', [ | ||
'autoincrement' => true, | ||
'notnull' => true, | ||
'length' => 20, | ||
]); | ||
$messagesTable->addColumn('uid', 'integer', [ | ||
'notnull' => true, | ||
'length' => 4, | ||
]); | ||
$messagesTable->addColumn('message_id', 'string', [ | ||
'notnull' => false, | ||
'length' => 255, | ||
]); | ||
$messagesTable->addColumn('mailbox_id', 'integer', [ | ||
'notnull' => true, | ||
'length' => 20, | ||
]); | ||
$messagesTable->addColumn('subject', 'string', [ | ||
'notnull' => true, | ||
'length' => 255, | ||
'default' => '', | ||
]); | ||
$messagesTable->addColumn('sent_at', 'integer', [ | ||
'notnull' => true, | ||
'length' => 4, | ||
]); | ||
$messagesTable->addColumn('flag_answered', 'boolean', [ | ||
'notnull' => true, | ||
'default' => false, | ||
]); | ||
$messagesTable->addColumn('flag_deleted', 'boolean', [ | ||
'notnull' => true, | ||
'default' => false, | ||
]); | ||
$messagesTable->addColumn('flag_draft', 'boolean', [ | ||
'notnull' => true, | ||
'default' => false, | ||
]); | ||
$messagesTable->addColumn('flag_flagged', 'boolean', [ | ||
'notnull' => true, | ||
'default' => false, | ||
]); | ||
$messagesTable->addColumn('flag_seen', 'boolean', [ | ||
'notnull' => true, | ||
'default' => false, | ||
]); | ||
$messagesTable->addColumn('flag_forwarded', 'boolean', [ | ||
'notnull' => true, | ||
'default' => false, | ||
]); | ||
$messagesTable->addColumn('flag_junk', 'boolean', [ | ||
'notnull' => true, | ||
'default' => false, | ||
]); | ||
$messagesTable->addColumn('flag_notjunk', 'boolean', [ | ||
'notnull' => true, | ||
'default' => false, | ||
]); | ||
$messagesTable->addColumn('flag_attachments', 'boolean', [ | ||
'notnull' => false, | ||
]); | ||
$messagesTable->addColumn('flag_important', 'boolean', [ | ||
'notnull' => true, | ||
'default' => false, | ||
]); | ||
$messagesTable->addColumn('structure_analyzed', 'boolean', [ | ||
'notnull' => true, | ||
'default' => false, | ||
]); | ||
$messagesTable->addColumn('preview_text', 'string', [ | ||
'notnull' => false, | ||
'length' => 255, | ||
]); | ||
$messagesTable->addColumn('updated_at', 'integer', [ | ||
'notnull' => false, | ||
'length' => 4, | ||
]); | ||
$messagesTable->setPrimaryKey(['id']); | ||
// We allow each UID just once | ||
$messagesTable->addUniqueIndex( | ||
[ | ||
'uid', | ||
'mailbox_id', | ||
], | ||
'mail_msg_mb_uid_idx' | ||
); | ||
$messagesTable->addIndex(['sent_at'], 'mail_msg_sent_idx'); | ||
|
||
return $schema; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
102 changes: 102 additions & 0 deletions
102
lib/Service/Classification/OftenContactedSenderClassifier.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* @copyright 2020 Christoph Wurst <christoph@winzerhof-wurst.at> | ||
* | ||
* @author 2020 Christoph Wurst <christoph@winzerhof-wurst.at> | ||
* | ||
* @license GNU AGPL version 3 or any later version | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as | ||
* published by the Free Software Foundation, either version 3 of the | ||
* License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
namespace OCA\Mail\Service\Classification; | ||
|
||
use OCA\Mail\Account; | ||
use OCA\Mail\Address; | ||
use OCA\Mail\Db\Mailbox; | ||
use OCA\Mail\Db\MailboxMapper; | ||
use OCA\Mail\Db\Message; | ||
use OCP\AppFramework\Db\DoesNotExistException; | ||
use OCP\DB\QueryBuilder\IQueryBuilder; | ||
use OCP\IDBConnection; | ||
|
||
class OftenContactedSenderClassifier extends AClassifier { | ||
use SafeRatio; | ||
|
||
/** @var MailboxMapper */ | ||
private $mailboxMapper; | ||
|
||
/** @var IDBConnection */ | ||
private $db; | ||
|
||
public function __construct(MailboxMapper $mailboxMapper, | ||
IDBConnection $db) { | ||
$this->mailboxMapper = $mailboxMapper; | ||
$this->db = $db; | ||
} | ||
|
||
public function isImportant(Account $account, Mailbox $mailbox, Message $message): bool { | ||
$sender = $message->getTo()->first(); | ||
if ($sender === null) { | ||
return false; | ||
} | ||
|
||
try { | ||
$mb = $this->mailboxMapper->findSpecial($account, 'sent'); | ||
} catch (DoesNotExistException $e) { | ||
return false; | ||
} | ||
|
||
return $this->greater( | ||
$this->getMessagesSentTo($mb, $sender->getEmail()), | ||
$this->getMessagesSentTotal($mb), | ||
0.1, | ||
true // The very first message is important | ||
); | ||
} | ||
|
||
private function getMessagesSentTotal(Mailbox $mb): int { | ||
$qb = $this->db->getQueryBuilder(); | ||
|
||
$select = $qb->select($qb->func()->count('*')) | ||
->from('mail_recipients', 'r') | ||
->join('r', 'mail_messages', 'm', $qb->expr()->eq('m.id', 'r.message_id', IQueryBuilder::PARAM_INT)) | ||
->join('r', 'mail_mailboxes', 'mb', $qb->expr()->eq('mb.id', $qb->expr()->castColumn('m.mailbox_id', IQueryBuilder::PARAM_INT), IQueryBuilder::PARAM_INT)) | ||
->where($qb->expr()->eq('r.id', $qb->createNamedParameter(Address::TYPE_FROM), IQueryBuilder::PARAM_INT)) | ||
->andWhere($qb->expr()->eq('mb.id', $qb->createNamedParameter($mb->getId(), IQueryBuilder::PARAM_INT))); | ||
$result = $select->execute(); | ||
$cnt = $result->fetchColumn(); | ||
$result->closeCursor(); | ||
return (int)$cnt; | ||
} | ||
|
||
private function getMessagesSentTo(Mailbox $mb, string $email): int { | ||
$qb = $this->db->getQueryBuilder(); | ||
|
||
$select = $qb->select($qb->func()->count('*')) | ||
->from('mail_recipients', 'r') | ||
->join('r', 'mail_messages', 'm', $qb->expr()->eq('m.id', 'r.message_id', IQueryBuilder::PARAM_INT)) | ||
->join('r', 'mail_mailboxes', 'mb', $qb->expr()->eq('mb.id', $qb->expr()->castColumn('m.mailbox_id', IQueryBuilder::PARAM_INT), IQueryBuilder::PARAM_INT)) | ||
->where($qb->expr()->eq('r.id', $qb->createNamedParameter(Address::TYPE_FROM), IQueryBuilder::PARAM_INT)) | ||
->andWhere($qb->expr()->eq('r.email', $qb->createNamedParameter($email))) | ||
->andWhere($qb->expr()->eq('mb.id', $qb->createNamedParameter($mb->getId(), IQueryBuilder::PARAM_INT))); | ||
$result = $select->execute(); | ||
$cnt = $result->fetchColumn(); | ||
$result->closeCursor(); | ||
return (int)$cnt; | ||
} | ||
} |
Oops, something went wrong.