0

Just getting started with Services so this is probably an easy question.

I'm attempting to allow users to access their private messages from a web app using this sandbox module. At this point, after referring to the documentation on testing resources, I can get a JSON collection of the messages when I visit http://example.com/drupalgap/privatemsg, and I can get a specific thread if I visit http://example.com/drupalgap/privatemsg/threadID.

I understand the basic syntax is example.com + endpoint (drupalgap) + resource (privatemsg) + query.

However, the sandbox module includes a function for getting the number of unread messages, which I would like to do. Here's the code:

 /**
 * Get the number of unread private messages of the logged-in user.
 *
 * @param int $uid
 *      User ID.
 *
 * @return int
 *   The unread count.
 */
function _privatemsg_services_unread_count($uid = '') {
  global $user;

  if (!user_is_logged_in()) {
    return services_error(t('This user is not logged in.'), 403);
  }

  /*
   * If a user id other than the current user's ID is passed, validate that the
   * authenticated user has the correct permissions to read another user's
   * messages.
   */
  if (is_numeric($uid) && ($uid != $user->uid)) {
    if (user_access("read all private messages")) {
      $account = user_load($uid);
    }
    else {
      return services_error(
        t('This user does not have permissions to use this service.'),
        403
      );
    }
  }
  else {
    $account = $user;
  }

  // Return unread count.
  return privatemsg_unread_count($account);
}

My question is, how do I call this with services? I've tried things like http://example.com/drupalgap/privatemsg/unread_count/ and http://example.com/drupalgap/privatemsg_unread_count but I get no results.

1

Functions are exposed via service resources, by a module implementing hook_services_resources().

You should take a look at whatever module your using to provide Services functionality for the private message module, specifically its implementation of hook_services_resources().

If you see an entry with a callback of _privatemsg_services_unread_count, then the function you want is available via a resource. If not, it currently isn't, and you can implement hook_services_resources() yourself to expose it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.