From 265b25dbc40b20e54163ac3688125c6dcc12867c Mon Sep 17 00:00:00 2001 From: Horacio Sanson Date: Tue, 17 May 2011 23:34:14 +0900 Subject: [PATCH] Implement post /message.json. Now we can add messages to heliotrope with labels, state and mailbox information in a single POST request. The request body has to be JSON with a format like: { body: , labels: ["inbox", "school", "home"], state: ["seen", "old"], mailbox: "inbox" } --- bin/heliotrope-server | 30 +++++++++++++++++++++++++++--- 1 files changed, 27 insertions(+), 3 deletions(-) diff --git a/bin/heliotrope-server b/bin/heliotrope-server index d71989d..bfa8fdd 100644 --- a/bin/heliotrope-server +++ b/bin/heliotrope-server @@ -302,10 +302,34 @@ class HeliotropeServer < Sinatra::Base end post "/message.json" do - body = params["body"] or raise RequestError, "need a 'body' param" - puts body + content_type :json + begin + rawbody = params["body"] or raise RequestError, "need a 'body' param" + rawbody.force_encoding "binary" if rawbody.respond_to?(:force_encoding) # sigh... - {:status => "ok"}.to_json + message = Heliotrope::Message.new(rawbody).parse! + + mbox = params["mailbox"] || "inbox" + doc_id = nil + thread_id = nil + state = nil + labels = [mbox] + if @index.contains_msgid? message.msgid + messageinfo = get_message_summary message.msgid + doc_id = messageinfo[:message_id] + thread_id = messageinfo[:thread_id] + { :response => :ok, :status => :seen, :doc_id => doc_id, :thread_id => thread_id } + else + # Set state + state = params["state"] || ["unseen"] + labels.push(params["labels"]).flatten!.uniq! if params["labels"] + loc = @store.add rawbody + doc_id, thread_id = @index.add_message message, state, labels, { :loc => loc, :mbox => mbox } + { :response => :ok, :status => state, :doc_id => doc_id, :thread_id => thread_id, :labels => labels } + end + rescue Heliotrope::InvalidMessageError => e + { :response => :error, :error_message => e.message } + end.to_json end private -- 1.7.4.1