<commit_before>import axios from 'axios'

import log from 'services/log'

export const state = {
  conversations: {}
}

export default {

  state,

  send (conversationId, body) {
    let data = new FormData()
    data.append('c', conversationId)
    data.append('b', body)
    return axios.post('/fs/xhrapp.php?app=msg&m=sendmsg', data)
      .then(({ data: { data: { msg } } }) => {
        Object.assign(msg, { cid: conversationId })
        this.receiveMessage(convertMessage(msg))
      })
  },

  loadConversation (id) {
    return axios.get(`/api/v1/conversation/${id}`).then(({ data: { conversation } }) => {
      state.conversations[id] = conversation
      return conversation
    })
  },

  receiveMessage (message) {
    let { conversationId } = message
    let conversation = state.conversations[conversationId]
    if (conversation) {
      conversation.messages.push(message)
      conversation.lastMessage = message
    }
    else {
      log.info('no conversation found for', conversationId)
    }
  }

}

export function fetchConversationList () {
  return axios.get('/api/v1/converations').then(({ data: { conversations } }) => {
    Object.assign(state, { conversations })
  })
}

export function ensureConversation (id) {
  let conversation = state.conversations[id]
  if (conversation) {
    return Promise.resolve(conversation)
  }
  else {
    // must fetch it
    return axios.get(`/api/v1/conversation/${id}`).then(({ data: { conversation } }) => {
      state.conversations[id] = conversation
      return conversation
    })
  }
}

/*
 * Convert message from existing foodsharing format to our format
 * See /api/doc#get--api-v1-conversation-{id}
 */
export function convertMessage ({
  time: sentAt,
  body,
  fs_name: firstName,
  fs_id: userId,
  id: messageId,
  cid: conversationId
}) {
  return {
    sentAt,
    body,
    sentBy: {
      id: parseInt(userId, 10),
      firstName
    },
    messageId: parseInt(messageId, 10),
    conversationId: parseInt(conversationId, 10)
  }
}<commit_msg>Decode html entities<commit_after>import axios from 'axios'

import log from 'services/log'

export const state = {
  conversations: {}
}

export default {

  state,

  send (conversationId, body) {
    let data = new FormData()
    data.append('c', conversationId)
    data.append('b', body)
    return axios.post('/fs/xhrapp.php?app=msg&m=sendmsg', data)
      .then(({ data: { data: { msg } } }) => {
        Object.assign(msg, { cid: conversationId })
        this.receiveMessage(convertMessage(msg))
      })
  },

  loadConversation (id) {
    return axios.get(`/api/v1/conversation/${id}`).then(({ data: { conversation } }) => {
      conversationDecodeHtmlEntities(conversation)
      state.conversations[id] = conversation
      return conversation
    })
  },

  receiveMessage (message) {
    let { conversationId } = message
    let conversation = state.conversations[conversationId]
    if (conversation) {
      conversation.messages.push(message)
      conversation.lastMessage = message
    }
    else {
      log.info('no conversation found for', conversationId)
    }
  }

}

export function fetchConversationList () {
  return axios.get('/api/v1/converations').then(({ data: { conversations } }) => {
    conversations.forEach(conversationDecodeHtmlEntities)
    Object.assign(state, { conversations })
  })
}

export function conversationDecodeHtmlEntities (conversation) {
  conversation.messages.forEach(message => {
    message.body = decodeHtmlEntities(message.body)
  })
  if (conversation.lastMessage) {
    conversation.lastMessage.body = decodeHtmlEntities(conversation.lastMessage.body)
  }
}

/*
 * Foodsharing db has encoded html entities (e.g. "fish &amp; chips")
 *
 * Function to reverse php htmlentities() function
 * Get list of entities by running:
 *
 *   php -r 'var_dump(get_html_translation_table());`
 *
 */
export function decodeHtmlEntities (str) {
  return str
    .replace(/&amp;/g, '&')
    .replace(/&quot;/g, '""')
    .replace(/&lt;/g, '<')
    .replace(/&gt;/g, '>')
}

/*
 * Convert message from existing foodsharing format to our format
 * See /api/doc#get--api-v1-conversation-{id}
 */
export function convertMessage ({
  time: sentAt,
  body,
  fs_name: firstName,
  fs_id: userId,
  id: messageId,
  cid: conversationId
}) {
  return {
    sentAt,
    body: decodeHtmlEntities(body),
    sentBy: {
      id: parseInt(userId, 10),
      firstName
    },
    messageId: parseInt(messageId, 10),
    conversationId: parseInt(conversationId, 10)
  }
}
{'fraction_non_alphanumeric': 0.1379082082965578, 'fraction_numerical': 0.004192409532215357, 'mean_word_length': 3.2723845428840717, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 12, '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': '6498934', 'n_tokens_mistral': 1438, 'n_tokens_neox': 1448, 'n_words': 302}