Skip to content

Commit

Permalink
fixup! Add tagging to messages
Browse files Browse the repository at this point in the history
  • Loading branch information
miaulalala committed Mar 22, 2021
1 parent 2f7785f commit 88b2a36
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 49 deletions.
11 changes: 10 additions & 1 deletion lib/Db/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,15 @@ public function setFlag(string $flag, bool $value = true) {
}

public function jsonSerialize() {
$tags = $this->getTags();
$indexed = array_combine(
array_map(
function (Tag $tag) {
return $tag->getImapLabel();
}, $tags),
$tags
);

return [
'databaseId' => $this->getId(),
'uid' => $this->getUid(),
Expand All @@ -277,7 +286,7 @@ public function jsonSerialize() {
'junk' => $this->getFlagJunk(),
'mdnsent' => $this->getFlagMdnsent(),
],
'tags' => $this->getTags(),
'tags' => $indexed,
'from' => $this->getFrom()->jsonSerialize(),
'to' => $this->getTo()->jsonSerialize(),
'cc' => $this->getCc()->jsonSerialize(),
Expand Down
4 changes: 2 additions & 2 deletions lib/Db/Tag.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,12 @@ public function __construct() {
*/
public function jsonSerialize() {
return [
'databaseId' => $this->getId(),
'id' => $this->getId(),
'userId' => $this->getUserId(),
'displayName' => $this->getDisplayName(),
'imapLabel' => $this->getImapLabel(),
'color' => $this->getColor(),
'isDefaultTag' => $this->getisDefaultTag(),
'isDefaultTag' => $this->getIsDefaultTag(),
];
}
}
55 changes: 12 additions & 43 deletions lib/Db/TagMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,31 +32,22 @@
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\IDBConnection;
use OCP\IL10N;
use Psr\Log\LoggerInterface;

/**
* @template-extends QBMapper<Tag>
*/
class TagMapper extends QBMapper {

/** @var LoggerInterface */
private $logger;

/** @var IL10N */
private $l10n;

public function __construct(IDBConnection $db,
LoggerInterface $logger,
IL10N $l10n) {
parent::__construct($db, 'mail_tags');
$this->logger = $logger;
$this->l10n = $l10n;
}

/**
* @param string $imapLabel
* @return Entity
*
* @throws DoesNotExistException
*/
public function getTagByImapLabel(string $imapLabel, string $userId): Entity {
Expand All @@ -71,10 +62,6 @@ public function getTagByImapLabel(string $imapLabel, string $userId): Entity {
}

/**
* @param integer $id
* @param string $userId
* @return Entity
*
* @throws DoesNotExistException
*/
public function getTagForUser(int $id, string $userId): Entity {
Expand All @@ -89,10 +76,7 @@ public function getTagForUser(int $id, string $userId): Entity {
}

/**
* @param integer $id
* @param string $userId
* @return array
*
* @return Tag[]
* @throws DoesNotExistException
*/
public function getAllTagForUser(string $userId): array {
Expand All @@ -105,12 +89,7 @@ public function getAllTagForUser(string $userId): array {
return $this->findEntities($qb);
}


/**
* @param integer $id
* @param string $userId
* @return Entity
*
* @throws DoesNotExistException
*/
public function getTag(int $id): Entity {
Expand All @@ -124,14 +103,9 @@ public function getTag(int $id): Entity {
/**
* Tag a message in the DB
*
* To tag (flag) a message on IMAP, @see OCA\Mail\Service\MailManager::tagMessage
*
* @param Tag $tag
* @param string $messageId
* @param string $userId
* @return void
* To tag (flag) a message on IMAP, @see \OCA\Mail\Service\MailManager::tagMessage
*/
public function tagMessage(Tag $tag, string $messageId, string $userId) {
public function tagMessage(Tag $tag, string $messageId, string $userId): void {
/** @var Tag $exists */
try {
$exists = $this->getTagByImapLabel($tag->getImapLabel(), $userId);
Expand All @@ -150,11 +124,9 @@ public function tagMessage(Tag $tag, string $messageId, string $userId) {
/**
* Remove a tag from a DB message
*
* @param Tag $tag
* @param string $messageId
* @return void
* This does not(!) untag a message on IMAP
*/
public function untagMessage(Tag $tag, string $messageId) {
public function untagMessage(Tag $tag, string $messageId): void {
$qb = $this->db->getQueryBuilder();
$qb->delete('mail_message_tags')
->where($qb->expr()->eq('imap_message_id', $qb->createNamedParameter($messageId)))
Expand All @@ -163,22 +135,22 @@ public function untagMessage(Tag $tag, string $messageId) {
}

/**
* @param array $messages
* @return array
* @param Message[] $messages
* @return Tag[][]
*/
public function getAllTagsForMessages(array $messages):array {
public function getAllTagsForMessages(array $messages): array {
$ids = array_map(function (Message $message) {
return $message->getMessageId();
}, $messages);

$qb = $this->db->getQueryBuilder();
$ids = $qb->select('mt.*')
$idsQuery = $qb->select('mt.*')
->from('mail_message_tags', 'mt')
->where(
$qb->expr()->in('imap_message_id', $qb->createNamedParameter($ids, IQueryBuilder::PARAM_STR_ARRAY))
);
$qb = $qb->execute();
$queryResult = $qb->fetchAll();
$idsQuery = $idsQuery->execute();
$queryResult = $idsQuery->fetchAll();
if (empty($queryResult)) {
return [];
}
Expand All @@ -199,12 +171,9 @@ public function getAllTagsForMessages(array $messages):array {
* <i>The array_udiff can be removed and the insert warpped in
* an exception as soon as NC20 is not supported any more</i>
*
* @param MailAccount $account
* @return void
*
* @link https://github.com/nextcloud/mail/issues/25
*/
public function createDefaultTags(MailAccount $account) {
public function createDefaultTags(MailAccount $account): void {
$tags = [];
for ($i = 1; $i < 6; $i++) {
$tag = new Tag();
Expand Down
1 change: 0 additions & 1 deletion lib/Migration/Version1100Date20210304143008.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,6 @@ public function changeSchema(IOutput $output, Closure $schemaClosure, array $opt
* @param array $options
*/
public function postSchemaChange(IOutput $output, Closure $schemaClosure, array $options): void {
// repair step!
$accounts = $this->mailAccountMapper->getAllUserIdsWithAccounts();
foreach ($accounts as $account) {
$this->tagMapper->createDefaultTags($account);
Expand Down
4 changes: 2 additions & 2 deletions lib/Model/IMAPMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -752,8 +752,8 @@ public function toDbMessage(int $mailboxId, MailAccount $account): Message {
*
* @link https://github.com/nextcloud/mail/issues/25
*
* @param array $tags
* @return array
* @param string[] $tags
* @return Tag[]
*/
private function generateTagEntites(array $tags, string $userId): array {
$t = [];
Expand Down

0 comments on commit 88b2a36

Please sign in to comment.