Fixed Double call to XenForo_Model_ProfilePost::prepareProfilePost

Rasmus Vind

Well-known member
I have an error occuring in my code based on the assumption that XenForo_Model_User :: prepareUser is only called once per user object. This assumption is wrong in one single place. It makes me think it is a bug. Take a look at this:

XenForo_ControllerPublic_ProfilePost
Code:
    public function actionShow()
    {
        $profilePostId = $this->_input->filterSingle('profile_post_id', XenForo_Input::UINT);
        $profilePostFetchOptions = array('likeUserId' => XenForo_Visitor::getUserId());
        list($profilePost, $user) = $this->getHelper('UserProfile')->assertProfilePostValidAndViewable($profilePostId, $profilePostFetchOptions);

        $profilePostModel = $this->_getProfilePostModel();

        $profilePost = $profilePostModel->prepareProfilePost($profilePost, $user);

        // ...
    }

XenForo_ControllerHelper_UserProfile

Code:
    public function assertProfilePostValidAndViewable($profilePostId, array $profilePostFetchOptions = array(),
        array $userFetchOptions = array()
    )
    {
        $profilePost = $this->getProfilePostOrError($profilePostId, $profilePostFetchOptions);
        $user = $this->assertUserProfileValidAndViewable($profilePost['profile_user_id'], $userFetchOptions);

        // ...

        $profilePost = $this->_controller->getModelFromCache('XenForo_Model_ProfilePost')->prepareProfilePost($profilePost, $user);

        return array($profilePost, $user);
    }

In XenForo_ControllerPublic_ProfilePost :: actionShow the function XenForo_ControllerHelper_UserProfile :: assertProfilePostValidAndViewable is called on the which in turn calls XenForo_Model_ProfilePost :: prepareProfilePost which is called later in the same code path in the calling function.
 
I've made a change here as it's duplicate work.

However, generally I would say that your code needs to be resilient to being called multiple times if possible. With the prepare functions, this should roughly be true:
Code:
prepare($data) == prepare(prepare($data))
 
Top Bottom