     options =
       date: originalMessage.date
       title: originalMessage.subject
+      threadId: originalMessage.threadId
+
+#    @findMessageByThreadId originalMessage.threadId
     @writeMessage postContent, options, callback
+
+
+  findMessageByThreadId: (threadId) ->
+    request = rest.get "#{@config.wpUrl}/wp-json/posts?filter[meta_key]=threadId&filter[meta_value]=#{threadId}",
+      username: @config.wpUsername, password: @config.wpPassword
+    request.on 'complete', (data)->
+      for d in data
+        log d.ID
 
   formatPost: (messages) ->
     contents = for message in messages

      title: options.title
      content_raw: postContent
      date: options.date.toISOString()

    request = rest.post "#{@config.wpUrl}/wp-json/posts",
      username: @config.wpUsername, password: @config.wpPassword,

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
