-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
Notify post author about new comments by sending email #421
Conversation
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. |
cd1f3b5
to
c166bcd
Compare
638fb82
to
f87b37f
Compare
f87b37f
to
b4a97be
Compare
@javiereguiluz, I've added an event listener and custom event trigger. BTW, I found that we have an example of using mailer in the |
class: AppBundle\EventListener\CommentNotificationListener | ||
arguments: ['@mailer', '@router', '@translator', '%app.notifications.email_sender%'] | ||
tags: | ||
- { name: kernel.event_listener, event: comment.created, method: onCommentCreated } |
There was a problem hiding this comment.
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:
- When the method name is
on + camelCasedEventName
, themethod
parameter is optional. Should we add it anyway? - 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?
There was a problem hiding this 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'; |
There was a problem hiding this comment.
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
?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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%'] |
There was a problem hiding this comment.
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 ?
There was a problem hiding this comment.
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(). |
There was a problem hiding this comment.
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(), |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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"> |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
@voronkovich another great feature added to the Demo app! Thank you very much for contributing this. |
…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
See #420