<commit_before>package im.actor.server.webactions

import akka.actor.ActorSystem
import im.actor.api.rpc.collections.{ ApiStringValue, ApiMapValueItem, ApiMapValue }
import im.actor.config.ActorConfig

import scala.concurrent.Future
import scala.collection.JavaConversions._
import scala.util.Try

object Webaction {

  /**
   * Retrieves list of all available webactions
   * @param path path ofconfig, containing list of registered webactions
   * @return mapping from webaction name to its FQN
   */
  def list(path: String): Map[String, String] =
    ActorConfig.load().getConfig(path).root.unwrapped.toMap map { case (k, v) ⇒ k → v.toString }

  def list: Map[String, String] = list("enabled-modules.webactions")

  /**
   * Instantiates webaction by fully qualified class name
   * @param actionFQN fully qualified class name
   * @param system actor system, required to execute actions inside webaction
   * @return `Success(webaction)` when webaction instantiation succeeds,
   *        and `Failure(exception)` when instantiation failed by various reasons(no such class/wrong constuctor)
   */
  def webactionOf(actionFQN: String, system: ActorSystem): Try[Webaction] = Try {
    val constructor = Class.forName(actionFQN).getConstructors()(0)
    constructor.newInstance(system).asInstanceOf[Webaction]
  }

  def failure(cause: String): ApiMapValue =
    ApiMapValue(Vector(ApiMapValueItem("error", ApiStringValue(cause))))

  def success(message: String): ApiMapValue =
    ApiMapValue(Vector(ApiMapValueItem("success", ApiStringValue(message))))
}

abstract class Webaction(system: ActorSystem) {
  def uri(params: ApiMapValue): String
  def regex: String
  def complete(userId: Int, url: String): Future[ApiMapValue]
}<commit_msg>fix(server:webactions): add optional cause in failure message<commit_after>package im.actor.server.webactions

import akka.actor.ActorSystem
import im.actor.api.rpc.collections.{ ApiStringValue, ApiMapValueItem, ApiMapValue }
import im.actor.config.ActorConfig

import scala.concurrent.Future
import scala.collection.JavaConversions._
import scala.util.Try

object Webaction {

  /**
   * Retrieves list of all available webactions
   * @param path path ofconfig, containing list of registered webactions
   * @return mapping from webaction name to its FQN
   */
  def list(path: String): Map[String, String] =
    ActorConfig.load().getConfig(path).root.unwrapped.toMap map { case (k, v) ⇒ k → v.toString }

  def list: Map[String, String] = list("enabled-modules.webactions")

  /**
   * Instantiates webaction by fully qualified class name
   * @param actionFQN fully qualified class name
   * @param system actor system, required to execute actions inside webaction
   * @return `Success(webaction)` when webaction instantiation succeeds,
   *        and `Failure(exception)` when instantiation failed by various reasons(no such class/wrong constuctor)
   */
  def webactionOf(actionFQN: String, system: ActorSystem): Try[Webaction] = Try {
    val constructor = Class.forName(actionFQN).getConstructors()(0)
    constructor.newInstance(system).asInstanceOf[Webaction]
  }

  def failure(error: String, optCause: Option[String]): ApiMapValue = {
    val m = ApiMapValueItem("error", ApiStringValue(error))
    val items = optCause map { cause ⇒
      Vector(m, ApiMapValueItem("cause", ApiStringValue(cause)))
    } getOrElse Vector(m)
    ApiMapValue(items)
  }

  def success(message: String): ApiMapValue =
    ApiMapValue(Vector(ApiMapValueItem("success", ApiStringValue(message))))
}

abstract class Webaction(system: ActorSystem) {
  def uri(params: ApiMapValue): String
  def regex: String
  def complete(userId: Int, url: String): Future[ApiMapValue]
}
{'fraction_non_alphanumeric': 0.10275080906148867, 'fraction_numerical': 0.0005393743257820927, 'mean_word_length': 4.8593996840442335, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '5566685', 'n_tokens_mistral': 1117, 'n_tokens_neox': 1100, 'n_words': 304}