UNPKG

@atomist/automation-client

Version:
239 lines • 10.2 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); const SlackMessages_1 = require("@atomist/slack-messages/SlackMessages"); const _ = require("lodash"); const MessageClient_1 = require("../../../spi/message/MessageClient"); const MessageClientSupport_1 = require("../../../spi/message/MessageClientSupport"); const logger_1 = require("../../util/logger"); const string_1 = require("../../util/string"); const RequestProcessor_1 = require("../RequestProcessor"); class AbstractWebSocketMessageClient extends MessageClientSupport_1.MessageClientSupport { constructor(ws, request, correlationId, team, source) { super(); this.ws = ws; this.request = request; this.correlationId = correlationId; this.team = team; this.source = source; } doSend(msg, destinations, options = {}) { return __awaiter(this, void 0, void 0, function* () { const ts = this.ts(options); if (!Array.isArray(destinations)) { destinations = [destinations]; } let destinationIdentifier; const responseDestinations = []; destinations.forEach(d => { if (d.userAgent === MessageClient_1.SlackDestination.SLACK_USER_AGENT) { destinationIdentifier = "slack"; const sd = d; string_1.toStringArray(sd.channels).forEach(c => { responseDestinations.push({ user_agent: MessageClient_1.SlackDestination.SLACK_USER_AGENT, slack: { team: { id: sd.team, }, channel: { name: c, }, }, }); }); string_1.toStringArray(sd.users).forEach(c => { responseDestinations.push({ user_agent: "slack", slack: { team: { id: sd.team, }, user: { name: c, }, }, }); }); } else if (d.userAgent === MessageClient_1.CustomEventDestination.INGESTER_USER_AGENT) { destinationIdentifier = "ingester"; responseDestinations.push({ user_agent: MessageClient_1.CustomEventDestination.INGESTER_USER_AGENT, ingester: { root_type: d.rootType, }, }); } }); if (responseDestinations.length === 0 && this.source) { // TODO CD this is probably not always going to be valid destinationIdentifier = "slack"; const responseDestination = _.cloneDeep(this.source); if (responseDestination.slack) { delete responseDestination.slack.user; } responseDestinations.push(this.source); } const response = { api_version: "1", correlation_id: this.correlationId, team: this.team, source: this.source ? this.source : undefined, command: RequestProcessor_1.isCommandIncoming(this.request) ? this.request.command : undefined, event: RequestProcessor_1.isEventIncoming(this.request) ? this.request.extensions.operationName : undefined, destinations: responseDestinations, id: options.id ? options.id : undefined, timestamp: ts, ttl: ts && options.ttl ? options.ttl : undefined, post_mode: options.post === "update_only" ? "update_only" : (options.post === "always" ? "always" : "ttl"), }; if (destinationIdentifier === "slack") { if (MessageClient_1.isSlackMessage(msg)) { const msgClone = _.cloneDeep(msg); const actions = mapActions(msgClone); response.content_type = MessageClient_1.MessageMimeTypes.SLACK_JSON; response.body = SlackMessages_1.render(msgClone, false); response.actions = actions; } else if (MessageClient_1.isFileMessage(msg)) { response.content_type = MessageClient_1.MessageMimeTypes.SLACK_FILE_JSON; response.body = JSON.stringify({ content: msg.content, filename: msg.fileName, filetype: msg.fileType, title: msg.title, initial_comment: msg.comment, }); } else if (typeof msg === "string") { response.content_type = MessageClient_1.MessageMimeTypes.PLAIN_TEXT; response.body = msg; } } else if (destinationIdentifier === "ingester") { response.content_type = MessageClient_1.MessageMimeTypes.APPLICATION_JSON; response.body = JSON.stringify(msg); response.id = (options.id ? options.id : string_1.guid()); } sendMessage(response, this.ws); return Promise.resolve(response); }); } ts(options) { if (options.id) { if (options.ts) { return options.ts; } else { return Date.now(); } } else { return undefined; } } } exports.AbstractWebSocketMessageClient = AbstractWebSocketMessageClient; class WebSocketCommandMessageClient extends AbstractWebSocketMessageClient { constructor(request, ws) { super(ws, request, request.correlation_id, request.team, request.source); } doSend(msg, destinations, options = {}) { const _super = name => super[name]; return __awaiter(this, void 0, void 0, function* () { return _super("doSend").call(this, msg, destinations, options); }); } } exports.WebSocketCommandMessageClient = WebSocketCommandMessageClient; class WebSocketEventMessageClient extends AbstractWebSocketMessageClient { constructor(request, ws) { super(ws, request, request.extensions.correlation_id, { id: request.extensions.team_id, name: request.extensions.team_name }, null); } doSend(msg, destinations, options = {}) { const _super = name => super[name]; return __awaiter(this, void 0, void 0, function* () { if (!Array.isArray(destinations)) { destinations = [destinations]; } if (destinations.length === 0) { throw new Error("Response messages are not supported for event handlers"); } else { return _super("doSend").call(this, msg, destinations, options); } }); } } exports.WebSocketEventMessageClient = WebSocketEventMessageClient; function mapActions(msg) { const actions = []; let counter = 0; if (msg.attachments) { msg.attachments.filter(attachment => attachment.actions).forEach(attachment => { attachment.actions.forEach(a => { if (a.command) { const cra = a; const id = counter++; cra.command.id = `${cra.command.id}-${id}`; a.name = `${a.name}-${id}`; const action = { id: cra.command.id, parameter_name: cra.command.parameterName, command: cra.command.name, parameters: mapParameters(cra.command.parameters), }; actions.push(action); // Lastly we need to delete our extension from the slack action cra.command = undefined; } }); }); return actions; } } exports.mapActions = mapActions; function mapParameters(data) { const parameters = []; for (const key in data) { if (data.hasOwnProperty(key)) { const value = data[key]; if (value) { parameters.push({ name: key, value: value.toString(), }); } else { // logger.debug(`Parameter value for '${key}' is null`); } } } return parameters; } function sendMessage(message, ws, log = true) { const payload = JSON.stringify(message); if (log) { logger_1.logger.debug(`Sending message '${payload}'`); } return ws.send(payload); } exports.sendMessage = sendMessage; function clean(addresses) { let na = string_1.toStringArray(addresses); if (na) { // Filter out any null addresses na = na.filter(nad => nad !== null && nad.length > 0); } return na; } exports.clean = clean; //# sourceMappingURL=WebSocketMessageClient.js.map