Send notification when topic/post deleted or modified

Discussion forum for Extension Writers regarding Extension Development.
Post Reply
Sanborn
Registered User
Posts: 63
Joined: Mon Mar 16, 2015 7:01 pm

Send notification when topic/post deleted or modified

Post by Sanborn »

Hi,

I'd like to write an extension where I'm sending a notification in following scenarios:
1/ mod/admin modifies a post and fills in a reason why the post was modified
2/ mod/admin deletes a post and fills in a reason why the post was deleted
3/ mod/admin deletes an entire topic/thread and fills in a reason why the topic/thread was deleted

Every time the notification should be send to the user who has posted the post or started the topic, and have the following info:
- all info of modified/deleted post (title, content, basically all you see when viewing the post on the forum)
- date/time when post was modified/deleted
- user_id who modified/deleted the post
- reason why the post was modified/deleted

I already have an extension on phpBB 3.2 which does all the above, but I had to write some custom hacks in the forum code to achieve all that because of some events missing to hook into some flows.
Now I'm moving to phpBB 3.3 and I'm revising my extension to see if I can get rid of the custom hacks, and get a clean extension without tampering with phpBB code.

Scenario 1:
I can achieve this using the event: core.posting_modify_submit_post_after
All info I need is there, so no problems here.

Scenario 2:
I don't see an event which allows me to get all the info I need.
I'm passing through event core.delete_posts_before, but it's lacking info on who deleted a post and why.
There's also an event core.delete_post_after, but I'm not passing through this one for some reason. Yet this one contains the info I need. :(

Scenario 3:
Same problem.
I'm passing through event core.delete_posts_before, but it's lacking info on who deleted a post and why.
Also passing through event core.delete_topics_before_query, but also lacking the info I need.
Also passing through core.delete_topics_after_query, but also lacking the info I need.
There's also an event core.delete_post_after, but I'm not passing through this one for some reason. Yet this one contains the info I need. :(

Any advice on how I can accomplish scenarios 2 & 3 with a clean extension, without writing additional custom hacks in the phpBB code?
Maybe some event I'm missing here ?

User avatar
mrgoldy
Jr. Extension Validator
Posts: 1298
Joined: Tue Oct 06, 2009 7:34 pm
Location: The Netherlands
Name: Gijs
Contact:

Re: Send notification when topic/post deleted or modified

Post by mrgoldy »

What do you mean with that you are ‘not passing through’ the required event? Does your extension listener not get triggered or what exactly do you mean?
And any chance you could show us some code, like the event listener or a github repository?

Sanborn
Registered User
Posts: 63
Joined: Mon Mar 16, 2015 7:01 pm

Re: Send notification when topic/post deleted or modified

Post by Sanborn »

mrgoldy wrote:
Fri Feb 14, 2020 10:42 pm
What do you mean with that you are ‘not passing through’ the required event? Does your extension listener not get triggered or what exactly do you mean?
And any chance you could show us some code, like the event listener or a github repository?
I listen to the event, and I write a message to the log file, to see if I'm passing through the event.
When I delete a post or topic (regardless if I'm admin or regular user), I don't get the expected log message in the log for the event.

Basically this is my listener, where each method linked to an event is just writing a message to a log file.
This allows me to see which events I'm passing through when doing a delete action (see below for the output of each action in the logs).

Code: Select all

<?php
namespace myboard\notifs\event;

use Symfony\Component\EventDispatcher\EventSubscriberInterface;

/**
 * Event listener
 */
class main_listener implements EventSubscriberInterface
{
	/**
	 * @var \phpbb\report\report_handler_interface
	 */
	protected $report_handler;
	
	/** @var string phpBB root path */
	protected $root_path;
	
	function dump_message($anything)
	{	
		$log_file = $this->root_path . 'store/debug_myboard.log';
		$entry = date('Y-m-d H:i:s ') . print_r($anything, true) . PHP_EOL;
		file_put_contents($log_file, $entry, FILE_APPEND | LOCK_EX);
	}
	
	static public function getSubscribedEvents()
	{
		return array(
			'core.user_setup'						=> 'load_language_on_setup',
			
			'core.posting_modify_submit_post_after'		=> 'send_notif_posting_modify_submit_post_after',
			
			'core.delete_post_after'					=> 'send_notif_delete_post_after',
			
			'core.delete_posts_before'					=> 'send_notif_delete_posts_before',
			'core.delete_post_after'					=> 'send_notif_delete_posts_after',
			
			'core.delete_topics_before_query'			=> 'send_notif_delete_topics_before_query',
			'core.delete_topics_after_query'			=> 'send_notif_delete_topics_after_query'			
		);
	}
	
	/**
	* Constructor
	* NOTE: The parameters of this method must match in order and type with
	* the dependencies defined in the services.yml file for this service.
	*
	* @param \phpbb\user				$user				User object
	* @param \phpbb\db\driver			$db					Database object
	*/
	public function __construct(\phpbb\user $user, \phpbb\db\driver\driver_interface $db, \phpbb\report\handler_factory $report_factory, $root_path)
	{
		$this->user 			= $user;
		$this->db 				= $db;
		$this->report_handler	= $report_factory;
		$this->root_path 		= $root_path;
	}

	public function load_language_on_setup($event)
	{
		$this->user->add_lang_ext('myboard/notifs', 'notifs');
	}
	
	/**
	* This event allows you to define errors after the post action is performed
	*
	* @event core.posting_modify_submit_post_after
	* @var	array	post_data	Array with post data
	* @var	array	poll		Array with poll data
	* @var	array	data		Array with post data going to be stored in the database
	* @var	string	mode		What action to take if the form is submitted
	*				post|reply|quote|edit|delete
	* @var	int	post_id		ID of the post
	* @var	int	topic_id	ID of the topic
	* @var	int	forum_id	ID of the forum
	* @var	string	post_author_name	Author name for guest posts
	* @var	bool	update_message		Boolean if the post message was changed
	* @var	bool	update_subject		Boolean if the post subject was changed
	* @var	string	redirect_url		URL the user is going to be redirected to
	*				NOTE: Should be actual language strings, NOT language keys.
	* @since 3.1.0-RC5
	* @changed 3.1.6-RC1 remove submit and error from event  Submit and Error are checked previously prior to running event
	* @change 3.2.0-a1 Removed undefined page_title
	*/
	public function send_notif_posting_modify_submit_post_after($event)
	{
		$this->dump_message("in send_notif_posting_modify_submit_post_after");
	}
	
	/**
	* This event is used for performing actions directly after a post or topic
	* has been deleted.
	*
	* @event core.delete_post_after
	* @var	int		forum_id			Post forum ID
	* @var	int		topic_id			Post topic ID
	* @var	int		post_id				Post ID
	* @var	array	data				Post data
	* @var	bool	is_soft				Soft delete flag
	* @var	string	softdelete_reason	Soft delete reason
	* @var	string	post_mode			delete_topic, delete_first_post, delete_last_post or delete
	* @var	mixed	next_post_id		Next post ID in the topic (post ID or false)
	*
	* @since 3.1.11-RC1
	*/
	public function send_notif_delete_post_after($event)
	{
		$this->dump_message("in send_notif_delete_post_after");
	}
	
	/**
	* Perform additional actions before post(s) deletion
	*
	* @event core.delete_posts_before
	* @var	string	where_type					Variable containing posts deletion mode
	* @var	mixed	where_ids					Array or comma separated list of posts ids to delete
	* @var	bool	auto_sync					Flag indicating if topics/forums should be synchronized
	* @var	bool	posted_sync					Flag indicating if topics_posted table should be resynchronized
	* @var	bool	post_count_sync				Flag indicating if posts count should be resynchronized
	* @var	bool	call_delete_topics			Flag indicating if topics having no posts should be deleted
	* @var	array	delete_notifications_types	Array with notifications types to delete
	* @since 3.1.0-a4
	*/
	public function send_notif_delete_posts_before($event)
	{
		$this->dump_message("in send_notif_delete_posts_before");
	}
	
	/**
	* Perform additional actions after post(s) deletion
	*
	* @event core.delete_posts_after
	* @var	array	post_ids					Array with deleted posts' ids
	* @var	array	poster_ids					Array with deleted posts' author ids
	* @var	array	topic_ids					Array with deleted posts' topic ids
	* @var	array	forum_ids					Array with deleted posts' forum ids
	* @var	string	where_type					Variable containing posts deletion mode
	* @var	mixed	where_ids					Array or comma separated list of posts ids to delete
	* @var	array	delete_notifications_types	Array with notifications types to delete
	* @since 3.1.0-a4
	*/
	public function send_notif_delete_posts_after($event)
	{
		$this->dump_message("in send_notif_delete_posts_after");
	}
	
	/**
	 * Perform additional actions before topic(s) deletion
	 *
	 * @event core.delete_topics_before_query
	 * @var	array	table_ary	Array of tables from which all rows will be deleted that hold a topic_id occuring in topic_ids
	 * @var	array	topic_ids	Array of topic ids to delete
	 * @since 3.1.4-RC1
	 */
	public function send_notif_delete_topics_before_query($event)
	{
		$this->dump_message("in send_notif_delete_topics_before_query");
	}
	
	/**
	 * Perform additional actions after topic(s) deletion
	 *
	 * @event core.delete_topics_after_query
	 * @var	array	topic_ids	Array of topic ids that were deleted
	 * @since 3.1.4-RC1
	 */
	public function send_notif_delete_topics_after_query($event)
	{
		$this->dump_message("in send_notif_delete_topics_after_query");
	}	
}
The output in the logs for each action (started with a new log on each action):

When modifying another users post as admin:

Code: Select all

2020-02-15 09:21:44 in send_notif_posting_modify_submit_post_after
So I'm passing through the event "posting_modify_submit_post_after", which is what I need, where I have all the info I need to send a notification to the author of the post. So this one is PERFECT and OK for my needs.

When deleting another users post (a reply in a thread) as admin:

Code: Select all

2020-02-15 09:37:34 in send_notif_delete_posts_before
2020-02-15 09:37:34 in send_notif_delete_posts_after
So I'm NOT passing through the event "delete_post_after", which is what I need, because that one contains the data I need to send a notification to the author of the post.

When deleting an entire topic/thread as admin:

Code: Select all

2020-02-15 09:36:31 in send_notif_delete_posts_before
2020-02-15 09:36:32 in send_notif_delete_topics_before_query
2020-02-15 09:36:32 in send_notif_delete_topics_after_query
So I'm NOT passing through the event "delete_post_after", which is what I need, because that one contains the data I need to send a notification to the author of the topic/thread (first post author).

So basically my question is: how can I get the following data when deleting a single post or an entire topic/thread:
- All info of the deleted post (or first post when deleting an entire thread/topic)
- Filled in reason why a post or topic/thread is deleted.

User avatar
mrgoldy
Jr. Extension Validator
Posts: 1298
Joined: Tue Oct 06, 2009 7:34 pm
Location: The Netherlands
Name: Gijs
Contact:

Re: Send notification when topic/post deleted or modified

Post by mrgoldy »

Well, you've a duplicate array key in your subscriber events.
You've got core.delete_post_after twice, rather than one with the plural, postS.
So you're not subscribing to the core.delete_posts_after event with that event listener.

Hope that helps.

Sanborn
Registered User
Posts: 63
Joined: Mon Mar 16, 2015 7:01 pm

Re: Send notification when topic/post deleted or modified

Post by Sanborn »

mrgoldy wrote:
Sat Feb 15, 2020 10:23 am
Well, you've a duplicate array key in your subscriber events.
You've got core.delete_post_after twice, rather than one with the plural, postS.
So you're not subscribing to the core.delete_posts_after event with that event listener.

Hope that helps.
I see, thanks!
I've updated my subscription to this now:

Code: Select all

static public function getSubscribedEvents()
	{
		return array(
			'core.user_setup'						=> 'load_language_on_setup',
			
			'core.posting_modify_submit_post_after'		=> 'send_notif_posting_modify_submit_post_after',
			
			'core.delete_post_after'					=> 'send_notif_delete_post_after',
			
			'core.delete_posts_before'					=> 'send_notif_delete_posts_before',
			'core.delete_posts_after'					=> 'send_notif_delete_posts_after',
			
			'core.delete_topics_before_query'			=> 'send_notif_delete_topics_before_query',
			'core.delete_topics_after_query'			=> 'send_notif_delete_topics_after_query'			
		);
	}
When deleting a single post, I'm now passing through method "send_notif_delete_post_after", which is OK.

But when deleting an entire topic/thread, I'm only passing through the following:

Code: Select all

2020-02-15 10:46:03 in send_notif_delete_posts_before
2020-02-15 10:46:03 in send_notif_delete_posts_after
2020-02-15 10:46:03 in send_notif_delete_topics_before_query
2020-02-15 10:46:03 in send_notif_delete_topics_after_query
None of these allow me to get info on who deleted or why. Or can I fetch this data somehow based on a post or topic id in the "before" events?
I would expect to have this info in one of the "topics" events, similar to the post event.
Or should I submit a request to extend the "delete_topics_after_query" event with this info ?

Post Reply

Return to “Extension Writers Discussion”