1. This area is dedicated to the support of our xF2 add-ons, for support of our xF1 add-ons please use the dedicated forums.
    Note that we only accept support question and bug reports in this section.

    For suggestions, please wait until xF2 stable is released as we'll reorganize our forum at that time and accept them.
Dismiss Notice
We have just posted about our product policy for XF2.
Click HERE to view it.

Media Of The Month Implemented Code refactoring

Discussion in 'Archives' started by MtoR, Oct 25, 2018.

  1. MtoR

    MtoR Freaky Coder
    Staff Member

    Joined:
    Nov 14, 2015
    Messages:
    4,626
    Likes Received:
    726
    You've extended the Thread Creator and Replier Services in your Core Add-on (which is used by this Add-on) in order to be able to automatically create threads/posts owned by a specific user:

    This is not how things should be done in XF 2.

    Example Code
    Code:
    /** @var \XF\Service\Thread\Replier $replier */
    $replier = \XF::app()->service('XF:Thread\Replier', $thread);
    $replier->setAutomatedUser($messagePoster);
    $replier->setMessage($this->message, false);
    
    $replier->getPost()->message_state = 'visible';
    
    if ($replier && $replier->validate())
    {
       $existingLastPostDate = $replier->getThread()->last_post_date;
    
       $post = $replier->save();
    
       if (\XF::visitor()->user_id && $post->Thread->getVisitorReadDate() >= $existingLastPostDate)
       {
           $this->repository('XF:Thread')->markThreadReadByVisitor($post->Thread);
       }
    }
    
    This (and other similar occurances) should be refactored as
    Code:
    $post = false;
    $existingLastPostDate = $thread->last_post_date;
    
    \XF::asVisitor($messagePoster, function () use ()
    {
       /** @var \XF\Service\Thread\Replier $replier */
       $replier = \XF::app()->service('XF:Thread\Replier', $thread);
       $replier->setIsAutomated();
       $replier->setMessage($this->message, false);
    
       $replier->getPost()->message_state = 'visible';
    
       if ($replier && $replier->validate())
       {
           $post = $replier->save();
       }
    }
    
    if ($post !== false && \XF::visitor()->user_id && $thread->getVisitorReadDate() >= $existingLastPostDate)
    {
       $this->repository('XF:Thread')->markThreadReadByVisitor($thread);
    }
    
    upload_2018-10-25_21-11-53.gif
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice