HackerOne users: Testing against this community violates our program's Terms of Service and will result in your bounty being denied.

How to include Likes in discussion json feed

Hi guys!

Please guide me on how to include the Likes count into the the json feed, for example, if I were to go to https://vanillaforums.org/discussion/31854.json 'LikesCount' would be displayed along with 'Name', 'Body', etc

Tagged:

Comments

  • vrijvlindervrijvlinder Papillon-Sauvage MVP

    Try the MembersListEnhanced plugin...

  • hgtonighthgtonight ∞ · New Moderator

    Heya!

    I would hook into the discussion model after the calculated columns have been added and loop through the data set and add the column:

    public function discussionModel_afterAddColumns_handler($sender) {
      $data =& $sender->EventArguments['Data'];
    
      $this->LikeModel->PreloadLikes($data->Comments);
    
      // iterate over data set and append count
      // TODO: implement this yourself
    }
    

    Search first

    Check out the Documentation! We are always looking for new content and pull requests.

    Click on insightful, awesome, and funny reactions to thank community volunteers for their valuable posts.

  • @hgtonight said:
    Heya!

    I would hook into the discussion model after the calculated columns have been added and loop through the data set and add the column:

    ```

    Much thanks for your reply! I can add the code you put up in the LikeModel.php file of the plugin or to a file in the core?

  • @vrijvlinder said:
    Try the MembersListEnhanced plugin...

    Thanks. I installed the plugin but it did it solve my issue. I'm trying to output the LikeCount in the JSON feed of each discussion.

  • hgtonighthgtonight ∞ · New Moderator

    I would put that code in the I like this plugin file, not the model file.

    You can't just copy and paste the code snippet, it is incomplete.

    Search first

    Check out the Documentation! We are always looking for new content and pull requests.

    Click on insightful, awesome, and funny reactions to thank community volunteers for their valuable posts.

  • edited April 2016

    @hgtonight I apologize for the delayed response. I've been scavenging for how to iterate within the 'discussionModel_afterAddColumns_handler'. I believe d2.* selects all the columns

    `public function discussionModel_afterAddColumns_handler($sender) {
    $data =& $sender->EventArguments['Data'];
    $this->LikeModel->PreloadLikes($data->Comments);
    //sql is used to get all discussions
    $Result = $Data->Result();

    DiscussionModel->getWhere(array(), 0, 25) slave 0.001010s

    select d2.*, w.UserID as WatchUserID, w.DateLastViewed as DateLastViewed, w.Dismissed as Dismissed, w.Bookmarked as Bookmarked, w.CountComments as CountCommentWatch, w.Participated as Participated
    from GDN_Discussion d
    join GDN_Discussion d2 on d.DiscussionID = d2.DiscussionID
    left join GDN_UserDiscussion w on w.DiscussionID = d2.DiscussionID and w.UserID = 2
    where d.CategoryID in ('-1', '10', '1', '2', '3', '7', '4', '6', '8')
    order by d.DateLastComment desc
    limit 25;

    $Data->ImportDataset($Result);

    }`

    By selecting d2.* I'm grabbing each field incuding the likeCounts. Am I close?

  • hgtonighthgtonight ∞ · New Moderator

    @jamesmercury said:
    Am I close?

    You shouldn't need to write any SQL. Use the existing model.

    Search first

    Check out the Documentation! We are always looking for new content and pull requests.

    Click on insightful, awesome, and funny reactions to thank community volunteers for their valuable posts.

  • edited April 2016

    You shouldn't need to write any SQL. Use the existing model.

    Much thanks @hgtonight for the past week or so I've been perusing the docs and the framework is making more sense to me.

    I grab the unique model concept but I could not find any function in the discussionModel ( at ...applications/vanilla/models/class.discussionmodel.php) for joining the 'discussions' table to the 'likes' table. At a quick glance the 'addDiscussionColumns' function appeared to be a good fit but it isn't. Please point me in the right direction.

    `
    public function discussionModel_afterAddColumns_handler($sender) {
    $data =& $sender->EventArguments['Data'];
    $this->LikeModel->PreloadLikes($data->Comments);

    $this->addDiscussionColumns(...)

    }
    `

  • R_JR_J Ex-Fanboy Munich Admin

    The discussionControllers index method uses the discussionModels getID method. This does not use the
    addDiscussionColumns(), fireEvent('AfterAddColumns') combination, but directly calls calculate(). So the only way is to hook into the SetCalculatedFields event which is fired at the end of calculate()

    public function discussionModel_setCalculatedFields_handler($sender, $args) {
        $likeCount = '?'; // your job
    
        $args['Discussion']->Likes = $likeCount;
    }
    
Sign In or Register to comment.