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
21 class MessagesHandler
extends DataRequestHandler
{
23 private static $MESSAGES_PATH = "/messages/{userId}/msgCollId/{messageId}";
26 public function __construct() {
27 $service = Config
::get('messages_service');
28 $this->service
= new $service();
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);
47 $this->service
->deleteMessages($userIds[0], $msgCollId, $messageIds, $token);
52 * Returns a list of message collections or messages.
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);
81 * Creates a new message collection or message.
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());
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());
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());