symphonyoss/hubot-symphony

View on GitHub
src/message.js

Summary

Maintainability
A
0 mins
Test Coverage
A
100%
/**
 *    Copyright 2017 Jon Freedman
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

// @flow

import {TextMessage} from 'hubot';
import type {SymphonyMessageV2Type} from './symphony';

/**
 * Represents a V2Message received from Symphony for use within Hubot
 */
export class V2Message extends TextMessage {
  symphonyMessage: SymphonyMessageV2Type;
  room: string;

  /**
   * @param {Object} user Hubot user
   * @param {SymphonyMessageV2Type} symphonyMessage Message from Symphony
   * @constructor
   */
  constructor(user: Object, symphonyMessage: SymphonyMessageV2Type) {
    super(user, V2Message._getMessageText(symphonyMessage), symphonyMessage.id);
    this.symphonyMessage = symphonyMessage;
    this.room = symphonyMessage.streamId;
  }

  /**
   * @param {SymphonyMessageV2Type} symphonyMessage Message from Symphony
   * @return {string} message text contained within messageML tag
   * @private
   */
  static _getMessageText(symphonyMessage: SymphonyMessageV2Type): string {
    const match = /<messageML>(.*)<\/messageML>/i.exec(symphonyMessage.message);
    if (match === undefined || match === null) {
      return symphonyMessage.message;
    }
    return match[1];
  }
}