Improve JSON responses for mailbox actions

This commit is contained in:
Francis Lachapelle 2015-12-21 21:07:43 -05:00
parent d8f5673a14
commit 3cd1c641ab

View file

@ -95,16 +95,14 @@
errorFormat = [self labelForKey: @"The folder with name \"%@\" could not be created." inContext: context]; errorFormat = [self labelForKey: @"The folder with name \"%@\" could not be created." inContext: context];
jsonResponse = [NSDictionary dictionaryWithObject: [NSString stringWithFormat: errorFormat, folderName] jsonResponse = [NSDictionary dictionaryWithObject: [NSString stringWithFormat: errorFormat, folderName]
forKey: @"error"]; forKey: @"error"];
response = [self responseWithStatus: 500 response = [self responseWithStatus: 500 andJSONRepresentation: jsonResponse];
andJSONRepresentation: jsonResponse];
} }
} }
else else
{ {
jsonResponse = [NSDictionary dictionaryWithObject: [self labelForKey: @"Missing 'name' parameter." inContext: context] jsonResponse = [NSDictionary dictionaryWithObject: [self labelForKey: @"Missing 'name' parameter." inContext: context]
forKey: @"error"]; forKey: @"error"];
response = [self responseWithStatus: 500 response = [self responseWithStatus: 500 andJSONRepresentation: jsonResponse];
andJSONRepresentation: jsonResponse];
} }
return response; return response;
@ -150,9 +148,8 @@
if (!newFolderName || [newFolderName length] == 0) if (!newFolderName || [newFolderName length] == 0)
{ {
message = [NSDictionary dictionaryWithObject: [self labelForKey: @"Missing name parameter" inContext: context] message = [NSDictionary dictionaryWithObject: [self labelForKey: @"Missing name parameter" inContext: context]
forKey: @"error"]; forKey: @"message"];
response = [self responseWithStatus: 500 response = [self responseWithStatus: 500 andJSONRepresentation: message];
andString: [message jsonRepresentation]];
} }
else else
{ {
@ -161,9 +158,8 @@
if (error) if (error)
{ {
message = [NSDictionary dictionaryWithObject: [self labelForKey: @"Unable to rename folder." inContext: context] message = [NSDictionary dictionaryWithObject: [self labelForKey: @"Unable to rename folder." inContext: context]
forKey: @"error"]; forKey: @"message"];
response = [self responseWithStatus: 500 response = [self responseWithStatus: 500 andJSONRepresentation: message];
andString: [message jsonRepresentation]];
} }
else else
{ {
@ -181,8 +177,7 @@
newFolderPath = [[[co imap4URL] path] substringFromIndex: 1]; // remove slash at beginning of path newFolderPath = [[[co imap4URL] path] substringFromIndex: 1]; // remove slash at beginning of path
message = [NSDictionary dictionaryWithObject: newFolderPath message = [NSDictionary dictionaryWithObject: newFolderPath
forKey: @"path"]; forKey: @"path"];
response = [self responseWithStatus: 200 response = [self responseWithStatus: 200 andJSONRepresentation: message];
andString: [message jsonRepresentation]];
} }
} }
@ -253,8 +248,7 @@
{ {
jsonResponse = [NSDictionary dictionaryWithObject: [self labelForKey: @"Unable to move folder." inContext: context] jsonResponse = [NSDictionary dictionaryWithObject: [self labelForKey: @"Unable to move folder." inContext: context]
forKey: @"error"]; forKey: @"error"];
response = [self responseWithStatus: 500 response = [self responseWithStatus: 500 andJSONRepresentation: jsonResponse];
andString: [jsonResponse jsonRepresentation]];
} }
else else
{ {
@ -284,9 +278,8 @@
else else
{ {
jsonResponse = [NSDictionary dictionaryWithObject: [self labelForKey: @"Unable to move folder." inContext: context] jsonResponse = [NSDictionary dictionaryWithObject: [self labelForKey: @"Unable to move folder." inContext: context]
forKey: @"error"]; forKey: @"message"];
response = [self responseWithStatus: 500 response = [self responseWithStatus: 500 andJSONRepresentation: jsonResponse];
andString: [jsonResponse jsonRepresentation]];
} }
return response; return response;
@ -351,9 +344,9 @@
} }
else else
{ {
response = [self responseWithStatus: 500]; data = [NSDictionary dictionaryWithObject: [self labelForKey: @"Missing 'uids' parameter." inContext: context]
data = [NSDictionary dictionaryWithObject: @"Missing 'uids' parameter." forKey: @"error"]; forKey: @"message"];
[response appendContentString: [data jsonRepresentation]]; response = [self responseWithStatus: 500 andJSONRepresentation: data];
} }
return response; return response;
@ -364,6 +357,7 @@
SOGoMailFolder *co; SOGoMailFolder *co;
WOResponse *response; WOResponse *response;
NSArray *uids; NSArray *uids;
NSDictionary *jsonResponse;
NSString *value; NSString *value;
co = [self clientObject]; co = [self clientObject];
@ -381,8 +375,9 @@
} }
else else
{ {
response = [self responseWithStatus: 500]; jsonResponse = [NSDictionary dictionaryWithObject: [self labelForKey: @"Missing 'uid' parameter." inContext: context]
[response appendContentString: @"Missing 'uid' parameter."]; forKey: @"message"];
response = [self responseWithStatus: 500 andJSONRepresentation: jsonResponse];
} }
return response; return response;
@ -430,16 +425,15 @@
{ {
// We return the inbox quota // We return the inbox quota
account = [co mailAccountFolder]; account = [co mailAccountFolder];
data = [NSDictionary dictionaryWithObjectsAndKeys: [account getInboxQuota], @"quotas", nil]; data = [NSDictionary dictionaryWithObject: [account getInboxQuota] forKey: @"quotas"];
response = [self responseWithStatus: 200 response = [self responseWithStatus: 200 andJSONRepresentation: data];
andString: [data jsonRepresentation]];
} }
} }
else else
{ {
data = [NSDictionary dictionaryWithObject: @"Error copying messages." forKey: @"error"]; data = [NSDictionary dictionaryWithObject: [self labelForKey: @"Error copying messages." inContext: context]
response = [self responseWithStatus: 500 forKey: @"message"];
andString: [data jsonRepresentation]]; response = [self responseWithStatus: 500 andJSONRepresentation: data];
} }
return response; return response;
@ -492,9 +486,9 @@
} }
else else
{ {
data = [NSDictionary dictionaryWithObject: @"Error moving messages." forKey: @"error"]; data = [NSDictionary dictionaryWithObject: [self labelForKey: @"Error moving messages." inContext: context]
response = [self responseWithStatus: 500 forKey: @"error"];
andString: [data jsonRepresentation]]; response = [self responseWithStatus: 500 andJSONRepresentation: data];
} }
return response; return response;
@ -519,6 +513,7 @@
{ {
NSArray *accounts; NSArray *accounts;
int realIdx; int realIdx;
NSDictionary *jsonResponse;
NSMutableDictionary *account, *mailboxes; NSMutableDictionary *account, *mailboxes;
WOResponse *response; WOResponse *response;
@ -541,14 +536,18 @@
response = [self responseWith204]; response = [self responseWith204];
} }
else else
response {
= [self responseWithStatus: 500 jsonResponse = [NSDictionary dictionaryWithObject: @"You reached an impossible end."
andString: @"You reached an impossible end."]; forKey: @"message"];
response = [self responseWithStatus: 500 andJSONRepresentation: jsonResponse];
}
} }
else else
response {
= [self responseWithStatus: 500 jsonResponse = [NSDictionary dictionaryWithObject: @"You reached an impossible end."
andString: @"You reached an impossible end."]; forKey: @"message"];
response = [self responseWithStatus: 500 andJSONRepresentation: jsonResponse];
}
return response; return response;
} }
@ -559,6 +558,7 @@
WOResponse *response; WOResponse *response;
SOGoUser *owner; SOGoUser *owner;
SOGoUserDefaults *ud; SOGoUserDefaults *ud;
NSDictionary *jsonResponse;
NSString *accountIdx, *traversal; NSString *accountIdx, *traversal;
co = [self clientObject]; co = [self clientObject];
@ -583,17 +583,20 @@
inUserDefaults: ud inUserDefaults: ud
to: traversal]; to: traversal];
else else
response {
= [self responseWithStatus: 500 jsonResponse = [NSDictionary dictionaryWithObject: @"You reached an impossible end."
andString: @"You reached an impossible end."]; forKey: @"message"];
response = [self responseWithStatus: 500 andJSONRepresentation: jsonResponse];
}
if ([response status] == 204) if ([response status] == 204)
[ud synchronize]; [ud synchronize];
} }
else else
{ {
response = [self responseWithStatus: 500]; jsonResponse = [NSDictionary dictionaryWithObject: [self labelForKey: @"Unable to change the purpose of this folder." inContext: context]
[response forKey: @"message"];
appendContentString: @"Unable to change the purpose of this folder."]; response = [self responseWithStatus: 500 andJSONRepresentation: jsonResponse];
} }
return response; return response;
@ -627,8 +630,9 @@
error = [co expunge]; error = [co expunge];
if (error) if (error)
{ {
response = [self responseWithStatus: 500 data = [NSDictionary dictionaryWithObject: [self labelForKey: @"Unable to expunge folder." inContext: context]
andJSONRepresentation: [NSDictionary dictionaryWithObject: @"Unable to expunge folder." forKey: @"message"]]; forKey: @"message"];
response = [self responseWithStatus: 500 andJSONRepresentation: data];
} }
else else
{ {
@ -636,9 +640,8 @@
// We return the inbox quota // We return the inbox quota
account = [co mailAccountFolder]; account = [co mailAccountFolder];
data = [NSDictionary dictionaryWithObjectsAndKeys: [account getInboxQuota], @"quotas", nil]; data = [NSDictionary dictionaryWithObject: [account getInboxQuota] forKey: @"quotas"];
response = [self responseWithStatus: 200 response = [self responseWithStatus: 200 andJSONRepresentation: data];
andString: [data jsonRepresentation]];
} }
return response; return response;
@ -675,16 +678,16 @@
} }
if (error) if (error)
{ {
response = [self responseWithStatus: 500]; data = [NSDictionary dictionaryWithObject: [self labelForKey: @"Unable to empty the trash folder." inContext: context]
[response appendContentString: @"Unable to empty the trash folder."]; forKey: @"message"];
response = [self responseWithStatus: 500 andJSONRepresentation: data];
} }
else else
{ {
// We return the inbox quota // We return the inbox quota
account = [co mailAccountFolder]; account = [co mailAccountFolder];
data = [NSDictionary dictionaryWithObjectsAndKeys: [account getInboxQuota], @"quotas", nil]; data = [NSDictionary dictionaryWithObject: [account getInboxQuota] forKey: @"quotas"];
response = [self responseWithStatus: 200 response = [self responseWithStatus: 200 andJSONRepresentation: data];
andString: [data jsonRepresentation]];
} }
return response; return response;
@ -767,7 +770,7 @@
if ([[result valueForKey: @"result"] boolValue]) if ([[result valueForKey: @"result"] boolValue])
response = [self responseWith204]; response = [self responseWith204];
else else
response = [self responseWithStatus:500 andJSONRepresentation:result]; response = [self responseWithStatus: 500 andJSONRepresentation: result];
return response; return response;
} }