JSONify [UIxMailFolderActions renameFolderAction]

This commit also introduces an initial effort to have a documentation
for the JSON-based Web API following the apiDoc format
(http://apidocjs.com/).
This commit is contained in:
Francis Lachapelle 2015-01-08 11:31:24 -05:00
parent 3526cadc96
commit c00e09f40b

View file

@ -53,6 +53,16 @@
@implementation UIxMailFolderActions @implementation UIxMailFolderActions
/**
* @api {post} /so/:username/Mail/:accountId/:parentMailboxPath/createFolder Create a mailbox
* @apiVersion 1.0.0
* @apiName PostCreateFolder
* @apiGroup Mail
*
* @apiParam {String} name Name of the mailbox
*
* @apiError (Error 500) {Object} error The error message
*/
- (id <WOActionResults>) createFolderAction - (id <WOActionResults>) createFolderAction
{ {
SOGoMailFolder *co, *newFolder; SOGoMailFolder *co, *newFolder;
@ -96,18 +106,31 @@
return response; return response;
} }
/**
* @api {post} /so/:username/Mail/:accountId/:mailboxPath/renameFolder Rename a mailbox
* @apiVersion 1.0.0
* @apiName PostRenameFolder
* @apiGroup Mail
*
* @apiParam {String} name Name of the mailbox
*
* @apiSuccess (Success 200) {String} path New mailbox path relative to account
* @apiError (Error 500) {Object} error The error message
*/
- (WOResponse *) renameFolderAction - (WOResponse *) renameFolderAction
{ {
SOGoMailFolder *co; SOGoMailFolder *co;
SOGoUserSettings *us; SOGoUserSettings *us;
WORequest *request;
WOResponse *response; WOResponse *response;
NSException *error; NSException *error;
NSString *newFolderName, *currentMailbox, *currentAccount, *keyForMsgUIDs, *newKeyForMsgUIDs; NSString *newFolderName, *newFolderPath, *currentMailbox, *currentAccount, *keyForMsgUIDs, *newKeyForMsgUIDs;
NSMutableDictionary *moduleSettings, *threadsCollapsed; NSMutableDictionary *params, *moduleSettings, *threadsCollapsed, *message;
NSArray *values; NSArray *values;
co = [self clientObject]; co = [self clientObject];
//Prepare the variables need to verify if the current folder have any collapsed threads saved in userSettings
// Prepare the variables need to verify if the current folder have any collapsed threads saved in userSettings
us = [[context activeUser] userSettings]; us = [[context activeUser] userSettings];
moduleSettings = [us objectForKey: @"Mail"]; moduleSettings = [us objectForKey: @"Mail"];
threadsCollapsed = [moduleSettings objectForKey:@"threadsCollapsed"]; threadsCollapsed = [moduleSettings objectForKey:@"threadsCollapsed"];
@ -115,29 +138,50 @@
currentAccount = [[co container] nameInContainer]; currentAccount = [[co container] nameInContainer];
keyForMsgUIDs = [NSString stringWithFormat:@"/%@/%@", currentAccount, currentMailbox]; keyForMsgUIDs = [NSString stringWithFormat:@"/%@/%@", currentAccount, currentMailbox];
newFolderName = [[context request] formValueForKey: @"name"]; // Retrieve new folder name from JSON payload
newKeyForMsgUIDs = [[NSString stringWithFormat:@"/%@/folder%@", currentAccount, newFolderName] asCSSIdentifier]; request = [context request];
error = [co renameTo: newFolderName]; params = [[request contentAsString] objectFromJSONString];
if (error) newFolderName = [params objectForKey: @"name"];
if (!newFolderName || [newFolderName length] == 0)
{ {
response = [self responseWithStatus: 500]; message = [NSDictionary dictionaryWithObject: [self labelForKey: @"Missing name parameter"]
[response appendContentString: @"Unable to rename folder."]; forKey: @"error"];
response = [self responseWithStatus: 500
andString: [message jsonRepresentation]];
} }
else else
{ {
// Verify if the current folder have any collapsed threads save under it old name and adjust the folderName newKeyForMsgUIDs = [[NSString stringWithFormat:@"/%@/folder%@", currentAccount, newFolderName] asCSSIdentifier];
if (threadsCollapsed) error = [co renameTo: newFolderName];
if (error)
{ {
if ([threadsCollapsed objectForKey:keyForMsgUIDs]) message = [NSDictionary dictionaryWithObject: [self labelForKey: @"Unable to rename folder."]
{ forKey: @"error"];
values = [NSArray arrayWithArray:[threadsCollapsed objectForKey:keyForMsgUIDs]]; response = [self responseWithStatus: 500
[threadsCollapsed setObject:values forKey:newKeyForMsgUIDs]; andString: [message jsonRepresentation]];
[threadsCollapsed removeObjectForKey:keyForMsgUIDs]; }
[us synchronize]; else
} {
// Verify if the current folder have any collapsed threads save under it old name and adjust the folderName
if (threadsCollapsed)
{
if ([threadsCollapsed objectForKey:keyForMsgUIDs])
{
values = [NSArray arrayWithArray:[threadsCollapsed objectForKey:keyForMsgUIDs]];
[threadsCollapsed setObject:values forKey:newKeyForMsgUIDs];
[threadsCollapsed removeObjectForKey:keyForMsgUIDs];
[us synchronize];
}
}
newFolderPath = [[[co imap4URL] path] substringFromIndex: 1]; // remove slash at beginning of path
message = [NSDictionary dictionaryWithObject: newFolderPath
forKey: @"path"];
response = [self responseWithStatus: 200
andString: [message jsonRepresentation]];
} }
response = [self responseWith204];
} }
return response; return response;
} }