PageRenderTime 53ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/Classes/Stacks/HappyFox/HSHappyFoxGear.m

https://gitlab.com/brunofernandeslopes/helpstack
Objective C | 236 lines | 186 code | 27 blank | 23 comment | 27 complexity | b5c6ae3b84e4e785f7a190d7443d121a MD5 | raw file
  1. // HAGearHappyFox.m
  2. //
  3. //Copyright (c) 2014 HelpStack (http://helpstack.io)
  4. //
  5. //Permission is hereby granted, free of charge, to any person obtaining a cop
  6. //of this software and associated documentation files (the "Software"), to deal
  7. //in the Software without restriction, including without limitation the rights
  8. //to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. //copies of the Software, and to permit persons to whom the Software is
  10. //furnished to do so, subject to the following conditions:
  11. //
  12. //The above copyright notice and this permission notice shall be included in
  13. //all copies or substantial portions of the Software.
  14. //
  15. //THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. //IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. //FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. //AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. //LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. //OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. //THE SOFTWARE.
  22. #import "HSHappyFoxGear.h"
  23. #import "HSAttachment.h"
  24. #import "UIImage+Extended.h"
  25. #import <AFNetworkActivityIndicatorManager.h>
  26. @interface HSHappyFoxGear ()
  27. @property (nonatomic, strong) NSString *api_key;
  28. @property (nonatomic, strong) NSString *auth_code;
  29. @property (nonatomic, strong) NSString *instanceUrl;
  30. @property (nonatomic, strong) NSDictionary *articleSections;
  31. @property (nonatomic, strong) NSString *hfPriorityID;
  32. @property (nonatomic, strong) NSString *hfCategoryID;
  33. @end
  34. @implementation HSHappyFoxGear
  35. - (id)initWithInstanceUrl:(NSString*) instanceUrl apiKey:(NSString *)api_key authCode:(NSString *)auth_code priorityID: (NSString *)priority_ID categoryID: (NSString *) category_ID{
  36. if ( (self = [super init]) ) {
  37. self.api_key = api_key;
  38. self.auth_code = auth_code;
  39. self.instanceUrl = instanceUrl;
  40. self.hfCategoryID = category_ID;
  41. self.hfPriorityID = priority_ID;
  42. self.networkManager = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:[NSURL URLWithString:instanceUrl]];
  43. [self.networkManager setRequestSerializer:[AFJSONRequestSerializer serializer]];
  44. [self.networkManager setResponseSerializer:[AFJSONResponseSerializer serializer]];
  45. [self.networkManager.requestSerializer setAuthorizationHeaderFieldWithUsername:self.api_key password:self.auth_code];
  46. [[AFNetworkActivityIndicatorManager sharedManager] setEnabled:YES];
  47. }
  48. return self;
  49. }
  50. - (void)fetchKBForSection:(HSKBItem*)section success:(void (^)(NSMutableArray* kbarray))success failure:(void(^)(NSError*))failure{
  51. if (section == nil){
  52. NSString *url = @"api/1.1/json/kb/sections/";
  53. if(self.hfSectionID){
  54. url = @"api/1.1/json/kb/section/";
  55. url = [url stringByAppendingString:self.hfSectionID];
  56. url = [url stringByAppendingString:@"/"];
  57. }
  58. [self.networkManager GET:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
  59. self.articleSections = responseObject;
  60. if(self.hfSectionID){
  61. NSMutableArray *articles = [self getArticlesFromSection:responseObject];
  62. success(articles);
  63. }else{
  64. NSMutableArray *sections = [self getSectionsFromData:responseObject];
  65. success(sections);
  66. }
  67. }failure:^(AFHTTPRequestOperation *operation, NSError *error) {
  68. failure(error);
  69. }];
  70. } else {
  71. NSString *section_id = section.kb_id;
  72. for(id section in self.articleSections){
  73. if([[[section objectForKey:@"id"] stringValue] isEqualToString:section_id]){
  74. NSMutableArray *kbArticles = [self getArticlesFromSection:section];
  75. success(kbArticles);
  76. }
  77. }
  78. }
  79. }
  80. -(NSMutableArray *)getArticlesFromSection:(NSDictionary *)section{
  81. NSMutableArray *articles = [[NSMutableArray alloc] init];
  82. NSArray *fetchedarticles = [section objectForKey:@"articles"];
  83. for(id article in fetchedarticles){
  84. HSKBItem *kbarticle = [[HSKBItem alloc] initAsArticle:[article objectForKey:@"title"] htmlContent:[article objectForKey:@"contents"] baseUrl:self.instanceUrl kbID:nil];
  85. [articles addObject:kbarticle];
  86. }
  87. return articles;
  88. }
  89. -(NSMutableArray *)getSectionsFromData:(NSDictionary *)responseData{
  90. NSMutableArray *sections = [[NSMutableArray alloc] init];
  91. for(id section in responseData){
  92. if([[section objectForKey:@"articles"] count]>0){
  93. HSKBItem *type = [[HSKBItem alloc] initAsSection:[section objectForKey:@"name"] kbID:[[section objectForKey:@"id"] stringValue]];
  94. [sections addObject:type];
  95. }
  96. }
  97. return sections;
  98. }
  99. - (void)fetchAllUpdateForTicket:(HSTicket *)ticket forUser:(HSUser *)user success:(void (^)(NSMutableArray* updateArray))success failure:(void (^)(NSError* e))failure {
  100. NSString *getString = @"api/1.1/json/ticket/";
  101. getString = [getString stringByAppendingString:ticket.ticketID];
  102. [self.networkManager GET:getString parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
  103. NSMutableArray *tickUpdates = [self getTicketUpdatesFromResponseData:responseObject];
  104. success(tickUpdates);
  105. }failure:^(AFHTTPRequestOperation *operation, NSError *error) {
  106. failure(error);
  107. }];
  108. }
  109. -(NSMutableArray *)getTicketUpdatesFromResponseData:(NSDictionary *)responseData{
  110. NSArray *updates = [responseData objectForKey:@"updates"];
  111. NSMutableArray *tickUpdates = [[NSMutableArray alloc] init];
  112. for(id updateDict in updates){
  113. if([updateDict objectForKey:@"message"] != [NSNull null]){
  114. HSUpdate *tick_update = [[HSUpdate alloc] init];
  115. NSDictionary *by = [updateDict objectForKey:@"by"];
  116. if ([by objectForKey:@"name"] != [NSNull null]) {
  117. tick_update.from = [by objectForKey:@"name"];
  118. }
  119. NSDictionary *message = [updateDict objectForKey:@"message"];
  120. tick_update.content = [message objectForKey:@"text"];
  121. NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
  122. [dateFormat setDateFormat:@"yyyy-MM-dd hh:mm:ss"];
  123. [dateFormat setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];
  124. NSDate *date = [dateFormat dateFromString:[updateDict objectForKey:@"timestamp"]];
  125. tick_update.updatedAt = date;
  126. NSString *type = [by objectForKey:@"type"];
  127. if([type isEqualToString:@"user"]){
  128. tick_update.updateType = HATypeUserReply;
  129. }else{
  130. tick_update.updateType = HATypeStaffReply;
  131. }
  132. if([message objectForKey:@"attachments"] != [NSNull null]){
  133. NSMutableArray *attachments = [[NSMutableArray alloc] init];
  134. for(NSDictionary *attachmentDict in [message objectForKey:@"attachments"]){
  135. HSAttachment *attachment = [[HSAttachment alloc] init];
  136. attachment.fileName = [attachmentDict objectForKey:@"filename"];
  137. attachment.url = [attachmentDict objectForKey:@"url"];
  138. [attachments addObject:attachment];
  139. }
  140. tick_update.attachments = attachments;
  141. }
  142. [tickUpdates addObject:tick_update];
  143. }
  144. }
  145. return tickUpdates;
  146. }
  147. - (void)createNewTicket:(HSNewTicket *)newTicket byUser:(HSUser *)user success:(void (^)(HSTicket* ticket, HSUser * user))success failure:(void (^)(NSError* e))failure {
  148. HSUser* hfUser = user;
  149. NSMutableDictionary *parameters = [[NSMutableDictionary alloc] init];
  150. [parameters setObject:hfUser.name forKey:@"name"];
  151. [parameters setObject:hfUser.email forKey:@"email"];
  152. [parameters setObject:self.hfCategoryID forKey:@"category"];
  153. [parameters setObject:self.hfPriorityID forKey:@"priority"];
  154. [parameters setObject:newTicket.subject forKey:@"subject"];
  155. [parameters setObject:newTicket.content forKey:@"text"];
  156. [parameters setObject:[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"] forKey:@"Build"];
  157. NSArray *attachments = newTicket.attachments;
  158. [self.networkManager POST:@"api/1.1/json/new_ticket/" parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData){
  159. if(attachments != nil && (attachments.count > 0)){
  160. for(HSAttachment *attachment in attachments){
  161. [formData appendPartWithFileData:attachment.attachmentData name:@"attachments" fileName:((HSAttachment *)attachment).fileName mimeType:((HSAttachment *)attachment).mimeType];
  162. }
  163. }
  164. }success:^(AFHTTPRequestOperation *operation, id responseObject) {
  165. HSTicket *issue = [[HSTicket alloc] init];
  166. issue.subject = [responseObject objectForKey:@"subject"];
  167. issue.ticketID = [[responseObject objectForKey:@"id"] stringValue];
  168. // Parsing user id
  169. NSString* userId = [[[responseObject objectForKey:@"user"] objectForKey:@"id"] stringValue];
  170. hfUser.apiHref = userId;
  171. success(issue, hfUser);
  172. }failure:^(AFHTTPRequestOperation *operation, NSError *error) {
  173. HALog(@"Failed to create a ticket %@", error);
  174. if (operation.responseString) {
  175. HALog(@"Error Description %@", operation.responseString);
  176. }
  177. failure(error);
  178. }];
  179. }
  180. - (void)addReply:(HSTicketReply *)reply forTicket:(HSTicket *)ticket byUser:(HSUser *)user success:(void (^)(HSUpdate* update))success failure:(void (^)(NSError* e))failure {
  181. NSString *qString = @"api/1.1/json/ticket/";
  182. qString = [qString stringByAppendingString:ticket.ticketID];
  183. qString = [qString stringByAppendingString:@"/user_reply/"];
  184. NSMutableDictionary *parameters = [[NSMutableDictionary alloc] init];
  185. [parameters setObject:user.apiHref forKey:@"user"];
  186. [parameters setObject:reply.content forKey:@"text"];
  187. NSArray *attachments = reply.attachments;
  188. [self.networkManager POST:qString parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData){
  189. for(HSAttachment *attachment in attachments){
  190. [formData appendPartWithFileData:attachment.attachmentData name:@"attachments" fileName:attachment.fileName mimeType:attachment.mimeType];
  191. }
  192. } success:^(AFHTTPRequestOperation *operation, id responseObject) {
  193. HSUpdate *recentUpdate = [[HSUpdate alloc] init];
  194. recentUpdate.from = user.name;
  195. recentUpdate.content = reply.content;
  196. recentUpdate.attachments = [[NSArray alloc] initWithArray:attachments];
  197. recentUpdate.updatedAt = [NSDate date];
  198. recentUpdate.updateType = HATypeUserReply;
  199. success(recentUpdate);
  200. //Send all the updates
  201. }failure:^(AFHTTPRequestOperation *operation, NSError *error) {
  202. HALog(@"Failed to update Ticket %@", error);
  203. if (operation.responseString) {
  204. HALog(@"Error Description %@", operation.responseString);
  205. }
  206. failure(error);
  207. }];
  208. }
  209. @end