const util = require("util"); module.exports = function (client) { /*section: comment *comment: create a new comment *params: * message: the message of the comment * properties: | * optional, an object containing any of the following * * handle: the handle to associate the comment with (e.g. "user@domain.com") * * related_event_id: the event to associate the comment with * callback: function(err, res) *example: | * ```javascript * var dogapi = require("dogapi"); * var options = { * api_key: "api_key", * app_key: "app_key" * }; * dogapi.initialize(options); * dogapi.comment.create("a comment message", function(err, res){ * console.dir(res); * }); * ``` */ function create(message, properties, callback) { if (arguments.length < 3 && typeof arguments[1] === "function") { callback = properties; properties = {}; } const params = { body: { message: message } }; if (typeof properties === "object") { if (properties.handle) { params.body.handle = properties.handle; } if (properties.related_event_id) { params.body.related_event_id = properties.related_event_id; } } client.request("POST", "/comments", params, callback); } /*section: comment *comment: update an existing comment *params: * commentId: the id of the comment to update * message: the message of the comment * handle: optional, the handle to associate the comment with (e.g. "user@domain.com") * callback: function(err, res) *example: | * ```javascript * var dogapi = require("dogapi"); * var options = { * api_key: "api_key", * app_key: "app_key" * }; * dogapi.initialize(options); * dogapi.comment.update(1234, "new message", function(err, res){ * console.dir(res); * }); * ``` */ function update(commentId, message, handle, callback) { if (arguments.length < 4 && typeof arguments[2] === "function") { callback = handle; handle = undefined; } const params = { body: { message: message } }; if (handle) { params.body.handle = properties.handle; } client.request("PUT", util.format("/comments/%s", commentId), params, callback); } /*section: comment *comment: remove a comment *params: * commentId: the id of the comment to remove * callback: function(err, res) *example: | * ```javascript * var dogapi = require("dogapi"); * var options = { * api_key: "api_key", * app_key: "app_key" * }; * dogapi.initialize(options); * dogapi.comment.remove(1234, function(err, res){ * console.dir(res); * }); * ``` */ function remove(commentId, callback) { client.request("DELETE", util.format("/comments/%s", commentId), callback); } return { create: create, update: update, remove: remove, getUsage: function () { return [ " dogapi comment create [--handle ] [--event ]", " dogapi comment update [--handle ]", " dogapi comment remove " ]; }, getHelp: function () { return [ "Comment:", " Subcommands:", " create add a new comment", " update update an existing comment", " remove delete a comment", "", " Options:", " --handle the handle to associate with the comment (e.g. \"user@domain.com\")", " --event related event id to associate the comment with" ]; }, handleCli: function (subcommand, args, callback) { if (subcommand === "create") { const message = args._[4]; const properties = {}; if (args["handle"]) { properties.handle = args["handle"]; } if (args["event"]) { properties.related_event_id = parseInt(args["event"]); } create(message, properties, callback); } else if (subcommand === "update") { const commentId = args._[4]; const message = args._[5]; update(commentId, message, args["handle"], callback); } else if (subcommand === "remove") { const commentId = args._[4]; remove(commentId, callback); } else { callback("unknown subcommand or arguments try `dogapi comment --help` for help", false); } } }; };