diff --git a/privatemsg_services/pm_service.inc b/privatemsg_services/pm_service.inc new file mode 100644 index 0000000..6089377 --- /dev/null +++ b/privatemsg_services/pm_service.inc @@ -0,0 +1,284 @@ +execute()); + foreach($query as $thread_id => $msg) { + $participants = privatemsg_thread_load($msg->thread_id); + $participants = $participants['participants']; + $parts = array(); + foreach($participants as $participant => $usr_obj) { + $participant_id = substr($participant, 5); + $org_name = profile2_load_by_user($participant_id, $type_name = 'exhibitor')->field_profile['und'][0]['organisation_name']; + if ($org_name) { + $parts[] = $participant_id . ',' . $org_name; + } + } + + $msg->participants = $parts; + $messages[] = $msg; + } + + // Return messages. + + return $messages; +} + +/** + * Get the number of unread private messages of the logged-in user. + * + * @return + * The unread count. + */ + +function pm_service_unread_count($uid = ''){ + + // User needs to be authenticated to proceed. + + 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); + } + } + + // Use the current user for the account. + + else { + $account = $user; + } + + // Return unread count. + + return privatemsg_unread_count($account); +} + +/** + * Send a private message to one or more recipients. + * + * @param $recipients + * String. A comma separated list of usernames for recipients of the message. + * + * @param $subject + * String. A message subject + * + * @param $body + * String. A message body + * + * @param $thread_id + * Integer. A thread ID. pass this parameter if you are sending a message reply. + * + * @return + * Boolean. Return TRUE if sending the message was successful. + */ + +function pm_service_send(array $message){ + $subject = $message['subject']; + $body = $message['body']; + $recipients=$message['recipients']; + $thread_id = $message['thread_id']; + $form_state = array(); + // Make sure the message author is logged in. + + global $user; + $account = user_load($user->uid); + if (!user_is_logged_in()) { + return services_error(t('Author is not logged in.') , 403); + } + + // Validate at least 1 recipient has been passed in. + + if ($recipients == '' && !$thread_id) { + return services_error(t('There are no recipients, please enter a recipient for the message.') , 400); + } + elseif (!$thread_id) { + + // No thread ID - we are sending a new message. + // Convert the recipients string to an array of user objects. + + list($recipients, $invalid) = _privatemsg_parse_userstring($recipients); + if (!empty($invalid)) { + + // At least one of the recipients could not be found. + + $invalid_usernames = array( + '@names' => implode(', ', $invalid) + ); + return services_error(t('One or more usernames are invalid: @names', $invalid_usernames) , 400); + } + + $result = privatemsg_new_thread($recipients, $subject, $body, array( + 'author' => $account + )); + if ($result['success']) { + return pm_service_thread_load($result["message"]->thread_id); + } + else { + return services_error(implode("\n", $result['messages']['error']) , 400); + } + } + else { + + // There is a thread id so we are sending a reply. + + $result = privatemsg_reply($thread_id, $body, array( + 'author' => $account + )); + if ($result['success']) { + return TRUE; + } + elseif (!empty($result[0])) { + + // If $result[0] this means the thread could not be loaded. + + return services_error($result[0], 404); + } + else { + + // Rlse there was some other problem. + + return services_error(implode("\n", $result['messages']['error']) , 400); + } + } +} + +/** + * Get all messages in a thread. + * + * @param @thread_id + * ID of the thread to be loaded. + * @param $offset + * Optional: Message offset from the start of the thread. + * @return + * An array of messages in a thread. + */ + +function pm_service_get_thread($thread_id, $offset = 0){ + + // Return if wrong paramters are passed. + + if (!$thread_id || !is_numeric($thread_id)) { + return services_error(t('Invalid parameters passed') , 400); + } + + // Make sure the user is logged in. + + global $user; + $account = user_load($user->uid); + if (!user_is_logged_in()) { + return services_error(t('The user is not logged in.') , 403); + } + + // Return the full thread. + + return pm_service_thread_load($thread_id, $account, $offset); +} + +function pm_service_thread_load($pmtid){ + $thread = privatemsg_thread_load($pmtid); + + $result = array( + 'pmtid' => $thread['thread_id'], + 'subject' => $thread['subject'], + 'participants' => array(), + 'messages' => array(), + ); + foreach ($thread['participants'] as $user){ + $result['participants'][] = $user->uid; + } + foreach ($thread['messages'] as $message){ + $result['messages'][] = array( + 'mid' => $message->mid, + 'author' => $message->author->uid, + 'timestamp' => $message->timestamp, + 'body' => $message->body, + 'is_new' => $message->is_new + ); + } + return $result; +} +function pm_service_change_delete($pmid) { + $delete_value = REQUEST_TIME; + global $user; + $account = clone $user; + $update = db_update('pm_index') + ->fields(array('deleted' => $delete_value)) + ->condition('mid', $pmid); + if ($account) { + $update + ->condition('recipient', $account->uid) + ->condition('type', array('user', 'hidden')); + } + $return = $update->execute(); + if ($return) { + return TRUE; + }else { + // Rlse there was some other problem. + return services_error(implode("\n", $result['messages']['error']) , 400); + } +} \ No newline at end of file diff --git a/privatemsg_services/pm_service.info b/privatemsg_services/pm_service.info new file mode 100644 index 0000000..f98f321 --- /dev/null +++ b/privatemsg_services/pm_service.info @@ -0,0 +1,7 @@ +; $Id$ +name = Privatemsg Service +description = Integrates PrivateMSG functionality with Services. +package = Services - services +dependencies[] = services +dependencies[] = privatemsg +core = 7.x diff --git a/privatemsg_services/pm_service.module b/privatemsg_services/pm_service.module new file mode 100644 index 0000000..d1ed978 --- /dev/null +++ b/privatemsg_services/pm_service.module @@ -0,0 +1,117 @@ + array( + 'title' => t('Load remote private messages') , + 'description' => t('View messages via services') , + ) , + 'send private messages from remote' => array( + 'title' => t('Send remote private messages') , + 'description' => t('Write messages via services') , + ) , + 'remove private messages from remote' => array( + 'title' => t('Remove remote private messages') , + 'description' => t('Remove messages via services') , + ) , + ); +} + +/** + * Implementation of hook_service(). + */ + +function pm_service_services_resources() +{ + return array( + 'privatemsg' => array( + 'index' => array( + 'file' => array( + 'type' => 'inc', + 'module' => 'pm_service', + 'name' => 'pm_service' + ) , + 'callback' => 'pm_service_get', + 'access callback' => 'pm_service_access', + 'access callback file' => array( + 'type' => 'inc', + 'module' => 'pm_service', + 'name' => 'pm_service' + ) , + 'args' => array() , + ) , + 'retrieve' => array( + 'callback' => 'pm_service_get_thread', + 'access arguments' => array( + 'get private messages from remote' + ) , + 'file' => array( + 'type' => 'inc', + 'module' => 'pm_service', + 'name' => 'pm_service' + ) , + 'args' => array( + array( + 'name' => 'thread_id', + 'type' => 'int', + 'description' => t('The ID of the thread that should be retrieved.') , + 'source' => array( + 'path' => 0 + ) , + ) , + ) , + ) , + 'create' => array( + 'callback' => 'pm_service_send', + 'access arguments' => array( + 'send private messages from remote' + ) , + 'file' => array( + 'type' => 'inc', + 'module' => 'pm_service', + 'name' => 'pm_service' + ) , + 'args' => array( + array( + 'name' => 'message', + 'type' => 'array', + 'description' => t('A privatemsg array') , + 'source' => 'data', + ) , + ) , + 'help' => t('Returns TRUE if the message sending was a success.') + ) , + 'delete' => array( + 'callback' => 'pm_service_change_delete', + 'access arguments' => array( + 'remove private messages from remote' + ) , + 'file' => array( + 'type' => 'inc', + 'module' => 'pm_service', + 'name' => 'pm_service' + ) , + 'args' => array( + array( + 'name' => 'thread_id', + 'type' => 'int', + 'description' => t('The ID of the thread that should be remove.') , + 'source' => array('path' => 0), + ) , + ) , + 'help' => t('Returns TRUE if the message deletion was a success.') + ) , + ) , + ); +}