<commit_before>/**
 *
 * Wechaty: Wechat for Bot. Connecting ChatBots
 *
 * Class PuppetWeb Firer
 *
 * Process the Message to find which event to FIRE
 *
 * Licenst: ISC
 * https://github.com/wechaty/wechaty
 *
 */

/**************************************
 *
 * Firer for Class PuppetWeb
 *
 * here `this` is a PuppetWeb Instance
 *
 ***************************************/
const util          = require('util')
const fs            = require('fs')
const co            = require('co')
const retryPromise  = require('retry-promise').default

const log = require('../brolog-env')
const Contact = require('../contact')
const Message = require('../message')
const FriendRequest = require('./friend-request')

const PuppetWebFirer = {
  fireFriendConfirm
  , fireFriendRequest

  , fireRoomJoin
  , fireRoomLeave

  // for testing
  , checkFriendConfirm
  , checkRoomJoin
  , checkRoomLeave
}

const regexConfig = {
  friendConfirm:  /^You have added (.+) as your WeChat contact. Start chatting!$/

  , roomJoin:     /^"?(.+?)"? invited "(.+)" to the group chat$/
  , roomLeave:    /^You removed "(.+)" from the group chat$/
}

function fireFriendRequest(m) {
  const info = m.rawObj.RecommendInfo
  log.verbose('PuppetWebFirer', 'fireFriendRequest(%s)', info)

  const request = new FriendRequest()
  request.receive(info)
  this.emit('friend', request.contact, request)
}

/**
 * try to find FriendRequest Confirmation Message
 */
function checkFriendConfirm(content) {
  const re = regexConfig.friendConfirm
  if (re.test(content)) {
    return true
  } else {
    return false
  }
}

function fireFriendConfirm(m) {
  const content = m.content()
  log.silly('PuppetWebFirer', 'fireFriendConfirm(%s)', content)

  if (!checkFriendConfirm(content)) {
    return
  }
  const request = new FriendRequest()
  const contact = Contact.load(m.get('from'))
  request.confirm(contact)

  this.emit('friend', contact)
}


/**
 * try to find 'join' event for Room
 *
1.
  You've invited "李卓桓" to the group chat
  You've invited "李卓桓.PreAngel、<NAME>" to the group chat
2.
   "李卓桓.PreAngel" invited "<NAME>" to the group chat
*/
function checkRoomJoin(content) {
  const re = regexConfig.roomJoin

  const found = content.match(re)
  if (!found) {
    return false
  }
  const [_, inviter, invitee] = found
  return [invitee, inviter] // put invitee at first place
}

function fireRoomJoin(m) {
  const room    = m.room()
  const content = m.content()

  let result = checkRoomJoin(content)
  if (!result) {
    return
  }
  const [invitee, inviter] = result

  let inviterContact, inviteeContact

  co.call(this, function* () {
    if (inviter === "You've") {
      inviterContact = Contact.load(this.userId)
    }

    const max = 15
    const backoff = 100
    // max = (2*totalTime/backoff) ^ (1/2)
    // timeout = 11,250 for {max: 15, backoff: 100}

    yield retryPromise({ max: max, backoff: backoff }, attempt => {
      log.silly('PuppetWebFirer', 'fireRoomJoin() retryPromise() attempt %d', attempt)<commit_msg>increase reload timeout, and log total timeout for convenience<commit_after>/**
 *
 * Wechaty: Wechat for Bot. Connecting ChatBots
 *
 * Class PuppetWeb Firer
 *
 * Process the Message to find which event to FIRE
 *
 * Licenst: ISC
 * https://github.com/wechaty/wechaty
 *
 */

/**************************************
 *
 * Firer for Class PuppetWeb
 *
 * here `this` is a PuppetWeb Instance
 *
 ***************************************/
const util          = require('util')
const fs            = require('fs')
const co            = require('co')
const retryPromise  = require('retry-promise').default

const log = require('../brolog-env')
const Contact = require('../contact')
const Message = require('../message')
const FriendRequest = require('./friend-request')

const PuppetWebFirer = {
  fireFriendConfirm
  , fireFriendRequest

  , fireRoomJoin
  , fireRoomLeave

  // for testing
  , checkFriendConfirm
  , checkRoomJoin
  , checkRoomLeave
}

const regexConfig = {
  friendConfirm:  /^You have added (.+) as your WeChat contact. Start chatting!$/

  , roomJoin:     /^"?(.+?)"? invited "(.+)" to the group chat$/
  , roomLeave:    /^You removed "(.+)" from the group chat$/
}

function fireFriendRequest(m) {
  const info = m.rawObj.RecommendInfo
  log.verbose('PuppetWebFirer', 'fireFriendRequest(%s)', info)

  const request = new FriendRequest()
  request.receive(info)
  this.emit('friend', request.contact, request)
}

/**
 * try to find FriendRequest Confirmation Message
 */
function checkFriendConfirm(content) {
  const re = regexConfig.friendConfirm
  if (re.test(content)) {
    return true
  } else {
    return false
  }
}

function fireFriendConfirm(m) {
  const content = m.content()
  log.silly('PuppetWebFirer', 'fireFriendConfirm(%s)', content)

  if (!checkFriendConfirm(content)) {
    return
  }
  const request = new FriendRequest()
  const contact = Contact.load(m.get('from'))
  request.confirm(contact)

  this.emit('friend', contact)
}


/**
 * try to find 'join' event for Room
 *
1.
  You've invited "李卓桓" to the group chat
  You've invited "李卓桓.PreAngel、<NAME>" to the group chat
2.
   "李卓桓.PreAngel" invited "<NAME>" to the group chat
*/
function checkRoomJoin(content) {
  const re = regexConfig.roomJoin

  const found = content.match(re)
  if (!found) {
    return false
  }
  const [_, inviter, invitee] = found
  return [invitee, inviter] // put invitee at first place
}

function fireRoomJoin(m) {
  const room    = m.room()
  const content = m.content()

  let result = checkRoomJoin(content)
  if (!result) {
    return
  }
  const [invitee, inviter] = result

  let inviterContact, inviteeContact

  co.call(this, function* () {
    if (inviter === "You've") {
      inviterContact = Contact.load(this.userId)
    }

    const max = 20
    const backoff = 300
    const timeout = max * (backoff * max) / 2
    // 20 / 300 => 63,000
    // max = (2*totalTime/backoff) ^ (1/2)
    // timeout = 11,250 for {max: 15, backoff: 100}

    yield retryPromise({ max: max, backoff: backoff }, attempt => {
      log.silly('PuppetWebFirer', 'fireRoomJoin() retryPromise() attempt %d with timeout %d', attempt, timeout)
{'fraction_non_alphanumeric': 0.15595585848750407, 'fraction_numerical': 0.008276533592989289, 'mean_word_length': 3.4498194945848377, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 10, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, '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': '3708106', 'n_tokens_mistral': 2082, 'n_tokens_neox': 1966, 'n_words': 536}