       title: options.title
       content_raw: postContent
       date: options.date.toISOString()
+      post_meta: {key: 'threadId', value: options.threadId}
 
     request = rest.post "#{@config.wpUrl}/wp-json/posts",
       username: @config.wpUsername, password: @config.wpPassword,

    options =
      date: originalMessage.date
      title: originalMessage.subject
    @writeMessage postContent, options, callback

  formatPost: (messages) ->
    contents = for message in messages

escape = require 'escape-html'

class Wordpress
  threadIdToPostId: {}

  constructor: (@config)->
    @buildThreadMapping()

  buildThreadMapping: ->
    @listAllPosts().then (posts)=>
      for post in posts
        for meta in post.post_meta
          if meta.key == 'threadId'
            @threadIdToPostId[meta.value] = post.ID
            break

  listAllPosts: ->
    request = rest.get "#{@config.wpUrl}/wp-json/posts?filter[post_status]=any&context=edit",
      username: @config.wpUsername, password: @config.wpPassword
    new Promise (resolve, reject)->
      request.on 'complete', resolve

  writeThread: (messages, callback) ->
    messages.sort (a, b) -> a.date - b.date
