<commit_before>/**
 *   Wechaty - https://github.com/chatie/wechaty
 *
 *   @copyright 2016-2017 <NAME> <<EMAIL>>
 *
 *   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.
 *   @ignore
 */
import * as fs    from 'fs'
import * as path  from 'path'
import {
  Readable,
}                 from 'stream'

import * as mime  from 'mime'

import {
  config,
  Raven,
  Sayable,
  log,
}                 from './config'

import Contact    from './contact'
import Room       from './room'
import Misc       from './misc'
import PuppetWeb  from './puppet-web/puppet-web'
import Bridge     from './puppet-web/bridge'

import {
  AppMsgType,
  MsgObj,
  MsgRawObj,
  MsgType,
}                 from './puppet-web/schema'

export type TypeName =  'attachment'
                      | 'audio'
                      | 'image'
                      | 'video'

/**
 * All wechat messages will be encapsulated as a Message.
 *
 * `Message` is `Sayable`,
 * [Example/Ding-Dong-Bot]{@link https://github.com/Chatie/wechaty/blob/master/example/ding-dong-bot.ts}
 */
export class Message implements Sayable {
  /**
   * @private
   */
  public static counter = 0

  /**
   * @private
   */
  public _counter: number

  /**
   * @private
   */
  public readonly id: string

  /**
   * @private
   */
  public obj = <MsgObj>{}

  /**
   * @private
   */
  public filename(): string {
    throw Error('not a media message')
  }

  /**
   * return the MIME Type of this Message
   * text message will always return `text/plain`
   */
  public mimeType(): string {
    return 'text/plain'
  }

  /**
   * @private
   */
  constructor(public rawObj?: MsgRawObj) {
    this._counter = Message.counter++
    log.silly('Message', 'constructor() SN:%d', this._counter)

    if (typeof rawObj === 'string') {
      this.rawObj = JSON.parse(rawObj)
    }

    this.rawObj = rawObj = rawObj || <MsgRawObj>{}
    this.obj = this.parse(rawObj)
    this.id = this.obj.id
  }

  /**
   * @private
   */
  // Transform rawObj to local obj
  private parse(rawObj): MsgObj {
    const obj: MsgObj = {<commit_msg>add mimeType() method to Message/MediaMessage<commit_after>/**
 *   Wechaty - https://github.com/chatie/wechaty
 *
 *   @copyright 2016-2017 <NAME> <<EMAIL>>
 *
 *   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.
 *   @ignore
 */
import * as fs    from 'fs'
import * as path  from 'path'
import {
  Readable,
}                 from 'stream'

import * as mime  from 'mime'

import {
  config,
  Raven,
  Sayable,
  log,
}                 from './config'

import Contact    from './contact'
import Room       from './room'
import Misc       from './misc'
import PuppetWeb  from './puppet-web/puppet-web'
import Bridge     from './puppet-web/bridge'

import {
  AppMsgType,
  MsgObj,
  MsgRawObj,
  MsgType,
}                 from './puppet-web/schema'

export type TypeName =  'attachment'
                      | 'audio'
                      | 'image'
                      | 'video'

/**
 * All wechat messages will be encapsulated as a Message.
 *
 * `Message` is `Sayable`,
 * [Example/Ding-Dong-Bot]{@link https://github.com/Chatie/wechaty/blob/master/example/ding-dong-bot.ts}
 */
export class Message implements Sayable {
  /**
   * @private
   */
  public static counter = 0

  /**
   * @private
   */
  public _counter: number

  /**
   * @private
   */
  public readonly id: string

  /**
   * @private
   */
  public obj = <MsgObj>{}

  /**
   * @private
   */
  public filename(): string {
    throw Error('not a media message')
  }

  /**
   * @private
   */
  constructor(public rawObj?: MsgRawObj) {
    this._counter = Message.counter++
    log.silly('Message', 'constructor() SN:%d', this._counter)

    if (typeof rawObj === 'string') {
      this.rawObj = JSON.parse(rawObj)
    }

    this.rawObj = rawObj = rawObj || <MsgRawObj>{}
    this.obj = this.parse(rawObj)
    this.id = this.obj.id
  }

  /**
   * @private
   */
  // Transform rawObj to local obj
  private parse(rawObj): MsgObj {
    const obj: MsgObj = {
{'fraction_non_alphanumeric': 0.12547076313181368, 'fraction_numerical': 0.005153617443012884, 'mean_word_length': 2.3153745072273324, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 4, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4641236', 'n_tokens_mistral': 1630, 'n_tokens_neox': 1574, 'n_words': 478}