Message api change for spec 0.9 patch by Jinhui Du
[shindig.git] / php / src / social / service / MessagesHandler.php
blob605c6ddc6e8c86108ea520eafa65271236505f93
1 <?php
2 /**
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
18 * under the License.
21 class MessagesHandler extends DataRequestHandler {
23 private static $MESSAGES_PATH = "/messages/{userId}/msgCollId/{messageId}";
24 private $service;
26 public function __construct() {
27 $service = Config::get('messages_service');
28 $this->service = new $service();
31 /**
32 * Deletes the message collection or the messages.
34 public function handleDelete(RequestItem $requestItem) {
35 $requestItem->applyUrlTemplate(self::$MESSAGES_PATH);
37 $userIds = $requestItem->getUsers();
38 HandlerPreconditions::requireSingular($userIds, "UserId can only be singular.");
39 $msgCollId = $requestItem->getParameter("msgCollId");
40 HandlerPreconditions::requireNotEmpty($msgCollId, "A message collection is required");
42 $token = $requestItem->getToken();
43 $messageIds = $requestItem->getListParameter("messageId");
44 if (empty($messageIds)) {
45 $this->service->deleteMessageCollection($userIds[0], $msgCollId, $token);
46 } else {
47 $this->service->deleteMessages($userIds[0], $msgCollId, $messageIds, $token);
51 /**
52 * Returns a list of message collections or messages.
53 * Examples:
54 * /messages/john.doe
55 * /messages/john.doe/notification
56 * /messages/john.doe/notification/1,2,3
58 public function handleGet(RequestItem $requestItem) {
59 $requestItem->applyUrlTemplate(self::$MESSAGES_PATH);
61 $userIds = $requestItem->getUsers();
62 HandlerPreconditions::requireSingular($userIds, "UserId is not singular.");
64 $options = new CollectionOptions($requestItem);
65 $msgCollId = $requestItem->getParameter("msgCollId");
67 $token = $requestItem->getToken();
68 if (empty($msgCollId)) {
69 // Gets the message collections.
70 return $this->service->getMessageCollections($userIds[0], $requestItem->getFields(MessageCollection::$DEFAULT_FIELDS), $options, $token);
73 $messageIds = $requestItem->getListParameter("messageId");
74 if (empty($messageIds)) {
75 $messageIds = array();
77 return $this->service->getMessages($userIds[0], $msgCollId, $requestItem->getFields(Message::$DEFAULT_FIELDS), $messageIds, $options, $token);
80 /**
81 * Creates a new message collection or message.
82 * Exapmples:
83 * /messages/john.doe
84 * /messages/john.doe/notification
86 public function handlePost(RequestItem $requestItem) {
87 $requestItem->applyUrlTemplate(self::$MESSAGES_PATH);
89 $userIds = $requestItem->getUsers();
90 HandlerPreconditions::requireSingular($userIds, "UserId is not singular.");
92 $msgCollId = $requestItem->getParameter("msgCollId");
93 if (empty($msgCollId)) {
94 // Creates a message collection.
95 $messageCollection = $requestItem->getParameter("entity");
96 HandlerPreconditions::requireNotEmpty($messageCollection, "Can't parse message collection.");
97 return $this->service->createMessageCollection($userIds[0], $messageCollection, $requestItem->getToken());
98 } else {
99 // Creates a message.
100 $messageIds = $requestItem->getListParameter("messageId");
101 HandlerPreconditions::requireEmpty($messageIds, "messageId cannot be specified in create method.");
102 $message = $requestItem->getParameter("entity");
103 HandlerPreconditions::requireNotEmpty($message, "Can't parse message.");
104 HandlerPreconditions::requireEmpty($messageIds, "messageId cannot be specified in create method.");
106 // Message fields validation.
107 HandlerPreconditions::requireCondition(! ($message['title'] === null && $message['body'] === null), "title and/or body should be specified.");
108 HandlerPreconditions::requireNotEmpty($message['recipients'], "Field recipients is required.");
110 return $this->service->createMessage($userIds[0], $msgCollId, $message, $requestItem->getToken());
115 * Updates a message or a message collection.
117 public function handlePut(RequestItem $requestItem) {
118 $requestItem->applyUrlTemplate(self::$MESSAGES_PATH);
120 $userIds = $requestItem->getUsers();
121 HandlerPreconditions::requireSingular("UserId is not singular.");
123 $msgCollId = $requestItem->getParameter("msgCollId");
124 HandlerPreconditions::requireNotEmpty($msgCollId, "msgCollId is required.");
126 $messageIds = $requestItem->getListParameter("messageId");
127 if (empty($messageIds)) {
128 // Updates message collection. NOTE: "message" is used here to represent message collection.
129 $messageCollection = $requestItem->getParameter("message");
130 HandlerPreconditions::requireNotEmpty($messageCollection, "Can't parse message collection.");
131 return $this->service->updateMessageCollection($userIds[0], $messageCollection, $requestItem->getToken());
132 } else {
133 // Updates a message.
134 HandlerPreconditions::requireSingular("UserId is not singular.");
135 $message = $requestItem->getParameter("message");
136 HandlerPreconditions::requireNotEmpty($message, "Can't parse message.");
137 return $this->service->updateMessage($userIds[0], $msgCollId, $message, $requestItem->getToken());