<commit_before>/**
 * Created by ksinger on 19.02.2016.
 */

import won from "../won-es6.js";

import { actionTypes, actionCreators } from "./actions.js";
import Immutable from "immutable";

import {
  buildConnectMessage,
  buildCloseAtomMessage,
  buildOpenAtomMessage,
  buildDeleteAtomMessage,
} from "../won-message-utils.js";
import * as connectionUtils from "../redux/utils/connection-utils.js";
import * as atomUtils from "../redux/utils/atom-utils.js";
import * as stateStore from "../redux/state-store.js";
import * as ownerApi from "../api/owner-api.js";
import { get, getIn } from "../utils.js";

export function fetchUnloadedAtom(atomUri) {
  return (dispatch, getState) =>
    stateStore.fetchAtomAndDispatch(atomUri, dispatch, getState);
}

export function atomsConnectSockets(
  senderSocketUri,
  targetSocketUri,
  connectMessage
) {
  return dispatch => {
    if (!senderSocketUri) {
      throw new Error("SenderSocketUri not present");
    }

    if (!targetSocketUri) {
      throw new Error("TargetSocketUri not present");
    }

    const cnctMsg = buildConnectMessage({
      connectMessage: connectMessage,
      socketUri: senderSocketUri,
      targetSocketUri: targetSocketUri,
    });

    return won.wonMessageFromJsonLd(cnctMsg.message).then(optimisticEvent =>
      ownerApi.sendMessage(cnctMsg.message).then(jsonResp => {
        dispatch({
          type: actionTypes.atoms.connectSockets,
          payload: {
            eventUri: jsonResp.messageUri,
            message: jsonResp.message,
            optimisticEvent: optimisticEvent,
            senderSocketUri: senderSocketUri,
            targetSocketUri: targetSocketUri,
          },
        });
      })
    );
  };
}

export function atomsConnectSocketTypes(
  senderAtomUri,
  targetAtomUri,
  senderSocketType,
  targetSocketType,
  connectMessage
) {
  return (dispatch, getState) => {
    const state = getState();
    const senderAtom = getIn(state, ["atoms", senderAtomUri]);
    const targetAtom = getIn(state, ["atoms", targetAtomUri]);

    const senderSocketUri = atomUtils.getSocketUri(
      senderAtom,
      senderSocketType
    );
    const targetSocketUri = atomUtils.getSocketUri(
      targetAtom,
      targetSocketType
    );

    if (!senderSocketUri) {
      throw new Error(
        `Atom ${get(senderAtom, "uri")} does not have a ${senderSocketType}`
      );
    }

    if (!targetSocketUri) {
      throw new Error(
        `Atom ${get(targetAtom, "uri")} does not have a ${targetSocketType}`
      );
    }

    return atomsConnectSockets(
      senderSocketUri,
      targetSocketUri,
      connectMessage
    );
  };
}

export function atomsClose(atomUri) {
  return (dispatch, getState) => {
    buildCloseAtomMessage(atomUri)
      .then(data => ownerApi.sendMessage(data.message))
      .then(jsonResp => {
        dispatch(
          actionCreators.messages__send({
            eventUri: jsonResp.messageUri,
            message: jsonResp.message,
          })
        );<commit_msg>Fixes connect Method<commit_after>/**
 * Created by ksinger on 19.02.2016.
 */

import won from "../won-es6.js";

import { actionTypes, actionCreators } from "./actions.js";
import Immutable from "immutable";

import {
  buildConnectMessage,
  buildCloseAtomMessage,
  buildOpenAtomMessage,
  buildDeleteAtomMessage,
} from "../won-message-utils.js";
import * as connectionUtils from "../redux/utils/connection-utils.js";
import * as atomUtils from "../redux/utils/atom-utils.js";
import * as stateStore from "../redux/state-store.js";
import * as ownerApi from "../api/owner-api.js";
import { get, getIn } from "../utils.js";

export function fetchUnloadedAtom(atomUri) {
  return (dispatch, getState) =>
    stateStore.fetchAtomAndDispatch(atomUri, dispatch, getState);
}

export function atomsConnectSockets(
  senderSocketUri,
  targetSocketUri,
  connectMessage
) {
  return dispatch => {
    if (!senderSocketUri) {
      throw new Error("SenderSocketUri not present");
    }

    if (!targetSocketUri) {
      throw new Error("TargetSocketUri not present");
    }

    const cnctMsg = buildConnectMessage({
      connectMessage: connectMessage,
      socketUri: senderSocketUri,
      targetSocketUri: targetSocketUri,
    });

    return won.wonMessageFromJsonLd(cnctMsg.message).then(optimisticEvent =>
      ownerApi.sendMessage(cnctMsg.message).then(jsonResp => {
        dispatch({
          type: actionTypes.atoms.connectSockets,
          payload: {
            eventUri: jsonResp.messageUri,
            message: jsonResp.message,
            optimisticEvent: optimisticEvent,
            senderSocketUri: senderSocketUri,
            targetSocketUri: targetSocketUri,
          },
        });
      })
    );
  };
}

export function atomsConnectSocketTypes(
  senderAtomUri,
  targetAtomUri,
  senderSocketType,
  targetSocketType,
  connectMessage
) {
  return (dispatch, getState) => {
    const state = getState();
    const senderAtom = getIn(state, ["atoms", senderAtomUri]);
    const targetAtom = getIn(state, ["atoms", targetAtomUri]);

    const senderSocketUri = atomUtils.getSocketUri(
      senderAtom,
      senderSocketType
    );
    const targetSocketUri = atomUtils.getSocketUri(
      targetAtom,
      targetSocketType
    );

    if (!senderSocketUri) {
      throw new Error(
        `Atom ${get(senderAtom, "uri")} does not have a ${senderSocketType}`
      );
    }

    if (!targetSocketUri) {
      throw new Error(
        `Atom ${get(targetAtom, "uri")} does not have a ${targetSocketType}`
      );
    }

    const cnctMsg = buildConnectMessage({
      connectMessage: connectMessage,
      socketUri: senderSocketUri,
      targetSocketUri: targetSocketUri,
    });

    return won.wonMessageFromJsonLd(cnctMsg.message).then(optimisticEvent =>
      ownerApi.sendMessage(cnctMsg.message).then(jsonResp => {
        dispatch({
          type: actionTypes.atoms.connectSockets,
          payload: {
            eventUri: jsonResp.messageUri,
            message: jsonResp.message,
            optimisticEvent: optimisticEvent,
            senderSocketUri: senderSocketUri,
            targetSocketUri: targetSocketUri,
          },
        });
      })
    );
  };
}

export function atomsClose(atomUri) {
  return (dispatch, getState) => {
    buildCloseAtomMessage(atomUri)
      .then(data => ownerApi.sendMessage(data.message))
      .then(jsonResp => {
        dispatch(
          actionCreators.messages__send({
            eventUri: jsonResp.messageUri,
            message: jsonResp.message,
          })
        );
{'fraction_non_alphanumeric': 0.11168515423187965, 'fraction_numerical': 0.002735146634250114, 'mean_word_length': 2.867215041128085, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 21, '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': '3438199', 'n_tokens_mistral': 1950, 'n_tokens_neox': 1910, 'n_words': 401}