Like API

Last modified by Admin on 2024/12/16 00:12

cogLike pages in XWiki
TypeJAR
CategoryAPI
Developed by

XWiki Development Team

Active Installs991
Rating
1 Votes
LicenseGNU Lesser General Public License 2.1
Bundled With

XWiki Standard

Installable with the Extension Manager

Description

This module provides the API to allow to Like pages in XWiki.

Design

The Like implementation internally relies on the Extension.Ratings API.
The way Like manipulates the Ratings data is configured in the LikeRatingsConfiguration file.

By default, this configuration specifies that Likes are stored in a Solr core, without average data, and without storing the vote "0". 

Properties

This is an experimental feature only specified internally, so it's possible that it changes in any future version.

XWiki 12.10.9+, 13.4.3+, 13.7+

It's possible to chose to store the Solr average ratings for Like by using the following properties in xwiki.properties:

## Specify if the average rating should be stored or not for likes. Default value is false.
like.averagerating.isStored=true

## Specify where the average ratings should be stored. Default value is xobject. Possible values are currently solr or xobject.
like.averagerating.hint=xobject

This capability has been provided to allow performing HQL request to retrieve the top-liked pages, since the total number of votes is also stored in average xobjects.

Events

Two events related to Like can be triggered:
  - LikeEvent
  - UnlikeEvent

Both those events are triggered with the following information:
  - source: the user who performed the like, sent as a UserReference
  - data: the entity reference which have been liked or unliked, sent as an EntityReference

Script service

The following script service API is available:

/**
* Check if current user is allowed to use Like on the given reference.
* Note that we explicitely deny the right for guests.
* @param entityReference the reference on which to use like.
* @return {@code true} only if current user is not guest and has Like right on the reference.
*/

public boolean isAuthorized(EntityReference entityReference)

/**
* Check if the display button should be displayed: should be {@code true} if the like feature is enabled
* (see {@link LikeConfiguration#isEnabled()}) and if current user is authorized to use like
* (see {@link #isAuthorized(EntityReference)}) or if the configuration is set to always display it
* (see {@link LikeConfiguration#alwaysDisplayButton()}).
*
* @param entityReference the reference for which to display the button
* @return {@code true} only if the button should be displayed.
*/

public boolean displayButton(EntityReference entityReference)

/**
* @return {@code true} if the Like feature is enabled.
* @since 13.1RC1
*/

public boolean isEnabled()

/**
* Perform a like on the given reference with the current user, only if allowed.
*
* @param entityReference the reference on which to perform a like.
* @return the new number of likes if the operation succeeded, else return an empty optional.
*/

public Optional<Long> like(EntityReference entityReference)

/**
* Perform a unlike on the given reference with the current user, only if allowed.
*
* @param entityReference the reference on which to perform a like.
* @return the new number of likes if the operation succeeded, else return an empty optional.
*/

public Optional<Long> unlike(EntityReference entityReference)

/**
* Retrieve like information for the given reference.
*
* @param entityReference the reference for which to retrieve like information.
* @return the number of likes, or an empty optional in case of problem.
*/

public Optional<Long> getLikes(EntityReference entityReference)

/**
* Retrieve likes performed by the given user.
*
* @param userReference the user for whom to retrieve likes.
* @param offset the offset used for pagination.
* @param limit the limit used for pagination.
* @return a list of liked references.
*/

public List<EntityReference> getUserLikes(UserReference userReference, int offset, int limit)

/**
* Count the number of likes performed by the given user.
*
* @param userReference the user for whom to count likes.
* @return the number of likes performed.
* @since 12.9RC1
*/

public Optional<Long> countUserLikes(UserReference userReference)

/**
* Determine if the current user already liked the given reference.
* @param entityReference the reference for which to check if the current liked it or not already.
* @return {@code true} if the entity has been already liked.
*/

public boolean isLiked(EntityReference entityReference)

/**
* Return the likers of a reference.
*
* @param target the reference being liked.
* @param offset the start offset for pagination.
* @param limit the limit of results for pagination.
* @return a list of user references who liked the reference.
* @since 12.8RC1
*/

public List<UserReference> getLikers(EntityReference target, int offset, int limit)

Prerequisites & Installation Instructions

We recommend using the Extension Manager to install this extension (Make sure that the text "Installable with the Extension Manager" is displayed at the top right location on this page to know if this extension can be installed with the Extension Manager).

You can also use the manual method which involves dropping the JAR file and all its dependencies into the WEB-INF/lib folder and restarting XWiki.

Dependencies

Dependencies for this extension (org.xwiki.platform:xwiki-platform-like-api 16.10.1):

Get Connected