<commit_before>/* @flow */

import { List }                          from 'immutable'
import { mailtoUri, midUri, midPartUri } from '../activity'
import { ActivityRecord }                from '../derivedActivity'
import { displayName }                   from '../notmuch'

import type { Message }         from '../notmuch'
import type { Zack }            from '../activity'
import type { DerivedActivity } from '../derivedActivity'

export {
  unwrapMessage,
}

function unwrapMessage(message: Message): List<DerivedActivity> {
  var activities: DerivedActivity[] = (message.attachments || []).filter(a => (
    a.contentType === 'application/activity+json'
  ))
  .map(attachment => new ActivityRecord({
    id: midPartUri(attachment, message),
    activity: JSON.parse(attachment.content.toString('utf8')),
    message,
  }))
  if (activities.length > 0) {
    return List(activities)
  }
  else {
    var note = asNote(message)
    return List(note ? [note] : [unknownActivity(message)])
  }
}

// TODO: Should the synthetic note and unknown activities be derived activities?
function asNote(message: Message): ?DerivedActivity {
  var from = message.from[0]
  var author = {
    objectType: 'person',
    displayName: displayName(from),
    id: mailtoUri(from.address),
  }
  if (message.text || message.html) {
    var activity = {
      title: message.subject,
      verb: 'post',
      object: {
        objectType: 'note',
        author,
        uri: midUri(message),
      }
    }
    return new ActivityRecord({
      id: midUri(message),
      activity,
      message,
    })
  }
}

function unknownActivity(message: Message): DerivedActivity {
  var activity = {
    title: message.subject,
    verb: 'unknown',
  }
  return new ActivityRecord({
    id: midUri(message),
    activity,
    message,
  })
}
<commit_msg>Annoying shim to handle messages that did not get Content-IDs for JSON parts<commit_after>/* @flow */

import { List }                          from 'immutable'
import { mailtoUri, midUri, midPartUri, parseMidUri, resolveUri } from '../activity'
import { ActivityRecord }                from '../derivedActivity'
import { displayName }                   from '../notmuch'

import type { Message }         from '../notmuch'
import type { Zack }            from '../activity'
import type { DerivedActivity } from '../derivedActivity'

export {
  unwrapMessage,
}

function unwrapMessage(message: Message): List<DerivedActivity> {
  var activities: DerivedActivity[] = (message.attachments || []).filter(a => (
    a.contentType === 'application/activity+json'
  ))
  .map(attachment => new ActivityRecord({
    id: handleLegacyId(attachment, message),
    activity: JSON.parse(attachment.content.toString('utf8')),
    message,
  }))
  if (activities.length > 0) {
    return List(activities)
  }
  else {
    var note = asNote(message)
    return List(note ? [note] : [unknownActivity(message)])
  }
}

// TODO: Get rid of this eventually.
function handleLegacyId(attachment, message) {
  if (attachment.contentId.match(/@mailparser$/)) {
    // This is a bogus ID generated by the mail parsing library - the actual
    // MIME part did not get a 'Content-ID' header
    var activity = JSON.parse(attachment.content.toString('utf8'))
    var parsed = !!activity.id && parseMidUri(activity.id)
    if (parsed) {
      return resolveUri(message, activity.id)
    }
  }
  return midPartUri(attachment, message)
}

// TODO: Should the synthetic note and unknown activities be derived activities?
function asNote(message: Message): ?DerivedActivity {
  var from = message.from[0]
  var author = {
    objectType: 'person',
    displayName: displayName(from),
    id: mailtoUri(from.address),
  }
  if (message.text || message.html) {
    var activity = {
      title: message.subject,
      verb: 'post',
      object: {
        objectType: 'note',
        author,
        uri: midUri(message),
      }
    }
    return new ActivityRecord({
      id: midUri(message),
      activity,
      message,
    })
  }
}

function unknownActivity(message: Message): DerivedActivity {
  var activity = {
    title: message.subject,
    verb: 'unknown',
  }
  return new ActivityRecord({
    id: midUri(message),
    activity,
    message,
  })
}

{'fraction_non_alphanumeric': 0.1267605633802817, 'fraction_numerical': 0.0016431924882629107, 'mean_word_length': 2.8318345323741005, 'pattern_counts': {'":': 0, '<': 5, '<?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': '7120626', 'n_tokens_mistral': 1252, 'n_tokens_neox': 1230, 'n_words': 331}