1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Rate limiting creation of new thread

Discussion in 'XenForo Development Discussions' started by viewfinder, Aug 2, 2012.

  1. viewfinder

    viewfinder Member

    Probably a strange arrangement, but the idea is to limit the rate at which a user can post in a forum, for example, only one new thread every 24 hours. This is to limit new thread, not reply.

    What would be the best way to approach this? Is there a simple way to query the time stamp of the most recent thread created by the user in the current forum? This would need to be a live query instead of relying on statistics collected by the cron jobs.

    Any suggestions would be very much appreciated. Thanks.
  2. Jake Bunce

    Jake Bunce XenForo Moderator Staff Member

    Extend this action using the load_class_controller event:


    The skeleton of your extended class would look like this:

    class YourAddon_ControllerPublic_Forum extends XFCP_YourAddon_ControllerPublic_Forum
    	public function actionAddThread()
    		return parent::actionAddThread();
    Then of course you need to fill in your code to check if the user has posted in the last 24 hours.
  3. ragtek

    ragtek Guest

    i would extend the model instead of the controller...

    the method which returns if you're allowed to create threads is: XenForo_Model_Forum::canPostThreadInForum
    Jake Bunce likes this.
  4. ragtek

    ragtek Guest


    1. you'll need to change actionAddThread AND actionCreateThread with the way jake suggested (or alternative: use _preDispatch )
    2. you'll need some other changes too, so it's IMO much better, to change direct the only method, which returns if you're allowed to create threads, or not:)

    you could also change _assertCanPostThreadInForum in the controller, but i've run into some problems as i've tried it.
    there's at least 1 place in the code, where they didn't use _assertCanPostThreadInForum and called direct XenForo_Model_Forum::canPostThreadInForum

    but that's just the way which i prefer to change permissions via code:)
  5. viewfinder

    viewfinder Member

    Thanks Jake for the heads up. I started down that path, and had a stub implemented to test the plumbing, my code gets called, a good start.

    Then I realised the check is done after the attempt to "create" the thread, and it threw an ugly error message (incidentally, how does one provide a friendly error message in cases like these?). So the permission option crossed my mind, i.e. to stop the creation of the thread in the first place. But I'm not sure where to start.

    ragtek's reply is timely, if I understood correctly, the model approach would stop the thread creation in the first place, is this correct? Can you provide a quick tip on how to hook into XenForo_Model_Forum::canPostThreadInForum? I will look through the forum in the meantime and see if I can find other model extension hints.

  6. ragtek

    ragtek Guest

Share This Page