/** * {@inheritDoc} * * Post a message for a set of users. */ public Future<Void> createMessage(UserId userId, String appId, String msgCollId, Message message, SecurityToken token) throws ProtocolException { if (message.getId() == null) { // Assign a new ID to the message message.setId(UUID.randomUUID().toString()); } try { JSONObject messagesTable = db.getJSONObject(MESSAGE_TABLE); JSONObject messageObject = convertToJson(message); for (String recipient : message.getRecipients()) { JSONObject collection = messagesTable.getJSONObject(recipient).getJSONObject(msgCollId); JSONArray outbox = collection.getJSONArray(MESSAGES_ARRAY); if (outbox == null) { outbox = new JSONArray(); collection.put(MESSAGES_ARRAY, outbox); } outbox.put(messageObject); } } catch (JSONException je) { throw new ProtocolException(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, je.getMessage(), je); } return Futures.immediateFuture(null); }
/** * {@inheritDoc} * * Post a message for a set of users. */ public Future<Void> createMessage(UserId userId, String appId, String msgCollId, Message message, SecurityToken token) throws ProtocolException { if (message.getId() == null) { // Assign a new ID to the message message.setId(UUID.randomUUID().toString()); } try { JSONObject messagesTable = db.getJSONObject(MESSAGE_TABLE); JSONObject messageObject = convertToJson(message); for (String recipient : message.getRecipients()) { JSONObject collection = messagesTable.getJSONObject(recipient).getJSONObject(msgCollId); JSONArray outbox = collection.getJSONArray(MESSAGES_ARRAY); if (outbox == null) { outbox = new JSONArray(); collection.put(MESSAGES_ARRAY, outbox); } outbox.put(messageObject); } } catch (JSONException je) { throw new ProtocolException(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, je.getMessage(), je); } return Futures.immediateFuture(null); }
JSONObject msg = messages.getJSONObject(i); Message message = filterFields(msg, fields, Message.class); if (msgIds.isEmpty() || msgIds.contains(message.getId())) { result.add(message);
JSONObject msg = messages.getJSONObject(i); Message message = filterFields(msg, fields, Message.class); if (msgIds.isEmpty() || msgIds.contains(message.getId())) { result.add(message);
@Test public void testGetMessagesSingleId() throws Exception { RestfulCollection<Message> messages = db.getMessages( CANON_USER, "publicMessage", Collections.<String>emptySet(), Collections.<String>singletonList("1"), new CollectionOptions(), token).get(); assertEquals(1, messages.getList().size()); assertEquals("1", messages.getList().get(0).getId()); }
@Test public void testGetMessagesSingleId() throws Exception { RestfulCollection<Message> messages = db.getMessages( CANON_USER, "publicMessage", Collections.<String>emptySet(), Collections.<String>singletonList("1"), new CollectionOptions(), token).get(); assertEquals(1, messages.getList().size()); assertEquals("1", messages.getList().get(0).getId()); }
@Test public void testCreateMessage() throws Exception { // Create a new private message Message msg = new MessageImpl(); msg.setBody("Hello"); msg.setRecipients(Collections.singletonList(JANE_DOE.getUserId())); db.createMessage(JOHN_DOE, APP_ID, "privateMessage", msg, token).get(); // Check that the message was created RestfulCollection<Message> messages = db.getMessages( JANE_DOE, "privateMessage", Collections.<String>emptySet(), Collections.<String>singletonList(msg.getId()), new CollectionOptions(), token).get(); assertEquals(1, messages.getList().size()); } }
@Test public void testCreateMessage() throws Exception { // Create a new private message Message msg = new MessageImpl(); msg.setBody("Hello"); msg.setRecipients(Collections.singletonList(JANE_DOE.getUserId())); db.createMessage(JOHN_DOE, APP_ID, "privateMessage", msg, token).get(); // Check that the message was created RestfulCollection<Message> messages = db.getMessages( JANE_DOE, "privateMessage", Collections.<String>emptySet(), Collections.<String>singletonList(msg.getId()), new CollectionOptions(), token).get(); assertEquals(1, messages.getList().size()); } }
if (message == null || message.getId() == null) { throw new ProtocolException(HttpServletResponse.SC_BAD_REQUEST, "cannot parse message or missing ID");
if (message == null || message.getId() == null) { throw new ProtocolException(HttpServletResponse.SC_BAD_REQUEST, "cannot parse message or missing ID");
if (message == null || message.getId() == null) { throw new ProtocolException(HttpServletResponse.SC_BAD_REQUEST, "cannot parse message or missing ID");