This plugin may be interesting to both Discourse admins and people learning how to write Discourse plugins. I’ve given each audience separate sections below.
For Discourse admins:
This plugin gives staff members a button to quickly reset a thread’s bump time to the original time of any post within the thread. The button is added to the admin menu (spanner icon) for each post:
This is useful if you make minor edits to the last post and don’t want the thread bumped or if the wrong person/post are currently marked as the latest in the thread due to a delete or un-delete of a later post.
The plugin doesn’t really let you do anything you could not do already (other than repairing the last post/poster if they go wrong), it just lets you do things more conveniently. The ability to change the timestamp of a thread is built in to Discourse, but the UI for doing so is modal and requires you to separately select the date and time. You can only see the detailed date and time of a post in another modal UI, and cannot copy and paste between the two UIs as they use different formats.
Each use of the Reset Bump To Here button will be logged in the Discourse staff actions log, including the topic and post IDs and URL, and old and new timestamps:
This can help detect abuse of the functionality (e.g. to hide new posts from other admins, rather than the intended use of avoiding bumps when making formatting-only fixes to old threads, or undoing a user’s gratuitous bump without deleting their message entirely).
The plugin is on github as usual: GitHub - LeoDavidson/discourse-reset-bump: A Discourse plugin to quickly reset a thread's bump time to that of a post in the thread.
Follow the Install a Plugin howto, using
git clone https://github.com/leodavidson/discourse-reset-bump.git as the plugin command.
Once you’ve installed it, it will be enabled by default and can be disabled under the site’s admin / plugin settings area, via the
For new plugin developers:
- The source code is simple, since what the plugin does is simple.
- The code is extensively commented. Comments far outweigh code.
- All logic is in just 2 files (1 client side, 1 server side).
- I unpacked inline functions into normal top-level functions so you can more easily see the structure of things, and only have to deal with one concept at a time.
I’m still fuzzy on some concepts but I hope I got the important details right. Corrections to code or comments greatly appreciated.
In terms of what the plugin does:
- Standard enable/disable setting.
- Client side uses the newer withPluginApi initialization method.
- Adds a button to the post admin menu (pictured above) below each post.
- (Also has commented-out code showing how to add to the main post buttons, outside the menu.)
- Client side checks for staff privileges (so the UI isn’t shown to people who can’t use it).
- Button click triggers AJAX request to the server side.
- Server side checks for staff privileges (can’t rely on client for that; it can be bypassed).
- Server side checks the plugin is enabled (so if a security issue was found, the AJAX endpoint can be stubbed out quickly without having to restart the server).
- Server side takes the post ID from the client and looks up the topic details (or returns an error for the client to display).
- Server side updates the database records of the topic accordingly, and returns success if it worked.
- Server side logs the action to the staff action log. (Working out the right place for the i18n translated action string was tricky here.)
All easy once you know how, but as a newbie it took me days of unravelling source code, (outdated) forum threads, multiple unfamiliar languages and frameworks, which was quite overwhelming.
I also recommend looking at other plugins to see how you can write things in a “nicer” (or at least more compact!) way once you understand what’s going on. This one’s code is meant to be easy to understand and not necessarily the “best” way to do things.
I hope it helps you!