Skip to content

Notify post author about new comments by sending email #421

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

Merged
merged 11 commits into from
Jan 12, 2017

Conversation

voronkovich
Copy link
Contributor

See #420

@javiereguiluz
Copy link
Member

I like this feature proposal ... but what about replacing the private method in the controller by an event listener + custom event trigger? That would allow us to show custom events in action.

@voronkovich voronkovich force-pushed the swiftmailer-example branch 2 times, most recently from 638fb82 to f87b37f Compare January 8, 2017 16:23
@voronkovich
Copy link
Contributor Author

@javiereguiluz, I've added an event listener and custom event trigger. BTW, I found that we have an example of using mailer in the ListUsersCommand https://github.com/symfony/symfony-demo/blob/master/src/AppBundle/Command/ListUsersCommand.php#L131

class: AppBundle\EventListener\CommentNotificationListener
arguments: ['@mailer', '@router', '@translator', '%app.notifications.email_sender%']
tags:
- { name: kernel.event_listener, event: comment.created, method: onCommentCreated }
Copy link
Member

Choose a reason for hiding this comment

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

I have two questions about the onCommentCreated method name:

  1. When the method name is on + camelCasedEventName, the method parameter is optional. Should we add it anyway?
  2. Some people like method names that describe the listened event (e.g. on + EventName) and others prefer semantic method names that describe their purposes (e.g. notifyCommentAdded()). Which is your preference?

Copy link
Member

@javiereguiluz javiereguiluz left a comment

Choose a reason for hiding this comment

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

I like this a lot. I've left a minor question and I'll tweak some help messages later ... but this looks ready and great. Thanks!

/**
* @var string
*/
private $sender = 'symfony-demo@localhost';
Copy link
Contributor

@bocharsky-bw bocharsky-bw Jan 9, 2017

Choose a reason for hiding this comment

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

What about to make this as a parameter in config.yml?

Copy link
Member

Choose a reason for hiding this comment

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

Yes. It's already passed to the constructor of the service, so I'd say: let's remove this private $sender property and just use the container parameter.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@javiereguiluz, @bocharsky-bw, do you suggest to inject container into the listener to get the sender parameter like there https://github.com/symfony/symfony-demo/blob/master/src/AppBundle/Command/ListUsersCommand.php#L138
As you can see in the listener definition those parameter (app.notifications.email_sender) is already injected.

Copy link
Member

@stof stof Jan 9, 2017

Choose a reason for hiding this comment

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

no, keep the property here, but remove its default value and make it a mandatory constructor argument

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok. I've removed the default sender's value.

# subscriber includes a method that returns the list of listened events.
app.comment_notification:
class: AppBundle\EventListener\CommentNotificationListener
arguments: ['@mailer', '@router', '@translator', '%app.notifications.email_sender%']
Copy link
Member

Choose a reason for hiding this comment

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

where is the app.notifications.email_sender parameter initialized ?

Copy link
Member

Choose a reason for hiding this comment

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

OK, it is already part of the codebase although not used for notifications today

arguments: ['@mailer', '@router', '@translator', '%app.notifications.email_sender%']
# There is an optional tag attribute called "method" which defines which method to execute when the event is triggered.
# By default the name of the method is on + "camel-cased event name".
# If the event is "comment.created" the method executed by default is onCommentCreated().
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@javiereguiluz, I've added a note about listener method's default name.

$subject = $this->translator->trans('post.recieved_comment');
$body = $this->translator->trans('post.recieved_comment_message', [
'%title%' => $post->getTitle(),
'%link%' => $linkToPost.'#comment_'.$comment->getId(),
Copy link
Member

Choose a reason for hiding this comment

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

@voronkovich first, thank you for your patience with this code review 😄

I have a last comment about your PR. What if we use the shinny new URL fragment support in Symfony 3.2 to forget about concatenating strings to define fragments?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@javiereguiluz, it's a good idea! I've updated an url generating to use the _fragment parameter.

@@ -276,6 +276,14 @@
<source>post.deleted_successfully</source>
<target>Post deleted successfully!</target>
</trans-unit>
<trans-unit id="post.comment_added">
Copy link
Member

Choose a reason for hiding this comment

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

Just for consistency matter (with event name and method name) shouldn't be named post.comment_created instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@yceruto, I think you're right! I've changed messages names and added a more appropriate prefix for them.

@javiereguiluz
Copy link
Member

@voronkovich another great feature added to the Demo app! Thank you very much for contributing this.

@javiereguiluz javiereguiluz merged commit 7dc958c into symfony:master Jan 12, 2017
javiereguiluz added a commit that referenced this pull request Jan 12, 2017
…voronkovich, javiereguiluz)

This PR was merged into the master branch.

Discussion
----------

Notify post author about new comments by sending email

See #420

Commits
-------

7dc958c Change prefix for translation messages
f918956 Use '_fragment' parameter
1e83a1d Final round of help notes improvements
bbc4077 Improved more help notes
a596761 Improved some help notes
ec601b6 Minor reword in the help note
f37e552 Minor tweaks in the translation strings
4125af9 Add note about event listener's method
f3b258a Remove default value for sender property
b4a97be Use event listener and custom event trigger
7b6ba9a Notify post author about new comments
@voronkovich voronkovich deleted the swiftmailer-example branch January 12, 2017 16:43
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.

5 participants