1 /*global GCN: true */
  2 (function (GCN) {
  3 	'use strict';
  4 
  5 	/**
  6 	 * @class
  7 	 * @name MessageAPI
  8 	 * @extends ContentObjectAPI
  9 	 */
 10 	var MessageAPI = GCN.defineChainback({
 11 		/** @lends message */
 12 
 13 		__chainbacktype__: 'MessageAPI',
 14 		_extends: GCN.ContentObjectAPI,
 15 		_type: 'msg',
 16 
 17 		/**
 18 		 * Lists all message of the currently authenticated user.
 19 		 * 
 20 		 * @public
 21 		 * @param {boolean}
 22 		 *            Specifies wether to list unread messages only (true),
 23 		 *            or to list all messages (false).
 24 		 * @param {function(MessageAPI)=}
 25 		 *            success Optional callback that will receive this object as
 26 		 *            its only argument.
 27 		 * @param {function(GCNError):boolean}
 28 		 *            error Optional custom error handler.
 29 		 */
 30 		'!list': function (unreadOnly, success, error) {
 31 			// Build the URL we will send to the RestAPI
 32 			var url = GCN.settings.BACKEND_PATH + '/rest/' + this._type +
 33 				'/list/?unread=' + (unreadOnly ? 'true' : 'false');
 34 
 35 			var that = this;
 36 
 37 			// Check that we are authenticated and send away
 38 			// the RestAPI request
 39 			this._authAjax({
 40 				url     : url,
 41 				type    : 'GET',
 42 				error: function (xhr, status, msg) {
 43 					GCN.handleHttpError(xhr, msg, error);
 44 				},
 45 				success: function (response) {
 46 					if (GCN.getResponseCode(response) === 'OK') {
 47 						that._invoke(success, [ response.messages ]);
 48 					} else {
 49 						GCN.handleResponseError(response, error);
 50 					}
 51 				}
 52 			});
 53 		},
 54 
 55 		/**
 56 		 * Marks one or multiple messages as read.
 57 		 * 
 58 		 * @public
 59 		 * @param {array|number}
 60 		 *            This either takes an array, which includes one
 61 		 *            or multiple message ID's, or an integer with a
 62 		 *            single ID.
 63 		 * @param {function(MessageAPI)=}
 64 		 *            success Optional callback that will receive the
 65 		 *            response object.
 66 		 * @param {function(GCNError):boolean}
 67 		 *            error Optional custom error handler.
 68 		 */
 69 		'!read': function (messageIds, success, error) {
 70 			if (messageIds === null ||
 71 					(!(messageIds instanceof Array) && typeof messageIds !== 'number')) {
 72 				GCN.handleError(
 73 					GCN.error('Wrong parameter given',
 74 							  '[Message.read] The first parameter should be an array or a number', this),
 75 					error
 76 				);
 77 			}
 78 
 79 			var ajaxMessageIds = [];
 80 
 81 			// Whatever we just got: make an array out of it!
 82 			if (messageIds instanceof Array) {
 83 				ajaxMessageIds = messageIds;
 84 			} else {
 85 				ajaxMessageIds = [ messageIds ];
 86 			}
 87 
 88 			// Build the URL we will send to the RestAPI
 89 			var url = GCN.settings.BACKEND_PATH + '/rest/' + this._type + '/read/';
 90 
 91 			var that = this;
 92 
 93 			// Check that we are authenticated and send away
 94 			// the RestAPI request
 95 			this._authAjax({
 96 				url    : url,
 97 				type   : 'POST',
 98 				json   : { 'messages': ajaxMessageIds },
 99 				error  : function (xhr, status, msg) {
100 					GCN.handleHttpError(xhr, msg, error);
101 				},
102 				success: function (response) {
103 					if (GCN.getResponseCode(response) === 'OK') {
104 						that._invoke(success, [ response ]);
105 					} else {
106 						GCN.handleResponseError(response, error);
107 					}
108 				}
109 			});
110 		},
111 
112 		/**
113 		 * Sends a message to one or multiple users or groups.
114 		 * 
115 		 * @public
116 		 * @param {object}
117 		 *            An object containing a 'users' or/and a 'groups' value:
118 		 *            { users: [ userId1, ... ], groups: [ groupId2, ... ] }
119 		 *            Note: instead of arrays you can also just pass a single
120 		 *            integer for each of both.
121 		 * @param {function(MessageAPI)=}
122 		 *            success Optional callback that will receive the response
123 		 *            object.
124 		 * @param {function(GCNError):boolean}
125 		 *            error Optional custom error handler.
126 		 */
127 		'!send': function (receivers, message, success, error) {
128 			if (receivers === null || typeof receivers !== 'object') {
129 				GCN.handleError(
130 					GCN.error('Wrong parameter given',
131 							  '[Message.send] The first parameter should be an object', this),
132 					error
133 				);
134 			}
135 
136 			// Define variables for the json object
137 			var
138 				toUserId  = [],
139 				toGroupId = [];
140 
141 			// Whatever we just got: make an array out of it!
142 			if (typeof receivers.users !== 'undefined') {
143 				if (receivers.users instanceof Array) {
144 					toUserId = receivers.users;
145 				} else {
146 					toUserId = [ receivers.users ];
147 				}
148 			}
149 			
150 			// The same goes for the group-ID's
151 			if (typeof receivers.groups !== 'undefined') {
152 				if (receivers.groups instanceof Array) {
153 					toGroupId = receivers.groups;
154 				} else {
155 					toGroupId = [ receivers.groups ];
156 				}
157 			}
158 
159 			var sendJsonObject = {
160 				'message'  : message,
161 				'toUserId' : toUserId,
162 				'toGroupId': toGroupId
163 			};
164 
165 			// Build the URL we will send to the RestAPI
166 			var url = GCN.settings.BACKEND_PATH + '/rest/' + this._type + '/send/';
167 
168 			var that = this;
169 
170 			// Check that we are authenticated and do the rest call
171 			// to send our message away to the receiver(s).
172 			this._authAjax({
173 				url    : url,
174 				type   : 'POST',
175 				json   : sendJsonObject,
176 				error  : function (xhr, status, msg) {
177 					GCN.handleHttpError(xhr, msg, error);
178 				},
179 				success: function (response) {
180 					if (GCN.getResponseCode(response) === 'OK') {
181 						that._invoke(success, [ response ]);
182 					} else {
183 						GCN.handleResponseError(response, error);
184 					}
185 				}
186 			});
187 		}
188 	});
189 
190 	/**
191 	* MessageAPI namespace. See the {@link MessageAPI} constructor for detailed information.
192 	* 
193 	* @function
194 	* @name Message
195 	* @memberOf GCN
196 	* @see MessageAPI
197 	*/
198 	GCN.Message = new MessageAPI();
199 }(GCN));
200