Index: services/comment_service.inc
===================================================================
RCS file: services/comment_service.inc
diff -N services/comment_service.inc
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ services/comment_service.inc	23 Dec 2008 19:53:45 -0000
@@ -0,0 +1,143 @@
+<?php
+// $Id$
+/**
+ * @author Services Dev Team
+ * @file
+ *  Link commenting functionality to services module.
+ */
+
+/**
+ * Adds a new comment to a node and returns the cid.
+ *
+ * If a cid is specified in $comment->cid then that comment is edited.
+ *
+ * @param $comment
+ *   An array matching the form values that would be submitted in the comment 
+ *   edit form.
+ * @return
+ *   Unique identifier for the comment (cid) or FALSE if there was a problem.
+ */
+function comment_service_save($comment) {
+  global $user;
+
+  // If a cid is present then the user is trying to edit an existing comment.
+  if (isset($comment['cid']) && ($comment['cid'] != 0)) {
+    $initial_comment = _comment_load($comment['cid']);
+    $admin = user_access("administer comments");
+
+    if ($initial_comment->uid == 0 && !$admin) {
+      return services_error(t("Anonymous comments can't be edited"));
+    }
+
+    if (($initial_comment->uid != $user->uid || comment_num_replies($comment->cid) != 0) && !$admin) {
+      return services_error(t("User does not have permission to edit this comment"));
+    }
+  }
+
+  $comment['uid'] = $user->uid;
+  $comment['name'] = $user->name;
+  $comment['timestamp'] = time();
+
+  return comment_save($comment);
+}
+
+/**
+ * Returns the comments of a specified node.
+ *
+ * @param $nid
+ *   Unique identifier for the node.
+ * @param $count
+ *   Number of comments to return.
+ * @param $start
+ *   Which comment to start with. if present, $start and $count are used together
+ *   to create a LIMIT clause for selecting comments. This could be used to do paging.
+ * @param $fields
+ *   An array indicating which fields should be returned. If not present, all 
+ *   fields will be returned.
+ * @return
+ *   An array of comment objects.
+ */
+function comment_service_node_comments_all($nid, $count = 0, $start = 0, $fields = array()) {
+  $limit = ((int)$count > 0 ? ' LIMIT '. (int)$start .', '. (int)$count .' ' : '');
+  if (!user_access('administer comments')) {
+    $result = db_query("SELECT c.*, u.name FROM {comments} c, {users} u WHERE u.uid = c.uid AND nid = %d AND c.status = %d ORDER BY thread DESC". $limit, $nid, COMMENT_PUBLISHED);
+  }
+  else {
+    $result = db_query("SELECT * FROM {comments} c, {users} u WHERE u.uid = c.uid AND nid = %d ORDER BY thread DESC". $limit, $nid);
+  }
+
+  $comments = array();
+
+  while ($comment = db_fetch_object($result)) {
+    $temp = NULL;
+
+    // loop through and get only requested fields
+    if (count($fields) > 0) {
+      foreach ($fields as $field) {
+        $temp->{$field} = $comment->{$field};
+      }
+    }
+    else {
+      $temp = $comment;
+    }
+
+    // safety check
+    if (!user_access('administer comments')) {
+      unset($temp->hostname);
+      unset($temp->pass);
+      unset($temp->mail);
+    }
+
+    $comments[] = $temp;
+  }
+
+  return $comments;
+}
+
+/**
+ * Returns the number of comments on a given node id.
+ *
+ * @param $nid
+ *   Unique identifier for the specified node.
+ * @return
+ *   Number of comments that node has.
+ */
+function comment_service_node_comments_count_all($nid) {
+  return comment_num_all($nid);
+}
+
+/**
+ * Returns the number of new comments on a given node id since timestamp.
+ *
+ * @param $nid
+ *   Unique identifier for the specified node.
+ * @param $since
+ *   Timestamp to indicate what nodes are new. Defaults to time of last user acces to node.
+ * @return
+ *   Number of comments that node has.
+ */
+function comment_service_node_comments_count_new($nid, $since = 0) {
+  return comment_num_new($nid, $timestamp);
+}
+
+/**
+ * Check to see if the user has permission to access comments.
+ *
+ * @return 
+ *   Boolean.
+ */
+function comment_service_get_access() {
+  return user_access('access comments');
+}
+
+/**
+ * Check to see if the user has permission to save comments.
+ *
+ * @return 
+ *   Boolean.
+ */
+function comment_service_save_access() {
+  if (user_access('post comments') || user_access('post comments without approval')) {
+    return TRUE;
+  }
+}
\ No newline at end of file
Index: services/comment_service.info
===================================================================
RCS file: services/comment_service.info
diff -N services/comment_service.info
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ services/comment_service.info	23 Dec 2008 19:53:45 -0000
@@ -0,0 +1,7 @@
+; $Id$
+name = Comment Service
+description = Provides a comment service.
+package = Services - services
+dependencies[] = services
+dependencies[] = comment
+core = 6.x
Index: services/comment_service.module
===================================================================
RCS file: services/comment_service.module
diff -N services/comment_service.module
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ services/comment_service.module	23 Dec 2008 19:53:46 -0000
@@ -0,0 +1,109 @@
+<?php
+// $Id$
+/**
+ * @author Services Dev Team
+ * @file
+ *  Link commenting functionality to services module.
+ */
+
+/**
+ * Implementation of hook_help().
+ */
+function comment_service_help($path, $arg) {
+  switch ($path) {
+    case 'admin/help#services_comment':
+      return t('<p>Provides comment methods to services applications. Requires services.module.</p>');
+    case 'admin/modules#description':
+      return t('Provides comment methods to services applications. Requires services.module.');
+  }
+}
+
+/**
+ * Implementation of hook_service().
+ */
+function comment_service_service() {
+  return array(
+
+    // node.comments.save
+    array(
+      '#method'   => 'comment.save',
+      '#callback' => 'comment_service_save',
+      '#access callback'  => 'comment_service_save_access',
+      '#file'     => array('file' => 'inc', 'module' => 'comment_service'),
+      '#args'     => array(
+        array(
+          '#name'         => 'comment',
+          '#type'         => 'struct',
+          '#optional'     => FALSE,
+          '#description'  => t('A comment object.'))),
+      '#return' => 'int',
+      '#help' => t('This method adds a comment to a node and returns a comment id.  If the comment object contains a numeric "cid", then the comment will be updated. Required fields: nid, comment. Optional fields: cid (comment id), pid (parent comment), subject, mail, homepage')),
+
+    // node.comments.all
+    array(
+      '#method'   => 'comment.load',
+      '#callback' => 'comment_service_node_comments_all',
+      '#access callback'  => 'comment_service_get_access',
+      '#file'     => array('file' => 'inc', 'module' => 'comment_service'),
+      '#key'      => FALSE,
+      '#args'     => array(
+        array(
+          '#name'         => 'nid',
+          '#type'         => 'int',
+          '#optional'     => FALSE,
+          '#description'  => t('A node id.')),
+        array(
+          '#name'         => 'count',
+          '#type'         => 'int',
+          '#optional'     => TRUE,
+          '#description'  => t('Number of comments to load.')),
+        array(
+          '#name'         => 'start',
+          '#type'         => 'int',
+          '#optional'     => TRUE,
+          '#description'  => t('If count is set to non-zero value, You can pass also non-zero value for start. For example to get comments from 5 to 15, pass count=10 and start=5.')),
+        array(
+          '#name'         => 'fields',
+          '#type'         => 'array',
+          '#optional'     => TRUE,
+          '#description'  => t('A list of fields to return.'))),
+      '#return' => 'array',
+      '#help'   => t('This method returns all or part of the comments on a given node.')),
+
+    // node.comments.countAll
+    array(
+      '#method'   => 'comment.countAll',
+      '#callback' => 'comment_service_node_comments_count_all',
+      '#access callback'  => 'comment_service_get_access',
+      '#key'      => FALSE,      
+      '#file'     => array('file' => 'inc', 'module' => 'comment_service'),
+      '#args'     => array(
+        array('#name' => 'nid',
+          '#type'         => 'int',
+          '#optional'     => FALSE,
+          '#description'  => t('A node id.'))),
+      '#return' => 'int',
+      '#help'   => t('This method returns the number of comments on a given node.')),
+
+    // node.comments.countNew
+    array(
+      '#method'   => 'comment.countNew',
+      '#callback' => 'comment_service_node_comments_count_new',
+      '#access callback'  => 'comment_service_get_access',
+      '#key'      => FALSE,
+      '#file'     => array('file' => 'inc', 'module' => 'comment_service'),
+      '#args'      => array(
+        array(
+          '#name'         => 'nid',
+          '#type'         => 'int',
+          '#optional'     => FALSE,
+          '#description'  => t('A node id')),
+        array(
+          '#name'         => 'since',
+          '#type'         => 'int',
+          '#optional'     => TRUE,
+          '#description'  => t('Time to count from (defaults to time of last user acces to node).'))),
+      '#return' => 'int',
+      '#help'   => t('This method returns the number of new comments on a given node since a given timestamp.')),
+  );
+}
