Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added bulk set / clear flags functionality for mailbox messages #225

Merged
merged 2 commits into from
Oct 9, 2017

Conversation

wujku
Copy link
Contributor

@wujku wujku commented Oct 5, 2017

No description provided.

@wujku wujku closed this Oct 5, 2017
@wujku wujku reopened this Oct 5, 2017
src/Mailbox.php Outdated
* Bulk Set Flag for Messages.
*
* @param $flag \Seen, \Answered, \Flagged, \Deleted, and \Draft
* @param array Message numbers
Copy link
Collaborator

@Slamdunk Slamdunk Oct 5, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should be:

@param string $flag   \Seen, \Answered, \Flagged, \Deleted, and \Draft
@param array $numbers Message numbers

Same rule goes for clearFlag

src/Mailbox.php Outdated
{
$this->init();

$result = \imap_setflag_full($this->resource->getStream(), (string) \implode(',', $numbers), $flag, \ST_UID);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can directly return the result.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Return type of implode is already a string, no need for the type casting

@Slamdunk Slamdunk added this to the 1.1 milestone Oct 5, 2017
src/Mailbox.php Outdated
{
$this->init();

$result = \imap_setflag_full($this->resource->getStream(), (string) \implode(',', $numbers), $flag, \ST_UID);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Return type of implode is already a string, no need for the type casting

src/Mailbox.php Outdated
@@ -120,6 +120,36 @@ public function getStatus(int $flags = null): \stdClass
}

/**
* Bulk Set Flag for Messages.
*
* @param $flag \Seen, \Answered, \Flagged, \Deleted, and \Draft
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$flag param misses the string type

@Slamdunk
Copy link
Collaborator

Slamdunk commented Oct 5, 2017

Hi, thank you for this PR.
Before merging I have only one doubt about the numbers parameters to be an array.
The manual says:

A sequence of message numbers. You can enumerate desired messages with the X,Y syntax, or retrieve all messages within an interval with the X:Y syntax

The user can seamlessly write:

$mailbox->setFlag('\\Seen', [
    '1',
    '2',
    '3:10',
    '11,12,15',
]);

And the code would work as it's expected.

But if I can write sequences by hand, what is the added benefit of requesting the parameter to be an array?
I can write

$mailbox->setFlag('\\Seen', ['1,2,3:10,11,12,15']);

Bypassing the check.

The only benefit I see is we can save an implode call to the user if he/she actually give the array filled as you wrote in the test.

$this->assertFalse($message->isFlagged());
}

$this->mailbox->setFlag('\\Flagged', \array_keys($messages));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've tested that creating the messages in different mailboxes and using the setFlag method on a random mailbox, the flags are changed to all the UIDs specificed because, as the name said, the flags are changed by UIDs regardless on the mailbox the mails are in.

As such I find misleading to have these two new methods in the Mailbox class since they don't depend on it.

What about moving the code (and the test) to the Connection?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check 189815a, UIDs are related with specific mailbox. Moving functionality to Connection would require a mailboxName parameter.

@wujku
Copy link
Contributor Author

wujku commented Oct 5, 2017

@Slamdunk check 9f9f665

@Slamdunk
Copy link
Collaborator

Slamdunk commented Oct 5, 2017

Can you rebase the PR please?

@wujku wujku force-pushed the feature/bulk_flags branch from 6decb3a to c401737 Compare October 5, 2017 17:33
@wujku
Copy link
Contributor Author

wujku commented Oct 5, 2017

@Slamdunk Done

@Slamdunk
Copy link
Collaborator

Slamdunk commented Oct 6, 2017

Hi, I have another API doubt. In the test you write:

$message = $mailbox->getMessage('1');
var_dump($message->isSeen()); // True

$mailbox->clearFlag('\\Seen', '1');

$message = $mailbox->getMessage('1');
var_dump($message->isSeen()); // False

And this works correctly because each getMessage call creates a new Message instance, so without cache. But if I do this:

$message = $mailbox->getMessage('1');
var_dump($message->isSeen()); // True

$mailbox->clearFlag('\\Seen', '1');

var_dump($message->isSeen()); // True

The user can be fooled by reusing the same previous message, and this is because the internal Message cache was not cleared.

As far as I can see, the only solution to this would be to couple Message instances to the Mailbox, calculate the sequence number provided to the new two methods and clear related caches: too complex, doesn't worth the effort.

So lets go for an easier path: @wujku plese update the README.md Mailboxes chapter showing the new functionalities and add a warning about this edge behaviour that we are not going to address: the user must be aware of it and retrieve new Message instances if they want to set bulk flags and refresh the single Messages flags.

@Slamdunk
Copy link
Collaborator

Slamdunk commented Oct 6, 2017

Another thing: please don't squash the commits, only rebase it. Squash makes review code changes harder. I'll squash anyway the PR before merge.

@wujku
Copy link
Contributor Author

wujku commented Oct 6, 2017

@Slamdunk What to do with the Scrutinizer error? I did nothing with composer packages.

@Slamdunk
Copy link
Collaborator

Slamdunk commented Oct 6, 2017

Just ignore it: look only at Travis, your PR is fine 👍

@Slamdunk
Copy link
Collaborator

Slamdunk commented Oct 9, 2017

@wujku I'll mere this PR as soon as you commit and push the changes to README.md I underlined:

So lets go for an easier path: @wujku plese update the README.md Mailboxes chapter showing the new functionalities and add a warning about this edge behaviour that we are not going to address: the user must be aware of it and retrieve new Message instances if they want to set bulk flags and refresh the single Messages flags.

@Slamdunk Slamdunk merged commit e073b0f into ddeboer:master Oct 9, 2017
@Slamdunk
Copy link
Collaborator

Slamdunk commented Oct 9, 2017

Thank you @wujku

Slamdunk added a commit to Slamdunk/imap that referenced this pull request Oct 9, 2017
Slamdunk added a commit that referenced this pull request Oct 9, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants