Skip to content

Retrieving a User Posts

Michele Angioni edited this page Feb 10, 2016 · 1 revision
MessageBoard::getOrderedUserPosts(MbUserInterface $user, $category = false, $private = null, 
    $page = 1, $limit = 20, $applyPresenter = false, $escapeText = false, MbUserInterface $userVisiting = null)

returns a Collection of Posts, ordered by datetime, posted in the $user message board, where $user is an instance of the User Model (which must implement the MbUserInterface):

 - $category defines the Category to which the Post belongs. Posts can belong to any Category;
 - $private defines is retrieved Posts must be private (=true), public (=false) or doesn"t matter (=null);
 - $page and $limit handle pagination;
 - $applyPresenter states if Posts and Comments must be passed to the presenter before being returned;  
 - $escateText states if post and comment texts must be escaped before being returned;
 - $userVisiting is the instance of the User model (which must implement the MbUserInterface) of the User who is
    requesting the posts. Leave it null if $user is requesting its own posts.  

By setting $applyPresenter to TRUE, the posts will also be passed to a PostPresenter before being returned.

By setting $escapeText to TRUE, the Post and Comment text will be escaped by HtmlPurifier so that it can be securely echoed in your views.

In the config file you can customize the HtmlPurifier behaviour in the mb_purifier_conf key. Take a look at the HtmlPurifier documentation.

If you want to use your own text purifier, create your own class which must implement the MicheleAngioni\MessageBoard\PurifierInterface interface. You then have to override the binding in the MessageBoardServiceProvider, that is define is a custom service provider

$this->app->bind(
    "MicheleAngioni\MessageBoard\Contracts\PurifierInterface",
    "Namespace\YourOwnPurifier"
);

You can also manually pass a single model to the presenter by using the presentModel(MbUserInterface $user, $model, $escapeText = false) method, or even an entire collection through presentCollection(MbUserInterface $user, Collection $collection, $escapeText = false).

When an User retrieves his/her own Posts, the datetime of the last retrieval will be saved. This way the MessageBoard can keep track of read and unread Posts.

To easily know if a Post has been read (i.e., the Post itself and all its Comments), just use the

$post->isNew()

method of the Post Presenter.

N.B. when a User is visiting the MessageBoard of another User, all Posts will be cosidered as already read.