<commit_before># -*- encoding: utf-8 -*-
require 'asakusa_satellite/hook'
class AsakusaSatellite::Hook::ASIPhoneNotifier < AsakusaSatellite::Hook::Listener

  def strip(str, n)
    str.to_json.scan(/((\\u[0-9a-f]{4})|(.))/).map{|m| m[0]}.reduce(""){|x,y|
      z = x + y
      z.size <= n ? z : x
    }
  end

  def after_create_message(context)
    message = context[:message]
    room = context[:room]

    text = strip "#{message.user.name} / #{message.body}", 256

    members = room.members - [ message.user ]

    devices = members.map {|user|
      user.devices
    }.flatten

    iphones = devices.select do |device|
      device.device_type.nil? or device.device_type == 'iphone'
    end

    iphones.to_a.map{|iphone|
      APNS::Notification.new(iphone.name,
        :alert => text,
        :sound => 'default',
        :other => {
          :id => room.id
        })
    }.tap{|xs|
      APNS.send_notifications xs
    }<commit_msg>strip return unescaped unicode string refs #1330<commit_after># -*- encoding: utf-8 -*-
require 'asakusa_satellite/hook'
class AsakusaSatellite::Hook::ASIPhoneNotifier < AsakusaSatellite::Hook::Listener

  def strip(str, n)
    s = str.to_json.scan(/((\\u[0-9a-f]{4})|(.))/).map{|m| m[0]}.reduce(""){|x,y|
      z = x + y
      z.size <= n ? z : x
    }
    (JSON.parse "[#{s}]")[0]
  end

  def after_create_message(context)
    message = context[:message]
    room = context[:room]

    text = strip "#{message.user.name} / #{message.body}", 256

    members = room.members - [ message.user ]

    devices = members.map {|user|
      user.devices
    }.flatten

    iphones = devices.select do |device|
      device.device_type.nil? or device.device_type == 'iphone'
    end

    iphones.to_a.map{|iphone|
      APNS::Notification.new(iphone.name,
        :alert => text,
        :sound => 'default',
        :other => {
          :id => room.id
        })
    }.tap{|xs|
      APNS.send_notifications xs
    }
{'fraction_non_alphanumeric': 0.17177914110429449, 'fraction_numerical': 0.010736196319018405, 'mean_word_length': 2.532490974729242, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 11, 'https://': 0, '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': '5102542', 'n_tokens_mistral': 744, 'n_tokens_neox': 681, 'n_words': 131}